-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathticket.class.php
More file actions
1320 lines (1140 loc) · 49.5 KB
/
ticket.class.php
File metadata and controls
1320 lines (1140 loc) · 49.5 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
/**
* -------------------------------------------------------------------------
* Escalade plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Escalade.
*
* Escalade is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Escalade is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Escalade. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2015-2023 by Escalade plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/escalade
* -------------------------------------------------------------------------
*/
use Glpi\Application\View\TemplateRenderer;
use Glpi\DBAL\QuerySubQuery;
use GlpiPlugin\Behaviors\Config;
if (!defined('GLPI_ROOT')) {
throw new Exception("Sorry. You can't access directly to this file");
}
class PluginEscaladeTicket
{
public const MANAGED_BY_CORE = -1; // Status managed by core, not by plugin
public static function pre_item_update(CommonDBTM $item)
{
// Only process escalation logic if we're dealing with actor assignments
// Don't interfere with other updates like solutions, status changes, etc.
if (!isset($item->input['_actors']) && !isset($item->input['_itil_assign']) && !isset($item->input['actortype'])) {
return $item;
}
// Special case: If we have _itil_assign without actortype, we need to distinguish between:
// 1. "Associate myself" button - many fields merged from ticket form
// 2. History button escalation (climb_group) - specific structure with groups_id and _type = 'group'
// 3. Other _itil_assign operations
if (isset($item->input['_itil_assign']) && !isset($item->input['actortype']) && !isset($item->input['_actors'])) {
$itil_assign = $item->input['_itil_assign'];
// Check if it's a history button escalation from climb_group
if (isset($itil_assign['groups_id']) && isset($itil_assign['_type']) && $itil_assign['_type'] === 'group' && count($item->input) <= 3) {
// This is a history button escalation - add required fields for template validation
// but don't interfere with the escalation logic
$item->input['_no_escalade_template_validation'] = true;
}
// Check if it's "Associate myself" (many fields merged)
elseif (count($item->input) > 10) {
// Associate myself - don't interfere
return $item;
}
}
$input = $item->input;
if ($item instanceof CommonITILObject) {
// Special handling for history button escalation to pass template validation
if (isset($item->input['_no_escalade_template_validation'])) {
// Add existing ticket fields temporarily for template validation
$temp_input = array_merge($item->fields, $item->input);
// Ensure required fields are not empty for template validation
if (empty($temp_input['content'])) {
$temp_input['content'] = 'Auto-generated content for escalation';
}
// Add existing actors to pass mandatory field validation
$ticket_user = new Ticket_User();
$ticket_group = new Group_Ticket();
// Get existing requesters
$requesters = $ticket_user->find([
'tickets_id' => $item->getID(),
'type' => CommonITILActor::REQUESTER,
]);
if (!empty($requesters)) {
$temp_input['_users_id_requester'] = array_column($requesters, 'users_id');
}
// Get existing requester groups
$requester_groups = $ticket_group->find([
'tickets_id' => $item->getID(),
'type' => CommonITILActor::REQUESTER,
]);
if (!empty($requester_groups)) {
$temp_input['_groups_id_requester'] = array_column($requester_groups, 'groups_id');
}
$temp_input['_disablenotif'] = true; // Disable notifications for this validation
$item->input['_skip_rules'] = true;
$input = $item->prepareInputForUpdate($temp_input);
$item->input['_skip_rules'] = false;
unset($item->input['_no_escalade_template_validation']); // Clean up flag
} else {
$item->input['_skip_rules'] = true;
$input = $item->prepareInputForUpdate($item->input);
$item->input['_skip_rules'] = false;
}
if (!$input) {
return false;
}
}
$item->input = array_merge($input, $item->input);
$old_groups = [];
$old_users = [];
// Get actual actors for the ticket
if ($item instanceof Ticket) {
$actorTypes = [CommonITILActor::REQUESTER, CommonITILActor::OBSERVER, CommonITILActor::ASSIGN];
$ticket_actors = array_reduce(
$actorTypes,
function ($carry, $type) use ($item) {
$carry[$item->getActorFieldNameType($type)] = $item->getActorsForType($type);
return $carry;
},
[],
);
$old_groups = array_filter($ticket_actors['assign'], fn($actor) => isset($actor['itemtype']) && $actor['itemtype'] === 'Group');
$old_users = array_filter($ticket_actors['assign'], fn($actor) => isset($actor['itemtype'])
&& $actor['itemtype'] === 'User'
&& (
!isset($item->input['_itil_assign'])
|| (isset($actor['items_id']) && $item->input['_itil_assign']['users_id'] != $actor['items_id'])
));
}
if (!isset($item->input['actortype'])) {
$groups = new Group_Ticket();
$groups = $groups->find(['tickets_id' => $item->getID(), 'type' => CommonITILActor::ASSIGN]);
if (!empty($item->input['_actors'])) {
foreach ($item->input['_actors']['assign'] as $actors) {
if ($actors['itemtype'] == 'Group' && !in_array($actors['items_id'], $groups)) {
$item->input['groups_id'] = $actors['items_id'];
$item->input['actortype'] = CommonITILActor::ASSIGN;
}
}
}
}
if (
isset($item->input['_actors']['assign']) || isset($item->input['_itil_assign'])
) {
$new_groups = $old_groups;
if (isset($item->input['_actors']['assign'])) {
$new_groups = array_filter($item->input['_actors']['assign'], fn($actor) => isset($actor['itemtype']) && $actor['itemtype'] === 'Group');
}
$new_users = $old_users;
if (isset($item->input['_actors']['assign'])) {
$new_users = array_filter($item->input['_actors']['assign'], fn($actor) => isset($actor['itemtype']) && $actor['itemtype'] === 'User');
} elseif (isset($item->input['_itil_assign']['_type']) && $item->input['_itil_assign']['_type'] === 'user') {
$new_users[] = [
'items_id' => $item->input['_itil_assign']['users_id'],
'itemtype' => 'User',
'use_notification' => $item->input['_itil_assign']['use_notification'] ?? false,
];
}
if (
(isset($item->input['actortype']) && $item->input['actortype'] == CommonITILActor::ASSIGN)
&& (
count($old_groups) < count($new_groups)
)
) {
//disable notification to prevent notification for old AND new group
$item->input['_disablenotif'] = true;
if ($_SESSION['glpi_plugins']['escalade']['config']['ticket_last_status'] != self::MANAGED_BY_CORE) {
$item->input['_do_not_compute_status'] = true;
$item->input['status'] = $_SESSION['glpi_plugins']['escalade']['config']['ticket_last_status'];
}
self::removeAssignUsers($item);
} elseif (count($old_groups) === count($new_groups)) {
$old_group_ids = [];
foreach ($old_groups as $old_group) {
$old_group_ids[$old_group['items_id']] = true;
}
foreach ($new_groups as $new_group) {
if (!isset($old_group_ids[$new_group['items_id']])) {
$item->input['_disablenotif'] = true;
if ($_SESSION['glpi_plugins']['escalade']['config']['ticket_last_status'] != self::MANAGED_BY_CORE) {
$item->input['_do_not_compute_status'] = true;
$item->input['status'] = $_SESSION['glpi_plugins']['escalade']['config']['ticket_last_status'];
}
}
}
}
return $item;
}
return null;
}
/**
* Provide a redirection to other functions
* @return void
*/
public static function item_update(CommonDBTM $item)
{
if ($_SESSION['glpi_plugins']['escalade']['config']['remove_group']) {
//solve ticket
if (isset($item->input['status']) && $item->input['status'] == CommonITILObject::SOLVED) {
self::AssignFirstGroupOnSolve($item);
//extend solve linked ticket to status change (when no solution provided)
if (
!in_array("solutiontypes_id", $item->updates)
&& !in_array("solution", $item->updates)
) {
self::linkedTickets($item, CommonITILObject::SOLVED);
}
}
//close ticket
if (isset($item->input['status']) && $item->input['status'] == CommonITILObject::CLOSED) {
//close linked tickets
self::linkedTickets($item, CommonITILObject::CLOSED);
} elseif (
isset($item->input['status'])
&& ($item->input['status'] == CommonITILObject::ASSIGNED || $item->input['status'] == CommonITILObject::INCOMING)
&& isset($item->oldvalues['status'])
&& $item->oldvalues['status'] == CommonITILObject::SOLVED
) {
//solution rejected
self::AssignLastGroupOnRejectedSolution($item);
// reopen linked tickets
self::linkedTickets($item, $item->input['status']);
}
}
//ticket qualification on cat change
// @phpstan-ignore-next-line - Offset 'itilcategories_id' on list<string> in isset() does not exist.
if (isset($item->updates['itilcategories_id']) || in_array('itilcategories_id', $item->updates)) {
self::qualification($item);
}
// notification on solve date modification
if (in_array('solvedate', $item->updates)) {
NotificationEvent::raiseEvent('update_solvedate', $item);
}
}
/**
* When a ticket is solved, if group histories exists, assign the first group on the ticket
* @param CommonDBTM $item the ticket object
*/
public static function AssignFirstGroupOnSolve(CommonDBTM $item)
{
if (
$_SESSION['glpi_plugins']['escalade']['config']['remove_group']
&& $_SESSION['glpi_plugins']['escalade']['config']['solve_return_group']
) {
$tickets_id = $item->fields['id'];
$first_history = PluginEscaladeHistory::getFirstLineForTicket($tickets_id);
$last_history = PluginEscaladeHistory::getLastLineForTicket($tickets_id);
//if no history
if ($first_history === false) {
return;
}
//if first history group == last history group
if ($first_history['id'] == $last_history['id']) {
return;
}
self::removeAssignGroups($tickets_id, $first_history['groups_id']);
self::removeAssignUsers($item);
//set session var to prevent double task message
$_SESSION['plugin_escalade']['solution'] = true;
//add a task to inform the escalation
if ($_SESSION['glpi_plugins']['escalade']['config']['task_history']) {
$group = new Group();
$group->getFromDB($first_history['groups_id']);
PluginEscaladeTaskmanager::setTicketTask([
'tickets_id' => $tickets_id,
'is_private' => false, //It allows the end user to see when their ticket is escalated to another areas
'_no_reopen' => true, //prevent reopening ticket
'state' => Planning::INFO,
'content' => __s("Solution provided, back to the group", "escalade") . " "
. $group->getName(),
]);
}
//add the first history group (if not already exist)
$group_ticket = new Group_Ticket();
$condition = [
'tickets_id' => $tickets_id,
'groups_id' => $first_history['groups_id'],
'type' => CommonITILActor::ASSIGN,
];
if (!$group_ticket->find($condition)) {
$group_ticket->add($condition);
}
}
}
/**
* When a ticket solution is rejected, if group histories exists,
* assign the last group on the ticket
* @param CommonDBTM $item the ticket object
*/
public static function AssignLastGroupOnRejectedSolution(CommonDBTM $item)
{
if (!isset($_POST['add_reopen'])) {
return;
}
if (
$_SESSION['glpi_plugins']['escalade']['config']['remove_group']
&& $_SESSION['glpi_plugins']['escalade']['config']['solve_return_group']
) {
$tickets_id = $item->fields['id'];
$last_history = PluginEscaladeHistory::getLastLineForTicket($tickets_id);
$full_history = PluginEscaladeHistory::getFullHistory($tickets_id);
if (count($full_history) <= 1) {
return; //no escalation found, return
}
array_shift($full_history); //remove current group in history
$rejected_history = array_shift($full_history); // get previous group
//if no history
if ($last_history === false) {
return;
}
//if first history group == last history group
if ($rejected_history['id'] == $last_history['id']) {
return;
}
self::removeAssignGroups($tickets_id, $rejected_history['groups_id']);
//set session var to prevent double task message
$_SESSION['plugin_escalade']['solution'] = true;
//add a task to inform the escalation
if ($_SESSION['glpi_plugins']['escalade']['config']['task_history']) {
$group = new Group();
$group->getFromDB($rejected_history['groups_id']);
PluginEscaladeTaskmanager::setTicketTask([
'tickets_id' => $tickets_id,
'is_private' => false,
'state' => Planning::INFO,
'content' => __s("Solution rejected, return to the group", "escalade") . " "
. $group->getName(),
]);
}
//add the first history group
$group_ticket = new Group_Ticket();
$group_ticket->add([
'tickets_id' => $tickets_id,
'groups_id' => $rejected_history['groups_id'],
'type' => CommonITILActor::ASSIGN,
]);
//update status
if ($_SESSION['glpi_plugins']['escalade']['config']['ticket_last_status'] != self::MANAGED_BY_CORE) {
$item->update([
'id' => $tickets_id,
'status' => $_SESSION['glpi_plugins']['escalade']['config']['ticket_last_status'],
]);
}
}
}
/**
* remove old groups to a ticket when a new group assigned
* called by "pre_item_add" hook on Group_Ticket object
* @param CommonDBTM $item the ticket object
*/
public static function addHistoryOnAddGroup(CommonDBTM $item)
{
/** @var DBmysql $DB */
global $DB;
if ($_SESSION['glpi_plugins']['escalade']['config']['remove_group'] == false) {
return true;
}
//if group sent is not an assign group, return
if ($item->input['actortype'] != CommonITILActor::ASSIGN) {
return null;
}
if ($item instanceof Ticket) {
$tickets_id = $item->input['id'];
} elseif (isset($item->input['tickets_id'])) {
$tickets_id = $item->input['tickets_id'];
} else {
return false;
}
$groups_id = $item->input['groups_id'];
$item->fields['status'] = CommonITILObject::ASSIGNED;
//add line in history table
$history = new PluginEscaladeHistory();
$group_ticket = new Group_Ticket();
$group_ticket->getFromDBByRequest([
'ORDER' => 'id DESC',
'LIMIT' => 1,
'tickets_id' => $tickets_id,
'type' => 2,
]);
$previous_groups_id = 0;
$counter = 0;
if (count($group_ticket->fields) > 0) {
$previous_groups_id = $group_ticket->fields['groups_id'];
$last_history_groups = PluginEscaladeHistory::getLastHistoryForTicketAndGroup($tickets_id, $groups_id, $previous_groups_id);
if (count($last_history_groups->fields) > 0) {
$counter = $last_history_groups->fields['counter'] + 1;
}
}
$history->add([
'tickets_id' => $tickets_id,
'groups_id' => $groups_id,
'groups_id_previous' => $previous_groups_id,
'counter' => $counter,
]);
// check if group assignment is made during ticket creation
// in this case, skip following steps as it cannot be considered as a group escalation
$backtraces = debug_backtrace();
foreach ($backtraces as $backtrace) {
if (
$backtrace['function'] == "add"
&& ($backtrace['object'] instanceof CommonITILObject)
) {
return null;
}
}
//add a task to inform the escalation (pass if solution)
if (isset($_SESSION['plugin_escalade']['solution'])) {
unset($_SESSION['plugin_escalade']['solution']);
return $item;
}
if ($_SESSION['glpi_plugins']['escalade']['config']['ticket_last_status'] != self::MANAGED_BY_CORE) {
$ticket = new Ticket();
$ticket->update([
'id' => $tickets_id,
'status' => $_SESSION['glpi_plugins']['escalade']['config']['ticket_last_status'],
]);
}
return $item;
}
public static function processAfterAddGroup(Group_Ticket $item)
{
$tickets_id = $item->fields['tickets_id'];
$groups_id = $item->fields['groups_id'];
//remove old groups (keep last assigned)
if ($_SESSION['glpi_plugins']['escalade']['config']['remove_group'] == true) {
self::removeAssignGroups($tickets_id, $groups_id);
}
// The config is checked in the function.
self::removeAssignUsers($item);
if ($_SESSION['glpi_plugins']['escalade']['config']['ticket_last_status'] != self::MANAGED_BY_CORE) {
$ticket = new Ticket();
$ticket->update([
'id' => $tickets_id,
'status' => $_SESSION['glpi_plugins']['escalade']['config']['ticket_last_status'],
]);
}
if ($_SESSION['glpi_plugins']['escalade']['config']['show_history'] == true) {
$item->input['actortype'] = $item->fields['type'];
PluginEscaladeTicket::addHistoryOnAddGroup($item);
}
$comment = $_POST['comment'] ?? '';
$group = new Group();
$group->getFromDB($groups_id);
$ticket = new Ticket();
$ticket->getFromDB($tickets_id);
//default task content
$task_content = '<p><i>' . sprintf(__s('Escalation to the group %s.', 'escalade'), $group->getName()) . '</i></p><hr />' . $comment;
PluginEscaladeTaskmanager::setTicketTask([
'tickets_id' => $tickets_id,
'is_private' => false,
'state' => Planning::INFO,
'content' => $task_content,
]);
PluginEscaladeTaskmanager::addTicketTaskInTimeline();
}
/**
* @return bool
*/
public static function assignUserGroup(Ticket $ticket)
{
if (!count($ticket->input)) {
// Already cancel by another plugin
return false;
}
//check plugin behaviors (for avoid conflict)
if (Plugin::isPluginActive('behaviors') && class_exists('GlpiPlugin\Behaviors\Config')) {
$behavior_config = Config::getInstance();
if ($behavior_config->getField('use_assign_user_group') != 0) {
return false;
}
}
//check this plugin config
if (
$_SESSION['glpi_plugins']['escalade']['config']['use_assign_user_group'] == 0
|| $_SESSION['glpi_plugins']['escalade']['config']['use_assign_user_group_creation'] == 0
) {
return false;
}
if (
isset($ticket->input['_users_id_assign'])
&& $ticket->input['_users_id_assign'] > 0
&& (!isset($ticket->input['_groups_id_assign'])
|| empty($ticket->input['_groups_id_assign']))
) {
if ($_SESSION['glpi_plugins']['escalade']['config']['use_assign_user_group'] == 1) {
// First group
$ticket->input['_groups_id_assign']
= PluginEscaladeUser::getTechnicianGroup(
$ticket->input['entities_id'],
current($ticket->input['_users_id_assign']),
true,
);
} else {
// All groups
$ticket->input['_groups_id_assign']
= PluginEscaladeUser::getTechnicianGroup(
$ticket->input['entities_id'],
current($ticket->input['_users_id_assign']),
false,
);
}
//prevent adding empty group
if (empty($ticket->input['_groups_id_assign'])) {
unset($ticket->input['_groups_id_assign']);
}
}
return true;
}
/**
* assign a previous group to the ticket
* @param int $tickets_id the ticket to change
* @param int $groups_id the group to assign
* @param bool $no_redirect if true, no redirection after the action
* @return void
*/
public static function climb_group($tickets_id, $groups_id, $no_redirect = false)
{
//don't add group if already exist for this ticket
$group = new Group();
$group->getFromDB($groups_id);
$group_ticket = new Group_Ticket();
$condition = [
'tickets_id' => $tickets_id,
'groups_id' => $groups_id,
'type' => CommonITILActor::ASSIGN,
];
if (!$group_ticket->find($condition)) {
$ticket_group = new Group_Ticket();
PluginEscaladeTaskmanager::setTicketTask([
'tickets_id' => $tickets_id,
'is_private' => false,
'state' => Planning::INFO,
'content' => '<p><i>' . sprintf(
__s('Escalation to the group %s.', 'escalade'),
$group->getName() . '</i></p><hr />',
),
]);
$ticket = new Ticket();
$ticket->update([
'id' => $tickets_id,
'_actors' => [
'assign' => [
[
'items_id' => $groups_id,
'itemtype' => 'Group',
],
],
],
]);
}
if (!$no_redirect) {
Html::back();
}
}
/**
* Clean all assigned groups for the ticket
* @param int $tickets_id
* @return void
*/
public static function removeAssignGroups($tickets_id, $keep_groups_id = false)
{
$where_keep = [
'tickets_id' => $tickets_id,
'type' => CommonITILActor::ASSIGN,
];
if ($keep_groups_id !== false) {
$where_keep[] = ['NOT' => ['groups_id' => $keep_groups_id]];
}
$group_ticket = new Group_Ticket();
$found = $group_ticket->find($where_keep);
foreach ($found as $gt) {
$group_ticket->delete($gt);
}
//add a var to prevent status changes unwanted
$_SESSION['plugin_escalade']['remove_assign'] = true;
}
/**
* Clean all assigned users for the ticket
* @param CommonDBTM $item the ticket object
* @return void
*/
public static function removeAssignUsers($item, $keep_users_id = false, $type = CommonITILActor::ASSIGN)
{
if (
$_SESSION['glpi_plugins']['escalade']['config']['remove_tech'] == false
&& $_SESSION['glpi_plugins']['escalade']['config']['remove_requester'] == false
) {
return;
}
if (
isset($_SESSION['plugin_escalade']['ticket_creation'])
&& $_SESSION['plugin_escalade']['ticket_creation']
) {
return;
}
if (
$_SESSION['glpi_plugins']['escalade']['config']['use_assign_user_group'] != 0
&& $_SESSION['glpi_plugins']['escalade']['config']['use_assign_user_group_creation'] != 0
&& isset($_SESSION['plugin_escalade']['ticket_creation'])
&& $_SESSION['plugin_escalade']['ticket_creation']
) {
return;
}
if ($type == CommonITILActor::ASSIGN && !$_SESSION['glpi_plugins']['escalade']['config']['remove_tech']) {
return;
}
if ($type == CommonITILActor::REQUESTER && !$_SESSION['glpi_plugins']['escalade']['config']['remove_requester']) {
return;
}
$tickets_id = $item->input['id'] ?? $item->fields['id'];
if ($item instanceof Group_Ticket) {
$tickets_id = $item->input['tickets_id'] ?? $item->fields['tickets_id'];
}
$where_keep = [
'tickets_id' => $tickets_id,
'type' => $type,
];
if ($keep_users_id !== false) {
$where_keep[] = ['NOT' => ['users_id' => $keep_users_id]];
}
$types = [
CommonITILActor::ASSIGN => 'assign',
CommonITILActor::REQUESTER => 'requester',
];
$ticket_user = new Ticket_User();
$found = $ticket_user->find($where_keep);
foreach ($found as $id => $tu) {
//if user must be keeped (see item_add_user function)
if (
isset($_SESSION['plugin_escalade']['keep_users'])
&& is_array($_SESSION['plugin_escalade']['keep_users'])
&& in_array($tu['users_id'], $_SESSION['plugin_escalade']['keep_users'])
) {
continue;
}
//delete user
$ticket_user->delete(['id' => $id]);
if (isset($item->input['_actors'])) {
foreach ($item->input['_actors'][$types[$type]] as $key => $actor) {
if (
$actor['items_id'] == $tu['users_id']
&& $actor['itemtype'] == User::class
) {
unset($item->input['_actors'][$types[$type]][$key]);
}
}
foreach ($item->input['_users_id_assign'] as $key => $actor) {
if ($actor == $tu['users_id']) {
unset($item->input['_users_id_assign'][$key]);
}
}
}
}
//clean session var (to prevent users be keeped post to this ticket update)
unset($_SESSION['plugin_escalade']['keep_users']);
//add a var to prevent status changes unwanted
$_SESSION['plugin_escalade']['remove_assign'] = true;
}
/**
* Update ticket status when user added.
* Trigger also adding user groups if feature enabled
* @param Ticket_User $item Ticket_User object
* @return bool
*/
public static function item_add_user(Ticket_User $item, $type = CommonITILActor::ASSIGN)
{
$tickets_id = $item->input['tickets_id'];
$ticket = new Ticket();
$ticket->getFromDB($tickets_id);
$groups_id = [];
// == Add user groups on modification ==
//check this plugin config
if (
$_SESSION['glpi_plugins']['escalade']['config']['use_assign_user_group'] == 0
|| $_SESSION['glpi_plugins']['escalade']['config']['use_assign_user_group_modification'] == 0
|| $type != CommonITILActor::ASSIGN
) {
return true;
}
if ($_SESSION['glpi_plugins']['escalade']['config']['use_assign_user_group'] == 1) {
// First group
$groups_id = PluginEscaladeUser::getTechnicianGroup(
$ticket->fields['entities_id'],
$item->fields['users_id'],
true,
);
} else {
// All groups
$groups_id = PluginEscaladeUser::getTechnicianGroup(
$ticket->fields['entities_id'],
$item->fields['users_id'],
false,
);
}
if (!empty($groups_id)) {
$group_ticket = new Group_Ticket();
//The ticket cannot have this group already assigned
$found = $group_ticket->find([
'tickets_id' => $tickets_id,
'groups_id' => $groups_id,
'type' => CommonITILActor::ASSIGN,
]);
if (!empty($found)) {
return false;
}
//prevent user removal
$_SESSION['plugin_escalade']['keep_users'][$item->fields['users_id']]
= $item->fields['users_id'];
//add new group to ticket
$group_ticket->add([
'tickets_id' => $tickets_id,
'groups_id' => $groups_id,
'type' => CommonITILActor::ASSIGN,
]);
} elseif ($_SESSION['glpi_plugins']['escalade']['config']['remove_tech']) {
self::removeAssignGroups($tickets_id);
}
//fix ticket status
return $ticket->update([
'id' => $tickets_id,
'status' => CommonITILObject::ASSIGNED,
]);
}
/**
* Close linked tickets when ticket passed in parameter is closed
* @param CommonDBTM $ticket the ticket object
* @param int $status
*
* @return void
*/
public static function linkedTickets(CommonDBTM $ticket, $status = CommonITILObject::SOLVED)
{
if (!$_SESSION['glpi_plugins']['escalade']['config']['close_linkedtickets']) {
return;
}
$tickets = Ticket_Ticket::getLinkedTo(Ticket::class, $ticket->getID());
if ($tickets === []) {
return;
}
// Pre-define status for solved/closed tickets
$has_predefined_status = ($status == CommonITILObject::SOLVED || $status == CommonITILObject::CLOSED);
$input_base = $has_predefined_status ? ['status' => $status] : [];
$linkedTicket = new Ticket();
foreach ($tickets as $data) {
if ($data['link'] !== Ticket_Ticket::LINK_TO || !$linkedTicket->can($data['tickets_id'], UPDATE)) {
continue;
}
$input = $input_base;
$input['id'] = $data['tickets_id'];
// Determine status only if not predefined
if (!$has_predefined_status) {
$linkedTicket->getFromDB($data['tickets_id']);
$actors = $linkedTicket->getActorsForType(CommonITILActor::ASSIGN);
$input['status'] = ($actors !== []) ? CommonITILObject::ASSIGNED : CommonITILObject::INCOMING;
}
$linkedTicket->update($input);
}
}
/**
* On ticket category change, add ticket category group and user
* @return void
*/
public static function qualification(CommonDBTM $item)
{
/** @var DBmysql $DB */
global $DB;
//get auto-assign mode (config in entity)
$auto_assign_mode = Entity::getUsedConfig('auto_assign_mode', $_SESSION['glpiactive_entity']);
if ($auto_assign_mode == Entity::CONFIG_NEVER) {
return;
}
//get category
$category = new ITILCategory();
$category->getFromDB($item->input['itilcategories_id']);
//category group
if (
!empty($category->fields['groups_id'])
&& $_SESSION['glpi_plugins']['escalade']['config']['reassign_group_from_cat']
) {
$group_ticket = new Group_Ticket();
//check if group is not already present
$group_condition = [
'tickets_id' => $item->fields['id'],
'groups_id' => $category->fields['groups_id'],
'type' => CommonITILActor::ASSIGN,
];
$group_found = $group_ticket->find($group_condition);
if (empty($group_found)) {
//add group to ticket
$group_ticket->add($group_condition);
//remove old group if needed
if ($_SESSION['glpi_plugins']['escalade']['config']['remove_group'] && isset($item->input['_groups_id_assign'])) {
foreach ($item->input['_groups_id_assign'] as $idActor => $actor) {
unset($item->input['_groups_id_assign'][$idActor]);
unset($item->input['_groups_id_assign_notif']['use_notification'][$idActor]);
unset($item->input['_groups_id_assign_notif']['alternative_email'][$idActor]);
}
}
}
}
//category user
if (
!empty($category->fields['users_id'])
&& $_SESSION['glpi_plugins']['escalade']['config']['reassign_tech_from_cat']
) {
$ticket_user = new Ticket_User();
//check if user is not already present
$user_condition = [
'tickets_id' => $item->fields['id'],
'users_id' => $category->fields['users_id'],
'type' => CommonITILActor::ASSIGN,
];
$user_found = $ticket_user->find($user_condition);
if (empty($user_found)) {
self::removeAssignUsers($item);
//add user to ticket
$ticket_user->add($user_condition);
//remove old tech if needed
if ($_SESSION['glpi_plugins']['escalade']['config']['remove_tech'] && isset($item->input['_users_id_assign'])) {
foreach ($item->input['_users_id_assign'] as $idActor => $actor) {
unset($item->input['_users_id_assign'][$idActor]);
unset($item->input['_users_id_assign_notif']['use_notification'][$idActor]);
unset($item->input['_users_id_assign_notif']['alternative_email'][$idActor]);
}
}
}
}
}
/**
* CLone a ticket and his relations
* @param integer $tickets_id id of the ticket to clone
* @return void print a json response (return nothing)
*/
public static function cloneAndLink($tickets_id)
{
/** @var DBmysql $DB */
global $DB;
//get old ticket
$ticket = new Ticket();
if (!$ticket->getFromDB($tickets_id)) {
Session::addMessageAfterRedirect(__s('Error : get old ticket', 'escalade'), false, ERROR);
return;
}
//set fields
$fields = $ticket->fields;
$fields['id'] = 0;
$fields['_users_id_requester'] = 0;
$fields['status'] = CommonITILObject::INCOMING;
/*var_dump($fields);
exit;*/
//create new ticket (duplicate from previous)
if (!$newID = $ticket->add($fields)) {
Session::addMessageAfterRedirect(__s('Error : adding new ticket', 'escalade'), false, ERROR);
return;
}
//add link between them
$ticket_ticket = new Ticket_Ticket();
if ($_SESSION['glpi_plugins']['escalade']['config']['close_linkedtickets']) {
$link_type = CommonITILObject_CommonITILObject::DUPLICATE_WITH;
} else {