Notify.php 6.3 KB

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