MoveTopic.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. <?php
  2. /**
  3. * This file contains the functions required to move topics from one board to
  4. * another board.
  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. * This function allows to move a topic, making sure to ask the moderator
  19. * to give reason for topic move.
  20. * It must be called with a topic specified. (that is, global $topic must
  21. * be set... @todo fix this thing.)
  22. * If the member is the topic starter requires the move_own permission,
  23. * otherwise the move_any permission.
  24. * Accessed via ?action=movetopic.
  25. *
  26. * @uses the MoveTopic template, main sub-template.
  27. */
  28. function MoveTopic()
  29. {
  30. global $txt, $board, $topic, $user_info, $context, $language, $scripturl, $settings, $smcFunc, $modSettings, $sourcedir;
  31. if (empty($topic))
  32. fatal_lang_error('no_access', false);
  33. $request = $smcFunc['db_query']('', '
  34. SELECT t.id_member_started, ms.subject, t.approved
  35. FROM {db_prefix}topics AS t
  36. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
  37. WHERE t.id_topic = {int:current_topic}
  38. LIMIT 1',
  39. array(
  40. 'current_topic' => $topic,
  41. )
  42. );
  43. list ($id_member_started, $context['subject'], $context['is_approved']) = $smcFunc['db_fetch_row']($request);
  44. $smcFunc['db_free_result']($request);
  45. // Can they see it - if not approved?
  46. if ($modSettings['postmod_active'] && !$context['is_approved'])
  47. isAllowedTo('approve_posts');
  48. // Permission check!
  49. // @todo
  50. if (!allowedTo('move_any'))
  51. {
  52. if ($id_member_started == $user_info['id'])
  53. {
  54. isAllowedTo('move_own');
  55. }
  56. else
  57. isAllowedTo('move_any');
  58. }
  59. $context['move_any'] = $user_info['is_admin'] || $modSettings['topic_move_any'];
  60. $boards = array();
  61. if (!$context['move_any'])
  62. {
  63. $boards = array_diff(boardsAllowedTo('post_new'), array($board));
  64. if (empty($boards))
  65. {
  66. // No boards? Too bad...
  67. fatal_lang_error('moveto_no_boards');
  68. }
  69. }
  70. loadTemplate('MoveTopic');
  71. $options = array(
  72. 'not_redirection' => true,
  73. );
  74. if (!empty($_SESSION['move_to_topic']) && $_SESSION['move_to_topic'] != $board)
  75. $options['selected_board'] = $_SESSION['move_to_topic'];
  76. if (!$context['move_any'])
  77. $options['included_boards'] = $boards;
  78. require_once($sourcedir . '/Subs-MessageIndex.php');
  79. $context['categories'] = getBoardList($options);
  80. $context['page_title'] = $txt['move_topic'];
  81. $context['linktree'][] = array(
  82. 'url' => $scripturl . '?topic=' . $topic . '.0',
  83. 'name' => $context['subject'],
  84. );
  85. $context['linktree'][] = array(
  86. 'name' => $txt['move_topic'],
  87. );
  88. $context['back_to_topic'] = isset($_REQUEST['goback']);
  89. if ($user_info['language'] != $language)
  90. {
  91. loadLanguage('index', $language);
  92. $temp = $txt['movetopic_default'];
  93. loadLanguage('index');
  94. $txt['movetopic_default'] = $temp;
  95. }
  96. moveTopicConcurrence();
  97. // Register this form and get a sequence number in $context.
  98. checkSubmitOnce('register');
  99. }
  100. /**
  101. * Execute the move of a topic.
  102. * It is called on the submit of MoveTopic.
  103. * This function logs that topics have been moved in the moderation log.
  104. * If the member is the topic starter requires the move_own permission,
  105. * otherwise requires the move_any permission.
  106. * Upon successful completion redirects to message index.
  107. * Accessed via ?action=movetopic2.
  108. *
  109. * @uses Subs-Post.php.
  110. */
  111. function MoveTopic2()
  112. {
  113. global $txt, $board, $topic, $scripturl, $sourcedir, $modSettings, $context;
  114. global $board, $language, $user_info, $smcFunc;
  115. if (empty($topic))
  116. fatal_lang_error('no_access', false);
  117. // You can't choose to have a redirection topic and use an empty reason.
  118. if (isset($_POST['postRedirect']) && (!isset($_POST['reason']) || trim($_POST['reason']) == ''))
  119. fatal_lang_error('movetopic_no_reason', false);
  120. moveTopicConcurrence();
  121. // Make sure this form hasn't been submitted before.
  122. checkSubmitOnce('check');
  123. $request = $smcFunc['db_query']('', '
  124. SELECT id_member_started, id_first_msg, approved
  125. FROM {db_prefix}topics
  126. WHERE id_topic = {int:current_topic}
  127. LIMIT 1',
  128. array(
  129. 'current_topic' => $topic,
  130. )
  131. );
  132. list ($id_member_started, $id_first_msg, $context['is_approved']) = $smcFunc['db_fetch_row']($request);
  133. $smcFunc['db_free_result']($request);
  134. // Can they see it?
  135. if (!$context['is_approved'])
  136. isAllowedTo('approve_posts');
  137. // Can they move topics on this board?
  138. if (!allowedTo('move_any'))
  139. {
  140. if ($id_member_started == $user_info['id'])
  141. {
  142. isAllowedTo('move_own');
  143. $boards = array_merge(boardsAllowedTo('move_own'), boardsAllowedTo('move_any'));
  144. }
  145. else
  146. isAllowedTo('move_any');
  147. }
  148. else
  149. $boards = boardsAllowedTo('move_any');
  150. // If this topic isn't approved don't let them move it if they can't approve it!
  151. if ($modSettings['postmod_active'] && !$context['is_approved'] && !allowedTo('approve_posts'))
  152. {
  153. // Only allow them to move it to other boards they can't approve it in.
  154. $can_approve = boardsAllowedTo('approve_posts');
  155. $boards = array_intersect($boards, $can_approve);
  156. }
  157. checkSession();
  158. require_once($sourcedir . '/Subs-Post.php');
  159. // The destination board must be numeric.
  160. $_POST['toboard'] = (int) $_POST['toboard'];
  161. // Make sure they can see the board they are trying to move to (and get whether posts count in the target board).
  162. $request = $smcFunc['db_query']('', '
  163. SELECT b.count_posts, b.name, m.subject
  164. FROM {db_prefix}boards AS b
  165. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
  166. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  167. WHERE {query_see_board}
  168. AND b.id_board = {int:to_board}
  169. AND b.redirect = {string:blank_redirect}
  170. LIMIT 1',
  171. array(
  172. 'current_topic' => $topic,
  173. 'to_board' => $_POST['toboard'],
  174. 'blank_redirect' => '',
  175. )
  176. );
  177. if ($smcFunc['db_num_rows']($request) == 0)
  178. fatal_lang_error('no_board');
  179. list ($pcounter, $board_name, $subject) = $smcFunc['db_fetch_row']($request);
  180. $smcFunc['db_free_result']($request);
  181. // Remember this for later.
  182. $_SESSION['move_to_topic'] = $_POST['toboard'];
  183. // Rename the topic...
  184. if (isset($_POST['reset_subject'], $_POST['custom_subject']) && $_POST['custom_subject'] != '')
  185. {
  186. $_POST['custom_subject'] = strtr($smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['custom_subject'])), array("\r" => '', "\n" => '', "\t" => ''));
  187. // Keep checking the length.
  188. if ($smcFunc['strlen']($_POST['custom_subject']) > 100)
  189. $_POST['custom_subject'] = $smcFunc['substr']($_POST['custom_subject'], 0, 100);
  190. // If it's still valid move onwards and upwards.
  191. if ($_POST['custom_subject'] != '')
  192. {
  193. if (isset($_POST['enforce_subject']))
  194. {
  195. // Get a response prefix, but in the forum's default language.
  196. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
  197. {
  198. if ($language === $user_info['language'])
  199. $context['response_prefix'] = $txt['response_prefix'];
  200. else
  201. {
  202. loadLanguage('index', $language, false);
  203. $context['response_prefix'] = $txt['response_prefix'];
  204. loadLanguage('index');
  205. }
  206. cache_put_data('response_prefix', $context['response_prefix'], 600);
  207. }
  208. $smcFunc['db_query']('', '
  209. UPDATE {db_prefix}messages
  210. SET subject = {string:subject}
  211. WHERE id_topic = {int:current_topic}',
  212. array(
  213. 'current_topic' => $topic,
  214. 'subject' => $context['response_prefix'] . $_POST['custom_subject'],
  215. )
  216. );
  217. }
  218. $smcFunc['db_query']('', '
  219. UPDATE {db_prefix}messages
  220. SET subject = {string:custom_subject}
  221. WHERE id_msg = {int:id_first_msg}',
  222. array(
  223. 'id_first_msg' => $id_first_msg,
  224. 'custom_subject' => $_POST['custom_subject'],
  225. )
  226. );
  227. // Fix the subject cache.
  228. updateStats('subject', $topic, $_POST['custom_subject']);
  229. }
  230. }
  231. // Create a link to this in the old board.
  232. // @todo Does this make sense if the topic was unapproved before? I'd just about say so.
  233. if (isset($_POST['postRedirect']))
  234. {
  235. // Should be in the boardwide language.
  236. if ($user_info['language'] != $language)
  237. loadLanguage('index', $language);
  238. $_POST['reason'] = $smcFunc['htmlspecialchars']($_POST['reason'], ENT_QUOTES);
  239. preparsecode($_POST['reason']);
  240. // Add a URL onto the message.
  241. $_POST['reason'] = strtr($_POST['reason'], array(
  242. $txt['movetopic_auto_board'] => '[url=' . $scripturl . '?board=' . $_POST['toboard'] . '.0]' . $board_name . '[/url]',
  243. $txt['movetopic_auto_topic'] => '[iurl]' . $scripturl . '?topic=' . $topic . '.0[/iurl]'
  244. ));
  245. // auto remove this MOVED redirection topic in the future?
  246. $redirect_expires = !empty($_POST['redirect_expires']) ? ((int) ($_POST['redirect_expires'] * 60) + time()) : 0;
  247. // redirect to the MOVED topic from topic list?
  248. $redirect_topic = isset($_POST['redirect_topic']) ? $topic : 0;
  249. $msgOptions = array(
  250. 'subject' => $txt['moved'] . ': ' . $subject,
  251. 'body' => $_POST['reason'],
  252. 'icon' => 'moved',
  253. 'smileys_enabled' => 1,
  254. );
  255. $topicOptions = array(
  256. 'board' => $board,
  257. 'lock_mode' => 1,
  258. 'mark_as_read' => true,
  259. 'redirect_expires' => $redirect_expires,
  260. 'redirect_topic' => $redirect_topic,
  261. );
  262. $posterOptions = array(
  263. 'id' => $user_info['id'],
  264. 'update_post_count' => empty($pcounter),
  265. );
  266. createPost($msgOptions, $topicOptions, $posterOptions);
  267. }
  268. $request = $smcFunc['db_query']('', '
  269. SELECT count_posts
  270. FROM {db_prefix}boards
  271. WHERE id_board = {int:current_board}
  272. LIMIT 1',
  273. array(
  274. 'current_board' => $board,
  275. )
  276. );
  277. list ($pcounter_from) = $smcFunc['db_fetch_row']($request);
  278. $smcFunc['db_free_result']($request);
  279. if ($pcounter_from != $pcounter)
  280. {
  281. $request = $smcFunc['db_query']('', '
  282. SELECT id_member
  283. FROM {db_prefix}messages
  284. WHERE id_topic = {int:current_topic}
  285. AND approved = {int:is_approved}',
  286. array(
  287. 'current_topic' => $topic,
  288. 'is_approved' => 1,
  289. )
  290. );
  291. $posters = array();
  292. while ($row = $smcFunc['db_fetch_assoc']($request))
  293. {
  294. if (!isset($posters[$row['id_member']]))
  295. $posters[$row['id_member']] = 0;
  296. $posters[$row['id_member']]++;
  297. }
  298. $smcFunc['db_free_result']($request);
  299. foreach ($posters as $id_member => $posts)
  300. {
  301. // The board we're moving from counted posts, but not to.
  302. if (empty($pcounter_from))
  303. updateMemberData($id_member, array('posts' => 'posts - ' . $posts));
  304. // The reverse: from didn't, to did.
  305. else
  306. updateMemberData($id_member, array('posts' => 'posts + ' . $posts));
  307. }
  308. }
  309. // Do the move (includes statistics update needed for the redirect topic).
  310. moveTopics($topic, $_POST['toboard']);
  311. // Log that they moved this topic.
  312. if (!allowedTo('move_own') || $id_member_started != $user_info['id'])
  313. logAction('move', array('topic' => $topic, 'board_from' => $board, 'board_to' => $_POST['toboard']));
  314. // Notify people that this topic has been moved?
  315. sendNotifications($topic, 'move');
  316. // Why not go back to the original board in case they want to keep moving?
  317. if (!isset($_REQUEST['goback']))
  318. redirectexit('board=' . $board . '.0');
  319. else
  320. redirectexit('topic=' . $topic . '.0');
  321. }
  322. /**
  323. * Moves one or more topics to a specific board. (doesn't check permissions.)
  324. * Determines the source boards for the supplied topics
  325. * Handles the moving of mark_read data
  326. * Updates the posts count of the affected boards
  327. *
  328. * @param type $topics
  329. * @param type $toBoard
  330. * @return type
  331. */
  332. function moveTopics($topics, $toBoard)
  333. {
  334. global $sourcedir, $user_info, $modSettings, $smcFunc;
  335. // Empty array?
  336. if (empty($topics))
  337. return;
  338. // Only a single topic.
  339. if (is_numeric($topics))
  340. $topics = array($topics);
  341. $num_topics = count($topics);
  342. $fromBoards = array();
  343. // Destination board empty or equal to 0?
  344. if (empty($toBoard))
  345. return;
  346. // Are we moving to the recycle board?
  347. $isRecycleDest = !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] == $toBoard;
  348. // Determine the source boards...
  349. $request = $smcFunc['db_query']('', '
  350. SELECT id_board, approved, COUNT(*) AS num_topics, SUM(unapproved_posts) AS unapproved_posts,
  351. SUM(num_replies) AS num_replies
  352. FROM {db_prefix}topics
  353. WHERE id_topic IN ({array_int:topics})
  354. GROUP BY id_board, approved',
  355. array(
  356. 'topics' => $topics,
  357. )
  358. );
  359. // Num of rows = 0 -> no topics found. Num of rows > 1 -> topics are on multiple boards.
  360. if ($smcFunc['db_num_rows']($request) == 0)
  361. return;
  362. while ($row = $smcFunc['db_fetch_assoc']($request))
  363. {
  364. if (!isset($fromBoards[$row['id_board']]['num_posts']))
  365. {
  366. $fromBoards[$row['id_board']] = array(
  367. 'num_posts' => 0,
  368. 'num_topics' => 0,
  369. 'unapproved_posts' => 0,
  370. 'unapproved_topics' => 0,
  371. 'id_board' => $row['id_board']
  372. );
  373. }
  374. // Posts = (num_replies + 1) for each approved topic.
  375. $fromBoards[$row['id_board']]['num_posts'] += $row['num_replies'] + ($row['approved'] ? $row['num_topics'] : 0);
  376. $fromBoards[$row['id_board']]['unapproved_posts'] += $row['unapproved_posts'];
  377. // Add the topics to the right type.
  378. if ($row['approved'])
  379. $fromBoards[$row['id_board']]['num_topics'] += $row['num_topics'];
  380. else
  381. $fromBoards[$row['id_board']]['unapproved_topics'] += $row['num_topics'];
  382. }
  383. $smcFunc['db_free_result']($request);
  384. // Move over the mark_read data. (because it may be read and now not by some!)
  385. $SaveAServer = max(0, $modSettings['maxMsgID'] - 50000);
  386. $request = $smcFunc['db_query']('', '
  387. SELECT lmr.id_member, lmr.id_msg, t.id_topic, IFNULL(lt.unwatched, 0) AS unwatched
  388. FROM {db_prefix}topics AS t
  389. INNER JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board
  390. AND lmr.id_msg > t.id_first_msg AND lmr.id_msg > {int:protect_lmr_msg})
  391. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = lmr.id_member)
  392. WHERE t.id_topic IN ({array_int:topics})
  393. AND lmr.id_msg > IFNULL(lt.id_msg, 0)',
  394. array(
  395. 'protect_lmr_msg' => $SaveAServer,
  396. 'topics' => $topics,
  397. )
  398. );
  399. $log_topics = array();
  400. while ($row = $smcFunc['db_fetch_assoc']($request))
  401. {
  402. $log_topics[] = array($row['id_topic'], $row['id_member'], $row['id_msg'], (is_null($row['unwatched']) ? 0 : $row['unwatched']));
  403. // Prevent queries from getting too big. Taking some steam off.
  404. if (count($log_topics) > 500)
  405. {
  406. $smcFunc['db_insert']('replace',
  407. '{db_prefix}log_topics',
  408. array('id_topic' => 'int', 'id_member' => 'int', 'id_msg' => 'int', 'unwatched' => 'int'),
  409. $log_topics,
  410. array('id_topic', 'id_member')
  411. );
  412. $log_topics = array();
  413. }
  414. }
  415. $smcFunc['db_free_result']($request);
  416. // Now that we have all the topics that *should* be marked read, and by which members...
  417. if (!empty($log_topics))
  418. {
  419. // Insert that information into the database!
  420. $smcFunc['db_insert']('replace',
  421. '{db_prefix}log_topics',
  422. array('id_topic' => 'int', 'id_member' => 'int', 'id_msg' => 'int', 'unwatched' => 'int'),
  423. $log_topics,
  424. array('id_topic', 'id_member')
  425. );
  426. }
  427. // Update the number of posts on each board.
  428. $totalTopics = 0;
  429. $totalPosts = 0;
  430. $totalUnapprovedTopics = 0;
  431. $totalUnapprovedPosts = 0;
  432. foreach ($fromBoards as $stats)
  433. {
  434. $smcFunc['db_query']('', '
  435. UPDATE {db_prefix}boards
  436. SET
  437. num_posts = CASE WHEN {int:num_posts} > num_posts THEN 0 ELSE num_posts - {int:num_posts} END,
  438. num_topics = CASE WHEN {int:num_topics} > num_topics THEN 0 ELSE num_topics - {int:num_topics} END,
  439. unapproved_posts = CASE WHEN {int:unapproved_posts} > unapproved_posts THEN 0 ELSE unapproved_posts - {int:unapproved_posts} END,
  440. unapproved_topics = CASE WHEN {int:unapproved_topics} > unapproved_topics THEN 0 ELSE unapproved_topics - {int:unapproved_topics} END
  441. WHERE id_board = {int:id_board}',
  442. array(
  443. 'id_board' => $stats['id_board'],
  444. 'num_posts' => $stats['num_posts'],
  445. 'num_topics' => $stats['num_topics'],
  446. 'unapproved_posts' => $stats['unapproved_posts'],
  447. 'unapproved_topics' => $stats['unapproved_topics'],
  448. )
  449. );
  450. $totalTopics += $stats['num_topics'];
  451. $totalPosts += $stats['num_posts'];
  452. $totalUnapprovedTopics += $stats['unapproved_topics'];
  453. $totalUnapprovedPosts += $stats['unapproved_posts'];
  454. }
  455. $smcFunc['db_query']('', '
  456. UPDATE {db_prefix}boards
  457. SET
  458. num_topics = num_topics + {int:total_topics},
  459. num_posts = num_posts + {int:total_posts},' . ($isRecycleDest ? '
  460. unapproved_posts = {int:no_unapproved}, unapproved_topics = {int:no_unapproved}' : '
  461. unapproved_posts = unapproved_posts + {int:total_unapproved_posts},
  462. unapproved_topics = unapproved_topics + {int:total_unapproved_topics}') . '
  463. WHERE id_board = {int:id_board}',
  464. array(
  465. 'id_board' => $toBoard,
  466. 'total_topics' => $totalTopics,
  467. 'total_posts' => $totalPosts,
  468. 'total_unapproved_topics' => $totalUnapprovedTopics,
  469. 'total_unapproved_posts' => $totalUnapprovedPosts,
  470. 'no_unapproved' => 0,
  471. )
  472. );
  473. // Move the topic. Done. :P
  474. $smcFunc['db_query']('', '
  475. UPDATE {db_prefix}topics
  476. SET id_board = {int:id_board}' . ($isRecycleDest ? ',
  477. unapproved_posts = {int:no_unapproved}, approved = {int:is_approved}' : '') . '
  478. WHERE id_topic IN ({array_int:topics})',
  479. array(
  480. 'id_board' => $toBoard,
  481. 'topics' => $topics,
  482. 'is_approved' => 1,
  483. 'no_unapproved' => 0,
  484. )
  485. );
  486. // If this was going to the recycle bin, check what messages are being recycled, and remove them from the queue.
  487. if ($isRecycleDest && ($totalUnapprovedTopics || $totalUnapprovedPosts))
  488. {
  489. $request = $smcFunc['db_query']('', '
  490. SELECT id_msg
  491. FROM {db_prefix}messages
  492. WHERE id_topic IN ({array_int:topics})
  493. and approved = {int:not_approved}',
  494. array(
  495. 'topics' => $topics,
  496. 'not_approved' => 0,
  497. )
  498. );
  499. $approval_msgs = array();
  500. while ($row = $smcFunc['db_fetch_assoc']($request))
  501. $approval_msgs[] = $row['id_msg'];
  502. $smcFunc['db_free_result']($request);
  503. // Empty the approval queue for these, as we're going to approve them next.
  504. if (!empty($approval_msgs))
  505. $smcFunc['db_query']('', '
  506. DELETE FROM {db_prefix}approval_queue
  507. WHERE id_msg IN ({array_int:message_list})
  508. AND id_attach = {int:id_attach}',
  509. array(
  510. 'message_list' => $approval_msgs,
  511. 'id_attach' => 0,
  512. )
  513. );
  514. // Get all the current max and mins.
  515. $request = $smcFunc['db_query']('', '
  516. SELECT id_topic, id_first_msg, id_last_msg
  517. FROM {db_prefix}topics
  518. WHERE id_topic IN ({array_int:topics})',
  519. array(
  520. 'topics' => $topics,
  521. )
  522. );
  523. $topicMaxMin = array();
  524. while ($row = $smcFunc['db_fetch_assoc']($request))
  525. {
  526. $topicMaxMin[$row['id_topic']] = array(
  527. 'min' => $row['id_first_msg'],
  528. 'max' => $row['id_last_msg'],
  529. );
  530. }
  531. $smcFunc['db_free_result']($request);
  532. // Check the MAX and MIN are correct.
  533. $request = $smcFunc['db_query']('', '
  534. SELECT id_topic, MIN(id_msg) AS first_msg, MAX(id_msg) AS last_msg
  535. FROM {db_prefix}messages
  536. WHERE id_topic IN ({array_int:topics})
  537. GROUP BY id_topic',
  538. array(
  539. 'topics' => $topics,
  540. )
  541. );
  542. while ($row = $smcFunc['db_fetch_assoc']($request))
  543. {
  544. // If not, update.
  545. if ($row['first_msg'] != $topicMaxMin[$row['id_topic']]['min'] || $row['last_msg'] != $topicMaxMin[$row['id_topic']]['max'])
  546. $smcFunc['db_query']('', '
  547. UPDATE {db_prefix}topics
  548. SET id_first_msg = {int:first_msg}, id_last_msg = {int:last_msg}
  549. WHERE id_topic = {int:selected_topic}',
  550. array(
  551. 'first_msg' => $row['first_msg'],
  552. 'last_msg' => $row['last_msg'],
  553. 'selected_topic' => $row['id_topic'],
  554. )
  555. );
  556. }
  557. $smcFunc['db_free_result']($request);
  558. }
  559. $smcFunc['db_query']('', '
  560. UPDATE {db_prefix}messages
  561. SET id_board = {int:id_board}' . ($isRecycleDest ? ',approved = {int:is_approved}' : '') . '
  562. WHERE id_topic IN ({array_int:topics})',
  563. array(
  564. 'id_board' => $toBoard,
  565. 'topics' => $topics,
  566. 'is_approved' => 1,
  567. )
  568. );
  569. $smcFunc['db_query']('', '
  570. UPDATE {db_prefix}log_reported
  571. SET id_board = {int:id_board}
  572. WHERE id_topic IN ({array_int:topics})',
  573. array(
  574. 'id_board' => $toBoard,
  575. 'topics' => $topics,
  576. )
  577. );
  578. $smcFunc['db_query']('', '
  579. UPDATE {db_prefix}calendar
  580. SET id_board = {int:id_board}
  581. WHERE id_topic IN ({array_int:topics})',
  582. array(
  583. 'id_board' => $toBoard,
  584. 'topics' => $topics,
  585. )
  586. );
  587. // Mark target board as seen, if it was already marked as seen before.
  588. $request = $smcFunc['db_query']('', '
  589. SELECT (IFNULL(lb.id_msg, 0) >= b.id_msg_updated) AS isSeen
  590. FROM {db_prefix}boards AS b
  591. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
  592. WHERE b.id_board = {int:id_board}',
  593. array(
  594. 'current_member' => $user_info['id'],
  595. 'id_board' => $toBoard,
  596. )
  597. );
  598. list ($isSeen) = $smcFunc['db_fetch_row']($request);
  599. $smcFunc['db_free_result']($request);
  600. if (!empty($isSeen) && !$user_info['is_guest'])
  601. {
  602. $smcFunc['db_insert']('replace',
  603. '{db_prefix}log_boards',
  604. array('id_board' => 'int', 'id_member' => 'int', 'id_msg' => 'int'),
  605. array($toBoard, $user_info['id'], $modSettings['maxMsgID']),
  606. array('id_board', 'id_member')
  607. );
  608. }
  609. // Update the cache?
  610. if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 3)
  611. foreach ($topics as $topic_id)
  612. cache_put_data('topic_board-' . $topic_id, null, 120);
  613. require_once($sourcedir . '/Subs-Post.php');
  614. $updates = array_keys($fromBoards);
  615. $updates[] = $toBoard;
  616. updateLastMessages(array_unique($updates));
  617. // Update 'em pesky stats.
  618. updateStats('topic');
  619. updateStats('message');
  620. updateSettings(array(
  621. 'calendar_updated' => time(),
  622. ));
  623. }
  624. /**
  625. * Called after a topic is moved to update $board_link and $topic_link to point to new location
  626. *
  627. */
  628. function moveTopicConcurrence()
  629. {
  630. global $board, $topic, $smcFunc, $scripturl;
  631. if (isset($_GET['current_board']))
  632. $move_from = (int) $_GET['current_board'];
  633. if (empty($move_from) || empty($board) || empty($topic))
  634. return true;
  635. if ($move_from == $board)
  636. return true;
  637. else
  638. {
  639. $request = $smcFunc['db_query']('', '
  640. SELECT m.subject, b.name
  641. FROM {db_prefix}topics as t
  642. LEFT JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board)
  643. LEFT JOIN {db_prefix}messages AS m ON (t.id_first_msg = m.id_msg)
  644. WHERE t.id_topic = {int:topic_id}
  645. LIMIT 1',
  646. array(
  647. 'topic_id' => $topic,
  648. )
  649. );
  650. list($topic_subject, $board_name) = $smcFunc['db_fetch_row']($request);
  651. $smcFunc['db_free_result']($request);
  652. $board_link = '<a href="' . $scripturl . '?board=' . $board . '.0">' . $board_name . '</a>';
  653. $topic_link = '<a href="' . $scripturl . '?topic=' . $topic . '.0">' . $topic_subject . '</a>';
  654. fatal_lang_error('topic_already_moved', false, array($topic_link, $board_link));
  655. }
  656. }
  657. ?>