-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathabstractitiltarget.class.php
More file actions
2563 lines (2270 loc) · 93.7 KB
/
abstractitiltarget.class.php
File metadata and controls
2563 lines (2270 loc) · 93.7 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
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator 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.
*
* Formcreator 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 Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2021 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/
use Glpi\Toolbox\Sanitizer;
use GlpiPlugin\Formcreator\Field\TextareaField;
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
abstract class PluginFormcreatorAbstractItilTarget extends PluginFormcreatorAbstractTarget implements
PluginFormcreatorExportableInterface,
PluginFormcreatorItilTargetInterface,
PluginFormcreatorConditionnableInterface,
PluginFormcreatorTranslatableInterface
{
/** @var array $requesters requester actors of the target */
protected $requesters;
/** @var array $observers watcher actors of the target */
protected $observers;
/** @var array $assigned assigned actors of the target */
protected $assigned;
/** @var array $assignedSuppliers assigned suppliers actors of the target */
protected $assignedSuppliers;
/** @var array $requesterGroups requester groups of the target */
protected $requesterGroups;
/** @var array $observerGroups watcher groups of the target */
protected $observerGroups;
/** @var array $assignedGroups assigned groups of the target */
protected $assignedGroups;
protected $attachedDocuments = [];
/** @var boolean $skipCreateActors Flag to disable creation of actors after creation of the item */
protected $skipCreateActors = false;
/**
* Gets an instance object for the relation between the target itemtype
* and an user
*
* @return CommonDBTM
*/
abstract protected function getItem_User();
/**
* Gets an instance object for the relation between the target itemtype
* and a group
*
* @return CommonDBTM
*/
abstract protected function getItem_Group();
/**
* Gets an instance object for the relation between the target itemtype
* and supplier
*
* @return CommonDBTM
*/
abstract protected function getItem_Supplier();
/**
* Get an instance object for the relation between the target itemtype
* and an object of any itemtype
*
* @return CommonDBRelation
*/
abstract public static function getItem_Item(): CommonDBRelation;
/**
* Get the class name of the target itemtype's template class
*
* @return string
*/
abstract protected function getTemplateItemtypeName(): string;
/**
* Get the class name of the target itemtype's template predefined field class
*
* @return string
*/
abstract protected function getTemplatePredefinedFieldItemtype(): string;
/**
* Get the query criterias to query the ITIL categories
* for the target
*
* @return array
*/
abstract protected function getCategoryFilter();
/**
* Determine the template ID to use as basis for target generation
*
* @param array $data Data of the target being crezated
* @return int
*/
abstract protected function getTargetTemplate(array $data): int;
const DUE_DATE_RULE_NONE = 1;
const DUE_DATE_RULE_ANSWER = 2;
const DUE_DATE_RULE_TICKET = 3;
const DUE_DATE_RULE_CALC = 4;
const DUE_DATE_PERIOD_MINUTE = 1;
const DUE_DATE_PERIOD_HOUR = 2;
const DUE_DATE_PERIOD_DAY = 3;
const DUE_DATE_PERIOD_MONTH = 4;
const URGENCY_RULE_NONE = 1;
const URGENCY_RULE_SPECIFIC = 2;
const URGENCY_RULE_ANSWER = 3;
const TAG_TYPE_NONE = 1;
const TAG_TYPE_QUESTIONS = 2;
const TAG_TYPE_SPECIFICS = 3;
const TAG_TYPE_QUESTIONS_AND_SPECIFIC = 4;
const TAG_TYPE_QUESTIONS_OR_SPECIFIC = 5;
const CATEGORY_RULE_NONE = 1;
const CATEGORY_RULE_SPECIFIC = 2;
const CATEGORY_RULE_ANSWER = 3;
const CATEGORY_RULE_LAST_ANSWER = 4;
const LOCATION_RULE_NONE = 1;
const LOCATION_RULE_SPECIFIC = 2;
const LOCATION_RULE_ANSWER = 3;
const LOCATION_RULE_LAST_ANSWER = 4;
const COMMONITIL_VALIDATION_RULE_NONE = 1;
const COMMONITIL_VALIDATION_RULE_SPECIFIC_USER_OR_GROUP = 2;
const COMMONITIL_VALIDATION_RULE_ANSWER_USER = 3;
const COMMONITIL_VALIDATION_RULE_ANSWER_GROUP = 4;
const OLA_RULE_NONE = 1;
const OLA_RULE_SPECIFIC = 2;
const OLA_RULE_FROM_ANWSER = 3;
const SLA_RULE_NONE = 1;
const SLA_RULE_SPECIFIC = 2;
const SLA_RULE_FROM_ANWSER = 3;
public static function getEnumTagType() {
return [
self::TAG_TYPE_NONE => __('None'),
self::TAG_TYPE_QUESTIONS => __('Tags from questions', 'formcreator'),
self::TAG_TYPE_SPECIFICS => __('Specific tags', 'formcreator'),
self::TAG_TYPE_QUESTIONS_AND_SPECIFIC => __('Tags from questions and specific tags', 'formcreator'),
self::TAG_TYPE_QUESTIONS_OR_SPECIFIC => __('Tags from questions or specific tags', 'formcreator')
];
}
public static function getEnumDueDateRule() {
return [
self::DUE_DATE_RULE_ANSWER => __('equals to the answer to the question', 'formcreator'),
self::DUE_DATE_RULE_TICKET => __('calculated from the ticket creation date', 'formcreator'),
self::DUE_DATE_RULE_CALC => __('calculated from the answer to the question', 'formcreator'),
];
}
public static function getEnumSlaRule() {
return [
self::SLA_RULE_NONE => __('SLA from template or none', 'formcreator'),
self::SLA_RULE_SPECIFIC => __('Specific SLA', 'formcreator'),
self::SLA_RULE_FROM_ANWSER => __('Equals to the answer to the question', 'formcreator'),
];
}
public static function getEnumOlaRule() {
return [
self::OLA_RULE_NONE => __('OLA from template or none', 'formcreator'),
self::OLA_RULE_SPECIFIC => __('Specific OLA', 'formcreator'),
self::OLA_RULE_FROM_ANWSER => __('Equals to the answer to the question', 'formcreator'),
];
}
public static function getEnumUrgencyRule() {
return [
self::URGENCY_RULE_NONE => __('Urgency from template or Medium', 'formcreator'),
self::URGENCY_RULE_SPECIFIC => __('Specific urgency', 'formcreator'),
self::URGENCY_RULE_ANSWER => __('Equals to the answer to the question', 'formcreator'),
];
}
public static function getEnumCategoryRule() {
return [
self::CATEGORY_RULE_NONE => __('Category from template or none', 'formcreator'),
self::CATEGORY_RULE_SPECIFIC => __('Specific category', 'formcreator'),
self::CATEGORY_RULE_ANSWER => __('Equals to the answer to the question', 'formcreator'),
self::CATEGORY_RULE_LAST_ANSWER => __('Last valid answer', 'formcreator'),
];
}
public static function getEnumLocationRule() {
return [
self::LOCATION_RULE_NONE => __('Location from template or none', 'formcreator'),
self::LOCATION_RULE_SPECIFIC => __('Specific location', 'formcreator'),
self::LOCATION_RULE_ANSWER => __('Equals to the answer to the question', 'formcreator'),
self::LOCATION_RULE_LAST_ANSWER => __('Last valid answer', 'formcreator'),
];
}
public static function getEnumValidationRule() {
return [
self::COMMONITIL_VALIDATION_RULE_NONE => __('No validation', 'formcreator'),
self::COMMONITIL_VALIDATION_RULE_SPECIFIC_USER_OR_GROUP => __('Specific user or group', 'formcreator'),
self::COMMONITIL_VALIDATION_RULE_ANSWER_USER => __('User from question answer', 'formcreator'),
self::COMMONITIL_VALIDATION_RULE_ANSWER_GROUP => __('Group from question answer', 'formcreator'),
];
}
/**
* @param array $data data of the target
* @param PluginFormcreatorFormAnswer $formanswer Answers to the form used to populate the target
* @return array
*/
protected function setTargetCategory(array $data, PluginFormcreatorFormAnswer $formanswer) : array {
global $DB;
$category = null;
switch ($this->fields['category_rule']) {
case self::CATEGORY_RULE_ANSWER:
$category = $DB->request([
'SELECT' => ['answer'],
'FROM' => PluginFormcreatorAnswer::getTable(),
'WHERE' => [
'plugin_formcreator_formanswers_id' => $formanswer->fields['id'],
'plugin_formcreator_questions_id' => $this->fields['category_question']
]
])->current();
$category = $category['answer'];
break;
case self::CATEGORY_RULE_SPECIFIC:
$category = $this->fields['category_question'];
break;
case self::CATEGORY_RULE_LAST_ANSWER:
$form_answer_id = $formanswer->fields['id'];
// Get all answers for dropdown questions of this form, ordered
// from last to first displayed
$answers = $DB->request([
'SELECT' => ['answer.plugin_formcreator_questions_id', 'answer.answer', 'question.values'],
'FROM' => PluginFormcreatorAnswer::getTable() . ' AS answer',
'JOIN' => [
PluginFormcreatorQuestion::getTable() . ' AS question' => [
'ON' => [
'answer' => 'plugin_formcreator_questions_id',
'question' => 'id',
]
]
],
'WHERE' => [
'answer.plugin_formcreator_formanswers_id' => $form_answer_id,
'question.fieldtype' => "dropdown",
],
'ORDER' => [
'row DESC',
'col DESC',
]
]);
foreach ($answers as $answer) {
// Decode dropdown settings
$question = PluginFormcreatorQuestion::getById($answer[PluginFormcreatorQuestion::getForeignKeyField()]);
$itemtype = $question->fields['itemtype'];
// Skip if not a dropdown on categories
if ($itemtype !== ITILCategory::class) {
continue;
}
// Skip if question was not answered
if (empty($answer['answer'])) {
continue;
}
// Skip if question is not visible
if (!$formanswer->isFieldVisible($answer['plugin_formcreator_questions_id'])) {
continue;
}
// Found a valid answer, stop here
$category = $answer['answer'];
break;
}
break;
}
if ($category !== null) {
$data['itilcategories_id'] = $category;
}
return $data;
}
protected function setSLA($data, $formanswer) {
global $DB;
switch ($this->fields['sla_rule']) {
case self::SLA_RULE_SPECIFIC:
if (isset($this->fields['sla_question_tto'])) {
$data['slas_id_tto'] = $this->fields['sla_question_tto'];
}
if (isset($this->fields['sla_question_ttr'])) {
$data['slas_id_ttr'] = $this->fields['sla_question_ttr'];
}
break;
case self::SLA_RULE_FROM_ANWSER:
$tto = $DB->request([
'SELECT' => ['answer'],
'FROM' => PluginFormcreatorAnswer::getTable(),
'WHERE' => [
'plugin_formcreator_formanswers_id' => $formanswer->getID(),
'plugin_formcreator_questions_id' => $this->fields['sla_question_tto']
]
])->current();
$data['slas_id_tto'] = $tto['answer'];
$ttr = $DB->request([
'SELECT' => ['answer'],
'FROM' => PluginFormcreatorAnswer::getTable(),
'WHERE' => [
'plugin_formcreator_formanswers_id' => $formanswer->getID(),
'plugin_formcreator_questions_id' => $this->fields['sla_question_ttr']
]
])->current();
$data['slas_id_ttr'] = $ttr['answer'];
break;
}
return $data;
}
protected function setOLA($data, $formanswer) {
global $DB;
switch ($this->fields['ola_rule']) {
case self::OLA_RULE_SPECIFIC:
if (isset($this->fields['ola_question_tto'])) {
$data['olas_id_tto'] = $this->fields['ola_question_tto'];
}
if (isset($this->fields['ola_question_ttr'])) {
$data['olas_id_ttr'] = $this->fields['ola_question_ttr'];
}
break;
case self::OLA_RULE_FROM_ANWSER:
$tto = $DB->request([
'SELECT' => ['answer'],
'FROM' => PluginFormcreatorAnswer::getTable(),
'WHERE' => [
'plugin_formcreator_formanswers_id' => $formanswer->getID(),
'plugin_formcreator_questions_id' => $this->fields['ola_question_tto']
]
])->current();
$data['olas_id_tto'] = $tto['answer'];
$ttr = $DB->request([
'SELECT' => ['answer'],
'FROM' => PluginFormcreatorAnswer::getTable(),
'WHERE' => [
'plugin_formcreator_formanswers_id' => $formanswer->getID(),
'plugin_formcreator_questions_id' => $this->fields['ola_question_ttr']
]
])->current();
$data['olas_id_ttr'] = $ttr['answer'];
break;
}
return $data;
}
protected function setTargetUrgency($data, $formanswer) {
global $DB;
$urgency = null;
switch ($this->fields['urgency_rule']) {
case self::URGENCY_RULE_ANSWER:
$urgency = $DB->request([
'SELECT' => ['answer'],
'FROM' => PluginFormcreatorAnswer::getTable(),
'WHERE' => [
'plugin_formcreator_formanswers_id' => $formanswer->getID(),
'plugin_formcreator_questions_id' => $this->fields['urgency_question']
]
])->current();
$urgency = $urgency['answer'];
break;
case self::URGENCY_RULE_SPECIFIC:
$urgency = $this->fields['urgency_question'];
break;
}
if (!is_null($urgency) && $urgency != 0) {
$data['urgency'] = $urgency;
}
return $data;
}
protected function setTargetPriority(array $data): array {
// Remove default priority so it can be computed
if (isset($data['urgency']) || isset($data['impact'])) {
unset($data['priority']);
}
return $data;
}
/**
* find all actors and prepare data for the ticket being created
*/
protected function prepareActors(PluginFormcreatorForm $form, PluginFormcreatorFormAnswer $formanswer) {
global $DB, $PLUGIN_HOOKS;
$rows = $DB->request([
'FROM' => PluginFormcreatorTarget_Actor::getTable(),
'WHERE' => [
'itemtype' => $this->getType(),
'items_id' => $this->getID(),
]
]);
foreach ($rows as $actor) {
// If actor type is validator and if the form doesn't have a validator, continue to other actors
if ($actor['actor_type'] == PluginFormcreatorTarget_Actor::ACTOR_TYPE_VALIDATOR && !$form->fields['validation_required']) {
continue;
}
switch ($actor['actor_type']) {
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_AUTHOR :
$userIds = [$formanswer->fields['requester_id']];
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_VALIDATOR :
$userIds = [$_SESSION['glpiID']];
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_SUPPLIER :
$userIds = [$actor['actor_value']];
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_PERSON :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_GROUP :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_SUPPLIER :
$answer = new PluginFormcreatorAnswer();
$actorValue = $actor['actor_value'];
$formanswerId = $formanswer->getID();
$answer->getFromDBByCrit([
'AND' => [
'plugin_formcreator_questions_id' => $actorValue,
'plugin_formcreator_formanswers_id' => $formanswerId
]
]);
if ($answer->isNewItem()) {
continue 2;
} else {
$userIds = [$answer->fields['answer']];
}
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_ACTORS:
$answer = new PluginFormcreatorAnswer();
$actorValue = $actor['actor_value'];
$formanswerId = $formanswer->getID();
$answer->getFromDBByCrit([
'AND' => [
'plugin_formcreator_questions_id' => $actorValue,
'plugin_formcreator_formanswers_id' => $formanswerId
]
]);
if ($answer->isNewItem()) {
continue 2;
} else {
$userIds = json_decode($answer->fields['answer'], JSON_OBJECT_AS_ARRAY);
}
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP_FROM_OBJECT:
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_TECH_GROUP_FROM_OBJECT:
// Get the object from the question
$answer = new PluginFormcreatorAnswer();
$actorValue = $actor['actor_value'];
$formanswerId = $formanswer->getID();
$answer->getFromDBByCrit([
'AND' => [
'plugin_formcreator_questions_id' => $actorValue,
'plugin_formcreator_formanswers_id' => $formanswerId
]
]);
if ($answer->isNewItem()) {
continue 2;
}
// Get the itemtype of the object
$question = new PluginFormcreatorQuestion();
$question->getFromDB($answer->fields[PluginFormcreatorQuestion::getForeignKeyField()]);
if ($question->isNewItem()) {
continue 2;
}
$itemtype = $question->fields['itemtype'];
if (!is_subclass_of($itemtype, CommonDBTM::class)) {
continue 2;
}
// Check the object has a group FK
$groupFk = Group::getForeignKeyField();
if ($actor['actor_type'] == PluginFormcreatorTarget_Actor::ACTOR_TYPE_TECH_GROUP_FROM_OBJECT) {
$groupFk = $groupFk . '_tech';
}
$object = new $itemtype();
if (!$DB->fieldExists($object->getTable(), $groupFk)) {
continue 2;
}
// get the group
if (!$object->getFromDB($answer->fields['answer'])) {
continue 2;
}
// ignore invalid ID
if (Group::isNewId($object->fields[$groupFk])) {
continue 2;
}
$userIds = [$object->fields[$groupFk]];
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_AUTHORS_SUPERVISOR:
$requester_id = $formanswer->fields['requester_id'];
$user = new User;
$user = User::getById($requester_id);
if (is_object($user)) {
$userIds = [$user->fields['users_id_supervisor']];
}
break;
}
$notify = $actor['use_notification'];
switch ($actor['actor_type']) {
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_AUTHOR :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_VALIDATOR :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_PERSON :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_ACTORS:
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_AUTHORS_SUPERVISOR:
foreach ($userIds as $userIdOrEmail) {
$this->addActor($actor['actor_role'], $userIdOrEmail, $notify);
}
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_GROUP :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP_FROM_OBJECT:
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_TECH_GROUP_FROM_OBJECT:
foreach ($userIds as $groupId) {
$this->addGroupActor($actor['actor_role'], $groupId);
}
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_SUPPLIER :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_SUPPLIER :
foreach ($userIds as $userId) {
$this->addActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_SUPPLIER, $userId, $notify);
}
break;
default:
foreach (($PLUGIN_HOOKS['formcreator_actors_type'] ?? []) as $plugin => $classes) {
foreach ($classes as $plugin_target) {
if (!is_a($plugin_target, PluginFormcreatorPluginTargetInterface::class, true)) {
continue;
}
if ($actor['actor_type']== $plugin_target::getId()) {
$value = $plugin_target::getActorId($formanswer, $actor['actor_value']);
if ($value) {
if ($plugin_target::getActorType() == PluginFormcreatorPluginTargetInterface::ACTOR_TYPE_USER) {
$this->addActor($actor['actor_role'], $value, $notify);
} else if (PluginFormcreatorPluginTargetInterface::ACTOR_TYPE_GROUP) {
$this->addGroupActor($actor['actor_role'], $value);
}
}
break 2;
}
}
}
break;
}
}
}
/**
* Adds an user to the given actor role (requester, observer assigned or supplier)
*
* @param string $role role of the user
* @param string $user user ID or email address for accountless users
* @param bool $notify true to enable notification for the actor
* @return boolean true on success, false on error
*/
protected function addActor($role, $user, $notify) {
if (filter_var($user, FILTER_VALIDATE_EMAIL) !== false) {
$userId = 0;
$alternativeEmail = $user;
} else {
$userId = (int) $user;
$alternativeEmail = '';
if ($userId == '0') {
// there is no actor
return false;
}
}
$actorType = null;
$actorTypeNotif = null;
switch ($role) {
case PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER:
$actorType = &$this->requesters['_users_id_requester'];
$actorTypeNotif = &$this->requesters['_users_id_requester_notif'];
break;
case PluginFormcreatorTarget_Actor::ACTOR_ROLE_OBSERVER:
$actorType = &$this->observers['_users_id_observer'];
$actorTypeNotif = &$this->observers['_users_id_observer_notif'];
break;
case PluginFormcreatorTarget_Actor::ACTOR_ROLE_ASSIGNED :
$actorType = &$this->assigned['_users_id_assign'];
$actorTypeNotif = &$this->assigned['_users_id_assign_notif'];
break;
case PluginFormcreatorTarget_Actor::ACTOR_ROLE_SUPPLIER :
$actorType = &$this->assignedSuppliers['_suppliers_id_assign'];
$actorTypeNotif = &$this->assignedSuppliers['_suppliers_id_assign_notif'];
break;
default:
return false;
}
if ($userId > 0) {
// search duplicate account
$actorKey = array_search($userId, $actorType);
} else {
// search duplicate email
$actorKey = array_search($alternativeEmail, $actorTypeNotif['alternative_email']);
}
if ($actorKey === false) {
// Add the actor
$actorType[] = $userId;
$actorTypeNotif['use_notification'][] = $notify;
$actorTypeNotif['alternative_email'][] = $alternativeEmail;
} else {
// New actor settings takes precedence
$actorType[$actorKey] = $userId;
$actorTypeNotif['use_notification'][$actorKey] = $notify;
$actorTypeNotif['alternative_email'][$actorKey] = $alternativeEmail;
}
return true;
}
/**
* Adds a group to the given actor role
*
* @param string $role Role of the group
* @param string $group Group ID
* @return boolean true on sucess, false on error
*/
protected function addGroupActor($role, $group) {
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$actorType = null;
switch ($role) {
case PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER:
$actorType = &$this->requesterGroups['_groups_id_requester'];
break;
case PluginFormcreatorTarget_Actor::ACTOR_ROLE_OBSERVER :
$actorType = &$this->observerGroups['_groups_id_observer'];
break;
case PluginFormcreatorTarget_Actor::ACTOR_ROLE_ASSIGNED :
$actorType = &$this->assignedGroups['_groups_id_assign'];
break;
default:
return false;
}
$actorKey = array_search($group, $actorType);
if ($actorKey !== false) {
return false;
}
// Add the group actor
$actorType[] = $group;
return true;
}
/**
* Attach documents of the answer to the target
*/
protected function attachDocument($formAnswerId, $itemtype, $targetID) {
global $CFG_GLPI;
$docItem = new Document_Item();
if (count($this->attachedDocuments) <= 0) {
return;
}
foreach ($this->attachedDocuments as $documentID => $dummy) {
$docItem->add([
'documents_id' => $documentID,
'itemtype' => $itemtype,
'items_id' => $targetID,
]);
if ($itemtype === Ticket::class) {
$document = new Document();
$documentCategoryFk = DocumentCategory::getForeignKeyField();
$document->update([
'id' => $documentID,
$documentCategoryFk => $CFG_GLPI["documentcategories_id_forticket"],
]);
}
}
}
public function addAttachedDocument($documentId) {
$this->attachedDocuments[$documentId] = true;
}
protected function showTemplateSettings() {
$templateType = $this->getTemplateItemtypeName();
$templateFk = $templateType::getForeignKeyField();
echo '<td width="15%">' . $templateType::getTypeName(1) . '</td>';
echo '<td width="25%">';
Dropdown::show($templateType, [
'name' => $templateFk,
'value' => $this->fields[$templateFk]
]);
echo '</td>';
}
protected function showDueDateSettings() {
echo '<td width="15%">' . __('Time to resolve') . '</td>';
echo '<td width="45%">';
// Due date type selection
Dropdown::showFromArray('due_date_rule', self::getEnumDueDateRule(),
[
'value' => $this->fields['due_date_rule'],
'on_change' => 'plugin_formcreator_formcreatorChangeDueDate(this.value)',
'display_emptychoice' => true
]
);
$questionTable = PluginFormcreatorQuestion::getTable();
$questions = (new PluginFormcreatorQuestion)->getQuestionsFromForm(
$this->getForm()->getID(),
[
"$questionTable.fieldtype" => ['date', 'datetime'],
]
);
$questions_list = [];
foreach ($questions as $question) {
$questions_list[$question->getID()] = $question->fields['name'];
}
// List questions
if ($this->fields['due_date_rule'] != self::DUE_DATE_RULE_ANSWER
&& $this->fields['due_date_rule'] != self::DUE_DATE_RULE_CALC) {
echo '<div id="due_date_questions" style="display:none">';
} else {
echo '<div id="due_date_questions">';
}
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm(),
[
'fieldtype' => ['date', 'datetime'],
],
'due_date_question',
$this->fields['due_date_question']
);
echo '</div>';
// time shift in minutes
if ($this->fields['due_date_rule'] != self::DUE_DATE_RULE_TICKET
&& $this->fields['due_date_rule'] != self::DUE_DATE_RULE_CALC) {
echo '<div id="due_date_time" style="display:none">';
} else {
echo '<div id="due_date_time">';
}
Dropdown::showNumber("due_date_value", [
'value' => $this->fields['due_date_value'],
'min' => -30,
'max' => 30
]);
Dropdown::showFromArray('due_date_period', [
self::DUE_DATE_PERIOD_MINUTE => _n('Minute', 'Minutes', 2),
self::DUE_DATE_PERIOD_HOUR => _n('Hour', 'Hours', 2),
self::DUE_DATE_PERIOD_DAY => _n('Day', 'Days', 2),
self::DUE_DATE_PERIOD_MONTH => _n('Month', 'Months', 2),
], [
'value' => $this->fields['due_date_period']
]);
echo '</div>';
echo '</td>';
}
protected function showSLASettings() {
$label = __("SLA");
echo '<tr>';
echo "<td width='15%'>$label</td>";
echo '<td width="25%">';
// Due date type selection
Dropdown::showFromArray("sla_rule", self::getEnumSlaRule(),
[
'value' => $this->fields["sla_rule"],
'on_change' => "plugin_formcreator_formcreatorChangeSla(this.value)",
'display_emptychoice' => true
]
);
echo '</td>';
$display_specific = $this->fields["sla_rule"] == self::SLA_RULE_SPECIFIC;
$display_questions = $this->fields["sla_rule"] == self::SLA_RULE_FROM_ANWSER;
$style_specific = !$display_specific ? "style='display: none'" : "";
$style_questions = !$display_questions ? "style='display: none'" : "";
echo '<td width="15%">';
echo "<span id='sla_specific_title' $style_specific>" . __('SLA (TTO/TTR)', 'formcreator') . '</span>';
echo "<span id='sla_question_title' $style_questions>" . __('Question (TTO/TTR)', 'formcreator') . '</span>';
echo '</td>';
echo '<td width="25%">';
echo "<div id='sla_specific_value' $style_specific>";
SLA::dropdown([
'name' => '_sla_specific_tto',
'value' => $this->fields["sla_question_tto"],
'condition' => ['type' => SLM::TTO],
]);
echo " ";
SLA::dropdown([
'name' => '_sla_specific_ttr',
'value' => $this->fields["sla_question_ttr"],
'condition' => ['type' => SLM::TTR],
]);
echo '</div>';
echo "<div id='sla_questions' $style_questions>";
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm(),
[
'fieldtype' => 'dropdown',
'itemtype' => SLA::getType(),
new QueryExpression("`values` LIKE '%\"show_service_level_types\":\"1\"%'"),
],
"_sla_questions_tto",
$this->fields["sla_question_tto"]
);
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm(),
[
'fieldtype' => 'dropdown',
'itemtype' => SLA::getType(),
new QueryExpression("`values` LIKE '%\"show_service_level_types\":\"0\"%'"),
],
"_sla_questions_ttr",
$this->fields["sla_question_ttr"]
);
echo '</div>';
echo '</td>';
echo '</tr>';
}
protected function showOLASettings() {
$label = __("OLA");
echo '<tr>';
echo "<td width='15%'>$label</td>";
echo '<td width="25%">';
// Due date type selection
Dropdown::showFromArray("ola_rule", self::getEnumOlaRule(),
[
'value' => $this->fields["ola_rule"],
'on_change' => "plugin_formcreator_formcreatorChangeOla(this.value)",
'display_emptychoice' => true
]
);
echo '</td>';
$display_specific = $this->fields["ola_rule"] == self::OLA_RULE_SPECIFIC;
$display_questions = $this->fields["ola_rule"] == self::OLA_RULE_FROM_ANWSER;
$style_specific = !$display_specific ? "style='display: none'" : "";
$style_questions = !$display_questions ? "style='display: none'" : "";
echo '<td width="15%">';
echo "<span id='ola_specific_title' $style_specific>" . __('OLA (TTO/TTR)', 'formcreator') . '</span>';
echo "<span id='ola_question_title' $style_questions>" . __('Question (TTO/TTR)', 'formcreator') . '</span>';
echo '</td>';
echo '<td width="25%">';
echo "<div id='ola_specific_value' $style_specific>";
OLA::dropdown([
'name' => '_ola_specific_tto',
'value' => $this->fields["ola_question_tto"],
'condition' => ['type' => SLM::TTO],
]);
echo " ";
OLA::dropdown([
'name' => '_ola_specific_ttr',
'value' => $this->fields["ola_question_ttr"],
'condition' => ['type' => SLM::TTR],
]);
echo '</div>';
echo "<div id='ola_questions' $style_questions>";
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm(),
[
'fieldtype' => 'dropdown',
'itemtype' => OLA::getType(),
new QueryExpression("`values` LIKE '%\"show_service_level_types\":\"1\"%'"),
],
"_ola_questions_tto",
$this->fields["ola_question_tto"]
);
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm(),
[
'fieldtype' => 'dropdown',
'itemtype' => OLA::getType(),
new QueryExpression("`values` LIKE '%\"show_service_level_types\":\"0\"%'"),
],
"_ola_questions_ttr",
$this->fields["ola_question_ttr"]
);
echo '</div>';
echo '</td>';
echo '</tr>';
}
protected function showCategorySettings($rand) {
echo '<tr>';
echo '<td width="15%">' . ITILCategory::getTypeName(1) . '</td>';
echo '<td width="25%">';
Dropdown::showFromArray(
'category_rule',
static::getEnumCategoryRule(),
[
'value' => $this->fields['category_rule'],
'on_change' => "plugin_formcreator_changeCategory($rand)",
'rand' => $rand,
]
);
echo Html::scriptBlock("plugin_formcreator_changeCategory($rand);");
echo '</td>';
echo '<td width="15%">';
echo '<span id="category_specific_title" style="display: none">' . PluginFormcreatorCategory::getTypeName(1) . '</span>';
echo '<span id="category_question_title" style="display: none">' . PluginFormcreatorQuestion::getTypeName(1) . '</span>';
echo '</td>';
echo '<td width="25%">';
echo '<div id="category_question_value" style="display: none">';
PluginFormcreatorQuestion::dropdownForForm(