SplitTopics.php 55 KB

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