Notify.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * This file contains just the functions that turn on and off notifications
  4. * to topics or boards.
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2013 Simple Machines and individual contributors
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('No direct access...');
  17. /**
  18. * Turn off/on notification for a particular topic.
  19. * Must be called with a topic specified in the URL.
  20. * The sub-action can be 'on', 'off', or nothing for what to do.
  21. * Requires the mark_any_notify permission.
  22. * Upon successful completion of action will direct user back to topic.
  23. * Accessed via ?action=notify.
  24. *
  25. * @uses Notify template, main sub-template
  26. */
  27. function Notify()
  28. {
  29. global $scripturl, $txt, $topic, $user_info, $context, $smcFunc;
  30. // Make sure they aren't a guest or something - guests can't really receive notifications!
  31. is_not_guest();
  32. isAllowedTo('mark_any_notify');
  33. // Make sure the topic has been specified.
  34. if (empty($topic))
  35. fatal_lang_error('not_a_topic', false);
  36. // What do we do? Better ask if they didn't say..
  37. if (empty($_GET['sa']))
  38. {
  39. // Load the template, but only if it is needed.
  40. loadTemplate('Notify');
  41. // Find out if they have notification set for this topic already.
  42. $request = $smcFunc['db_query']('', '
  43. SELECT id_member
  44. FROM {db_prefix}log_notify
  45. WHERE id_member = {int:current_member}
  46. AND id_topic = {int:current_topic}
  47. LIMIT 1',
  48. array(
  49. 'current_member' => $user_info['id'],
  50. 'current_topic' => $topic,
  51. )
  52. );
  53. $context['notification_set'] = $smcFunc['db_num_rows']($request) != 0;
  54. $smcFunc['db_free_result']($request);
  55. // Set the template variables...
  56. $context['topic_href'] = $scripturl . '?topic=' . $topic . '.' . $_REQUEST['start'];
  57. $context['start'] = $_REQUEST['start'];
  58. $context['page_title'] = $txt['notification'];
  59. return;
  60. }
  61. elseif ($_GET['sa'] == 'on')
  62. {
  63. checkSession('get');
  64. // Attempt to turn notifications on.
  65. $smcFunc['db_insert']('ignore',
  66. '{db_prefix}log_notify',
  67. array('id_member' => 'int', 'id_topic' => 'int'),
  68. array($user_info['id'], $topic),
  69. array('id_member', 'id_topic')
  70. );
  71. }
  72. else
  73. {
  74. checkSession('get');
  75. // Just turn notifications off.
  76. $smcFunc['db_query']('', '
  77. DELETE FROM {db_prefix}log_notify
  78. WHERE id_member = {int:current_member}
  79. AND id_topic = {int:current_topic}',
  80. array(
  81. 'current_member' => $user_info['id'],
  82. 'current_topic' => $topic,
  83. )
  84. );
  85. }
  86. // Send them back to the topic.
  87. redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
  88. }
  89. /**
  90. * Turn off/on notification for a particular board.
  91. * Must be called with a board specified in the URL.
  92. * Only uses the template if no sub action is used. (on/off)
  93. * Requires the mark_notify permission.
  94. * Redirects the user back to the board after it is done.
  95. * Accessed via ?action=notifyboard.
  96. *
  97. * @uses Notify template, notify_board sub-template.
  98. */
  99. function BoardNotify()
  100. {
  101. global $scripturl, $txt, $board, $user_info, $context, $smcFunc;
  102. // Permissions are an important part of anything ;).
  103. is_not_guest();
  104. isAllowedTo('mark_notify');
  105. // You have to specify a board to turn notifications on!
  106. if (empty($board))
  107. fatal_lang_error('no_board', false);
  108. // No subaction: find out what to do.
  109. if (empty($_GET['sa']))
  110. {
  111. // We're gonna need the notify template...
  112. loadTemplate('Notify');
  113. // Find out if they have notification set for this board already.
  114. $request = $smcFunc['db_query']('', '
  115. SELECT id_member
  116. FROM {db_prefix}log_notify
  117. WHERE id_member = {int:current_member}
  118. AND id_board = {int:current_board}
  119. LIMIT 1',
  120. array(
  121. 'current_board' => $board,
  122. 'current_member' => $user_info['id'],
  123. )
  124. );
  125. $context['notification_set'] = $smcFunc['db_num_rows']($request) != 0;
  126. $smcFunc['db_free_result']($request);
  127. // Set the template variables...
  128. $context['board_href'] = $scripturl . '?board=' . $board . '.' . $_REQUEST['start'];
  129. $context['start'] = $_REQUEST['start'];
  130. $context['page_title'] = $txt['notification'];
  131. $context['sub_template'] = 'notify_board';
  132. return;
  133. }
  134. // Turn the board level notification on....
  135. elseif ($_GET['sa'] == 'on')
  136. {
  137. checkSession('get');
  138. // Turn notification on. (note this just blows smoke if it's already on.)
  139. $smcFunc['db_insert']('ignore',
  140. '{db_prefix}log_notify',
  141. array('id_member' => 'int', 'id_board' => 'int'),
  142. array($user_info['id'], $board),
  143. array('id_member', 'id_board')
  144. );
  145. }
  146. // ...or off?
  147. else
  148. {
  149. checkSession('get');
  150. // Turn notification off for this board.
  151. $smcFunc['db_query']('', '
  152. DELETE FROM {db_prefix}log_notify
  153. WHERE id_member = {int:current_member}
  154. AND id_board = {int:current_board}',
  155. array(
  156. 'current_board' => $board,
  157. 'current_member' => $user_info['id'],
  158. )
  159. );
  160. }
  161. // Back to the board!
  162. redirectexit('board=' . $board . '.' . $_REQUEST['start']);
  163. }
  164. /**
  165. * Turn off/on unread replies subscription for a topic
  166. * Must be called with a topic specified in the URL.
  167. * The sub-action can be 'on', 'off', or nothing for what to do.
  168. * Requires the mark_any_notify permission.
  169. * Upon successful completion of action will direct user back to topic.
  170. * Accessed via ?action=disregardtopic.
  171. */
  172. function TopicDisregard()
  173. {
  174. global $smcFunc, $user_info, $topic, $modSettings;
  175. // Let's do something only if the function is enabled
  176. if (!$user_info['is_guest'] && $modSettings['enable_disregard'])
  177. {
  178. checkSession('get');
  179. if (isset($_GET['sa']))
  180. {
  181. $request = $smcFunc['db_query']('', '
  182. SELECT id_member, id_topic, id_msg, disregarded
  183. FROM {db_prefix}log_topics
  184. WHERE id_member = {int:current_user}
  185. AND id_topic = {int:current_topic}',
  186. array(
  187. 'current_user' => $user_info['id'],
  188. 'current_topic' => $topic,
  189. )
  190. );
  191. $log = $smcFunc['db_fetch_assoc']($request);
  192. $smcFunc['db_free_result']($request);
  193. if (empty($log))
  194. {
  195. $insert = true;
  196. $log['disregarded'] = $_GET['sa'] == 'on' ? 1 : 0;
  197. }
  198. else
  199. {
  200. $insert = false;
  201. $log = array(
  202. 'id_member' => $user_info['id'],
  203. 'id_topic' => $topic,
  204. 'id_msg' => 0,
  205. 'disregarded' => $_GET['sa'] == 'on' ? 1 : 0,
  206. );
  207. }
  208. $smcFunc['db_insert']($insert ? 'insert' : 'replace',
  209. '{db_prefix}log_topics',
  210. array(
  211. 'id_member' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'disregarded' => 'int',
  212. ),
  213. $log,
  214. array('id_member', 'id_topic')
  215. );
  216. }
  217. }
  218. // Back to the topic.
  219. redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
  220. }
  221. ?>