MoveTopic.php 21 KB

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