MoveTopic.php 22 KB

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