PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/Sources/SplitTopics.php

https://github.com/leftnode/nooges.com
PHP | 1559 lines | 1164 code | 151 blank | 244 comment | 169 complexity | efce8966914181ee9c4cb07d45ad26a4 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**********************************************************************************
  3. * SplitTopics.php *
  4. ***********************************************************************************
  5. * SMF: Simple Machines Forum *
  6. * Open-Source Project Inspired by Zef Hemel (zef@zefhemel.com) *
  7. * =============================================================================== *
  8. * Software Version: SMF 2.0 RC2 *
  9. * Software by: Simple Machines (http://www.simplemachines.org) *
  10. * Copyright 2006-2009 by: Simple Machines LLC (http://www.simplemachines.org) *
  11. * 2001-2006 by: Lewis Media (http://www.lewismedia.com) *
  12. * Support, News, Updates at: http://www.simplemachines.org *
  13. ***********************************************************************************
  14. * This program is free software; you may redistribute it and/or modify it under *
  15. * the terms of the provided license as published by Simple Machines LLC. *
  16. * *
  17. * This program is distributed in the hope that it is and will be useful, but *
  18. * WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY *
  19. * or FITNESS FOR A PARTICULAR PURPOSE. *
  20. * *
  21. * See the "license.txt" file for details of the Simple Machines license. *
  22. * The latest version can always be found at http://www.simplemachines.org. *
  23. **********************************************************************************/
  24. /* Original module by Mach8 - We'll never forget you. */
  25. /*********************************************************************************/
  26. if (!defined('SMF'))
  27. die('Hacking attempt...');
  28. /* This file handles merging and splitting topics... it does this with:
  29. void SplitTopics()
  30. - splits a topic into two topics.
  31. - delegates to the other functions (based on the URL parameter 'sa').
  32. - loads the SplitTopics template.
  33. - requires the split_any permission.
  34. - is accessed with ?action=splittopics.
  35. void SplitIndex()
  36. - screen shown before the actual split.
  37. - is accessed with ?action=splittopics;sa=index.
  38. - default sub action for ?action=splittopics.
  39. - uses 'ask' sub template of the SplitTopics template.
  40. - redirects to SplitSelectTopics if the message given turns out to be
  41. the first message of a topic.
  42. - shows the user three ways to split the current topic.
  43. void SplitExecute()
  44. - do the actual split.
  45. - is accessed with ?action=splittopics;sa=execute.
  46. - uses the main SplitTopics template.
  47. - supports three ways of splitting:
  48. (1) only one message is split off.
  49. (2) all messages after and including a given message are split off.
  50. (3) select topics to split (redirects to SplitSelectTopics()).
  51. - uses splitTopic function to do the actual splitting.
  52. void SplitSelectTopics()
  53. - allows the user to select the messages to be split.
  54. - is accessed with ?action=splittopics;sa=selectTopics.
  55. - uses 'select' sub template of the SplitTopics template or (for
  56. XMLhttp) the 'split' sub template of the Xml template.
  57. - supports XMLhttp for adding/removing a message to the selection.
  58. - uses a session variable to store the selected topics.
  59. - shows two independent page indexes for both the selected and
  60. not-selected messages (;topic=1.x;start2=y).
  61. void SplitSelectionExecute()
  62. - do the actual split of a selection of topics.
  63. - is accessed with ?action=splittopics;sa=splitSelection.
  64. - uses the main SplitTopics template.
  65. - uses splitTopic function to do the actual splitting.
  66. int splitTopic(int topicID, array messagesToBeSplit, string newSubject)
  67. - general function to split off a topic.
  68. - creates a new topic and moves the messages with the IDs in
  69. array messagesToBeSplit to the new topic.
  70. - the subject of the newly created topic is set to 'newSubject'.
  71. - marks the newly created message as read for the user splitting it.
  72. - updates the statistics to reflect a newly created topic.
  73. - logs the action in the moderation log.
  74. - a notification is sent to all users monitoring this topic.
  75. - returns the topic ID of the new split topic.
  76. void MergeTopics()
  77. - merges two or more topics into one topic.
  78. - delegates to the other functions (based on the URL parameter sa).
  79. - loads the SplitTopics template.
  80. - requires the merge_any permission.
  81. - is accessed with ?action=mergetopics.
  82. void MergeIndex()
  83. - allows to pick a topic to merge the current topic with.
  84. - is accessed with ?action=mergetopics;sa=index
  85. - default sub action for ?action=mergetopics.
  86. - uses 'merge' sub template of the SplitTopics template.
  87. - allows to set a different target board.
  88. void MergeExecute(array topics = request)
  89. - set merge options and do the actual merge of two or more topics.
  90. - the merge options screen:
  91. - shows topics to be merged and allows to set some merge options.
  92. - is accessed by ?action=mergetopics;sa=options.and can also
  93. internally be called by QuickModeration() (Subs-Boards.php).
  94. - uses 'merge_extra_options' sub template of the SplitTopics
  95. template.
  96. - the actual merge:
  97. - is accessed with ?action=mergetopics;sa=execute.
  98. - updates the statistics to reflect the merge.
  99. - logs the action in the moderation log.
  100. - sends a notification is sent to all users monitoring this topic.
  101. - redirects to ?action=mergetopics;sa=done.
  102. void MergeDone()
  103. - shows a 'merge completed' screen.
  104. - is accessed with ?action=mergetopics;sa=done.
  105. - uses 'merge_done' sub template of the SplitTopics template.
  106. */
  107. // Split a topic into two separate topics... in case it got offtopic, etc.
  108. function SplitTopics()
  109. {
  110. global $topic, $sourcedir;
  111. // And... which topic were you splitting, again?
  112. if (empty($topic))
  113. fatal_lang_error('numbers_one_to_nine', false);
  114. // Are you allowed to split topics?
  115. isAllowedTo('split_any');
  116. // Load up the "dependencies" - the template, getMsgMemberID(), and sendNotifications().
  117. if (!isset($_REQUEST['xml']))
  118. loadTemplate('SplitTopics');
  119. require_once($sourcedir . '/Subs-Boards.php');
  120. require_once($sourcedir . '/Subs-Post.php');
  121. $subActions = array(
  122. 'selectTopics' => 'SplitSelectTopics',
  123. 'execute' => 'SplitExecute',
  124. 'index' => 'SplitIndex',
  125. 'splitSelection' => 'SplitSelectionExecute',
  126. );
  127. // ?action=splittopics;sa=LETSBREAKIT won't work, sorry.
  128. if (empty($_REQUEST['sa']) || !isset($subActions[$_REQUEST['sa']]))
  129. SplitIndex();
  130. else
  131. $subActions[$_REQUEST['sa']]();
  132. }
  133. // Part 1: General stuff.
  134. function SplitIndex()
  135. {
  136. global $txt, $topic, $context, $smcFunc, $modSettings;
  137. // Validate "at".
  138. if (empty($_GET['at']))
  139. fatal_lang_error('numbers_one_to_nine', false);
  140. $_GET['at'] = (int) $_GET['at'];
  141. // Retrieve the subject and stuff of the specific topic/message.
  142. $request = $smcFunc['db_query']('', '
  143. SELECT m.subject, t.num_replies, t.id_first_msg, t.approved
  144. FROM {db_prefix}messages AS m
  145. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
  146. WHERE m.id_msg = {int:split_at}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  147. AND m.approved = 1') . '
  148. AND m.id_topic = {int:current_topic}
  149. LIMIT 1',
  150. array(
  151. 'current_topic' => $topic,
  152. 'split_at' => $_GET['at'],
  153. )
  154. );
  155. if ($smcFunc['db_num_rows']($request) == 0)
  156. fatal_lang_error('cant_find_messages');
  157. list ($_REQUEST['subname'], $num_replies, $id_first_msg, $approved) = $smcFunc['db_fetch_row']($request);
  158. $smcFunc['db_free_result']($request);
  159. // If not approved validate they can see it.
  160. if ($modSettings['postmod_active'] && !$approved)
  161. isAllowedTo('approve_posts');
  162. // Check if there is more than one message in the topic. (there should be.)
  163. if ($num_replies < 1)
  164. fatal_lang_error('topic_one_post', false);
  165. // Check if this is the first message in the topic (if so, the first and second option won't be available)
  166. if ($id_first_msg == $_GET['at'])
  167. return SplitSelectTopics();
  168. // Basic template information....
  169. $context['message'] = array(
  170. 'id' => $_GET['at'],
  171. 'subject' => $_REQUEST['subname']
  172. );
  173. $context['sub_template'] = 'ask';
  174. $context['page_title'] = $txt['split'];
  175. }
  176. // Alright, you've decided what you want to do with it.... now to do it.
  177. function SplitExecute()
  178. {
  179. global $txt, $board, $topic, $context, $user_info, $smcFunc, $modSettings;
  180. // Clean up the subject.
  181. if (!isset($_POST['subname']) || $_POST['subname'] == '')
  182. $_POST['subname'] = $txt['new_topic'];
  183. // Redirect to the selector if they chose selective.
  184. if ($_POST['step2'] == 'selective')
  185. {
  186. $_REQUEST['subname'] = $_POST['subname'];
  187. return SplitSelectTopics();
  188. }
  189. // Check the session to make sure they meant to do this.
  190. checkSession();
  191. $_POST['at'] = (int) $_POST['at'];
  192. $messagesToBeSplit = array();
  193. if ($_POST['step2'] == 'afterthis')
  194. {
  195. // Fetch the message IDs of the topic that are at or after the message.
  196. $request = $smcFunc['db_query']('', '
  197. SELECT id_msg
  198. FROM {db_prefix}messages
  199. WHERE id_topic = {int:current_topic}
  200. AND id_msg >= {int:split_at}',
  201. array(
  202. 'current_topic' => $topic,
  203. 'split_at' => $_POST['at'],
  204. )
  205. );
  206. while ($row = $smcFunc['db_fetch_assoc']($request))
  207. $messagesToBeSplit[] = $row['id_msg'];
  208. $smcFunc['db_free_result']($request);
  209. }
  210. // Only the selected message has to be split. That should be easy.
  211. elseif ($_POST['step2'] == 'onlythis')
  212. $messagesToBeSplit[] = $_POST['at'];
  213. // There's another action?!
  214. else
  215. fatal_lang_error('no_access', false);
  216. $context['old_topic'] = $topic;
  217. $context['new_topic'] = splitTopic($topic, $messagesToBeSplit, $_POST['subname']);
  218. $context['page_title'] = $txt['split'];
  219. }
  220. // Get a selective list of topics...
  221. function SplitSelectTopics()
  222. {
  223. global $txt, $scripturl, $topic, $context, $modSettings, $original_msgs, $smcFunc, $options;
  224. $context['page_title'] = $txt['split'] . ' - ' . $txt['select_split_posts'];
  225. // Haven't selected anything have we?
  226. $_SESSION['split_selection'][$topic] = empty($_SESSION['split_selection'][$topic]) ? array() : $_SESSION['split_selection'][$topic];
  227. $context['not_selected'] = array(
  228. 'num_messages' => 0,
  229. 'start' => empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'],
  230. 'messages' => array(),
  231. );
  232. $context['selected'] = array(
  233. 'num_messages' => 0,
  234. 'start' => empty($_REQUEST['start2']) ? 0 : (int) $_REQUEST['start2'],
  235. 'messages' => array(),
  236. );
  237. $context['topic'] = array(
  238. 'id' => $topic,
  239. 'subject' => urlencode($_REQUEST['subname']),
  240. );
  241. // Some stuff for our favorite template.
  242. $context['new_subject'] = $_REQUEST['subname'];
  243. // Using the "select" sub template.
  244. $context['sub_template'] = isset($_REQUEST['xml']) ? 'split' : 'select';
  245. // Are we using a custom messages per page?
  246. $context['messages_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
  247. // Get the message ID's from before the move.
  248. if (isset($_REQUEST['xml']))
  249. {
  250. $original_msgs = array(
  251. 'not_selected' => array(),
  252. 'selected' => array(),
  253. );
  254. $request = $smcFunc['db_query']('', '
  255. SELECT id_msg
  256. FROM {db_prefix}messages
  257. WHERE id_topic = {int:current_topic}' . (empty($_SESSION['split_selection'][$topic]) ? '' : '
  258. AND id_msg NOT IN ({array_int:no_split_msgs})') . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  259. AND approved = {int:is_approved}') . '
  260. ORDER BY id_msg DESC
  261. LIMIT {int:start}, {int:messages_per_page}',
  262. array(
  263. 'current_topic' => $topic,
  264. 'no_split_msgs' => empty($_SESSION['split_selection'][$topic]) ? array() : $_SESSION['split_selection'][$topic],
  265. 'is_approved' => 1,
  266. 'start' => $context['not_selected']['start'],
  267. 'messages_per_page' => $context['messages_per_page'],
  268. )
  269. );
  270. // You can't split the last message off.
  271. if (empty($context['not_selected']['start']) && $smcFunc['db_num_rows']($request) <= 1 && $_REQUEST['move'] == 'down')
  272. $_REQUEST['move'] = '';
  273. while ($row = $smcFunc['db_fetch_assoc']($request))
  274. $original_msgs['not_selected'][] = $row['id_msg'];
  275. $smcFunc['db_free_result']($request);
  276. if (!empty($_SESSION['split_selection'][$topic]))
  277. {
  278. $request = $smcFunc['db_query']('', '
  279. SELECT id_msg
  280. FROM {db_prefix}messages
  281. WHERE id_topic = {int:current_topic}
  282. AND id_msg IN ({array_int:split_msgs})' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  283. AND approved = {int:is_approved}') . '
  284. ORDER BY id_msg DESC
  285. LIMIT {int:start}, {int:messages_per_page}',
  286. array(
  287. 'current_topic' => $topic,
  288. 'split_msgs' => $_SESSION['split_selection'][$topic],
  289. 'is_approved' => 1,
  290. 'start' => $context['selected']['start'],
  291. 'messages_per_page' => $context['messages_per_page'],
  292. )
  293. );
  294. while ($row = $smcFunc['db_fetch_assoc']($request))
  295. $original_msgs['selected'][] = $row['id_msg'];
  296. $smcFunc['db_free_result']($request);
  297. }
  298. }
  299. // (De)select a message..
  300. if (!empty($_REQUEST['move']))
  301. {
  302. $_REQUEST['msg'] = (int) $_REQUEST['msg'];
  303. if ($_REQUEST['move'] == 'reset')
  304. $_SESSION['split_selection'][$topic] = array();
  305. elseif ($_REQUEST['move'] == 'up')
  306. $_SESSION['split_selection'][$topic] = array_diff($_SESSION['split_selection'][$topic], array($_REQUEST['msg']));
  307. else
  308. $_SESSION['split_selection'][$topic][] = $_REQUEST['msg'];
  309. }
  310. // Make sure the selection is still accurate.
  311. if (!empty($_SESSION['split_selection'][$topic]))
  312. {
  313. $request = $smcFunc['db_query']('', '
  314. SELECT id_msg
  315. FROM {db_prefix}messages
  316. WHERE id_topic = {int:current_topic}
  317. AND id_msg IN ({array_int:split_msgs})' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  318. AND approved = {int:is_approved}'),
  319. array(
  320. 'current_topic' => $topic,
  321. 'split_msgs' => $_SESSION['split_selection'][$topic],
  322. 'is_approved' => 1,
  323. )
  324. );
  325. $_SESSION['split_selection'][$topic] = array();
  326. while ($row = $smcFunc['db_fetch_assoc']($request))
  327. $_SESSION['split_selection'][$topic][] = $row['id_msg'];
  328. $smcFunc['db_free_result']($request);
  329. }
  330. // Get the number of messages (not) selected to be split.
  331. $request = $smcFunc['db_query']('', '
  332. SELECT ' . (empty($_SESSION['split_selection'][$topic]) ? '0' : 'm.id_msg IN ({array_int:split_msgs})') . ' AS is_selected, COUNT(*) AS num_messages
  333. FROM {db_prefix}messages AS m
  334. WHERE m.id_topic = {int:current_topic}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  335. AND approved = {int:is_approved}') . (empty($_SESSION['split_selection'][$topic]) ? '' : '
  336. GROUP BY is_selected'),
  337. array(
  338. 'current_topic' => $topic,
  339. 'split_msgs' => !empty($_SESSION['split_selection'][$topic]) ? $_SESSION['split_selection'][$topic] : array(),
  340. 'is_approved' => 1,
  341. )
  342. );
  343. while ($row = $smcFunc['db_fetch_assoc']($request))
  344. $context[empty($row['is_selected']) ? 'not_selected' : 'selected']['num_messages'] = $row['num_messages'];
  345. $smcFunc['db_free_result']($request);
  346. // Fix an oversized starting page (to make sure both pageindexes are properly set).
  347. if ($context['selected']['start'] >= $context['selected']['num_messages'])
  348. $context['selected']['start'] = $context['selected']['num_messages'] <= $context['messages_per_page'] ? 0 : ($context['selected']['num_messages'] - (($context['selected']['num_messages'] % $context['messages_per_page']) == 0 ? $context['messages_per_page'] : ($context['selected']['num_messages'] % $context['messages_per_page'])));
  349. // Build a page list of the not-selected topics...
  350. $context['not_selected']['page_index'] = constructPageIndex($scripturl . '?action=splittopics;sa=selectTopics;subname=' . strtr(urlencode($_REQUEST['subname']), array('%' => '%%')) . ';topic=' . $topic . '.%1$d;start2=' . $context['selected']['start'], $context['not_selected']['start'], $context['not_selected']['num_messages'], $context['messages_per_page'], true);
  351. // ...and one of the selected topics.
  352. $context['selected']['page_index'] = constructPageIndex($scripturl . '?action=splittopics;sa=selectTopics;subname=' . strtr(urlencode($_REQUEST['subname']), array('%' => '%%')) . ';topic=' . $topic . '.' . $context['not_selected']['start'] . ';start2=%1$d', $context['selected']['start'], $context['selected']['num_messages'], $context['messages_per_page'], true);
  353. // Get the messages and stick them into an array.
  354. $request = $smcFunc['db_query']('', '
  355. SELECT m.subject, IFNULL(mem.real_name, m.poster_name) AS real_name, m.body, m.id_msg, m.smileys_enabled
  356. FROM {db_prefix}messages AS m
  357. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  358. WHERE m.id_topic = {int:current_topic}' . (empty($_SESSION['split_selection'][$topic]) ? '' : '
  359. AND id_msg NOT IN ({array_int:no_split_msgs})') . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  360. AND approved = {int:is_approved}') . '
  361. ORDER BY m.id_msg DESC
  362. LIMIT {int:start}, {int:messages_per_page}',
  363. array(
  364. 'current_topic' => $topic,
  365. 'no_split_msgs' => !empty($_SESSION['split_selection'][$topic]) ? $_SESSION['split_selection'][$topic] : array(),
  366. 'is_approved' => 1,
  367. 'start' => $context['not_selected']['start'],
  368. 'messages_per_page' => $context['messages_per_page'],
  369. )
  370. );
  371. $context['messages'] = array();
  372. while ($row = $smcFunc['db_fetch_assoc']($request))
  373. {
  374. censorText($row['subject']);
  375. censorText($row['body']);
  376. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  377. $context['not_selected']['messages'][$row['id_msg']] = array(
  378. 'id' => $row['id_msg'],
  379. 'subject' => $row['subject'],
  380. 'body' => $row['body'],
  381. 'poster' => $row['real_name'],
  382. );
  383. }
  384. $smcFunc['db_free_result']($request);
  385. // Now get the selected messages.
  386. if (!empty($_SESSION['split_selection'][$topic]))
  387. {
  388. // Get the messages and stick them into an array.
  389. $request = $smcFunc['db_query']('', '
  390. SELECT m.subject, IFNULL(mem.real_name, m.poster_name) AS real_name, m.body, m.id_msg, m.smileys_enabled
  391. FROM {db_prefix}messages AS m
  392. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  393. WHERE m.id_topic = {int:current_topic}
  394. AND m.id_msg IN ({array_int:split_msgs})' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  395. AND approved = {int:is_approved}') . '
  396. ORDER BY m.id_msg DESC
  397. LIMIT {int:start}, {int:messages_per_page}',
  398. array(
  399. 'current_topic' => $topic,
  400. 'split_msgs' => $_SESSION['split_selection'][$topic],
  401. 'is_approved' => 1,
  402. 'start' => $context['selected']['start'],
  403. 'messages_per_page' => $context['messages_per_page'],
  404. )
  405. );
  406. $context['messages'] = array();
  407. while ($row = $smcFunc['db_fetch_assoc']($request))
  408. {
  409. censorText($row['subject']);
  410. censorText($row['body']);
  411. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  412. $context['selected']['messages'][$row['id_msg']] = array(
  413. 'id' => $row['id_msg'],
  414. 'subject' => $row['subject'],
  415. 'body' => $row['body'],
  416. 'poster' => $row['real_name']
  417. );
  418. }
  419. $smcFunc['db_free_result']($request);
  420. }
  421. // The XMLhttp method only needs the stuff that changed, so let's compare.
  422. if (isset($_REQUEST['xml']))
  423. {
  424. $changes = array(
  425. 'remove' => array(
  426. 'not_selected' => array_diff($original_msgs['not_selected'], array_keys($context['not_selected']['messages'])),
  427. 'selected' => array_diff($original_msgs['selected'], array_keys($context['selected']['messages'])),
  428. ),
  429. 'insert' => array(
  430. 'not_selected' => array_diff(array_keys($context['not_selected']['messages']), $original_msgs['not_selected']),
  431. 'selected' => array_diff(array_keys($context['selected']['messages']), $original_msgs['selected']),
  432. ),
  433. );
  434. $context['changes'] = array();
  435. foreach ($changes as $change_type => $change_array)
  436. foreach ($change_array as $section => $msg_array)
  437. {
  438. if (empty($msg_array))
  439. continue;
  440. foreach ($msg_array as $id_msg)
  441. {
  442. $context['changes'][$change_type . $id_msg] = array(
  443. 'id' => $id_msg,
  444. 'type' => $change_type,
  445. 'section' => $section,
  446. );
  447. if ($change_type == 'insert')
  448. $context['changes']['insert' . $id_msg]['insert_value'] = $context[$section]['messages'][$id_msg];
  449. }
  450. }
  451. }
  452. }
  453. // Actually and selectively split the topics out.
  454. function SplitSelectionExecute()
  455. {
  456. global $txt, $board, $topic, $context, $user_info;
  457. // Make sure the session id was passed with post.
  458. checkSession();
  459. // Default the subject in case it's blank.
  460. if (!isset($_POST['subname']) || $_POST['subname'] == '')
  461. $_POST['subname'] = $txt['new_topic'];
  462. // The old topic's ID is the current one.
  463. $split1_ID_TOPIC = $topic;
  464. // You must've selected some messages! Can't split out none!
  465. if (empty($_SESSION['split_selection'][$topic]))
  466. fatal_lang_error('no_posts_selected', false);
  467. $context['old_topic'] = $topic;
  468. $context['new_topic'] = splitTopic($topic, $_SESSION['split_selection'][$topic], $_POST['subname']);
  469. $context['page_title'] = $txt['split'];
  470. }
  471. // Split a topic in two topics.
  472. function splitTopic($split1_ID_TOPIC, $splitMessages, $new_subject)
  473. {
  474. global $user_info, $topic, $board, $modSettings, $smcFunc, $txt;
  475. // Nothing to split?
  476. if (empty($splitMessages))
  477. fatal_lang_error('no_posts_selected', false);
  478. // Get some board info.
  479. $request = $smcFunc['db_query']('', '
  480. SELECT id_board, approved
  481. FROM {db_prefix}topics
  482. WHERE id_topic = {int:id_topic}
  483. LIMIT 1',
  484. array(
  485. 'id_topic' => $split1_ID_TOPIC,
  486. )
  487. );
  488. list ($id_board, $split1_approved) = $smcFunc['db_fetch_row']($request);
  489. $smcFunc['db_free_result']($request);
  490. // Find the new first and last not in the list. (old topic)
  491. $request = $smcFunc['db_query']('', '
  492. SELECT
  493. MIN(m.id_msg) AS myid_first_msg, MAX(m.id_msg) AS myid_last_msg, COUNT(*) AS message_count, m.approved
  494. FROM {db_prefix}messages AS m
  495. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:id_topic})
  496. WHERE m.id_msg NOT IN ({array_int:no_msg_list})
  497. AND m.id_topic = {int:id_topic}
  498. GROUP BY m.approved
  499. ORDER BY m.approved DESC
  500. LIMIT 2',
  501. array(
  502. 'id_topic' => $split1_ID_TOPIC,
  503. 'no_msg_list' => $splitMessages,
  504. )
  505. );
  506. // You can't select ALL the messages!
  507. if ($smcFunc['db_num_rows']($request) == 0)
  508. fatal_lang_error('selected_all_posts', false);
  509. while ($row = $smcFunc['db_fetch_assoc']($request))
  510. {
  511. // Get the right first and last message dependant on approved state...
  512. if (empty($split1_first_msg) || $row['myid_first_msg'] < $split1_first_msg)
  513. $split1_first_msg = $row['myid_first_msg'];
  514. if (empty($split1_last_msg) || $row['approved'])
  515. $split1_last_msg = $row['myid_last_msg'];
  516. // Get the counts correct...
  517. if ($row['approved'])
  518. {
  519. $split1_replies = $row['message_count'] - 1;
  520. $split1_unapprovedposts = 0;
  521. }
  522. else
  523. {
  524. if (!isset($split1_replies))
  525. $split1_replies = 0;
  526. // If the topic isn't approved then num replies must go up by one... as first post wouldn't be counted.
  527. elseif (!$split1_approved)
  528. $split1_replies++;
  529. $split1_unapprovedposts = $row['message_count'];
  530. }
  531. }
  532. $smcFunc['db_free_result']($request);
  533. $split1_firstMem = getMsgMemberID($split1_first_msg);
  534. $split1_lastMem = getMsgMemberID($split1_last_msg);
  535. // Find the first and last in the list. (new topic)
  536. $request = $smcFunc['db_query']('', '
  537. SELECT MIN(id_msg) AS myid_first_msg, MAX(id_msg) AS myid_last_msg, COUNT(*) AS message_count, approved
  538. FROM {db_prefix}messages
  539. WHERE id_msg IN ({array_int:msg_list})
  540. AND id_topic = {int:id_topic}
  541. GROUP BY id_topic, approved
  542. ORDER BY approved DESC
  543. LIMIT 2',
  544. array(
  545. 'msg_list' => $splitMessages,
  546. 'id_topic' => $split1_ID_TOPIC,
  547. )
  548. );
  549. while ($row = $smcFunc['db_fetch_assoc']($request))
  550. {
  551. // As before get the right first and last message dependant on approved state...
  552. if (empty($split2_first_msg) || $row['myid_first_msg'] < $split2_first_msg)
  553. $split2_first_msg = $row['myid_first_msg'];
  554. if (empty($split2_last_msg) || $row['approved'])
  555. $split2_last_msg = $row['myid_last_msg'];
  556. // Then do the counts again...
  557. if ($row['approved'])
  558. {
  559. $split2_approved = true;
  560. $split2_replies = $row['message_count'] - 1;
  561. $split2_unapprovedposts = 0;
  562. }
  563. else
  564. {
  565. // Should this one be approved??
  566. if ($split2_first_msg == $row['myid_first_msg'])
  567. $split2_approved = false;
  568. if (!isset($split2_replies))
  569. $split2_replies = 0;
  570. // As before, fix number of replies.
  571. elseif (!$split2_approved)
  572. $split2_replies++;
  573. $split2_unapprovedposts = $row['message_count'];
  574. }
  575. }
  576. $smcFunc['db_free_result']($request);
  577. $split2_firstMem = getMsgMemberID($split2_first_msg);
  578. $split2_lastMem = getMsgMemberID($split2_last_msg);
  579. // No database changes yet, so let's double check to see if everything makes at least a little sense.
  580. if ($split1_first_msg <= 0 || $split1_last_msg <= 0 || $split2_first_msg <= 0 || $split2_last_msg <= 0 || $split1_replies < 0 || $split2_replies < 0 || $split1_unapprovedposts < 0 || $split2_unapprovedposts < 0 || !isset($split1_approved) || !isset($split2_approved))
  581. fatal_lang_error('cant_find_messages');
  582. // You cannot split off the first message of a topic.
  583. if ($split1_first_msg > $split2_first_msg)
  584. fatal_lang_error('split_first_post', false);
  585. // We're off to insert the new topic! Use 0 for now to avoid UNIQUE errors.
  586. $smcFunc['db_insert']('',
  587. '{db_prefix}topics',
  588. array(
  589. 'id_board' => 'int',
  590. 'id_member_started' => 'int',
  591. 'id_member_updated' => 'int',
  592. 'id_first_msg' => 'int',
  593. 'id_last_msg' => 'int',
  594. 'num_replies' => 'int',
  595. 'unapproved_posts' => 'int',
  596. 'approved' => 'int',
  597. 'is_sticky' => 'int',
  598. ),
  599. array(
  600. (int) $id_board, $split2_firstMem, $split2_lastMem, 0,
  601. 0, $split2_replies, $split2_unapprovedposts, (int) $split2_approved, 0,
  602. ),
  603. array('id_topic')
  604. );
  605. $split2_ID_TOPIC = $smcFunc['db_insert_id']('{db_prefix}topics', 'id_topic');
  606. if ($split2_ID_TOPIC <= 0)
  607. fatal_lang_error('cant_insert_topic');
  608. // Move the messages over to the other topic.
  609. $new_subject = strtr($smcFunc['htmltrim']($smcFunc['htmlspecialchars']($new_subject)), array("\r" => '', "\n" => '', "\t" => ''));
  610. // Check the subject length.
  611. if ($smcFunc['strlen']($new_subject) > 100)
  612. $new_subject = $smcFunc['substr']($new_subject, 0, 100);
  613. // Valid subject?
  614. if ($new_subject != '')
  615. {
  616. $smcFunc['db_query']('', '
  617. UPDATE {db_prefix}messages
  618. SET
  619. id_topic = {int:id_topic},
  620. subject = CASE WHEN id_msg = {int:split_first_msg} THEN {string:new_subject} ELSE {string:new_subject_replies} END
  621. WHERE id_msg IN ({array_int:split_msgs})',
  622. array(
  623. 'split_msgs' => $splitMessages,
  624. 'id_topic' => $split2_ID_TOPIC,
  625. 'new_subject' => $new_subject,
  626. 'split_first_msg' => $split2_first_msg,
  627. 'new_subject_replies' => $txt['response_prefix'] . $new_subject,
  628. )
  629. );
  630. // Cache the new topics subject... we can do it now as all the subjects are the same!
  631. updateStats('subject', $split2_ID_TOPIC, $new_subject);
  632. }
  633. // Any associated reported posts better follow...
  634. $smcFunc['db_query']('', '
  635. UPDATE {db_prefix}log_reported
  636. SET id_topic = {int:id_topic}
  637. WHERE id_msg IN ({array_int:split_msgs})',
  638. array(
  639. 'split_msgs' => $splitMessages,
  640. 'id_topic' => $split2_ID_TOPIC,
  641. )
  642. );
  643. // Mess with the old topic's first, last, and number of messages.
  644. $smcFunc['db_query']('', '
  645. UPDATE {db_prefix}topics
  646. SET
  647. num_replies = {int:num_replies},
  648. id_first_msg = {int:id_first_msg},
  649. id_last_msg = {int:id_last_msg},
  650. id_member_started = {int:id_member_started},
  651. id_member_updated = {int:id_member_updated},
  652. unapproved_posts = {int:unapproved_posts}
  653. WHERE id_topic = {int:id_topic}',
  654. array(
  655. 'num_replies' => $split1_replies,
  656. 'id_first_msg' => $split1_first_msg,
  657. 'id_last_msg' => $split1_last_msg,
  658. 'id_member_started' => $split1_firstMem,
  659. 'id_member_updated' => $split1_lastMem,
  660. 'unapproved_posts' => $split1_unapprovedposts,
  661. 'id_topic' => $split1_ID_TOPIC,
  662. )
  663. );
  664. // Now, put the first/last message back to what they should be.
  665. $smcFunc['db_query']('', '
  666. UPDATE {db_prefix}topics
  667. SET
  668. id_first_msg = {int:id_first_msg},
  669. id_last_msg = {int:id_last_msg}
  670. WHERE id_topic = {int:id_topic}',
  671. array(
  672. 'id_first_msg' => $split2_first_msg,
  673. 'id_last_msg' => $split2_last_msg,
  674. 'id_topic' => $split2_ID_TOPIC,
  675. )
  676. );
  677. // If the new topic isn't approved ensure the first message flags this just in case.
  678. if (!$split2_approved)
  679. $smcFunc['db_query']('', '
  680. UPDATE {db_prefix}messages
  681. SET approved = {int:approved}
  682. WHERE id_msg = {int:id_msg}
  683. AND id_topic = {int:id_topic}',
  684. array(
  685. 'approved' => 0,
  686. 'id_msg' => $split2_first_msg,
  687. 'id_topic' => $split2_ID_TOPIC,
  688. )
  689. );
  690. // The board has more topics now (Or more unapproved ones!).
  691. $smcFunc['db_query']('', '
  692. UPDATE {db_prefix}boards
  693. SET ' . ($split2_approved ? '
  694. num_topics = num_topics + 1' : '
  695. unapproved_topics = unapproved_topics + 1') . '
  696. WHERE id_board = {int:id_board}',
  697. array(
  698. 'id_board' => $id_board,
  699. )
  700. );
  701. // We're going to assume they bothered to read it before splitting it.
  702. if (!$user_info['is_guest'])
  703. $smcFunc['db_insert']('replace',
  704. '{db_prefix}log_topics',
  705. array('id_msg' => 'int', 'id_member' => 'int', 'id_topic' => 'int'),
  706. array($modSettings['maxMsgID'], $user_info['id'], $split2_ID_TOPIC),
  707. array('id_member', 'id_topic')
  708. );
  709. // Housekeeping.
  710. updateStats('topic');
  711. updateLastMessages($id_board);
  712. logAction('split', array('topic' => $split1_ID_TOPIC, 'new_topic' => $split2_ID_TOPIC, 'board' => $id_board));
  713. // Notify people that this topic has been split?
  714. sendNotifications($split1_ID_TOPIC, 'split');
  715. // Return the ID of the newly created topic.
  716. return $split2_ID_TOPIC;
  717. }
  718. // Merge two topics into one topic... useful if they have the same basic subject.
  719. function MergeTopics()
  720. {
  721. // Load the template....
  722. loadTemplate('SplitTopics');
  723. $subActions = array(
  724. 'done' => 'MergeDone',
  725. 'execute' => 'MergeExecute',
  726. 'index' => 'MergeIndex',
  727. 'options' => 'MergeExecute',
  728. );
  729. // ?action=mergetopics;sa=LETSBREAKIT won't work, sorry.
  730. if (empty($_REQUEST['sa']) || !isset($subActions[$_REQUEST['sa']]))
  731. MergeIndex();
  732. else
  733. $subActions[$_REQUEST['sa']]();
  734. }
  735. // Merge two topics together.
  736. function MergeIndex()
  737. {
  738. global $txt, $board, $context, $smcFunc;
  739. global $scripturl, $topic, $user_info, $modSettings;
  740. if (!isset($_GET['from']))
  741. fatal_lang_error('no_access', false);
  742. $_GET['from'] = (int) $_GET['from'];
  743. $_REQUEST['targetboard'] = isset($_REQUEST['targetboard']) ? (int) $_REQUEST['targetboard'] : $board;
  744. $context['target_board'] = $_REQUEST['targetboard'];
  745. // Prepare a handy query bit for approval...
  746. if ($modSettings['postmod_active'])
  747. {
  748. $can_approve_boards = boardsAllowedTo('approve_posts');
  749. $onlyApproved = $can_approve_boards !== array(0) && !in_array($_REQUEST['targetboard'], $can_approve_boards);
  750. }
  751. else
  752. $onlyApproved = false;
  753. // How many topics are on this board? (used for paging.)
  754. $request = $smcFunc['db_query']('', '
  755. SELECT COUNT(*)
  756. FROM {db_prefix}topics AS t
  757. WHERE t.id_board = {int:id_board}' . ($onlyApproved ? '
  758. AND t.approved = {int:is_approved}' : ''),
  759. array(
  760. 'id_board' => $_REQUEST['targetboard'],
  761. 'is_approved' => 1,
  762. )
  763. );
  764. list ($topiccount) = $smcFunc['db_fetch_row']($request);
  765. $smcFunc['db_free_result']($request);
  766. // Make the page list.
  767. $context['page_index'] = constructPageIndex($scripturl . '?action=mergetopics;from=' . $_GET['from'] . ';targetboard=' . $_REQUEST['targetboard'] . ';board=' . $board . '.%1$d', $_REQUEST['start'], $topiccount, $modSettings['defaultMaxTopics'], true);
  768. // Get the topic's subject.
  769. $request = $smcFunc['db_query']('', '
  770. SELECT m.subject
  771. FROM {db_prefix}topics AS t
  772. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  773. WHERE t.id_topic = {int:id_topic}
  774. AND t.id_board = {int:current_board}' . ($onlyApproved ? '
  775. AND t.approved = {int:is_approved}' : '') . '
  776. LIMIT 1',
  777. array(
  778. 'current_board' => $board,
  779. 'id_topic' => $_GET['from'],
  780. 'is_approved' => 1,
  781. )
  782. );
  783. if ($smcFunc['db_num_rows']($request) == 0)
  784. fatal_lang_error('no_board');
  785. list ($subject) = $smcFunc['db_fetch_row']($request);
  786. $smcFunc['db_free_result']($request);
  787. // Tell the template a few things..
  788. $context['origin_topic'] = $_GET['from'];
  789. $context['origin_subject'] = $subject;
  790. $context['origin_js_subject'] = addcslashes(addslashes($subject), '/');
  791. $context['page_title'] = $txt['merge'];
  792. // Check which boards you have merge permissions on.
  793. $merge_boards = boardsAllowedTo('merge_any');
  794. if (empty($merge_boards))
  795. fatal_lang_error('cannot_merge_any', 'user');
  796. // Get a list of boards they can navigate to to merge.
  797. $request = $smcFunc['db_query']('order_by_board_order', '
  798. SELECT b.id_board, b.name AS board_name, c.name AS cat_name
  799. FROM {db_prefix}boards AS b
  800. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  801. WHERE {query_see_board}' . (!in_array(0, $merge_boards) ? '
  802. AND b.id_board IN ({array_int:merge_boards})' : ''),
  803. array(
  804. 'merge_boards' => $merge_boards,
  805. )
  806. );
  807. $context['boards'] = array();
  808. while ($row = $smcFunc['db_fetch_assoc']($request))
  809. $context['boards'][] = array(
  810. 'id' => $row['id_board'],
  811. 'name' => $row['board_name'],
  812. 'category' => $row['cat_name']
  813. );
  814. $smcFunc['db_free_result']($request);
  815. // Get some topics to merge it with.
  816. $request = $smcFunc['db_query']('', '
  817. SELECT t.id_topic, m.subject, m.id_member, IFNULL(mem.real_name, m.poster_name) AS poster_name
  818. FROM {db_prefix}topics AS t
  819. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  820. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  821. WHERE t.id_board = {int:id_board}
  822. AND t.id_topic != {int:id_topic}' . ($onlyApproved ? '
  823. AND t.approved = {int:is_approved}' : '') . '
  824. ORDER BY {raw:sort}
  825. LIMIT {int:offset}, {int:limit}',
  826. array(
  827. 'id_board' => $_REQUEST['targetboard'],
  828. 'id_topic' => $_GET['from'],
  829. 'sort' => (!empty($modSettings['enableStickyTopics']) ? 't.is_sticky DESC, ' : '') . 't.id_last_msg DESC',
  830. 'offset' => $_REQUEST['start'],
  831. 'limit' => $modSettings['defaultMaxTopics'],
  832. 'is_approved' => 1,
  833. )
  834. );
  835. $context['topics'] = array();
  836. while ($row = $smcFunc['db_fetch_assoc']($request))
  837. {
  838. censorText($row['subject']);
  839. $context['topics'][] = array(
  840. 'id' => $row['id_topic'],
  841. 'poster' => array(
  842. 'id' => $row['id_member'],
  843. 'name' => $row['poster_name'],
  844. 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
  845. 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" target="_blank" class="new_win">' . $row['poster_name'] . '</a>'
  846. ),
  847. 'subject' => $row['subject'],
  848. 'js_subject' => addcslashes(addslashes($row['subject']), '/')
  849. );
  850. }
  851. $smcFunc['db_free_result']($request);
  852. if (empty($context['topics']) && count($context['boards']) <= 1)
  853. fatal_lang_error('merge_need_more_topics');
  854. $context['sub_template'] = 'merge';
  855. }
  856. // Now that the topic IDs are known, do the proper merging.
  857. function MergeExecute($topics = array())
  858. {
  859. global $user_info, $txt, $context, $scripturl, $sourcedir;
  860. global $smcFunc, $language, $modSettings;
  861. // The parameters of MergeExecute were set, so this must've been an internal call.
  862. if (!empty($topics))
  863. {
  864. isAllowedTo('merge_any');
  865. loadTemplate('SplitTopics');
  866. }
  867. // Handle URLs from MergeIndex.
  868. if (!empty($_GET['from']) && !empty($_GET['to']))
  869. $topics = array((int) $_GET['from'], (int) $_GET['to']);
  870. // If we came from a form, the topic IDs came by post.
  871. if (!empty($_POST['topics']) && is_array($_POST['topics']))
  872. $topics = $_POST['topics'];
  873. // There's nothing to merge with just one topic...
  874. if (empty($topics) || !is_array($topics) || count($topics) == 1)
  875. fatal_lang_error('merge_need_more_topics');
  876. // Make sure every topic is numeric, or some nasty things could be done with the DB.
  877. foreach ($topics as $id => $topic)
  878. $topics[$id] = (int) $topic;
  879. // Joy of all joys, make sure they're not pi**ing about with unapproved topics they can't see :P
  880. if ($modSettings['postmod_active'])
  881. $can_approve_boards = boardsAllowedTo('approve_posts');
  882. // Get info about the topics and polls that will be merged.
  883. $request = $smcFunc['db_query']('', '
  884. SELECT
  885. t.id_topic, t.id_board, t.id_poll, t.num_views, t.is_sticky, t.approved, t.num_replies, t.unapproved_posts,
  886. m1.subject, m1.poster_time AS time_started, IFNULL(mem1.id_member, 0) AS id_member_started, IFNULL(mem1.real_name, m1.poster_name) AS name_started,
  887. m2.poster_time AS time_updated, IFNULL(mem2.id_member, 0) AS id_member_updated, IFNULL(mem2.real_name, m2.poster_name) AS name_updated
  888. FROM {db_prefix}topics AS t
  889. INNER JOIN {db_prefix}messages AS m1 ON (m1.id_msg = t.id_first_msg)
  890. INNER JOIN {db_prefix}messages AS m2 ON (m2.id_msg = t.id_last_msg)
  891. LEFT JOIN {db_prefix}members AS mem1 ON (mem1.id_member = m1.id_member)
  892. LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = m2.id_member)
  893. WHERE t.id_topic IN ({array_int:topic_list})
  894. ORDER BY t.id_first_msg
  895. LIMIT ' . count($topics),
  896. array(
  897. 'topic_list' => $topics,
  898. )
  899. );
  900. if ($smcFunc['db_num_rows']($request) < 2)
  901. fatal_lang_error('no_topic_id');
  902. $num_views = 0;
  903. $is_sticky = 0;
  904. $boardTotals = array();
  905. $boards = array();
  906. $polls = array();
  907. while ($row = $smcFunc['db_fetch_assoc']($request))
  908. {
  909. // Make a note for the board counts...
  910. if (!isset($boardTotals[$row['id_board']]))
  911. $boardTotals[$row['id_board']] = array(
  912. 'posts' => 0,
  913. 'topics' => 0,
  914. 'unapproved_posts' => 0,
  915. 'unapproved_topics' => 0
  916. );
  917. // We can't see unapproved topics here?
  918. if ($modSettings['postmod_active'] && !$row['approved'] && $can_approve_boards != array(0) && in_array($row['id_board'], $can_approve_boards))
  919. continue;
  920. elseif (!$row['approved'])
  921. $boardTotals[$row['id_board']]['unapproved_topics']++;
  922. else
  923. $boardTotals[$row['id_board']]['topics']++;
  924. $boardTotals[$row['id_board']]['unapproved_posts'] += $row['unapproved_posts'];
  925. $boardTotals[$row['id_board']]['posts'] += $row['num_replies'] + ($row['approved'] ? 1 : 0);
  926. $topic_data[$row['id_topic']] = array(
  927. 'id' => $row['id_topic'],
  928. 'board' => $row['id_board'],
  929. 'poll' => $row['id_poll'],
  930. 'num_views' => $row['num_views'],
  931. 'subject' => $row['subject'],
  932. 'started' => array(
  933. 'time' => timeformat($row['time_started']),
  934. 'timestamp' => forum_time(true, $row['time_started']),
  935. 'href' => empty($row['id_member_started']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member_started'],
  936. 'link' => empty($row['id_member_started']) ? $row['name_started'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_started'] . '">' . $row['name_started'] . '</a>'
  937. ),
  938. 'updated' => array(
  939. 'time' => timeformat($row['time_updated']),
  940. 'timestamp' => forum_time(true, $row['time_updated']),
  941. 'href' => empty($row['id_member_updated']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member_updated'],
  942. 'link' => empty($row['id_member_updated']) ? $row['name_updated'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_updated'] . '">' . $row['name_updated'] . '</a>'
  943. )
  944. );
  945. $num_views += $row['num_views'];
  946. $boards[] = $row['id_board'];
  947. // If there's no poll, id_poll == 0...
  948. if ($row['id_poll'] > 0)
  949. $polls[] = $row['id_poll'];
  950. // Store the id_topic with the lowest id_first_msg.
  951. if (empty($firstTopic))
  952. $firstTopic = $row['id_topic'];
  953. $is_sticky = max($is_sticky, $row['is_sticky']);
  954. }
  955. $smcFunc['db_free_result']($request);
  956. // If we didn't get any topics then they've been messing with unapproved stuff.
  957. if (empty($topic_data))
  958. fatal_lang_error('no_topic_id');
  959. $boards = array_values(array_unique($boards));
  960. // Get the boards a user is allowed to merge in.
  961. $merge_boards = boardsAllowedTo('merge_any');
  962. if (empty($merge_boards))
  963. fatal_lang_error('cannot_merge_any', 'user');
  964. // Make sure they can see all boards....
  965. $request = $smcFunc['db_query']('', '
  966. SELECT b.id_board
  967. FROM {db_prefix}boards AS b
  968. WHERE b.id_board IN ({array_int:boards})
  969. AND {query_see_board}' . (!in_array(0, $merge_boards) ? '
  970. AND b.id_board IN ({array_int:merge_boards})' : '') . '
  971. LIMIT ' . count($boards),
  972. array(
  973. 'boards' => $boards,
  974. 'merge_boards' => $merge_boards,
  975. )
  976. );
  977. // If the number of boards that's in the output isn't exactly the same as we've put in there, you're in trouble.
  978. if ($smcFunc['db_num_rows']($request) != count($boards))
  979. fatal_lang_error('no_board');
  980. $smcFunc['db_free_result']($request);
  981. if (empty($_REQUEST['sa']) || $_REQUEST['sa'] == 'options')
  982. {
  983. if (count($polls) > 1)
  984. {
  985. $request = $smcFunc['db_query']('', '
  986. SELECT t.id_topic, t.id_poll, m.subject, p.question
  987. FROM {db_prefix}polls AS p
  988. INNER JOIN {db_prefix}topics AS t ON (t.id_poll = p.id_poll)
  989. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  990. WHERE p.id_poll IN ({array_int:polls})
  991. LIMIT ' . count($polls),
  992. array(
  993. 'polls' => $polls,
  994. )
  995. );
  996. while ($row = $smcFunc['db_fetch_assoc']($request))
  997. $context['polls'][] = array(
  998. 'id' => $row['id_poll'],
  999. 'topic' => array(
  1000. 'id' => $row['id_topic'],
  1001. 'subject' => $row['subject']
  1002. ),
  1003. 'question' => $row['question'],
  1004. 'selected' => $row['id_topic'] == $firstTopic
  1005. );
  1006. $smcFunc['db_free_result']($request);
  1007. }
  1008. if (count($boards) > 1)
  1009. {
  1010. $request = $smcFunc['db_query']('', '
  1011. SELECT id_board, name
  1012. FROM {db_prefix}boards
  1013. WHERE id_board IN ({array_int:boards})
  1014. ORDER BY name
  1015. LIMIT ' . count($boards),
  1016. array(
  1017. 'boards' => $boards,
  1018. )
  1019. );
  1020. while ($row = $smcFunc['db_fetch_assoc']($request))
  1021. $context['boards'][] = array(
  1022. 'id' => $row['id_board'],
  1023. 'name' => $row['name'],
  1024. 'selected' => $row['id_board'] == $topic_data[$firstTopic]['board']
  1025. );
  1026. $smcFunc['db_free_result']($request);
  1027. }
  1028. $context['topics'] = $topic_data;
  1029. foreach ($topic_data as $id => $topic)
  1030. $context['topics'][$id]['selected'] = $topic['id'] == $firstTopic;
  1031. $context['page_title'] = $txt['merge'];
  1032. $context['sub_template'] = 'merge_extra_options';
  1033. return;
  1034. }
  1035. // Determine target board.
  1036. $target_board = count($boards) > 1 ? (int) $_REQUEST['board'] : $boards[0];
  1037. if (!in_array($target_board, $boards))
  1038. fatal_lang_error('no_board');
  1039. // Determine which poll will survive and which polls won't.
  1040. $target_poll = count($polls) > 1 ? (int) $_POST['poll'] : (count($polls) == 1 ? $polls[0] : 0);
  1041. if ($target_poll > 0 && !in_array($target_poll, $polls))
  1042. fatal_lang_error('no_access', false);
  1043. $deleted_polls = empty($target_poll) ? $polls : array_diff($polls, array($target_poll));
  1044. // Determine the subject of the newly merged topic - was a custom subject specified?
  1045. if (empty($_POST['subject']) && isset($_POST['custom_subject']) && $_POST['custom_subject'] != '')
  1046. {
  1047. $target_subject = strtr($smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['custom_subject'])), array("\r" => '', "\n" => '', "\t" => ''));
  1048. // Keep checking the length.
  1049. if ($smcFunc['strlen']($target_subject) > 100)
  1050. $target_subject = $smcFunc['substr']($target_subject, 0, 100);
  1051. // Nothing left - odd but pick the first topics subject.
  1052. if ($target_subject == '')
  1053. $target_subject = $topic_data[$firstTopic]['subject'];
  1054. }
  1055. // A subject was selected from the list.
  1056. elseif (!empty($topic_data[(int) $_POST['subject']]['subject']))
  1057. $target_subject = $topic_data[(int) $_POST['subject']]['subject'];
  1058. // Nothing worked? Just take the subject of the first message.
  1059. else
  1060. $target_subject = $topic_data[$firstTopic]['subject'];
  1061. // Get the first and last message and the number of messages....
  1062. $request = $smcFunc['db_query']('', '
  1063. SELECT approved, MIN(id_msg) AS first_msg, MAX(id_msg) AS last_msg, COUNT(*) AS message_count
  1064. FROM {db_prefix}messages
  1065. WHERE id_topic IN ({array_int:topics})
  1066. GROUP BY approved
  1067. ORDER BY approved DESC',
  1068. array(
  1069. 'topics' => $topics,
  1070. )
  1071. );
  1072. $topic_approved = 1;
  1073. while ($row = $smcFunc['db_fetch_assoc']($request))
  1074. {
  1075. // If this is approved, or is fully unapproved.
  1076. if ($row['approved'] || !isset($first_msg))
  1077. {
  1078. $first_msg = $row['first_msg'];
  1079. $last_msg = $row['last_msg'];
  1080. if ($row['approved'])
  1081. {
  1082. $num_replies = $row['message_count'] - 1;
  1083. $num_unapproved = 0;
  1084. }
  1085. else
  1086. {
  1087. $topic_approved = 0;
  1088. $num_replies = 0;
  1089. $num_unapproved = $row['message_count'];
  1090. }
  1091. }
  1092. else
  1093. {
  1094. // If this has a lower first_msg then the first post is not approved and hence the number of replies was wrong!
  1095. if ($first_msg > $row['first_msg'])
  1096. {
  1097. $first_msg = $row['first_msg'];
  1098. $num_replies++;
  1099. $topic_approved = 0;
  1100. }
  1101. $num_unapproved = $row['message_count'];
  1102. }
  1103. }
  1104. $smcFunc['db_free_result']($request);
  1105. // Ensure we have a board stat for the target board.
  1106. if (!isset($boardTotals[$target_board]))
  1107. {
  1108. $boardTotals[$target_board] = array(
  1109. 'posts' => 0,
  1110. 'topics' => 0,
  1111. 'unapproved_posts' => 0,
  1112. 'unapproved_topics' => 0
  1113. );
  1114. }
  1115. // Fix the topic count stuff depending on what the new one counts as.
  1116. if ($topic_approved)
  1117. $boardTotals[$target_board]['topics']--;
  1118. else
  1119. $boardTotals[$target_board]['unapproved_topics']--;
  1120. $boardTotals[$target_board]['unapproved_posts'] -= $num_unapproved;
  1121. $boardTotals[$target_board]['posts'] -= $topic_approved ? $num_replies + 1 : $num_replies;
  1122. // Get the member ID of the first and last message.
  1123. $request = $smcFunc['db_query']('', '
  1124. SELECT id_member
  1125. FROM {db_prefix}messages
  1126. WHERE id_msg IN ({int:first_msg}, {int:last_msg})
  1127. ORDER BY id_msg
  1128. LIMIT 2',
  1129. array(
  1130. 'first_msg' => $first_msg,
  1131. 'last_msg' => $last_msg,
  1132. )
  1133. );
  1134. list ($member_started) = $smcFunc['db_fetch_row']($request);
  1135. list ($member_updated) = $smcFunc['db_fetch_row']($request);
  1136. // First and last message are the same, so only row was returned.
  1137. if ($member_updated === NULL)
  1138. $member_updated = $member_started;
  1139. $smcFunc['db_free_result']($request);
  1140. // Assign the first topic ID to be the merged topic.
  1141. $id_topic = min($topics);
  1142. // Delete the remaining topics.
  1143. $deleted_topics = array_diff($topics, array($id_topic));
  1144. $smcFunc['db_query']('', '
  1145. DELETE FROM {db_prefix}topics
  1146. WHERE id_topic IN ({array_int:deleted_topics})',
  1147. array(
  1148. 'deleted_topics' => $deleted_topics,
  1149. )
  1150. );
  1151. $smcFunc['db_query']('', '
  1152. DELETE FROM {db_prefix}log_search_subjects
  1153. WHERE id_topic IN ({array_int:deleted_topics})',
  1154. array(
  1155. 'deleted_topics' => $deleted_topics,
  1156. )
  1157. );
  1158. // Asssign the properties of the newly merged topic.
  1159. $smcFunc['db_query']('', '
  1160. UPDATE {db_prefix}topics
  1161. SET
  1162. id_board = {int:id_board},
  1163. id_member_started = {int:id_member_started},
  1164. id_member_updated = {int:id_member_updated},
  1165. id_first_msg = {int:id_first_msg},
  1166. id_last_msg = {int:id_last_msg},
  1167. id_poll = {int:id_poll},
  1168. num_replies = {int:num_replies},
  1169. unapproved_posts = {int:unapproved_posts},
  1170. num_views = {int:num_views},
  1171. is_sticky = {int:is_sticky},
  1172. approved = {int:approved}
  1173. WHERE id_topic = {int:id_topic}',
  1174. array(
  1175. 'id_board' => $target_board,
  1176. 'is_sticky' => $is_sticky,
  1177. 'approved' => $topic_approved,
  1178. 'id_topic' => $id_topic,
  1179. 'id_member_started' => $member_started,
  1180. 'id_member_updated' => $member_updated,
  1181. 'id_first_msg' => $first_msg,
  1182. 'id_last_msg' => $last_msg,
  1183. 'id_poll' => $target_poll,
  1184. 'num_replies' => $num_replies,
  1185. 'unapproved_posts' => $num_unapproved,
  1186. 'num_views' => $num_views,
  1187. )
  1188. );
  1189. // Grab the response prefix (like 'Re: ') in the default forum language.
  1190. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
  1191. {
  1192. if ($language === $user_info['language'])
  1193. $context['response_prefix'] = $txt['response_prefix'];
  1194. else
  1195. {
  1196. loadLanguage('index', $language, false);
  1197. $context['response_prefix'] = $txt['response_prefix'];
  1198. loadLanguage('index');
  1199. }
  1200. cache_put_data('response_prefix', $context['response_prefix'], 600);
  1201. }
  1202. // Change the topic IDs of all messages that will be merged. Also adjust subjects if 'enforce subject' was checked.
  1203. $smcFunc['db_query']('', '
  1204. UPDATE {db_prefix}messages
  1205. SET
  1206. id_topic = {int:id_topic},
  1207. id_board = {int:target_board}' . (empty($_POST['enforce_subject']) ? '' : ',
  1208. subject = {string:subject}') . '
  1209. WHERE id_topic IN ({array_int:topic_list})',
  1210. array(
  1211. 'topic_list' => $topics,
  1212. 'id_topic' => $id_topic,
  1213. 'target_board' => $target_board,
  1214. 'subject' => $context['response_prefix'] . $target_subject,
  1215. )
  1216. );
  1217. // Any reported posts should reflect the new board.
  1218. $smcFunc['db_query']('', '
  1219. UPDATE {db_prefix}log_reported
  1220. SET
  1221. id_topic = {int:id_topic},
  1222. id_board = {int:target_board}
  1223. WHERE id_topic IN ({array_int:topics_list})',
  1224. array(
  1225. 'topics_list' => $topics,
  1226. 'id_topic' => $id_topic,
  1227. 'target_board' => $target_board,
  1228. )
  1229. );
  1230. // Change the subject of the first message...
  1231. $smcFunc['db_query']('', '
  1232. UPDATE {db_prefix}messages
  1233. SET subject = {string:target_subject}
  1234. WHERE id_msg = {int:first_msg}',
  1235. array(
  1236. 'first_msg' => $first_msg,
  1237. 'target_subject' => $target_subject,
  1238. )
  1239. );
  1240. // Adjust all calendar events to point to the new topic.
  1241. $smcFunc['db_query']('', '
  1242. UPDATE {db_prefix}calendar
  1243. SET
  1244. id_topic = {int:id_topic},
  1245. id_board = {int:target_board}
  1246. WHERE id

Large files files are truncated, but you can click here to view the full file