PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/controllers/MoveTopic.controller.php

https://github.com/Arantor/Elkarte
PHP | 376 lines | 250 code | 53 blank | 73 comment | 58 complexity | 52864fd0f4892794c396bd6c5b855d56 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. */
  16. if (!defined('ELKARTE'))
  17. die('No access...');
  18. /**
  19. * This function allows to move a topic, making sure to ask the moderator
  20. * to give reason for topic move.
  21. * It must be called with a topic specified. (that is, global $topic must
  22. * be set... @todo fix this thing.)
  23. * If the member is the topic starter requires the move_own permission,
  24. * otherwise the move_any permission.
  25. * Accessed via ?action=movetopic.
  26. *
  27. * @uses the MoveTopic template, main sub-template.
  28. */
  29. function action_movetopic()
  30. {
  31. global $txt, $board, $topic, $user_info, $context, $language, $scripturl, $settings, $smcFunc, $modSettings;
  32. global $cat_tree, $boards, $boardList;
  33. if (empty($topic))
  34. fatal_lang_error('no_access', false);
  35. // Retrieve the basic topic information for whats being moved
  36. require_once(SUBSDIR . '/Topic.subs.php');
  37. $topic_info = getTopicInfo($topic, true);
  38. if ($topic_info === false)
  39. fatal_lang_error('topic_gone', false);
  40. $context['is_approved'] = $topic_info['approved'];
  41. $context['subject'] = $topic_info['subject'];
  42. // Can they see it - if not approved?
  43. if ($modSettings['postmod_active'] && !$context['is_approved'])
  44. isAllowedTo('approve_posts');
  45. // Are they allowed to actually move any topics or even their own?
  46. if (!allowedTo('move_any') && ($topic_info['id_member_started'] == $user_info['id'] && !allowedTo('move_own')))
  47. fatal_lang_error('cannot_move_any', false);
  48. loadTemplate('MoveTopic');
  49. // Get a list of boards this moderator can move to.
  50. $request = $smcFunc['db_query']('order_by_board_order', '
  51. SELECT b.id_board, b.name, b.child_level, c.name AS cat_name, c.id_cat
  52. FROM {db_prefix}boards AS b
  53. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  54. WHERE {query_see_board}
  55. AND b.redirect = {string:blank_redirect}',
  56. array(
  57. 'blank_redirect' => '',
  58. 'current_board' => $board,
  59. )
  60. );
  61. $number_of_boards = $smcFunc['db_num_rows']($request);
  62. while ($row = $smcFunc['db_fetch_assoc']($request))
  63. {
  64. if (!isset($context['categories'][$row['id_cat']]))
  65. $context['categories'][$row['id_cat']] = array (
  66. 'name' => strip_tags($row['cat_name']),
  67. 'boards' => array(),
  68. );
  69. $context['categories'][$row['id_cat']]['boards'][] = array(
  70. 'id' => $row['id_board'],
  71. 'name' => strip_tags($row['name']),
  72. 'category' => strip_tags($row['cat_name']),
  73. 'child_level' => $row['child_level'],
  74. 'selected' => !empty($_SESSION['move_to_topic']) && $_SESSION['move_to_topic'] == $row['id_board'] && $row['id_board'] != $board,
  75. );
  76. }
  77. $smcFunc['db_free_result']($request);
  78. // No boards?
  79. if (empty($context['categories']) || (!empty($number_of_boards) && $number_of_boards == 1))
  80. fatal_lang_error('moveto_noboards', false);
  81. $context['page_title'] = $txt['move_topic'];
  82. $context['linktree'][] = array(
  83. 'url' => $scripturl . '?topic=' . $topic . '.0',
  84. 'name' => $context['subject'],
  85. );
  86. $context['linktree'][] = array(
  87. 'url' => '#',
  88. 'name' => $txt['move_topic'],
  89. );
  90. $context['back_to_topic'] = isset($_REQUEST['goback']);
  91. // Ugly !
  92. if ($user_info['language'] != $language)
  93. {
  94. loadLanguage('index', $language);
  95. $temp = $txt['movetopic_default'];
  96. loadLanguage('index');
  97. $txt['movetopic_default'] = $temp;
  98. }
  99. // We will need this
  100. require_once(SUBSDIR . '/Topic.subs.php');
  101. moveTopicConcurrence();
  102. // Register this form and get a sequence number in $context.
  103. checkSubmitOnce('register');
  104. }
  105. /**
  106. * Execute the move of a topic.
  107. * It is called on the submit of action_movetopic.
  108. * This function logs that topics have been moved in the moderation log.
  109. * If the member is the topic starter requires the move_own permission,
  110. * otherwise requires the move_any permission.
  111. * Upon successful completion redirects to message index.
  112. * Accessed via ?action=movetopic2.
  113. *
  114. * @uses subs/Post.subs.php.
  115. */
  116. function action_movetopic2()
  117. {
  118. global $txt, $board, $topic, $scripturl, $modSettings, $context;
  119. global $board, $language, $user_info, $smcFunc;
  120. if (empty($topic))
  121. fatal_lang_error('no_access', false);
  122. // You can't choose to have a redirection topic and use an empty reason.
  123. if (isset($_POST['postRedirect']) && (!isset($_POST['reason']) || trim($_POST['reason']) == ''))
  124. fatal_lang_error('movetopic_no_reason', false);
  125. // You have to tell us were you are moving to
  126. if (!isset($_POST['toboard']))
  127. fatal_lang_error('movetopic_no_board', false);
  128. // We will need this
  129. require_once(SUBSDIR . '/Topic.subs.php');
  130. moveTopicConcurrence();
  131. // Make sure this form hasn't been submitted before.
  132. checkSubmitOnce('check');
  133. // Get the basic details on this topic
  134. $topic_info = getTopicInfo($topic);
  135. $context['is_approved'] = $topic_info['approved'];
  136. // Can they see it?
  137. if (!$context['is_approved'])
  138. isAllowedTo('approve_posts');
  139. // Can they move topics on this board?
  140. if (!allowedTo('move_any'))
  141. {
  142. if ($topic_info['id_member_started'] == $user_info['id'])
  143. {
  144. isAllowedTo('move_own');
  145. $boards = array_merge(boardsAllowedTo('move_own'), boardsAllowedTo('move_any'));
  146. }
  147. else
  148. isAllowedTo('move_any');
  149. }
  150. else
  151. $boards = boardsAllowedTo('move_any');
  152. // If this topic isn't approved don't let them move it if they can't approve it!
  153. if ($modSettings['postmod_active'] && !$context['is_approved'] && !allowedTo('approve_posts'))
  154. {
  155. // Only allow them to move it to other boards they can't approve it in.
  156. $can_approve = boardsAllowedTo('approve_posts');
  157. $boards = array_intersect($boards, $can_approve);
  158. }
  159. checkSession();
  160. require_once(SUBSDIR . '/Post.subs.php');
  161. // The destination board must be numeric.
  162. $toboard = (int) $_POST['toboard'];
  163. // Make sure they can see the board they are trying to move to (and get whether posts count in the target board).
  164. $request = $smcFunc['db_query']('', '
  165. SELECT b.count_posts, b.name, m.subject
  166. FROM {db_prefix}boards AS b
  167. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
  168. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  169. WHERE {query_see_board}
  170. AND b.id_board = {int:to_board}
  171. AND b.redirect = {string:blank_redirect}
  172. LIMIT 1',
  173. array(
  174. 'current_topic' => $topic,
  175. 'to_board' => $toboard,
  176. 'blank_redirect' => '',
  177. )
  178. );
  179. if ($smcFunc['db_num_rows']($request) == 0)
  180. fatal_lang_error('no_board');
  181. list ($pcounter, $board_name, $subject) = $smcFunc['db_fetch_row']($request);
  182. $smcFunc['db_free_result']($request);
  183. // Remember this for later.
  184. $_SESSION['move_to_topic'] = $toboard;
  185. // Rename the topic...
  186. if (isset($_POST['reset_subject'], $_POST['custom_subject']) && $_POST['custom_subject'] != '')
  187. {
  188. $custom_subject = strtr($smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['custom_subject'])), array("\r" => '', "\n" => '', "\t" => ''));
  189. // Keep checking the length.
  190. if ($smcFunc['strlen']($custom_subject) > 100)
  191. $custom_subject = $smcFunc['substr']($custom_subject, 0, 100);
  192. // If it's still valid move onwards and upwards.
  193. if ($custom_subject != '')
  194. {
  195. if (isset($_POST['enforce_subject']))
  196. {
  197. // Get a response prefix, but in the forum's default language.
  198. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
  199. {
  200. if ($language === $user_info['language'])
  201. $context['response_prefix'] = $txt['response_prefix'];
  202. else
  203. {
  204. loadLanguage('index', $language, false);
  205. $context['response_prefix'] = $txt['response_prefix'];
  206. loadLanguage('index');
  207. }
  208. cache_put_data('response_prefix', $context['response_prefix'], 600);
  209. }
  210. $smcFunc['db_query']('', '
  211. UPDATE {db_prefix}messages
  212. SET subject = {string:subject}
  213. WHERE id_topic = {int:current_topic}',
  214. array(
  215. 'current_topic' => $topic,
  216. 'subject' => $context['response_prefix'] . $custom_subject,
  217. )
  218. );
  219. }
  220. $smcFunc['db_query']('', '
  221. UPDATE {db_prefix}messages
  222. SET subject = {string:custom_subject}
  223. WHERE id_msg = {int:id_first_msg}',
  224. array(
  225. 'id_first_msg' => $topic_info['id_first_msg'],
  226. 'custom_subject' => $custom_subject,
  227. )
  228. );
  229. // Fix the subject cache.
  230. updateStats('subject', $topic, $custom_subject);
  231. }
  232. }
  233. // Create a link to this in the old board.
  234. // @todo Does this make sense if the topic was unapproved before? I'd just about say so.
  235. if (isset($_POST['postRedirect']))
  236. {
  237. // Should be in the boardwide language.
  238. if ($user_info['language'] != $language)
  239. loadLanguage('index', $language);
  240. $reason = $smcFunc['htmlspecialchars']($_POST['reason'], ENT_QUOTES);
  241. preparsecode($reason);
  242. // Add a URL onto the message.
  243. $reason = strtr($reason, array(
  244. $txt['movetopic_auto_board'] => '[url=' . $scripturl . '?board=' . $toboard . '.0]' . $board_name . '[/url]',
  245. $txt['movetopic_auto_topic'] => '[iurl]' . $scripturl . '?topic=' . $topic . '.0[/iurl]'
  246. ));
  247. // auto remove this MOVED redirection topic in the future?
  248. $redirect_expires = !empty($_POST['redirect_expires']) ? ((int) ($_POST['redirect_expires'] * 60) + time()) : 0;
  249. // redirect to the MOVED topic from topic list?
  250. $redirect_topic = isset($_POST['redirect_topic']) ? $topic : 0;
  251. $msgOptions = array(
  252. 'subject' => $txt['moved'] . ': ' . $subject,
  253. 'body' => $reason,
  254. 'icon' => 'moved',
  255. 'smileys_enabled' => 1,
  256. );
  257. $topicOptions = array(
  258. 'board' => $board,
  259. 'lock_mode' => 1,
  260. 'mark_as_read' => true,
  261. 'redirect_expires' => $redirect_expires,
  262. 'redirect_topic' => $redirect_topic,
  263. );
  264. $posterOptions = array(
  265. 'id' => $user_info['id'],
  266. 'update_post_count' => empty($pcounter),
  267. );
  268. createPost($msgOptions, $topicOptions, $posterOptions);
  269. }
  270. $request = $smcFunc['db_query']('', '
  271. SELECT count_posts
  272. FROM {db_prefix}boards
  273. WHERE id_board = {int:current_board}
  274. LIMIT 1',
  275. array(
  276. 'current_board' => $board,
  277. )
  278. );
  279. list ($pcounter_from) = $smcFunc['db_fetch_row']($request);
  280. $smcFunc['db_free_result']($request);
  281. if ($pcounter_from != $pcounter)
  282. {
  283. $request = $smcFunc['db_query']('', '
  284. SELECT id_member
  285. FROM {db_prefix}messages
  286. WHERE id_topic = {int:current_topic}
  287. AND approved = {int:is_approved}',
  288. array(
  289. 'current_topic' => $topic,
  290. 'is_approved' => 1,
  291. )
  292. );
  293. $posters = array();
  294. while ($row = $smcFunc['db_fetch_assoc']($request))
  295. {
  296. if (!isset($posters[$row['id_member']]))
  297. $posters[$row['id_member']] = 0;
  298. $posters[$row['id_member']]++;
  299. }
  300. $smcFunc['db_free_result']($request);
  301. foreach ($posters as $id_member => $posts)
  302. {
  303. // The board we're moving from counted posts, but not to.
  304. if (empty($pcounter_from))
  305. updateMemberData($id_member, array('posts' => 'posts - ' . $posts));
  306. // The reverse: from didn't, to did.
  307. else
  308. updateMemberData($id_member, array('posts' => 'posts + ' . $posts));
  309. }
  310. }
  311. // Do the move (includes statistics update needed for the redirect topic).
  312. moveTopics($topic, $toboard);
  313. // Log that they moved this topic.
  314. if (!allowedTo('move_own') || $topic_info['id_member_started'] != $user_info['id'])
  315. logAction('move', array('topic' => $topic, 'board_from' => $board, 'board_to' => $toboard));
  316. // Notify people that this topic has been moved?
  317. sendNotifications($topic, 'move');
  318. // Why not go back to the original board in case they want to keep moving?
  319. if (!isset($_REQUEST['goback']))
  320. redirectexit('board=' . $board . '.0');
  321. else
  322. redirectexit('topic=' . $topic . '.0');
  323. }