forked from SimpleMachines/SimpleDesk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleDesk-AdminMaint.php
More file actions
1168 lines (1030 loc) · 38.8 KB
/
SimpleDesk-AdminMaint.php
File metadata and controls
1168 lines (1030 loc) · 38.8 KB
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
/**************************************************************
* Simple Desk Project - www.simpledesk.net *
***************************************************************
* An advanced help desk modification built on SMF *
***************************************************************
* *
* * Copyright 2025 - SimpleDesk.net *
* *
* This file and its contents are subject to the license *
* included with this distribution, license.txt, which *
* states that this software is New BSD Licensed. *
* Any questions, please contact SimpleDesk.net *
* *
***************************************************************
* SimpleDesk Version: 2.1.0 *
* File Info: SimpleDesk-AdminMaint.php *
**************************************************************/
/**
* This file handles the core of SimpleDesk's administrative maintenance.
*
* @package source
* @since 2.0
*/
if (!defined('SMF'))
die('Hacking attempt...');
/**
* The start point for maintenance.
*
* We're directed here from the main administration centre, after permission checks and a few dependencies loaded.
*
* @since 2.0
*/
function shd_admin_maint()
{
global $context, $txt, $db_show_debug, $settings;
// Right, if we're here, we really, really need to turn this off. Because anything we do from this page onwards hurts the log badly.
$db_show_debug = false;
loadTemplate('sd_template/SimpleDesk-AdminMaint');
loadTemplate(false, array('admin', 'helpdesk_admin'));
loadLanguage('ManageMaintenance');
$subactions = array(
'main' => array(
'function' => 'shd_admin_maint_home',
'icon' => 'maintenance.png',
'title' => $txt['shd_admin_maint'],
),
'reattribute' => array(
'function' => 'shd_admin_maint_reattribute',
'icon' => 'user.png',
'title' => $txt['shd_admin_maint_reattribute'],
'description' => $txt['shd_admin_maint_reattribute_desc'],
),
'massdeptmove' => array(
'function' => 'shd_admin_maint_massdeptmove',
'icon' => 'movedept.png',
'title' => $txt['shd_admin_maint_massdeptmove'],
'description' => $txt['shd_admin_maint_massdeptmove'],
),
'findrepair' => array(
'function' => 'shd_admin_maint_findrepair',
'icon' => 'find_repair.png',
'title' => $txt['shd_admin_maint_findrepair'],
'description' => $txt['shd_admin_maint_findrepair_desc'],
),
'search' => array(
'function' => 'shd_admin_maint_search',
'icon' => 'search.png',
'title' => $txt['shd_maint_search_settings'],
),
);
$context['shd_current_subaction'] = isset($_REQUEST['sa']) && isset($subactions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'main';
$context[$context['admin_menu_name']]['tab_data'] = array(
'title' => '<img src="' . $settings['default_theme_url'] . '/images/simpledesk/' . $subactions[$context['shd_current_subaction']]['icon'] . '" class="icon" alt="*">' . $subactions[$context['shd_current_subaction']]['title'],
'description' => $txt['shd_admin_options_desc'],
'tabs' => array(
'main' => array(
'description' => $txt['shd_admin_maint_desc'],
),
'search' => array(
'description' => $txt['shd_maint_search_settings_desc'],
),
),
);
// We need to fix the descriptions just in case.
if (isset($subactions[$context['shd_current_subaction']]['description']))
$context[$context['admin_menu_name']]['tab_data']['tabs']['main']['description'] = $subactions[$context['shd_current_subaction']]['description'];
call_user_func($subactions[$context['shd_current_subaction']]['function']);
}
function shd_admin_maint_home()
{
global $context, $txt, $smcFunc;
$context['dept_list'] = array(
0 => $txt['shd_admin_maint_massdeptmove_select'],
);
$depts = shd_allowed_to('access_helpdesk', false);
if (!is_bool($depts) && count($depts) > 1)
{
$query = $smcFunc['db_query']('', '
SELECT id_dept, dept_name
FROM {db_prefix}helpdesk_depts
WHERE id_dept IN ({array_int:depts})
ORDER BY dept_order',
array(
'depts' => $depts,
)
);
while ($row = $smcFunc['db_fetch_assoc']($query))
$context['dept_list'][$row['id_dept']] = $row['dept_name'];
$smcFunc['db_free_result']($query);
}
$context['sub_template'] = 'shd_admin_maint_home';
$context['page_title'] = $txt['shd_admin_maint'];
}
function shd_admin_maint_reattribute()
{
global $context, $txt, $smcFunc, $sourcedir;
checkSession('request');
$context['page_title'] = $txt['shd_admin_maint_reattribute'];
$context['sub_template'] = 'shd_admin_maint_reattributedone';
// Find the member.
require_once($sourcedir . '/Subs-Auth.php');
$members = findMembers($_POST['to']);
if (empty($members))
shd_fatal_lang_error('shd_reattribute_cannot_find_member');
$memID = array_shift($members);
$memID = $memID['id'];
if ($_POST['type'] == 'email')
{
if (empty($_POST['from_email']))
shd_fatal_lang_error('shd_reattribute_no_email');
$clause = 'poster_email = {string:attribute}';
$attribute = $_POST['from_email'];
}
elseif ($_POST['type'] == 'name')
{
if (empty($_POST['from_name']))
shd_fatal_lang_error('shd_reattribute_no_user');
$clause = 'poster_name = {string:attribute}';
$attribute = $_POST['from_name'];
}
elseif ($_POST['type'] == 'starter')
{
if (empty($_POST['from_starter']))
shd_fatal_lang_error('shd_reattribute_no_user');
$from = findMembers($_POST['from_starter']);
if (empty($from))
shd_fatal_lang_error('shd_reattribute_cannot_find_member_from');
$fromID = array_shift($from);
$attribute = $fromID['id'];
$clause = 'id_msg in (
SELECT id_first_msg
FROM {db_prefix}helpdesk_tickets
WHERE id_member_started = {int:attribute})';
}
else
return shd_fatal_lang_error('shd_reattribute_no_user');
// Now, we don't delete the user id from posts on account deletion, never have.
// So, get all the user ids attached to this user/email, make sure they're not in use, and then reattribute them.
$members = array();
$request = $smcFunc['db_query']('', '
SELECT id_member
FROM {db_prefix}helpdesk_ticket_replies
WHERE ' . $clause,
array(
'attribute' => $attribute,
)
);
while ($row = $smcFunc['db_fetch_row']($request))
$members[] = $row[0];
$smcFunc['db_free_result']($request);
// Did we find any members? If not, bail.
if (empty($members))
shd_fatal_lang_error('shd_reattribute_no_messages', false);
// Topic starters are a bit easier.
if ($_POST['type'] == 'starter')
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}helpdesk_ticket_replies
SET id_member = {int:new_id}
WHERE id_msg IN (
SELECT id_first_msg
FROM {db_prefix}helpdesk_tickets
WHERE id_member_started = {int:from_id})',
array(
'new_id' => $memID,
'from_id' => $attribute,
)
);
}
else
{
// So we found some old member ids. Are any of them still in use?
$temp_members = loadMemberData($members, false, 'minimal');
if (empty($temp_members))
$temp_members = array();
$members = array_diff($members, $temp_members);
if (empty($members))
shd_fatal_lang_error('shd_reattribute_in_use', false);
// OK, let's go!
$smcFunc['db_query']('', '
UPDATE {db_prefix}helpdesk_ticket_replies
SET id_member = {int:new_id}
WHERE id_member IN ({array_int:old_ids})',
array(
'new_id' => $memID,
'old_ids' => $members,
)
);
}
// Log this.
shd_admin_log('admin_maint', array(
'action' => 'reattribute',
'type' => $_POST['type'],
'to' => $memID,
'from' => $attribute
));
}
function shd_admin_maint_massdeptmove()
{
global $context, $txt, $smcFunc, $sourcedir;
checkSession('request');
$context['page_title'] = $txt['shd_admin_maint_massdeptmove'];
$depts = shd_allowed_to('access_helpdesk', false);
$_POST['id_dept_from'] = isset($_POST['id_dept_from']) ? (int) $_POST['id_dept_from'] : 0;
$_POST['id_dept_to'] = isset($_POST['id_dept_to']) ? (int) $_POST['id_dept_to'] : 0;
if ($_POST['id_dept_from'] == 0 || $_POST['id_dept_to'] == 0 || !in_array($_POST['id_dept_from'], $depts) || !in_array($_POST['id_dept_to'], $depts))
shd_fatal_lang_error('shd_unknown_dept', false);
elseif ($_POST['id_dept_from'] == $_POST['id_dept_to'])
shd_fatal_lang_error('shd_admin_maint_massdeptmove_samedept', false);
$clauses = array();
if (empty($_POST['moveopen']))
$clauses[] = 'AND status NOT IN (' . implode(',', array(TICKET_STATUS_NEW, TICKET_STATUS_PENDING_USER, TICKET_STATUS_PENDING_STAFF, TICKET_STATUS_WITH_SUPERVISOR, TICKET_STATUS_ESCALATED)) . ')';
if (empty($_POST['moveclosed']))
$clauses[] = 'AND status != ' . TICKET_STATUS_CLOSED;
if (empty($_POST['movedeleted']))
$clauses[] = 'AND status != ' . TICKET_STATUS_DELETED;
$_POST['movelast_less_days'] = isset($_POST['movelast_less_days']) && !empty($_POST['movelast_less']) ? (int) $_POST['movelast_less_days'] : 0;
if ($_POST['movelast_less_days'] > 0)
$clauses[] = 'AND last_updated >= ' . (time() - ($_POST['movelast_less_days'] * 86400));
$_POST['movelast_more_days'] = isset($_POST['movelast_more_days']) && !empty($_POST['movelast_more']) ? (int) $_POST['movelast_more_days'] : 0;
if ($_POST['movelast_more_days'] > 0)
$clauses[] = 'AND last_updated < ' . (time() - ($_POST['movelast_more_days'] * 86400));
// OK, let's start. How many tickets are there to move?
if (empty($_POST['massdeptmove']))
{
$query = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}helpdesk_tickets
WHERE id_dept = {int:dept_from} ' . implode(' ', $clauses),
array(
'dept_from' => $_POST['id_dept_from'],
)
);
list($count) = $smcFunc['db_fetch_row']($query);
$smcFunc['db_free_result']($query);
if (!empty($count))
$_POST['massdeptmove'] = $count;
else
$_GET['done'] = true;
}
// OK, so we know we're going to be doing some tickets, or do we?
$_POST['tickets_done'] = isset($_POST['tickets_done']) ? (int) $_POST['tickets_done'] : 0;
if (isset($_GET['done']) || $_POST['tickets_done'] >= $_POST['massdeptmove'])
{
// Log this.
shd_admin_log('admin_maint', array(
'action' => 'move_dept',
'to' => $_POST['id_dept_to'],
'from' => $_POST['id_dept_from']
));
$context['sub_template'] = 'shd_admin_maint_massdeptmovedone';
return;
}
// So, do this batch.
$step_count = 10;
$tickets = array();
// We don't need to get particularly clever; whatever tickets we did in any previous batch, well, they will be gone by now.
$query = $smcFunc['db_query']('', '
SELECT id_ticket, subject
FROM {db_prefix}helpdesk_tickets
WHERE id_dept = {int:dept_from} ' . implode(' ', $clauses) . '
ORDER BY id_ticket
LIMIT {int:step}',
array(
'dept_from' => $_POST['id_dept_from'],
'step' => $step_count,
)
);
while ($row = $smcFunc['db_fetch_assoc']($query))
$tickets[$row['id_ticket']] = $row['subject'];
$smcFunc['db_free_result']($query);
if (!empty($tickets))
{
// Get department ids.
$query = $smcFunc['db_query']('', '
SELECT id_dept, dept_name
FROM {db_prefix}helpdesk_depts
WHERE id_dept IN ({array_int:depts})',
array(
'depts' => array($_POST['id_dept_from'], $_POST['id_dept_to']),
)
);
$depts = array();
while ($row = $smcFunc['db_fetch_assoc']($query))
$depts[$row['id_dept']] = $row['dept_name'];
$smcFunc['db_free_result']($query);
// OK, we have the ticket ids. Now we'll move the set and log each one moved.
$smcFunc['db_query']('', '
UPDATE {db_prefix}helpdesk_tickets
SET id_dept = {int:dept_to}
WHERE id_ticket IN ({array_int:ids})',
array(
'dept_to' => $_POST['id_dept_to'],
'ids' => array_keys($tickets),
)
);
// This is the same every time.
$log_params = array(
'old_dept_id' => $_POST['id_dept_from'],
'old_dept_name' => $depts[$_POST['id_dept_from']],
'new_dept_id' => $_POST['id_dept_to'],
'new_dept_name' => $depts[$_POST['id_dept_to']],
);
foreach ($tickets as $id => $subject)
{
$log_params['subject'] = $subject;
$log_params['ticket'] = $id;
shd_log_action('move_dept', $log_params);
}
shd_clear_active_tickets($_POST['id_dept_from']);
shd_clear_active_tickets($_POST['id_dept_to']);
$_POST['tickets_done'] += $step_count;
}
// Prepare to shove everything we need into the form so we can go again.
$context['continue_countdown'] = 3;
$context['continue_get_data'] = '?action=admin;area=helpdesk_maint;sa=massdeptmove;' . $context['session_var'] . '=' . $context['session_id'];
$context['continue_post_data'] = '
<input type="hidden" name="id_dept_from" value="' . $_POST['id_dept_from'] . '">
<input type="hidden" name="id_dept_to" value="' . $_POST['id_dept_to'] . '">
<input type="hidden" name="tickets_done" value="' . $_POST['tickets_done'] . '">
<input type="hidden" name="massdeptmove" value="' . $_POST['massdeptmove'] . '">';
if (!empty($_POST['moveopen']))
$context['continue_post_data'] .= '
<input type="hidden" name="moveopen" value="' . $_POST['moveopen'] . '">';
if (!empty($_POST['moveclosed']))
$context['continue_post_data'] .= '
<input type="hidden" name="moveclosed" value="' . $_POST['moveclosed'] . '">';
if (!empty($_POST['movedeleted']))
$context['continue_post_data'] .= '
<input type="hidden" name="movedeleted" value="' . $_POST['movedeleted'] . '">';
if ($_POST['movelast_less_days'] > 0)
$context['continue_post_data'] .= '
<input type="hidden" name="movelast_less" value="1">
<input type="hidden" name="movelast_less_days" value="' . $_POST['movelast_less_days'] . '">';
if ($_POST['movelast_more_days'] > 0)
$context['continue_post_data'] .= '
<input type="hidden" name="movelast_more" value="1">
<input type="hidden" name="movelast_more_days" value="' . $_POST['movelast_more_days'] . '">';
$context['sub_template'] = 'not_done';
$context['continue_percent'] = $_POST['tickets_done'] > $_POST['massdeptmove'] ? 100 : floor($_POST['tickets_done'] / $_POST['massdeptmove'] * 100);
}
function shd_admin_maint_findrepair()
{
global $context, $txt;
checkSession('request');
$context['page_title'] = $txt['shd_admin_maint_findrepair'];
$context['maint_steps'] = array(
array(
'name' => 'zero_entries',
'pc' => 15,
),
array(
'name' => 'deleted',
'pc' => 20,
),
array(
'name' => 'first_last',
'pc' => 20,
),
array(
'name' => 'status',
'pc' => 15,
),
array(
'name' => 'starter_updater',
'pc' => 15,
),
array(
'name' => 'invalid_dept',
'pc' => 10,
),
array(
'name' => 'clean_cache',
'pc' => 5,
),
);
if (isset($_GET['done']))
{
// Log this.
shd_admin_log('admin_maint', array('action' => 'findrepair'));
$context['sub_template'] = 'shd_admin_maint_findrepairdone';
$context['maintenance_result'] = !empty($_SESSION['shd_maint']) ? $_SESSION['shd_maint'] : array();
unset($_SESSION['shd_maint']);
return;
}
$context['step'] = isset($_REQUEST['step']) ? (int) $_REQUEST['step'] : 0;
if (!isset($context['maint_steps'][$context['step']]))
$context['step'] = 0;
$context['continue_countdown'] = 3;
$context['continue_get_data'] = '?action=admin;area=helpdesk_maint;sa=findrepair;' . $context['session_var'] . '=' . $context['session_id'];
$context['continue_post_data'] = '';
$context['sub_template'] = 'not_done';
$context['continue_percent'] = 0;
for ($i = 0; $i <= $context['step']; $i++)
$context['continue_percent'] += $context['maint_steps'][$i]['pc'];
call_user_func('shd_maint_' . $context['maint_steps'][$context['step']]['name']);
}
// Validate that all tickets and messages have a valid id number
function shd_maint_zero_entries()
{
global $context, $smcFunc;
// Check for tickets with id-ticket of 0.
$query = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}helpdesk_tickets
WHERE id_ticket = 0');
list($tickets) = $smcFunc['db_fetch_row']($query);
if (!empty($tickets))
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}helpdesk_tickets
SET id_ticket = NULL
WHERE id_ticket = 0');
$_SESSION['shd_maint']['zero_tickets'] = $smcFunc['db_affected_rows']();
}
$smcFunc['db_free_result']($query);
// And ticket replies with an id-msg 0
$query = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}helpdesk_ticket_replies
WHERE id_msg = 0');
list($msgs) = $smcFunc['db_fetch_row']($query);
if (!empty($msgs))
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}helpdesk_ticket_replies
SET id_msg = NULL
WHERE id_msg = 0');
$_SESSION['shd_maint']['zero_msgs'] = $smcFunc['db_affected_rows']();
}
$smcFunc['db_free_result']($query);
// This is a short operation, no suboperation, so just tell it to go onto the next step.
$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . ($context['step'] + 1) . '">';
}
// Ensure that the count of number of replies/deleted replies/whether ticket contains deleted replies are all correct.
function shd_maint_deleted()
{
global $context, $smcFunc, $txt;
// First we need the number of tickets
$query = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}helpdesk_tickets');
list($ticket_count) = $smcFunc['db_fetch_row']($query);
$smcFunc['db_free_result']($query);
$_REQUEST['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
$step_size = 100;
$tickets = array();
$tickets_modify = array();
$query = $smcFunc['db_query']('', '
SELECT id_ticket, num_replies, deleted_replies, withdeleted
FROM {db_prefix}helpdesk_tickets
ORDER BY id_ticket ASC
LIMIT {int:start}, {int:limit}',
array(
'start' => $_REQUEST['start'],
'limit' => $step_size,
)
);
while ($row = $smcFunc['db_fetch_assoc']($query))
$tickets[$row['id_ticket']] = $row;
$smcFunc['db_free_result']($query);
if (!empty($tickets))
{
// Firstly, let's check the numbers of replies and deleted replies, see if they're right.
$query = $smcFunc['db_query']('', '
SELECT id_ticket, message_status, COUNT(*) AS count
FROM {db_prefix}helpdesk_ticket_replies
WHERE id_ticket IN ({array_int:tickets})
GROUP BY id_ticket, message_status',
array(
'tickets' => array_keys($tickets),
)
);
$ticket_cache = array();
while ($row = $smcFunc['db_fetch_assoc']($query))
{
if (!isset($ticket_cache[$row['id_ticket']]))
$ticket_cache[$row['id_ticket']] = array(
'num_replies' => 0,
'deleted_replies' => 0,
'withdeleted' => 0,
);
if ($row['message_status'] == MSG_STATUS_NORMAL)
$ticket_cache[$row['id_ticket']]['num_replies'] = $row['count'] - 1; // since we never want to count the first message (which can't ever be deleted on its own)
elseif ($row['message_status'] == MSG_STATUS_DELETED)
{
$ticket_cache[$row['id_ticket']]['deleted_replies'] = $row['count'];
$ticket_cache[$row['id_ticket']]['withdeleted'] = 1;
}
}
$smcFunc['db_free_result']($query);
// OK so we now have the ticket counts for normal/deleted posts. Are they right?
foreach ($ticket_cache as $id_ticket => $ticket_details)
if ($ticket_cache[$id_ticket]['num_replies'] != $tickets[$id_ticket]['num_replies'] || $ticket_cache[$id_ticket]['deleted_replies'] != $tickets[$id_ticket]['deleted_replies'] || $ticket_cache[$id_ticket]['withdeleted'] != $tickets[$id_ticket]['withdeleted'])
$tickets_modify[$id_ticket] = $ticket_cache[$id_ticket];
// Any to update?
if (!empty($tickets_modify))
{
// Oh crap.
foreach ($tickets_modify as $id_ticket => $columns)
$smcFunc['db_query']('', '
UPDATE {db_prefix}helpdesk_tickets
SET num_replies = {int:num_replies},
deleted_replies = {int:deleted_replies},
withdeleted = {int:withdeleted}
WHERE id_ticket = {int:id_ticket}',
array(
'id_ticket' => $id_ticket,
'num_replies' => $columns['num_replies'],
'deleted_replies' => $columns['deleted_replies'],
'withdeleted' => $columns['withdeleted'],
)
);
$_SESSION['shd_maint']['deleted'] = count($tickets_modify);
}
}
// Another round?
$_REQUEST['start'] += $step_size;
// All done
if ($_REQUEST['start'] > $ticket_count)
$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . ($context['step'] + 1) . '">';
else
{
// More to do, call back - and provide the subtitle
$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . $context['step'] . '">
<input type="hidden" name="start" value="' . $_REQUEST['start'] . '">';
$context['substep_enabled'] = true;
$context['substep_title'] = $txt['shd_admin_maint_findrepair_status'];
$context['substep_continue_percent'] = round(100 * $_REQUEST['start'] / $ticket_count);
}
}
// Make sure the first and last messages on a ticket are correct.
function shd_maint_first_last()
{
global $context, $smcFunc, $txt;
// First we need the number of tickets
$query = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}helpdesk_tickets');
list($ticket_count) = $smcFunc['db_fetch_row']($query);
$smcFunc['db_free_result']($query);
$_REQUEST['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
$step_size = 150;
$tickets = array();
$tickets_modify = array();
$query = $smcFunc['db_query']('', '
SELECT id_ticket, id_first_msg, id_last_msg
FROM {db_prefix}helpdesk_tickets
ORDER BY id_ticket ASC
LIMIT {int:start}, {int:limit}',
array(
'start' => $_REQUEST['start'],
'limit' => $step_size,
)
);
while ($row = $smcFunc['db_fetch_assoc']($query))
$tickets[$row['id_ticket']] = $row;
$smcFunc['db_free_result']($query);
if (!empty($tickets))
{
// Firstly, let's get the first/last messages from the messages table.
$query = $smcFunc['db_query']('', '
SELECT id_ticket, MIN(id_msg) AS id_first_msg, MAX(id_msg) AS id_last_msg
FROM {db_prefix}helpdesk_ticket_replies
WHERE id_ticket IN ({array_int:tickets})
AND message_status = ({int:normal})
GROUP BY id_ticket',
array(
'tickets' => array_keys($tickets),
'normal' => MSG_STATUS_NORMAL,
)
);
$ticket_cache = array();
while ($row = $smcFunc['db_fetch_assoc']($query))
$ticket_cache[$row['id_ticket']] = $row;
$smcFunc['db_free_result']($query);
// OK so we now have the message ids for first/last message. Are they right?
foreach ($ticket_cache as $id_ticket => $ticket_details)
if ($ticket_cache[$id_ticket]['id_first_msg'] != $tickets[$id_ticket]['id_first_msg'] || $ticket_cache[$id_ticket]['id_last_msg'] != $tickets[$id_ticket]['id_last_msg'])
$tickets_modify[$id_ticket] = $ticket_cache[$id_ticket];
// Any to update?
if (!empty($tickets_modify))
{
// Oh crap.
foreach ($tickets_modify as $id_ticket => $columns)
$smcFunc['db_query']('', '
UPDATE {db_prefix}helpdesk_tickets
SET id_first_msg = {int:id_first_msg},
id_last_msg = {int:id_last_msg}
WHERE id_ticket = {int:id_ticket}',
$columns
);
$_SESSION['shd_maint']['first_last'] = count($tickets_modify);
}
}
// Another round?
$_REQUEST['start'] += $step_size;
// All done
if ($_REQUEST['start'] > $ticket_count)
$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . ($context['step'] + 1) . '">';
else
{
// More to do, call back - and provide the subtitle
$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . $context['step'] . '">
<input type="hidden" name="start" value="' . $_REQUEST['start'] . '">';
$context['substep_enabled'] = true;
$context['substep_title'] = $txt['shd_admin_maint_findrepair_firstlast'];
$context['substep_continue_percent'] = round(100 * $_REQUEST['start'] / $ticket_count);
}
}
// Make sure the first and last posters on a ticket are correct.
function shd_maint_starter_updater()
{
global $context, $smcFunc, $txt;
// First we need the number of tickets
$query = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}helpdesk_tickets');
list($ticket_count) = $smcFunc['db_fetch_row']($query);
$smcFunc['db_free_result']($query);
$_REQUEST['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
$step_size = 150;
$tickets = array();
$tickets_modify = array();
$query = $smcFunc['db_query']('', '
SELECT id_ticket, id_member_started, id_member_updated
FROM {db_prefix}helpdesk_tickets
ORDER BY id_ticket ASC
LIMIT {int:start}, {int:limit}',
array(
'start' => $_REQUEST['start'],
'limit' => $step_size,
)
);
while ($row = $smcFunc['db_fetch_assoc']($query))
$tickets[$row['id_ticket']] = $row;
$smcFunc['db_free_result']($query);
if (!empty($tickets))
{
// Firstly, let's get the first/last messages from the messages table.
$query = $smcFunc['db_query']('', '
SELECT hdt.id_ticket, hdtr_first.id_member AS id_member_started, hdtr_last.id_member AS id_member_updated
FROM {db_prefix}helpdesk_tickets AS hdt
LEFT JOIN {db_prefix}helpdesk_ticket_replies AS hdtr_first ON (hdt.id_first_msg = hdtr_first.id_msg)
LEFT JOIN {db_prefix}helpdesk_ticket_replies AS hdtr_last ON (hdt.id_last_msg = hdtr_last.id_msg)
WHERE hdt.id_ticket IN ({array_int:tickets})
GROUP BY 1, 2, 3',
array(
'tickets' => array_keys($tickets),
)
);
$ticket_cache = array();
while ($row = $smcFunc['db_fetch_assoc']($query))
$ticket_cache[$row['id_ticket']] = $row;
$smcFunc['db_free_result']($query);
// OK so we now have the message ids for first/last message. Are they right?
foreach ($ticket_cache as $id_ticket => $ticket_details)
if ($ticket_cache[$id_ticket]['id_member_started'] != $tickets[$id_ticket]['id_member_started'] || $ticket_cache[$id_ticket]['id_member_updated'] != $tickets[$id_ticket]['id_member_updated'])
$tickets_modify[$id_ticket] = $ticket_cache[$id_ticket];
// Any to update?
if (!empty($tickets_modify))
{
// Oh crap.
foreach ($tickets_modify as $id_ticket => $columns)
$smcFunc['db_query']('', '
UPDATE {db_prefix}helpdesk_tickets
SET id_member_started = {int:id_member_started},
id_member_updated = {int:id_member_updated}
WHERE id_ticket = {int:id_ticket}',
$columns
);
$_SESSION['shd_maint']['starter_updater'] = count($tickets_modify);
}
}
// Another round?
$_REQUEST['start'] += $step_size;
// All done
if ($_REQUEST['start'] > $ticket_count)
$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . ($context['step'] + 1) . '">';
else
{
// More to do, call back - and provide the subtitle
$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . $context['step'] . '">
<input type="hidden" name="start" value="' . $_REQUEST['start'] . '">';
$context['substep_enabled'] = true;
$context['substep_title'] = $txt['shd_admin_maint_findrepair_starterupdater'];
$context['substep_continue_percent'] = round(100 * $_REQUEST['start'] / $ticket_count);
}
}
// Make sure all open tickets have the right statuses.
function shd_maint_status()
{
global $context, $smcFunc, $txt;
$open = array(TICKET_STATUS_NEW, TICKET_STATUS_PENDING_STAFF, TICKET_STATUS_PENDING_USER);
// First we need the number of tickets
$query = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}helpdesk_tickets
WHERE status IN ({array_int:open})',
array(
'open' => $open,
)
);
list($ticket_count) = $smcFunc['db_fetch_row']($query);
$smcFunc['db_free_result']($query);
$_REQUEST['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
$step_size = 100;
$tickets = array();
$tickets_modify = array();
$query = $smcFunc['db_query']('', '
SELECT id_ticket, num_replies, id_member_started, id_member_updated, status, id_dept
FROM {db_prefix}helpdesk_tickets
WHERE status IN ({array_int:open})
ORDER BY id_ticket ASC
LIMIT {int:start}, {int:limit}',
array(
'open' => $open,
'start' => $_REQUEST['start'],
'limit' => $step_size,
)
);
while ($row = $smcFunc['db_fetch_assoc']($query))
$tickets[$row['id_ticket']] = $row;
$smcFunc['db_free_result']($query);
if (!empty($tickets))
{
foreach ($tickets as $ticket)
{
$new_status = shd_determine_status('reply', $ticket['id_member_started'], $ticket['id_member_updated'], $ticket['num_replies'], $ticket['id_dept']);
if ($ticket['status'] != $new_status)
$tickets_modify[$ticket['id_ticket']] = $new_status;
}
// Any to update?
if (!empty($tickets_modify))
{
// Oh crap.
foreach ($tickets_modify as $id_ticket => $status)
$smcFunc['db_query']('', '
UPDATE {db_prefix}helpdesk_tickets
SET status = {int:status}
WHERE id_ticket = {int:id_ticket}',
array(
'id_ticket' => $id_ticket,
'status' => $status,
)
);
$_SESSION['shd_maint']['status'] = count($tickets_modify);
}
}
// Another round?
$_REQUEST['start'] += $step_size;
// All done
if ($_REQUEST['start'] > $ticket_count)
$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . ($context['step'] + 1) . '">';
else
{
// More to do, call back - and provide the subtitle
$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . $context['step'] . '">
<input type="hidden" name="start" value="' . $_REQUEST['start'] . '">';
$context['substep_enabled'] = true;
$context['substep_title'] = $txt['shd_admin_maint_findrepair_firstlast'];
$context['substep_continue_percent'] = round(100 * $_REQUEST['start'] / $ticket_count);
}
}
// Make sure all tickets are in a valid department, creating a new one if necessary.
function shd_maint_invalid_dept()
{
global $context, $smcFunc, $txt;
$tickets = array();
$query = $smcFunc['db_query']('', '
SELECT hdt.id_ticket
FROM {db_prefix}helpdesk_tickets AS hdt
LEFT JOIN {db_prefix}helpdesk_depts AS hdd ON (hdt.id_dept = hdd.id_dept)
WHERE hdd.id_dept IS NULL');
while ($row = $smcFunc['db_fetch_assoc']($query))
$tickets[] = $row['id_ticket'];
$smcFunc['db_free_result']($query);
if (!empty($tickets))
{
// Uh-oh. OK, so let's make a new department.
// First, we get the last dept_order.
$query = $smcFunc['db_query']('', '
SELECT MAX(dept_order)
FROM {db_prefix}helpdesk_depts');
list($dept_order) = $smcFunc['db_fetch_row']($query);
$smcFunc['db_free_result']($query);
$dept_order++;
$last_dept = $smcFunc['db_insert']('replace',
'{db_prefix}helpdesk_depts',
array('dept_name' => 'string', 'description' => 'string', 'board_cat' => 'int', 'before_after' => 'int', 'dept_order' => 'int',),
array($txt['shd_admin_recovered_dept'], $txt['shd_admin_recovered_dept_desc'], 0, 0, $dept_order,),
array('id_dept'),
1
);
$smcFunc['db_query']('', '
UPDATE {db_prefix}helpdesk_tickets
SET id_dept = {int:new_dept}
WHERE id_ticket IN ({array_int:tickets})',
array(
'new_dept' => $last_dept,
'tickets' => $tickets,
)
);
$_SESSION['shd_maint']['invalid_dept'] = count($tickets);
}
// This is a simple operation, no suboperation, so just tell it to go onto the next step.
$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . ($context['step'] + 1) . '">';
}
// Make sure all SimpleDesk cache items are forcibly flushed.
function shd_maint_clean_cache()
{
global $context;
clean_cache();
// Log this.
shd_admin_log('admin_maint', array(
'action' => 'clean_cache',
));
// Normally, we'd update $context['continue_post_data'] to indicate our next port of call. But here, we don't have to.
redirectexit('action=admin;area=helpdesk_maint;sa=findrepair;done;' . $context['session_var'] . '=' . $context['session_id']);
}
function shd_admin_maint_search()
{
global $context, $txt, $modSettings, $sourcedir, $smcFunc;
$context['sub_template'] = 'shd_admin_maint_search';
$context['page_title'] = $txt['shd_admin_maint'];
checkSession('request');
// Reset the defaults if they're not set.
if (empty($modSettings['shd_search_charset']))
$modSettings['shd_search_charset'] = '0..9, A..Z, a..z, &, ~';
$modSettings['shd_search_min_size'] = !empty($modSettings['shd_search_min_size']) ? $modSettings['shd_search_min_size'] : 3;
$modSettings['shd_search_max_size'] = !empty($modSettings['shd_search_max_size']) ? $modSettings['shd_search_max_size'] : 8;
$modSettings['shd_search_prefix_size'] = !empty($modSettings['shd_search_prefix_size']) ? $modSettings['shd_search_prefix_size'] : 0;
// Are we doing some fancy work?
if (isset($_REQUEST['rebuild']))
{
require_once($sourcedir . '/sd_source/Subs-SimpleDeskSearch.php');
// How many tickets are there?
$query = $smcFunc['db_query']('', '
SELECT COUNT(id_ticket)
FROM {db_prefix}helpdesk_tickets');
list($total) = $smcFunc['db_fetch_row']($query);
// Where are we starting?
$start = isset($_POST['start']) ? (int) $_POST['start'] : 0;
// Get the ids we need to do.
$per_inst = 10;
$tickets = array();
$query = $smcFunc['db_query']('', '
SELECT id_ticket, subject
FROM {db_prefix}helpdesk_tickets
ORDER BY id_ticket ASC
LIMIT {int:start}, {int:limit}',
array(
'start' => $start,
'limit' => $per_inst,
)
);
while ($row = $smcFunc['db_fetch_assoc']($query))
$tickets[$row['id_ticket']] = $row['subject'];
$smcFunc['db_free_result']($query);
// Nothing to do?
if ($start >= $total || empty($tickets))
{
// Make sure we flag the index as built, then leave.