MoveTopic.php 23 KB

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