PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/controllers/Announce.controller.php

https://github.com/Arantor/Elkarte
PHP | 257 lines | 161 code | 40 blank | 56 comment | 14 complexity | 3b91564502f7abe19961e385821b6c4a MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * Handles announce topic functionality.
  16. *
  17. */
  18. if (!defined('ELKARTE'))
  19. die('No access...');
  20. /**
  21. * Default (sub)action for ?action=announce
  22. */
  23. function action_announce()
  24. {
  25. // default for action=announce: action_selectgroup() function.
  26. action_selectgroup();
  27. }
  28. /**
  29. * Set up the context for the announce topic function (action=announce).
  30. * This function is called before the flow is redirected to action_selectgroup() or action_send().
  31. *
  32. * checks the topic announcement permissions and loads the announcement template.
  33. * requires the announce_topic permission.
  34. * uses the ManageMembers template and Post language file.
  35. */
  36. function pre_announce()
  37. {
  38. global $context, $txt, $topic;
  39. isAllowedTo('announce_topic');
  40. validateSession();
  41. if (empty($topic))
  42. fatal_lang_error('topic_gone', false);
  43. loadLanguage('Post');
  44. loadTemplate('Announce');
  45. $context['page_title'] = $txt['announce_topic'];
  46. }
  47. /**
  48. * Allow a user to chose the membergroups to send the announcement to.
  49. * Lets the user select the membergroups that will receive the topic announcement.
  50. * Accessed by action=announce;sa=selectgroup
  51. */
  52. function action_selectgroup()
  53. {
  54. global $txt, $context, $topic, $board, $board_info, $smcFunc;
  55. $groups = array_merge($board_info['groups'], array(1));
  56. foreach ($groups as $id => $group)
  57. $groups[$id] = (int) $group;
  58. require_once(SUBSDIR . '/Membergroups.subs.php');
  59. $context['groups'] = array();
  60. if (in_array(0, $groups))
  61. {
  62. $context['groups'][0] = array(
  63. 'id' => 0,
  64. 'name' => $txt['announce_regular_members'],
  65. 'member_count' => 'n/a',
  66. );
  67. }
  68. // Get all membergroups that have access to the board the announcement was made on.
  69. $request = $smcFunc['db_query']('', '
  70. SELECT mg.id_group, COUNT(mem.id_member) AS num_members
  71. FROM {db_prefix}membergroups AS mg
  72. LEFT JOIN {db_prefix}members AS mem ON (mem.id_group = mg.id_group OR FIND_IN_SET(mg.id_group, mem.additional_groups) != 0 OR mg.id_group = mem.id_post_group)
  73. WHERE mg.id_group IN ({array_int:group_list})
  74. GROUP BY mg.id_group',
  75. array(
  76. 'group_list' => $groups,
  77. 'newbie_id_group' => 4,
  78. )
  79. );
  80. while ($row = $smcFunc['db_fetch_assoc']($request))
  81. {
  82. $context['groups'][$row['id_group']] = array(
  83. 'id' => $row['id_group'],
  84. 'name' => '',
  85. 'member_count' => $row['num_members'],
  86. );
  87. }
  88. $smcFunc['db_free_result']($request);
  89. // Now get the membergroup names.
  90. $groups_info = membergroupsById($groups, 0);
  91. foreach ($groups_info as $id_group => $group_info)
  92. $context['groups'][$id_group]['name'] = $group_info['group_name'];
  93. // Get the subject of the topic we're about to announce.
  94. $request = $smcFunc['db_query']('', '
  95. SELECT m.subject
  96. FROM {db_prefix}topics AS t
  97. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  98. WHERE t.id_topic = {int:current_topic}',
  99. array(
  100. 'current_topic' => $topic,
  101. )
  102. );
  103. list ($context['topic_subject']) = $smcFunc['db_fetch_row']($request);
  104. $smcFunc['db_free_result']($request);
  105. censorText($context['announce_topic']['subject']);
  106. $context['move'] = isset($_REQUEST['move']) ? 1 : 0;
  107. $context['go_back'] = isset($_REQUEST['goback']) ? 1 : 0;
  108. $context['sub_template'] = 'announce';
  109. }
  110. /**
  111. * Send the announcement in chunks.
  112. *
  113. * splits the members to be sent a topic announcement into chunks.
  114. * composes notification messages in all languages needed.
  115. * does the actual sending of the topic announcements in chunks.
  116. * calculates a rough estimate of the percentage items sent.
  117. * Accessed by action=announce;sa=send
  118. */
  119. function action_send()
  120. {
  121. global $topic, $board, $board_info, $context, $modSettings;
  122. global $language, $scripturl, $txt, $user_info, $smcFunc;
  123. checkSession();
  124. $context['start'] = empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
  125. $groups = array_merge($board_info['groups'], array(1));
  126. if (isset($_POST['membergroups']))
  127. $_POST['who'] = explode(',', $_POST['membergroups']);
  128. // Check whether at least one membergroup was selected.
  129. if (empty($_POST['who']))
  130. fatal_lang_error('no_membergroup_selected');
  131. // Make sure all membergroups are integers and can access the board of the announcement.
  132. foreach ($_POST['who'] as $id => $mg)
  133. $_POST['who'][$id] = in_array((int) $mg, $groups) ? (int) $mg : 0;
  134. // Get the topic subject and censor it.
  135. $request = $smcFunc['db_query']('', '
  136. SELECT m.id_msg, m.subject, m.body
  137. FROM {db_prefix}topics AS t
  138. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  139. WHERE t.id_topic = {int:current_topic}',
  140. array(
  141. 'current_topic' => $topic,
  142. )
  143. );
  144. list ($id_msg, $context['topic_subject'], $message) = $smcFunc['db_fetch_row']($request);
  145. $smcFunc['db_free_result']($request);
  146. censorText($context['topic_subject']);
  147. censorText($message);
  148. $message = trim(un_htmlspecialchars(strip_tags(strtr(parse_bbc($message, false, $id_msg), array('<br />' => "\n", '</div>' => "\n", '</li>' => "\n", '&#91;' => '[', '&#93;' => ']')))));
  149. // We need this in order to be able send emails.
  150. require_once(SUBSDIR . '/Mail.subs.php');
  151. // Select the email addresses for this batch.
  152. $request = $smcFunc['db_query']('', '
  153. SELECT mem.id_member, mem.email_address, mem.lngfile
  154. FROM {db_prefix}members AS mem
  155. WHERE (mem.id_group IN ({array_int:group_list}) OR mem.id_post_group IN ({array_int:group_list}) OR FIND_IN_SET({raw:additional_group_list}, mem.additional_groups) != 0)' . (!empty($modSettings['allow_disableAnnounce']) ? '
  156. AND mem.notify_announcements = {int:notify_announcements}' : '') . '
  157. AND mem.is_activated = {int:is_activated}
  158. AND mem.id_member > {int:start}
  159. ORDER BY mem.id_member
  160. LIMIT {int:chunk_size}',
  161. array(
  162. 'group_list' => $_POST['who'],
  163. 'notify_announcements' => 1,
  164. 'is_activated' => 1,
  165. 'start' => $context['start'],
  166. 'additional_group_list' => implode(', mem.additional_groups) != 0 OR FIND_IN_SET(', $_POST['who']),
  167. // @todo Might need an interface?
  168. 'chunk_size' => empty($modSettings['mail_queue']) ? 50 : 500,
  169. )
  170. );
  171. // All members have received a mail. Go to the next screen.
  172. if ($smcFunc['db_num_rows']($request) == 0)
  173. {
  174. logAction('announce_topic', array('topic' => $topic), 'user');
  175. if (!empty($_REQUEST['move']) && allowedTo('move_any'))
  176. redirectexit('action=movetopic;topic=' . $topic . '.0' . (empty($_REQUEST['goback']) ? '' : ';goback'));
  177. elseif (!empty($_REQUEST['goback']))
  178. redirectexit('topic=' . $topic . '.new;boardseen#new', isBrowser('ie'));
  179. else
  180. redirectexit('board=' . $board . '.0');
  181. }
  182. $announcements = array();
  183. // Loop through all members that'll receive an announcement in this batch.
  184. while ($row = $smcFunc['db_fetch_assoc']($request))
  185. {
  186. $cur_language = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];
  187. // If the language wasn't defined yet, load it and compose a notification message.
  188. if (!isset($announcements[$cur_language]))
  189. {
  190. $replacements = array(
  191. 'TOPICSUBJECT' => $context['topic_subject'],
  192. 'MESSAGE' => $message,
  193. 'TOPICLINK' => $scripturl . '?topic=' . $topic . '.0',
  194. );
  195. $emaildata = loadEmailTemplate('new_announcement', $replacements, $cur_language);
  196. $announcements[$cur_language] = array(
  197. 'subject' => $emaildata['subject'],
  198. 'body' => $emaildata['body'],
  199. 'recipients' => array(),
  200. );
  201. }
  202. $announcements[$cur_language]['recipients'][$row['id_member']] = $row['email_address'];
  203. $context['start'] = $row['id_member'];
  204. }
  205. $smcFunc['db_free_result']($request);
  206. // For each language send a different mail - low priority...
  207. foreach ($announcements as $lang => $mail)
  208. sendmail($mail['recipients'], $mail['subject'], $mail['body'], null, null, false, 5);
  209. $context['percentage_done'] = round(100 * $context['start'] / $modSettings['latestMember'], 1);
  210. $context['move'] = empty($_REQUEST['move']) ? 0 : 1;
  211. $context['go_back'] = empty($_REQUEST['goback']) ? 0 : 1;
  212. $context['membergroups'] = implode(',', $_POST['who']);
  213. $context['sub_template'] = 'announcement_send';
  214. // Go back to the correct language for the user ;).
  215. if (!empty($modSettings['userLanguage']))
  216. loadLanguage('Post');
  217. }