forked from SimpleMachines/SMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplitTopics.php
1805 lines (1604 loc) · 60.1 KB
/
SplitTopics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Handle merging and splitting of topics
*
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines https://www.simplemachines.org
* @copyright 2022 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 2.1.0
*
* Original module by Mach8 - We'll never forget you.
*/
if (!defined('SMF'))
die('No direct access...');
/**
* splits a topic into two topics.
* delegates to the other functions (based on the URL parameter 'sa').
* loads the SplitTopics template.
* requires the split_any permission.
* is accessed with ?action=splittopics.
*/
function SplitTopics()
{
global $topic, $sourcedir;
// And... which topic were you splitting, again?
if (empty($topic))
fatal_lang_error('numbers_one_to_nine', false);
// Are you allowed to split topics?
isAllowedTo('split_any');
// Load up the "dependencies" - the template, getMsgMemberID(), and sendNotifications().
if (!isset($_REQUEST['xml']))
loadTemplate('SplitTopics');
require_once($sourcedir . '/Subs-Boards.php');
require_once($sourcedir . '/Subs-Post.php');
$subActions = array(
'selectTopics' => 'SplitSelectTopics',
'execute' => 'SplitExecute',
'index' => 'SplitIndex',
'splitSelection' => 'SplitSelectionExecute',
);
// ?action=splittopics;sa=LETSBREAKIT won't work, sorry.
if (empty($_REQUEST['sa']) || !isset($subActions[$_REQUEST['sa']]))
SplitIndex();
else
call_helper($subActions[$_REQUEST['sa']]);
}
/**
* screen shown before the actual split.
* is accessed with ?action=splittopics;sa=index.
* default sub action for ?action=splittopics.
* uses 'ask' sub template of the SplitTopics template.
* redirects to SplitSelectTopics if the message given turns out to be
* the first message of a topic.
* shows the user three ways to split the current topic.
*/
function SplitIndex()
{
global $txt, $topic, $context, $smcFunc, $modSettings;
// Validate "at".
if (empty($_GET['at']))
fatal_lang_error('numbers_one_to_nine', false);
$_GET['at'] = (int) $_GET['at'];
// Retrieve the subject and stuff of the specific topic/message.
$request = $smcFunc['db_query']('', '
SELECT m.subject, t.num_replies, t.unapproved_posts, t.id_first_msg, t.approved
FROM {db_prefix}messages AS m
INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
WHERE m.id_msg = {int:split_at}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
AND m.approved = 1') . '
AND m.id_topic = {int:current_topic}
LIMIT 1',
array(
'current_topic' => $topic,
'split_at' => $_GET['at'],
)
);
if ($smcFunc['db_num_rows']($request) == 0)
fatal_lang_error('cant_find_messages');
list ($_REQUEST['subname'], $num_replies, $unapproved_posts, $id_first_msg, $approved) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
// If not approved validate they can see it.
if ($modSettings['postmod_active'] && !$approved)
isAllowedTo('approve_posts');
// If this topic has unapproved posts, we need to count them too...
if ($modSettings['postmod_active'] && allowedTo('approve_posts'))
$num_replies += $unapproved_posts - ($approved ? 0 : 1);
// Check if there is more than one message in the topic. (there should be.)
if ($num_replies < 1)
fatal_lang_error('topic_one_post', false);
// Check if this is the first message in the topic (if so, the first and second option won't be available)
if ($id_first_msg == $_GET['at'])
return SplitSelectTopics();
// Basic template information....
$context['message'] = array(
'id' => $_GET['at'],
'subject' => $_REQUEST['subname']
);
$context['sub_template'] = 'ask';
$context['page_title'] = $txt['split'];
}
/**
* do the actual split.
* is accessed with ?action=splittopics;sa=execute.
* uses the main SplitTopics template.
* supports three ways of splitting:
* (1) only one message is split off.
* (2) all messages after and including a given message are split off.
* (3) select topics to split (redirects to SplitSelectTopics()).
* uses splitTopic function to do the actual splitting.
*/
function SplitExecute()
{
global $txt, $topic, $context, $smcFunc;
// Check the session to make sure they meant to do this.
checkSession();
// Clean up the subject.
if (!isset($_POST['subname']) || $_POST['subname'] == '')
$_POST['subname'] = $txt['new_topic'];
// Redirect to the selector if they chose selective.
if ($_POST['step2'] == 'selective')
redirectexit ('action=splittopics;sa=selectTopics;subname=' . $_POST['subname'] . ';topic=' . $topic . '.0;start2=0');
$_POST['at'] = (int) $_POST['at'];
$messagesToBeSplit = array();
if ($_POST['step2'] == 'afterthis')
{
// Fetch the message IDs of the topic that are at or after the message.
$request = $smcFunc['db_query']('', '
SELECT id_msg
FROM {db_prefix}messages
WHERE id_topic = {int:current_topic}
AND id_msg >= {int:split_at}',
array(
'current_topic' => $topic,
'split_at' => $_POST['at'],
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$messagesToBeSplit[] = $row['id_msg'];
$smcFunc['db_free_result']($request);
}
// Only the selected message has to be split. That should be easy.
elseif ($_POST['step2'] == 'onlythis')
$messagesToBeSplit[] = $_POST['at'];
// There's another action?!
else
fatal_lang_error('no_access', false);
$context['old_topic'] = $topic;
$context['new_topic'] = splitTopic($topic, $messagesToBeSplit, $_POST['subname']);
$context['page_title'] = $txt['split'];
}
/**
* allows the user to select the messages to be split.
* is accessed with ?action=splittopics;sa=selectTopics.
* uses 'select' sub template of the SplitTopics template or (for
* XMLhttp) the 'split' sub template of the Xml template.
* supports XMLhttp for adding/removing a message to the selection.
* uses a session variable to store the selected topics.
* shows two independent page indexes for both the selected and
* not-selected messages (;topic=1.x;start2=y).
*/
function SplitSelectTopics()
{
global $txt, $scripturl, $topic, $context, $modSettings, $original_msgs, $smcFunc, $options;
$context['page_title'] = $txt['split'] . ' - ' . $txt['select_split_posts'];
// Haven't selected anything have we?
$_SESSION['split_selection'][$topic] = empty($_SESSION['split_selection'][$topic]) ? array() : $_SESSION['split_selection'][$topic];
// This is a special case for split topics from quick-moderation checkboxes
if (isset($_REQUEST['subname_enc']))
$_REQUEST['subname'] = urldecode($_REQUEST['subname_enc']);
$context['not_selected'] = array(
'num_messages' => 0,
'start' => empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'],
'messages' => array(),
);
$context['selected'] = array(
'num_messages' => 0,
'start' => empty($_REQUEST['start2']) ? 0 : (int) $_REQUEST['start2'],
'messages' => array(),
);
$context['topic'] = array(
'id' => $topic,
'subject' => urlencode($_REQUEST['subname']),
);
// Some stuff for our favorite template.
$context['new_subject'] = $_REQUEST['subname'];
// Using the "select" sub template.
$context['sub_template'] = isset($_REQUEST['xml']) ? 'split' : 'select';
// Are we using a custom messages per page?
$context['messages_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
// Get the message ID's from before the move.
if (isset($_REQUEST['xml']))
{
$original_msgs = array(
'not_selected' => array(),
'selected' => array(),
);
$request = $smcFunc['db_query']('', '
SELECT id_msg
FROM {db_prefix}messages
WHERE id_topic = {int:current_topic}' . (empty($_SESSION['split_selection'][$topic]) ? '' : '
AND id_msg NOT IN ({array_int:no_split_msgs})') . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
AND approved = {int:is_approved}') . '
' . (empty($options['view_newest_first']) ? '' : 'ORDER BY id_msg DESC') . '
LIMIT {int:start}, {int:messages_per_page}',
array(
'current_topic' => $topic,
'no_split_msgs' => empty($_SESSION['split_selection'][$topic]) ? array() : $_SESSION['split_selection'][$topic],
'is_approved' => 1,
'start' => $context['not_selected']['start'],
'messages_per_page' => $context['messages_per_page'],
)
);
// You can't split the last message off.
if (empty($context['not_selected']['start']) && $smcFunc['db_num_rows']($request) <= 1 && $_REQUEST['move'] == 'down')
$_REQUEST['move'] = '';
while ($row = $smcFunc['db_fetch_assoc']($request))
$original_msgs['not_selected'][] = $row['id_msg'];
$smcFunc['db_free_result']($request);
if (!empty($_SESSION['split_selection'][$topic]))
{
$request = $smcFunc['db_query']('', '
SELECT id_msg
FROM {db_prefix}messages
WHERE id_topic = {int:current_topic}
AND id_msg IN ({array_int:split_msgs})' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
AND approved = {int:is_approved}') . '
' . (empty($options['view_newest_first']) ? '' : 'ORDER BY id_msg DESC') . '
LIMIT {int:start}, {int:messages_per_page}',
array(
'current_topic' => $topic,
'split_msgs' => $_SESSION['split_selection'][$topic],
'is_approved' => 1,
'start' => $context['selected']['start'],
'messages_per_page' => $context['messages_per_page'],
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$original_msgs['selected'][] = $row['id_msg'];
$smcFunc['db_free_result']($request);
}
}
// (De)select a message..
if (!empty($_REQUEST['move']))
{
$_REQUEST['msg'] = (int) $_REQUEST['msg'];
if ($_REQUEST['move'] == 'reset')
$_SESSION['split_selection'][$topic] = array();
elseif ($_REQUEST['move'] == 'up')
$_SESSION['split_selection'][$topic] = array_diff($_SESSION['split_selection'][$topic], array($_REQUEST['msg']));
else
$_SESSION['split_selection'][$topic][] = $_REQUEST['msg'];
}
// Make sure the selection is still accurate.
if (!empty($_SESSION['split_selection'][$topic]))
{
$request = $smcFunc['db_query']('', '
SELECT id_msg
FROM {db_prefix}messages
WHERE id_topic = {int:current_topic}
AND id_msg IN ({array_int:split_msgs})' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
AND approved = {int:is_approved}'),
array(
'current_topic' => $topic,
'split_msgs' => $_SESSION['split_selection'][$topic],
'is_approved' => 1,
)
);
$_SESSION['split_selection'][$topic] = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$_SESSION['split_selection'][$topic][] = $row['id_msg'];
$smcFunc['db_free_result']($request);
}
// Get the number of messages (not) selected to be split.
$request = $smcFunc['db_query']('', '
SELECT ' . (empty($_SESSION['split_selection'][$topic]) ? '0' : 'm.id_msg IN ({array_int:split_msgs})') . ' AS is_selected, COUNT(*) AS num_messages
FROM {db_prefix}messages AS m
WHERE m.id_topic = {int:current_topic}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
AND approved = {int:is_approved}') . (empty($_SESSION['split_selection'][$topic]) ? '' : '
GROUP BY is_selected'),
array(
'current_topic' => $topic,
'split_msgs' => !empty($_SESSION['split_selection'][$topic]) ? $_SESSION['split_selection'][$topic] : array(),
'is_approved' => 1,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$context[empty($row['is_selected']) || $row['is_selected'] == 'f' ? 'not_selected' : 'selected']['num_messages'] = $row['num_messages'];
$smcFunc['db_free_result']($request);
// Fix an oversized starting page (to make sure both pageindexes are properly set).
if ($context['selected']['start'] >= $context['selected']['num_messages'])
$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'])));
// Build a page list of the not-selected topics...
$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);
// ...and one of the selected topics.
$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);
// Get the messages and stick them into an array.
$request = $smcFunc['db_query']('', '
SELECT m.subject, COALESCE(mem.real_name, m.poster_name) AS real_name, m.poster_time, m.body, m.id_msg, m.smileys_enabled
FROM {db_prefix}messages AS m
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE m.id_topic = {int:current_topic}' . (empty($_SESSION['split_selection'][$topic]) ? '' : '
AND id_msg NOT IN ({array_int:no_split_msgs})') . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
AND approved = {int:is_approved}') . '
' . (empty($options['view_newest_first']) ? '' : 'ORDER BY m.id_msg DESC') . '
LIMIT {int:start}, {int:messages_per_page}',
array(
'current_topic' => $topic,
'no_split_msgs' => !empty($_SESSION['split_selection'][$topic]) ? $_SESSION['split_selection'][$topic] : array(),
'is_approved' => 1,
'start' => $context['not_selected']['start'],
'messages_per_page' => $context['messages_per_page'],
)
);
$context['messages'] = array();
for ($counter = 0; $row = $smcFunc['db_fetch_assoc']($request); $counter++)
{
censorText($row['subject']);
censorText($row['body']);
$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
$context['not_selected']['messages'][$row['id_msg']] = array(
'id' => $row['id_msg'],
'subject' => $row['subject'],
'time' => timeformat($row['poster_time']),
'timestamp' => $row['poster_time'],
'body' => $row['body'],
'poster' => $row['real_name'],
);
}
$smcFunc['db_free_result']($request);
// Now get the selected messages.
if (!empty($_SESSION['split_selection'][$topic]))
{
// Get the messages and stick them into an array.
$request = $smcFunc['db_query']('', '
SELECT m.subject, COALESCE(mem.real_name, m.poster_name) AS real_name, m.poster_time, m.body, m.id_msg, m.smileys_enabled
FROM {db_prefix}messages AS m
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE m.id_topic = {int:current_topic}
AND m.id_msg IN ({array_int:split_msgs})' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
AND approved = {int:is_approved}') . '
' . (empty($options['view_newest_first']) ? '' : 'ORDER BY m.id_msg DESC') . '
LIMIT {int:start}, {int:messages_per_page}',
array(
'current_topic' => $topic,
'split_msgs' => $_SESSION['split_selection'][$topic],
'is_approved' => 1,
'start' => $context['selected']['start'],
'messages_per_page' => $context['messages_per_page'],
)
);
$context['messages'] = array();
for ($counter = 0; $row = $smcFunc['db_fetch_assoc']($request); $counter++)
{
censorText($row['subject']);
censorText($row['body']);
$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
$context['selected']['messages'][$row['id_msg']] = array(
'id' => $row['id_msg'],
'subject' => $row['subject'],
'time' => timeformat($row['poster_time']),
'timestamp' => $row['poster_time'],
'body' => $row['body'],
'poster' => $row['real_name']
);
}
$smcFunc['db_free_result']($request);
}
// The XMLhttp method only needs the stuff that changed, so let's compare.
if (isset($_REQUEST['xml']))
{
$changes = array(
'remove' => array(
'not_selected' => array_diff($original_msgs['not_selected'], array_keys($context['not_selected']['messages'])),
'selected' => array_diff($original_msgs['selected'], array_keys($context['selected']['messages'])),
),
'insert' => array(
'not_selected' => array_diff(array_keys($context['not_selected']['messages']), $original_msgs['not_selected']),
'selected' => array_diff(array_keys($context['selected']['messages']), $original_msgs['selected']),
),
);
$context['changes'] = array();
foreach ($changes as $change_type => $change_array)
foreach ($change_array as $section => $msg_array)
{
if (empty($msg_array))
continue;
foreach ($msg_array as $id_msg)
{
$context['changes'][$change_type . $id_msg] = array(
'id' => $id_msg,
'type' => $change_type,
'section' => $section,
);
if ($change_type == 'insert')
$context['changes']['insert' . $id_msg]['insert_value'] = $context[$section]['messages'][$id_msg];
}
}
}
}
/**
* do the actual split of a selection of topics.
* is accessed with ?action=splittopics;sa=splitSelection.
* uses the main SplitTopics template.
* uses splitTopic function to do the actual splitting.
*/
function SplitSelectionExecute()
{
global $txt, $topic, $context;
// Make sure the session id was passed with post.
checkSession();
// Default the subject in case it's blank.
if (!isset($_POST['subname']) || $_POST['subname'] == '')
$_POST['subname'] = $txt['new_topic'];
// You must've selected some messages! Can't split out none!
if (empty($_SESSION['split_selection'][$topic]))
fatal_lang_error('no_posts_selected', false);
$context['old_topic'] = $topic;
$context['new_topic'] = splitTopic($topic, $_SESSION['split_selection'][$topic], $_POST['subname']);
$context['page_title'] = $txt['split'];
}
/**
* general function to split off a topic.
* creates a new topic and moves the messages with the IDs in
* array messagesToBeSplit to the new topic.
* the subject of the newly created topic is set to 'newSubject'.
* marks the newly created message as read for the user splitting it.
* updates the statistics to reflect a newly created topic.
* logs the action in the moderation log.
* a notification is sent to all users monitoring this topic.
*
* @param int $split1_ID_TOPIC The ID of the topic we're splitting
* @param array $splitMessages The IDs of the messages being split
* @param string $new_subject The subject of the new topic
* @return int The ID of the new split topic.
*/
function splitTopic($split1_ID_TOPIC, $splitMessages, $new_subject)
{
global $smcFunc, $txt, $sourcedir;
// Nothing to split?
if (empty($splitMessages))
fatal_lang_error('no_posts_selected', false);
// Get some board info.
$request = $smcFunc['db_query']('', '
SELECT id_board, approved
FROM {db_prefix}topics
WHERE id_topic = {int:id_topic}
LIMIT 1',
array(
'id_topic' => $split1_ID_TOPIC,
)
);
list ($id_board, $split1_approved) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
// Find the new first and last not in the list. (old topic)
$request = $smcFunc['db_query']('', '
SELECT
MIN(m.id_msg) AS myid_first_msg, MAX(m.id_msg) AS myid_last_msg, COUNT(*) AS message_count, m.approved
FROM {db_prefix}messages AS m
INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:id_topic})
WHERE m.id_msg NOT IN ({array_int:no_msg_list})
AND m.id_topic = {int:id_topic}
GROUP BY m.approved
ORDER BY m.approved DESC
LIMIT 2',
array(
'id_topic' => $split1_ID_TOPIC,
'no_msg_list' => $splitMessages,
)
);
// You can't select ALL the messages!
if ($smcFunc['db_num_rows']($request) == 0)
fatal_lang_error('selected_all_posts', false);
$split1_first_msg = null;
$split1_last_msg = null;
while ($row = $smcFunc['db_fetch_assoc']($request))
{
// Get the right first and last message dependant on approved state...
if (empty($split1_first_msg) || $row['myid_first_msg'] < $split1_first_msg)
$split1_first_msg = $row['myid_first_msg'];
if (empty($split1_last_msg) || $row['approved'])
$split1_last_msg = $row['myid_last_msg'];
// Get the counts correct...
if ($row['approved'])
{
$split1_replies = $row['message_count'] - 1;
$split1_unapprovedposts = 0;
}
else
{
if (!isset($split1_replies))
$split1_replies = 0;
// If the topic isn't approved then num replies must go up by one... as first post wouldn't be counted.
elseif (!$split1_approved)
$split1_replies++;
$split1_unapprovedposts = $row['message_count'];
}
}
$smcFunc['db_free_result']($request);
$split1_firstMem = getMsgMemberID($split1_first_msg);
$split1_lastMem = getMsgMemberID($split1_last_msg);
// Find the first and last in the list. (new topic)
$request = $smcFunc['db_query']('', '
SELECT MIN(id_msg) AS myid_first_msg, MAX(id_msg) AS myid_last_msg, COUNT(*) AS message_count, approved
FROM {db_prefix}messages
WHERE id_msg IN ({array_int:msg_list})
AND id_topic = {int:id_topic}
GROUP BY id_topic, approved
ORDER BY approved DESC
LIMIT 2',
array(
'msg_list' => $splitMessages,
'id_topic' => $split1_ID_TOPIC,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
// As before get the right first and last message dependant on approved state...
if (empty($split2_first_msg) || $row['myid_first_msg'] < $split2_first_msg)
$split2_first_msg = $row['myid_first_msg'];
if (empty($split2_last_msg) || $row['approved'])
$split2_last_msg = $row['myid_last_msg'];
// Then do the counts again...
if ($row['approved'])
{
$split2_approved = true;
$split2_replies = $row['message_count'] - 1;
$split2_unapprovedposts = 0;
}
else
{
// Should this one be approved??
if ($split2_first_msg == $row['myid_first_msg'])
$split2_approved = false;
if (!isset($split2_replies))
$split2_replies = 0;
// As before, fix number of replies.
elseif (!$split2_approved)
$split2_replies++;
$split2_unapprovedposts = $row['message_count'];
}
}
$smcFunc['db_free_result']($request);
$split2_firstMem = getMsgMemberID($split2_first_msg);
$split2_lastMem = getMsgMemberID($split2_last_msg);
// No database changes yet, so let's double check to see if everything makes at least a little sense.
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))
fatal_lang_error('cant_find_messages');
// You cannot split off the first message of a topic.
if ($split1_first_msg > $split2_first_msg)
fatal_lang_error('split_first_post', false);
// We're off to insert the new topic! Use 0 for now to avoid UNIQUE errors.
$split2_ID_TOPIC = $smcFunc['db_insert']('',
'{db_prefix}topics',
array(
'id_board' => 'int',
'id_member_started' => 'int',
'id_member_updated' => 'int',
'id_first_msg' => 'int',
'id_last_msg' => 'int',
'num_replies' => 'int',
'unapproved_posts' => 'int',
'approved' => 'int',
'is_sticky' => 'int',
),
array(
(int) $id_board, $split2_firstMem, $split2_lastMem, 0,
0, $split2_replies, $split2_unapprovedposts, (int) $split2_approved, 0,
),
array('id_topic'),
1
);
if ($split2_ID_TOPIC <= 0)
fatal_lang_error('cant_insert_topic');
// Move the messages over to the other topic.
$new_subject = strtr($smcFunc['htmltrim']($smcFunc['htmlspecialchars']($new_subject)), array("\r" => '', "\n" => '', "\t" => ''));
// Check the subject length.
if ($smcFunc['strlen']($new_subject) > 100)
$new_subject = $smcFunc['substr']($new_subject, 0, 100);
// Valid subject?
if ($new_subject != '')
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET
id_topic = {int:id_topic},
subject = CASE WHEN id_msg = {int:split_first_msg} THEN {string:new_subject} ELSE {string:new_subject_replies} END
WHERE id_msg IN ({array_int:split_msgs})',
array(
'split_msgs' => $splitMessages,
'id_topic' => $split2_ID_TOPIC,
'new_subject' => $new_subject,
'split_first_msg' => $split2_first_msg,
'new_subject_replies' => $txt['response_prefix'] . $new_subject,
)
);
// Cache the new topics subject... we can do it now as all the subjects are the same!
updateStats('subject', $split2_ID_TOPIC, $new_subject);
}
// Any associated reported posts better follow...
$smcFunc['db_query']('', '
UPDATE {db_prefix}log_reported
SET id_topic = {int:id_topic}
WHERE id_msg IN ({array_int:split_msgs})',
array(
'split_msgs' => $splitMessages,
'id_topic' => $split2_ID_TOPIC,
)
);
// Mess with the old topic's first, last, and number of messages.
$smcFunc['db_query']('', '
UPDATE {db_prefix}topics
SET
num_replies = {int:num_replies},
id_first_msg = {int:id_first_msg},
id_last_msg = {int:id_last_msg},
id_member_started = {int:id_member_started},
id_member_updated = {int:id_member_updated},
unapproved_posts = {int:unapproved_posts}
WHERE id_topic = {int:id_topic}',
array(
'num_replies' => $split1_replies,
'id_first_msg' => $split1_first_msg,
'id_last_msg' => $split1_last_msg,
'id_member_started' => $split1_firstMem,
'id_member_updated' => $split1_lastMem,
'unapproved_posts' => $split1_unapprovedposts,
'id_topic' => $split1_ID_TOPIC,
)
);
// Now, put the first/last message back to what they should be.
$smcFunc['db_query']('', '
UPDATE {db_prefix}topics
SET
id_first_msg = {int:id_first_msg},
id_last_msg = {int:id_last_msg}
WHERE id_topic = {int:id_topic}',
array(
'id_first_msg' => $split2_first_msg,
'id_last_msg' => $split2_last_msg,
'id_topic' => $split2_ID_TOPIC,
)
);
// If the new topic isn't approved ensure the first message flags this just in case.
if (!$split2_approved)
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET approved = {int:approved}
WHERE id_msg = {int:id_msg}
AND id_topic = {int:id_topic}',
array(
'approved' => 0,
'id_msg' => $split2_first_msg,
'id_topic' => $split2_ID_TOPIC,
)
);
// The board has more topics now (Or more unapproved ones!).
$smcFunc['db_query']('', '
UPDATE {db_prefix}boards
SET ' . ($split2_approved ? '
num_topics = num_topics + 1' : '
unapproved_topics = unapproved_topics + 1') . '
WHERE id_board = {int:id_board}',
array(
'id_board' => $id_board,
)
);
// Copy log topic entries.
// @todo This should really be chunked.
$request = $smcFunc['db_query']('', '
SELECT id_member, id_msg, unwatched
FROM {db_prefix}log_topics
WHERE id_topic = {int:id_topic}',
array(
'id_topic' => (int) $split1_ID_TOPIC,
)
);
if ($smcFunc['db_num_rows']($request) > 0)
{
$replaceEntries = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$replaceEntries[] = array($row['id_member'], $split2_ID_TOPIC, $row['id_msg'], $row['unwatched']);
$smcFunc['db_insert']('ignore',
'{db_prefix}log_topics',
array('id_member' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'unwatched' => 'int'),
$replaceEntries,
array('id_member', 'id_topic')
);
unset($replaceEntries);
}
$smcFunc['db_free_result']($request);
// Housekeeping.
updateStats('topic');
updateLastMessages($id_board);
logAction('split', array('topic' => $split1_ID_TOPIC, 'new_topic' => $split2_ID_TOPIC, 'board' => $id_board));
// Notify people that this topic has been split?
sendNotifications($split1_ID_TOPIC, 'split');
// If there's a search index that needs updating, update it...
require_once($sourcedir . '/Search.php');
$searchAPI = findSearchAPI();
if (is_callable(array($searchAPI, 'topicSplit')))
$searchAPI->topicSplit($split2_ID_TOPIC, $splitMessages);
// Maybe we want to let an external CMS know about this split
$split1 = array(
'num_replies' => $split1_replies,
'id_first_msg' => $split1_first_msg,
'id_last_msg' => $split1_last_msg,
'id_member_started' => $split1_firstMem,
'id_member_updated' => $split1_lastMem,
'unapproved_posts' => $split1_unapprovedposts,
'id_topic' => $split1_ID_TOPIC,
);
$split2 = array(
'num_replies' => $split2_replies,
'id_first_msg' => $split2_first_msg,
'id_last_msg' => $split2_last_msg,
'id_member_started' => $split2_firstMem,
'id_member_updated' => $split2_lastMem,
'unapproved_posts' => $split2_unapprovedposts,
'id_topic' => $split2_ID_TOPIC,
);
call_integration_hook('integrate_split_topic', array($split1, $split2, $new_subject, $id_board));
// Return the ID of the newly created topic.
return $split2_ID_TOPIC;
}
/**
* merges two or more topics into one topic.
* delegates to the other functions (based on the URL parameter sa).
* loads the SplitTopics template.
* requires the merge_any permission.
* is accessed with ?action=mergetopics.
*/
function MergeTopics()
{
// Load the template....
loadTemplate('MoveTopic');
$subActions = array(
'done' => 'MergeDone',
'execute' => 'MergeExecute',
'index' => 'MergeIndex',
'options' => 'MergeExecute',
);
// ?action=mergetopics;sa=LETSBREAKIT won't work, sorry.
if (empty($_REQUEST['sa']) || !isset($subActions[$_REQUEST['sa']]))
MergeIndex();
else
call_helper($subActions[$_REQUEST['sa']]);
}
/**
* allows to pick a topic to merge the current topic with.
* is accessed with ?action=mergetopics;sa=index
* default sub action for ?action=mergetopics.
* uses 'merge' sub template of the MoveTopic template.
* allows to set a different target board.
*/
function MergeIndex()
{
global $txt, $board, $context, $smcFunc, $sourcedir;
global $scripturl, $modSettings;
if (!isset($_GET['from']))
fatal_lang_error('no_access', false);
$_GET['from'] = (int) $_GET['from'];
$_REQUEST['targetboard'] = isset($_REQUEST['targetboard']) ? (int) $_REQUEST['targetboard'] : $board;
$context['target_board'] = $_REQUEST['targetboard'];
// Prepare a handy query bit for approval...
if ($modSettings['postmod_active'])
{
$can_approve_boards = boardsAllowedTo('approve_posts');
$onlyApproved = $can_approve_boards !== array(0) && !in_array($_REQUEST['targetboard'], $can_approve_boards);
}
else
$onlyApproved = false;
// How many topics are on this board? (used for paging.)
$request = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}topics AS t
WHERE t.id_board = {int:id_board}' . ($onlyApproved ? '
AND t.approved = {int:is_approved}' : ''),
array(
'id_board' => $_REQUEST['targetboard'],
'is_approved' => 1,
)
);
list ($topiccount) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
// Make the page list.
$context['page_index'] = constructPageIndex($scripturl . '?action=mergetopics;from=' . $_GET['from'] . ';targetboard=' . $_REQUEST['targetboard'] . ';board=' . $board . '.%1$d', $_REQUEST['start'], $topiccount, $modSettings['defaultMaxTopics'], true);
// Get the topic's subject.
$request = $smcFunc['db_query']('', '
SELECT m.subject
FROM {db_prefix}topics AS t
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
WHERE t.id_topic = {int:id_topic}
AND t.id_board = {int:current_board}' . ($onlyApproved ? '
AND t.approved = {int:is_approved}' : '') . '
LIMIT 1',
array(
'current_board' => $board,
'id_topic' => $_GET['from'],
'is_approved' => 1,
)
);
if ($smcFunc['db_num_rows']($request) == 0)
fatal_lang_error('no_board');
list ($subject) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
// Tell the template a few things..
$context['origin_topic'] = $_GET['from'];
$context['origin_subject'] = $subject;
$context['origin_js_subject'] = addcslashes(addslashes($subject), '/');
$context['page_title'] = $txt['merge'];
// Check which boards you have merge permissions on.
$merge_boards = boardsAllowedTo('merge_any');
if (empty($merge_boards))
fatal_lang_error('cannot_merge_any', 'user');
// No sense in loading this if you can only merge on this board
if (count($merge_boards) > 1 || in_array(0, $merge_boards))
{
require_once($sourcedir . '/Subs-MessageIndex.php');
// Set up a couple of options for our board list
$options = array(
'not_redirection' => true,
'selected_board' => $context['target_board'],
);
// Only include these boards in the list (0 means you're an admin')
if (!in_array(0, $merge_boards))
$options['included_boards'] = $merge_boards;
$context['merge_categories'] = getBoardList($options);
}
// Get some topics to merge it with.
$request = $smcFunc['db_query']('', '
SELECT t.id_topic, m.subject, m.id_member, COALESCE(mem.real_name, m.poster_name) AS poster_name
FROM {db_prefix}topics AS t
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE t.id_board = {int:id_board}
AND t.id_topic != {int:id_topic}
AND t.id_redirect_topic = {int:not_redirect}' . ($onlyApproved ? '
AND t.approved = {int:is_approved}' : '') . '
ORDER BY {raw:sort}
LIMIT {int:offset}, {int:limit}',
array(
'id_board' => $_REQUEST['targetboard'],
'id_topic' => $_GET['from'],
'sort' => 't.is_sticky DESC, t.id_last_msg DESC',
'offset' => $_REQUEST['start'],
'limit' => $modSettings['defaultMaxTopics'],
'is_approved' => 1,
'not_redirect' => 0,
)
);
$context['topics'] = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
censorText($row['subject']);
$context['topics'][] = array(
'id' => $row['id_topic'],
'poster' => array(
'id' => $row['id_member'],
'name' => $row['poster_name'],
'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" target="_blank" rel="noopener">' . $row['poster_name'] . '</a>'
),
'subject' => $row['subject'],
'js_subject' => addcslashes(addslashes($row['subject']), '/')
);
}
$smcFunc['db_free_result']($request);
if (empty($context['topics']) && count($merge_boards) <= 1 && !in_array(0, $merge_boards))
fatal_lang_error('merge_need_more_topics');
$context['sub_template'] = 'merge';
}
/**
* set merge options and do the actual merge of two or more topics.
*
* the merge options screen:
* * shows topics to be merged and allows to set some merge options.
* * is accessed by ?action=mergetopics;sa=options.and can also internally be called by QuickModeration() (Subs-Boards.php).
* * uses 'merge_extra_options' sub template of the MoveTopic template.
*