Issues (1014)

Sources/MoveTopic.php (1 issue)

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