forked from glpi-project/glpi
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CommonITILObject.php
9031 lines (8050 loc) · 320 KB
/
CommonITILObject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2022 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/
use Glpi\Application\View\TemplateRenderer;
use Glpi\Event;
use Glpi\Plugin\Hooks;
use Glpi\RichText\RichText;
use Glpi\Team\Team;
use Glpi\Toolbox\Sanitizer;
/**
* CommonITILObject Class
**/
abstract class CommonITILObject extends CommonDBTM
{
use \Glpi\Features\Clonable;
use \Glpi\Features\Timeline;
use \Glpi\Features\Kanban;
use Glpi\Features\Teamwork;
/// Users by type
protected $users = [];
public $userlinkclass = '';
/// Groups by type
protected $groups = [];
public $grouplinkclass = '';
/// Suppliers by type
protected $suppliers = [];
public $supplierlinkclass = '';
/// Use user entity to select entity of the object
protected $userentity_oncreate = false;
public $deduplicate_queued_notifications = false;
const MATRIX_FIELD = '';
const URGENCY_MASK_FIELD = '';
const IMPACT_MASK_FIELD = '';
const STATUS_MATRIX_FIELD = '';
// STATUS
const INCOMING = 1; // new
const ASSIGNED = 2; // assign
const PLANNED = 3; // plan
const WAITING = 4; // waiting
const SOLVED = 5; // solved
const CLOSED = 6; // closed
const ACCEPTED = 7; // accepted
const OBSERVED = 8; // observe
const EVALUATION = 9; // evaluation
const APPROVAL = 10; // approbation
const TEST = 11; // test
const QUALIFICATION = 12; // qualification
const NO_TIMELINE = -1;
const TIMELINE_NOTSET = 0;
const TIMELINE_LEFT = 1;
const TIMELINE_MIDLEFT = 2;
const TIMELINE_MIDRIGHT = 3;
const TIMELINE_RIGHT = 4;
const TIMELINE_ORDER_NATURAL = 'natural';
const TIMELINE_ORDER_REVERSE = 'reverse';
abstract public static function getTaskClass();
public function post_getFromDB()
{
$this->loadActors();
}
/**
* @since 0.84
**/
public function loadActors()
{
if (!empty($this->grouplinkclass)) {
$class = new $this->grouplinkclass();
$this->groups = $class->getActors($this->fields['id']);
}
if (!empty($this->userlinkclass)) {
$class = new $this->userlinkclass();
$this->users = $class->getActors($this->fields['id']);
}
if (!empty($this->supplierlinkclass)) {
$class = new $this->supplierlinkclass();
$this->suppliers = $class->getActors($this->fields['id']);
}
}
/**
* Return the number of actors currently assigned to the object
*
* @since 10.0
*
* @return int
*/
public function countActors(): int
{
return count($this->groups) + count($this->users) + count($this->suppliers);
}
/**
* Return the list of actors for a given actor type
* We try to retrieve them by:
* - in case new ticket
* - from virtual _actor field (present after a reload)
* - from template (predefined actor field)
* - from default actor if setting is defined in user preference
* - for existing ticket (with an id > 0), directly from saved actors
*
* @since 10.0
*
* @param int $actortype 1=requester, 2=assign, 3=observer
* @param array $params posted data of itil object
*
* @return array of actors
*/
public function getActorsForType(int $actortype = 1, array $params = []): array
{
$actors = [];
$actortypestring = self::getActorFieldNameType($actortype);
if ($this->isNewItem()) {
$entities_id = $params['entities_id'] ?? $_SESSION['glpiactive_entity'];
$default_use_notif = Entity::getUsedConfig('is_notif_enable_default', $entities_id, '', 1);
// load default user from preference only at the first load of new ticket form
// we don't want to trigger it on form reload
// at first load, the key _skip_default_actor is not present (can only be present after a submit)
if (!isset($params['_skip_default_actor'])) {
$users_id_default = $this->getDefaultActor($actortype);
if ($users_id_default > 0) {
$userobj = new User();
if ($userobj->getFromDB($users_id_default)) {
$name = formatUserName(
$userobj->fields["id"],
$userobj->fields["name"],
$userobj->fields["realname"],
$userobj->fields["firstname"]
);
$email = UserEmail::getDefaultForUser($users_id_default);
$actors[] = [
'items_id' => $users_id_default,
'itemtype' => 'User',
'text' => $name,
'title' => $name,
'use_notification' => $email === '' ? false : $default_use_notif,
'alternative_email' => $email,
];
}
}
}
// load default actors from itiltemplate passed from showForm in `params` var
// we find this key on the first load of template (when opening form)
// or when the template change (by category loading)
if (isset($params['_template_changed'])) {
$users_id = (int) ($params['_predefined_fields']['_users_id_' . $actortypestring] ?? 0);
if ($users_id > 0) {
$userobj = new User();
if ($userobj->getFromDB($users_id)) {
$name = formatUserName(
$userobj->fields["id"],
$userobj->fields["name"],
$userobj->fields["realname"],
$userobj->fields["firstname"]
);
$email = UserEmail::getDefaultForUser($users_id);
$actors[] = [
'items_id' => $users_id,
'itemtype' => 'User',
'text' => $name,
'title' => $name,
'use_notification' => $email === '' ? false : $default_use_notif,
'alternative_email' => $email,
];
}
}
$groups_id = (int) ($params['_predefined_fields']['_groups_id_' . $actortypestring] ?? 0);
if ($groups_id > 0) {
$group_obj = new Group();
if ($group_obj->getFromDB($groups_id)) {
$actors[] = [
'items_id' => $group_obj->fields['id'],
'itemtype' => 'Group',
'text' => $group_obj->getName(),
'title' => $group_obj->getRawCompleteName(),
];
}
}
$suppliers_id = (int) ($params['_predefined_fields']['_suppliers_id_' . $actortypestring] ?? 0);
if ($suppliers_id > 0) {
$supplier_obj = new Supplier();
if ($supplier_obj->getFromDB($suppliers_id)) {
$actors[] = [
'items_id' => $supplier_obj->fields['id'],
'itemtype' => 'Supplier',
'text' => $supplier_obj->fields['name'],
'title' => $supplier_obj->fields['name'],
'use_notification' => $supplier_obj->fields['email'] === '' ? false : $default_use_notif,
'alternative_email' => $supplier_obj->fields['email'],
];
}
}
}
// if we load any actor from _itemtype_actortype_id, we are loading template,
// and so we don't want more actors.
// if any actor exists and was absent in a field from template, it will be loaded by the POST data.
// we choose to erase existing actors for any defined in the template.
if (count($actors)) {
return $actors;
}
// existing actors (from a form reload)
if (isset($params['_actors'])) {
foreach ($params['_actors'] as $existing_actortype => $existing_actors) {
if ($existing_actortype != $actortypestring) {
continue;
}
foreach ($existing_actors as &$existing_actor) {
$actor_obj = new $existing_actor['itemtype']();
if ($actor_obj->getFromDB($existing_actor['items_id'])) {
if ($actor_obj instanceof User) {
$name = formatUserName(
$actor_obj->fields["id"],
$actor_obj->fields["name"],
$actor_obj->fields["realname"],
$actor_obj->fields["firstname"]
);
$completename = $name;
} else {
$name = $actor_obj->getName();
$completename = $actor_obj->getRawCompleteName();
}
$actors[] = $existing_actor + [
'text' => $name,
'title' => $completename,
];
} elseif (
$actor_obj instanceof User
&& $existing_actor['items_id'] == 0
&& strlen($existing_actor['alternative_email']) > 0
) {
// direct mail actor
$actors[] = $existing_actor + [
'text' => $existing_actor['alternative_email'],
'title' => $existing_actor['alternative_email'],
];
}
}
}
return $actors;
}
}
// load existing actors (from existing itilobject)
if (isset($this->users[$actortype])) {
foreach ($this->users[$actortype] as $user) {
$name = getUserName($user['users_id']);
$actors[] = [
'id' => $user['id'],
'items_id' => $user['users_id'],
'itemtype' => 'User',
'text' => $name,
'title' => $name,
'use_notification' => $user['use_notification'],
'alternative_email' => $user['alternative_email'],
];
}
}
if (isset($this->groups[$actortype])) {
foreach ($this->groups[$actortype] as $group) {
$group_obj = new Group();
if ($group_obj->getFromDB($group['groups_id'])) {
$actors[] = [
'id' => $group['id'],
'items_id' => $group['groups_id'],
'itemtype' => 'Group',
'text' => $group_obj->getName(),
'title' => $group_obj->getRawCompleteName(),
];
}
}
}
if (isset($this->suppliers[$actortype])) {
foreach ($this->suppliers[$actortype] as $supplier) {
$name = Dropdown::getDropdownName(Supplier::getTable(), $supplier['suppliers_id']);
$actors[] = [
'id' => $supplier['id'],
'items_id' => $supplier['suppliers_id'],
'itemtype' => 'Supplier',
'text' => $name,
'title' => $name,
'use_notification' => $supplier['use_notification'],
'alternative_email' => $supplier['alternative_email'],
];
}
}
return $actors;
}
/**
* @param $ID
* @param $options array
**/
public function showForm($ID, array $options = [])
{
if (!static::canView()) {
return false;
}
$default_values = static::getDefaultValues();
// Restore saved value or override with page parameter
$options['_saved'] = $this->restoreInput();
// Restore saved values and override $this->fields
$this->restoreSavedValues($options['_saved']);
// Set default options
if (!$ID) {
foreach ($default_values as $key => $val) {
if (!isset($options[$key])) {
if (isset($options['_saved'][$key])) {
$options[$key] = $options['_saved'][$key];
} else {
$options[$key] = $val;
}
}
}
}
$this->initForm($ID, $options);
$canupdate = !$ID || (Session::getCurrentInterface() == "central" && $this->canUpdateItem());
if ($ID && in_array($this->fields['status'], $this->getClosedStatusArray())) {
$canupdate = false;
// No update for actors
$options['_noupdate'] = true;
}
if (!$this->isNewItem()) {
$options['formtitle'] = sprintf(
__('%1$s - ID %2$d'),
$this->getTypeName(1),
$ID
);
//set ID as already defined
$options['noid'] = true;
}
$type = null;
if (is_a($this, Ticket::class)) {
$type = ($ID ? $this->fields['type'] : $options['type']);
}
// Load template if available
$tt = $this->getITILTemplateToUse(
$options['template_preview'] ?? 0,
$type,
($ID ? $this->fields['itilcategories_id'] : $options['itilcategories_id']),
($ID ? $this->fields['entities_id'] : $options['entities_id'])
);
$predefined_fields = $this->setPredefinedFields($tt, $options, $default_values);
TemplateRenderer::getInstance()->display('components/itilobject/layout.html.twig', [
'item' => $this,
'timeline_itemtypes' => $this->getTimelineItemtypes(),
'legacy_timeline_actions' => $this->getLegacyTimelineActionsHTML(),
'params' => $options,
'entities_id' => $ID ? $this->fields['entities_id'] : $options['entities_id'],
'timeline' => $this->getTimelineItems(),
'itiltemplate_key' => static::getTemplateFormFieldName(),
'itiltemplate' => $tt,
'predefined_fields' => Toolbox::prepareArrayForInput($predefined_fields),
'canupdate' => $canupdate,
'canpriority' => $canupdate,
'canassign' => $canupdate,
]);
return true;
}
/**
* Return an array of predefined fields from provided template
* Append also data to $options param (passed by reference) :
* - if we transform a ticket (form change and problem) or a problem (for change) override with its field
* - override form fields from template (ex: if content field is set in template, content field in option will be overriden)
* - if template changed (provided template doesn't match the one found in options), append a key _template_changed in $options
* - finally, append templates_id in options
*
* @param ITILTemplate $tt The ticket template to use
* @param array $options The current options array (PASSED BY REFERENCE)
* @param array $default_values The default values to use in case they are not predefined
* @return array An array of the predefined values
*/
protected function setPredefinedFields(ITILTemplate $tt, array &$options, array $default_values): array
{
// Predefined fields from template : reset them
if (isset($options['_predefined_fields'])) {
$options['_predefined_fields'] = Toolbox::decodeArrayFromInput($options['_predefined_fields']);
} else {
$options['_predefined_fields'] = [];
}
if (!isset($options['_hidden_fields'])) {
$options['_hidden_fields'] = [];
}
// check original ticket for change and problem
$tickets_id = $options['tickets_id'] ?? $options['_tickets_id'] ?? null;
$ticket = new Ticket();
$ticket->getEmpty();
if (in_array($this->getType(), ['Change', 'Problem']) && $tickets_id) {
$ticket->getFromDB($tickets_id);
$options['content'] = $ticket->fields['content'];
$options['name'] = $ticket->fields['name'];
$options['impact'] = $ticket->fields['impact'];
$options['urgency'] = $ticket->fields['urgency'];
$options['priority'] = $ticket->fields['priority'];
if (isset($options['tickets_id'])) {
//page is reloaded on category change, we only want category on the very first load
$category = new ITILCategory();
$category->getFromDB($ticket->fields['itilcategories_id']);
$options['itilcategories_id'] = $category->fields['is_change'] ? $ticket->fields['itilcategories_id'] : 0;
}
$options['time_to_resolve'] = $ticket->fields['time_to_resolve'];
$options['entities_id'] = $ticket->fields['entities_id'];
}
// check original problem for change
$problems_id = $options['problems_id'] ?? $options['_problems_id'] ?? null;
$problem = new Problem();
$problem->getEmpty();
if ($this->getType() == "Change" && $problems_id) {
$problem->getFromDB($problems_id);
$options['content'] = $problem->fields['content'];
$options['name'] = $problem->fields['name'];
$options['impact'] = $problem->fields['impact'];
$options['urgency'] = $problem->fields['urgency'];
$options['priority'] = $problem->fields['priority'];
if (isset($options['problems_id'])) {
//page is reloaded on category change, we only want category on the very first load
$options['itilcategories_id'] = $problem->fields['itilcategories_id'];
}
$options['time_to_resolve'] = $problem->fields['time_to_resolve'];
$options['entities_id'] = $problem->fields['entities_id'];
}
// Store predefined fields to be able not to take into account on change template
$predefined_fields = [];
$tpl_key = static::getTemplateFormFieldName();
if ($this->isNewItem()) {
if (isset($tt->predefined) && count($tt->predefined)) {
foreach ($tt->predefined as $predeffield => $predefvalue) {
if (isset($options[$predeffield]) && isset($default_values[$predeffield])) {
// Is always default value : not set
// Set if already predefined field
// Set if ticket template change
if (
((count($options['_predefined_fields']) == 0)
&& ($options[$predeffield] == $default_values[$predeffield]))
|| (isset($options['_predefined_fields'][$predeffield])
&& ($options[$predeffield] == $options['_predefined_fields'][$predeffield]))
|| (isset($options[$tpl_key])
&& ($options[$tpl_key] != $tt->getID()))
// user pref for requestype can't overwrite requestype from template
// when change category
|| ($predeffield == 'requesttypes_id'
&& empty(($options['_saved'] ?? [])))
// tests specificic for change & problem
|| ($tickets_id != null
&& $options[$predeffield] == $ticket->fields[$predeffield])
|| ($problems_id != null
&& $options[$predeffield] == $problem->fields[$predeffield])
) {
$options[$predeffield] = $predefvalue;
$predefined_fields[$predeffield] = $predefvalue;
$this->fields[$predeffield] = $predefvalue;
}
} else { // Not defined options set as hidden field
$options['_hidden_fields'][$predeffield] = $predefvalue;
}
}
// All predefined override : add option to say predifined exists
if (count($predefined_fields) == 0) {
$predefined_fields['_all_predefined_override'] = 1;
}
} else { // No template load : reset predefined values
if (count($options['_predefined_fields'])) {
foreach ($options['_predefined_fields'] as $predeffield => $predefvalue) {
if ($options[$predeffield] == $predefvalue) {
$options[$predeffield] = $default_values[$predeffield];
}
}
}
}
}
// append to options to know later we added predefined values
// we may need this especially for actors
if (count($predefined_fields)) {
$options['_predefined_fields'] = $predefined_fields;
}
// check if we load the default template (when openning form for example) or the template changed
if (!isset($options[$tpl_key]) || $options[$tpl_key] != $tt->getId()) {
$options['_template_changed'] = true;
}
// Put ticket template id on $options for actors
$options[str_replace('s_id', '', $tpl_key)] = $tt->getId();
// Add all values to fields of tickets for template preview
if (($options['template_preview'] ?? false)) {
foreach ($options as $key => $val) {
if (!isset($this->fields[$key])) {
$this->fields[$key] = $val;
}
}
}
return $predefined_fields;
}
/**
* Retrieve all possible entities for an itilobject posted data.
* We try to retrieve requesters in the data:
* - from `_users_id_requester` (data from template or default actor)
* - from `_actors` (virtual field when the form is reloaded)
* By default, if none of these fields are present, entities are get from current active entity.
*
* @since 10.0
*
* @param array $params posted data by an itil object
* @return array of possible entities_id
*/
public function getEntitiesForRequesters(array $params = [])
{
$requesters = [];
if (array_key_exists('_users_id_requester', $params) && !empty($params["_users_id_requester"])) {
$requesters = !is_array($params["_users_id_requester"])
? [$params["_users_id_requester"]]
: $params["_users_id_requester"];
}
if (isset($params['_actors']['requester'])) {
foreach ($params['_actors']['requester'] as $actor) {
if ($actor['itemtype'] == "User") {
$requesters[] = $actor['items_id'];
}
}
}
$entities = $_SESSION['glpiactiveentities'] ?? [];
foreach ($requesters as $users_id) {
$user_entities = Profile_User::getUserEntities($users_id, true, true);
$entities = array_intersect($entities, $user_entities);
}
$entities = array_values($entities); // Ensure keys are starting at 0
return $entities;
}
/**
* Retrieve an item from the database with datas associated (hardwares)
*
* @param integer $ID ID of the item to get
*
* @return boolean true if succeed else false
**/
public function getFromDBwithData($ID)
{
if ($this->getFromDB($ID)) {
$this->getAdditionalDatas();
return true;
}
return false;
}
public function getAdditionalDatas()
{
}
/**
* Can manage actors
*
* @return boolean
*/
public function canAdminActors()
{
if (isset($this->fields['is_deleted']) && $this->fields['is_deleted'] == 1) {
return false;
}
return Session::haveRight(static::$rightname, UPDATE);
}
/**
* Can assign object
*
* @return boolean
*/
public function canAssign()
{
if (
isset($this->fields['is_deleted']) && ($this->fields['is_deleted'] == 1)
|| isset($this->fields['status']) && in_array($this->fields['status'], $this->getClosedStatusArray())
) {
return false;
}
return Session::haveRight(static::$rightname, UPDATE);
}
/**
* Can be assigned to me
*
* @return boolean
*/
public function canAssignToMe()
{
if (
isset($this->fields['is_deleted']) && $this->fields['is_deleted'] == 1
|| isset($this->fields['status']) && in_array($this->fields['status'], $this->getClosedStatusArray())
) {
return false;
}
return Session::haveRight(static::$rightname, UPDATE);
}
/**
* Is the current user have right to approve solution of the current ITIL object.
*
* @since 9.4.0
*
* @return boolean
*/
public function canApprove()
{
return (($this->fields["users_id_recipient"] === Session::getLoginUserID())
|| $this->isUser(CommonITILActor::REQUESTER, Session::getLoginUserID())
|| (isset($_SESSION["glpigroups"])
&& $this->haveAGroup(CommonITILActor::REQUESTER, $_SESSION["glpigroups"])));
}
/**
* Is the current user have right to add followups to the current ITIL Object ?
*
* @since 9.4.0
*
* @return boolean
*/
public function canAddFollowups()
{
return (
(
Session::haveRight("followup", ITILFollowup::ADDMYTICKET)
&& (
$this->isUser(CommonITILActor::REQUESTER, Session::getLoginUserID())
|| (
isset($this->fields["users_id_recipient"])
&& ($this->fields["users_id_recipient"] == Session::getLoginUserID())
)
)
)
|| (
Session::haveRight("followup", ITILFollowup::ADD_AS_OBSERVER)
&& $this->isUser(CommonITILActor::OBSERVER, Session::getLoginUserID())
)
|| Session::haveRight('followup', ITILFollowup::ADDALLTICKET)
|| (
Session::haveRight('followup', ITILFollowup::ADDGROUPTICKET)
&& isset($_SESSION["glpigroups"])
&& $this->haveAGroup(CommonITILActor::REQUESTER, $_SESSION['glpigroups'])
)
|| $this->isUser(CommonITILActor::ASSIGN, Session::getLoginUserID())
|| (
isset($_SESSION["glpigroups"])
&& $this->haveAGroup(CommonITILActor::ASSIGN, $_SESSION['glpigroups'])
)
|| $this->isValidator(Session::getLoginUserID())
);
}
/**
* Do the current ItilObject need to be reopened by a requester answer
*
* @since 10.0.1
*
* @return boolean
*/
public function needReopen(): bool
{
$my_id = Session::getLoginUserID();
$my_groups = $_SESSION["glpigroups"] ?? [];
$ami_requester = $this->isUser(CommonITILActor::REQUESTER, $my_id);
$ami_requester_group = $this->isGroup(CommonITILActor::REQUESTER, $my_groups);
$ami_assignee = $this->isUser(CommonITILActor::ASSIGN, $my_id);
$ami_assignee_group = $this->isGroup(CommonITILActor::ASSIGN, $my_groups);
return in_array($this->fields["status"], static::getReopenableStatusArray())
&& ($ami_requester || $ami_requester_group)
&& !($ami_assignee || $ami_assignee_group);
}
/**
* Check if the given users is a validator
* @param int $users_id
* @return bool
*/
public function isValidator($users_id): bool
{
if (!$users_id) {
// Invalid parameter
return false;
}
if (!$this instanceof Ticket && !$this instanceof Change) {
// Not a valid validation target
return false;
}
$validation_class = static::class . "Validation";
$valitation_obj = new $validation_class();
$validation_requests = $valitation_obj->find([
getForeignKeyFieldForItemType(static::class) => $this->getID(),
'users_id_validate' => $users_id,
]);
return count($validation_requests) > 0;
}
/**
* Does current user have right to solve the current item?
*
* @return boolean
**/
public function canSolve()
{
return ((Session::haveRight(static::$rightname, UPDATE)
|| $this->isUser(CommonITILActor::ASSIGN, Session::getLoginUserID())
|| (isset($_SESSION["glpigroups"])
&& $this->haveAGroup(CommonITILActor::ASSIGN, $_SESSION["glpigroups"])))
&& static::isAllowedStatus($this->fields['status'], self::SOLVED)
// No edition on closed status
&& !in_array($this->fields['status'], $this->getClosedStatusArray()));
}
/**
* Does current user have right to solve the current item; if it was not closed?
*
* @return boolean
**/
public function maySolve()
{
return ((Session::haveRight(static::$rightname, UPDATE)
|| $this->isUser(CommonITILActor::ASSIGN, Session::getLoginUserID())
|| (isset($_SESSION["glpigroups"])
&& $this->haveAGroup(CommonITILActor::ASSIGN, $_SESSION["glpigroups"])))
&& static::isAllowedStatus($this->fields['status'], self::SOLVED));
}
/**
* Get the ITIL object closed, solved or waiting status list
*
* @since 9.4.0
*
* @return array
*/
public static function getReopenableStatusArray()
{
return [self::CLOSED, self::SOLVED, self::WAITING];
}
/**
* Is a user linked to the object ?
*
* @param integer $type type to search (see constants)
* @param integer $users_id user ID
*
* @return boolean
**/
public function isUser($type, $users_id)
{
if (isset($this->users[$type])) {
foreach ($this->users[$type] as $data) {
if ($data['users_id'] == $users_id) {
return true;
}
}
}
return false;
}
/**
* Is a group linked to the object ?
*
* @param integer $type type to search (see constants)
* @param integer $groups_id group ID
*
* @return boolean
**/
public function isGroup($type, $groups_id)
{
if (isset($this->groups[$type])) {
foreach ($this->groups[$type] as $data) {
if ($data['groups_id'] == $groups_id) {
return true;
}
}
}
return false;
}
/**
* Is a supplier linked to the object ?
*
* @since 0.84
*
* @param integer $type type to search (see constants)
* @param integer $suppliers_id supplier ID
*
* @return boolean
**/
public function isSupplier($type, $suppliers_id)
{
if (isset($this->suppliers[$type])) {
foreach ($this->suppliers[$type] as $data) {
if ($data['suppliers_id'] == $suppliers_id) {
return true;
}
}
}
return false;
}
/**
* get users linked to a object
*
* @param integer $type type to search (see constants)
*
* @return array
**/
public function getUsers($type)
{
if (isset($this->users[$type])) {
return $this->users[$type];
}
return [];
}
/**
* get groups linked to a object
*
* @param integer $type type to search (see constants)
*
* @return array
**/
public function getGroups($type)
{
if (isset($this->groups[$type])) {
return $this->groups[$type];
}
return [];
}
/**
* get users linked to an object including groups ones
*
* @since 0.85
*
* @param integer $type type to search (see constants)
*
* @return array
**/
public function getAllUsers($type)
{
$users = [];
foreach ($this->getUsers($type) as $link) {
$users[$link['users_id']] = $link['users_id'];
}
foreach ($this->getGroups($type) as $link) {
$gusers = Group_User::getGroupUsers($link['groups_id']);
foreach ($gusers as $user) {
$users[$user['id']] = $user['id'];
}
}
return $users;
}
/**
* get suppliers linked to a object
*
* @since 0.84
*
* @param integer $type type to search (see constants)
*
* @return array
**/
public function getSuppliers($type)
{
if (isset($this->suppliers[$type])) {
return $this->suppliers[$type];
}
return [];
}
/**
* count users linked to object by type or global
*
* @param integer $type type to search (see constants) / 0 for all (default 0)
*