MoveTopic.php 23 KB

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