2
0

MoveTopic.php 23 KB

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