Notify.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 2012 Simple Machines Forum 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('Hacking attempt...');
  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. ?>