-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhook.php
1601 lines (1430 loc) · 63.2 KB
/
hook.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
/**
* ---------------------------------------------------------------------
* projectBridge is a plugin allows to count down time from contracts
* by linking tickets with project tasks and project tasks with contracts.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of projectBridge.
*
* projectBridge 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.
*
* projectBridge 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 © 2022-2023 probeSys'
* @license http://www.gnu.org/licenses/agpl.txt AGPLv3+
* @link https://github.com/Probesys/glpi-plugins-projectbridge
* @link https://plugins.glpi-project.org/#/plugin/projectbridge
* ---------------------------------------------------------------------
*/
/**
* Install the plugin
*
* @return boolean
*/
function plugin_projectbridge_install() {
global $DB;
if (!$DB->tableExists(PluginProjectbridgeEntity::$table_name)) {
$create_table_query = "
CREATE TABLE IF NOT EXISTS `" . PluginProjectbridgeEntity::$table_name . "`
(
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`entity_id` INT(11) NOT NULL,
`contract_id` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX (`entity_id`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
";
$DB->query($create_table_query) or die($DB->error());
}
if (!$DB->tableExists(PluginProjectbridgeContract::$table_name)) {
$create_table_query = "
CREATE TABLE IF NOT EXISTS `" . PluginProjectbridgeContract::$table_name . "`
(
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`contract_id` INT(11) NOT NULL,
`project_id` INT(11) NOT NULL,
`nb_hours` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX (`contract_id`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
";
$DB->query($create_table_query) or die($DB->error());
}
if (!$DB->tableExists(PluginProjectbridgeTicket::$table_name)) {
$create_table_query = "
CREATE TABLE IF NOT EXISTS `" . PluginProjectbridgeTicket::$table_name . "`
(
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`ticket_id` INT(11) NOT NULL,
`projecttasks_id` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX (`ticket_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC
";
$DB->query($create_table_query) or die($DB->error());
} else {
// test if old version of plugin
$fields = $DB->listFields(PluginProjectbridgeTicket::$table_name);
if (array_key_exists('project_id', $fields)) {
$update_structure_query = "ALTER TABLE `" . PluginProjectbridgeTicket::$table_name . "` CHANGE `project_id` `projecttasks_id` INT(11) NOT NULL;";
$DB->query($update_structure_query) or die($DB->error());
}
}
// configs datatable
$create_tableConfig_query = "
CREATE TABLE IF NOT EXISTS `" . PluginProjectbridgeConfig::$table_name . "`
(
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL ,
`value` VARCHAR(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC
";
if (!$DB->tableExists(PluginProjectbridgeConfig::$table_name)) {
$DB->query($create_tableConfig_query) or die($DB->error());
$insert_table_query = "INSERT INTO `" . PluginProjectbridgeConfig::$table_name . "` (`id`, `name`, `value`) VALUES
(1, 'RecipientIds', '[]'),
(2, 'CountOnlyPublicTasks', '1'),
(3, 'AddContractSelectorOnCreatingTicketForm', '0'),
(4, 'ElementsAssociateToExcessTicket', '[\\\"tasks\\\",\\\"followups\\\",\\\"documents\\\",\\\"solutions\\\",\\\"requester_groups\\\",\\\"requester\\\",\\\"assign_groups\\\",\\\"assign_technician\\\",\\\"watcher_user\\\",\\\"watcher_group\\\",\\\"tickets\\\"]')
;";
$DB->query($insert_table_query) or die($DB->error());
} else {
// test if old version of glpi_plugin_projectbridge_configs
$fields = $DB->listFields(PluginProjectbridgeConfig::$table_name);
if (array_key_exists('user_id', $fields)) {
// save old values of user_id
$userIds = [];
$req = $DB->request([
'SELECT' => ['user_id'],
'FROM' => PluginProjectbridgeConfig::$table_name,
]);
foreach ($req as $row) {
$userIds[] = (int) $row['user_id'];
}
// delete old table
$DB->queryOrDie(
"DROP TABLE `" . PluginProjectbridgeConfig::$table_name . "`",
$DB->error()
);
// create table with new format
$DB->query($create_tableConfig_query) or die($DB->error());
// insert values
$insert_table_query = "INSERT INTO `" . PluginProjectbridgeConfig::$table_name . "` (`id`, `name`, `value`) VALUES
(1, 'RecipientIds', '" . json_encode(array_unique($userIds)) . "'),
(2, 'CountOnlyPublicTasks', '1');";
$DB->query($insert_table_query) or die($DB->error());
}
// test if config addContractSelectorOnCreatingTicketForm is present
$req = $DB->request([
'FROM' => PluginProjectbridgeConfig::$table_name,
'WHERE' => ['name' => 'AddContractSelectorOnCreatingTicketForm']
]);
if (!count($req)) {
$insert_table_query = "INSERT INTO `" . PluginProjectbridgeConfig::$table_name . "` (`id`, `name`, `value`) VALUES
(3, 'AddContractSelectorOnCreatingTicketForm', '0');";
$DB->query($insert_table_query) or die($DB->error());
}
// test if config ElementsAssociateToExcessTicket is present
$req = $DB->request([
'FROM' => PluginProjectbridgeConfig::$table_name,
'WHERE' => ['name' => 'ElementsAssociateToExcessTicket']
]);
if (!count($req)) {
$insert_table_query = "INSERT INTO `" . PluginProjectbridgeConfig::$table_name . "` (`id`, `name`, `value`) VALUES
(4, 'ElementsAssociateToExcessTicket', '[\"tasks\",\"followups\",\"documents\",\"solutions\",\"requester_groups\",\"requester\",\"assign_groups\",\"assign_technician\",\"watcher_user\",\"watcher_group\",\"tickets\"]');";
$DB->query($insert_table_query) or die($DB->error());
}
}
if (!$DB->tableExists(PluginProjectbridgeState::$table_name)) {
$create_table_query = "
CREATE TABLE IF NOT EXISTS `" . PluginProjectbridgeState::$table_name . "`
(
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`status` VARCHAR(250) NOT NULL,
`projectstates_id` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC
";
$DB->query($create_table_query) or die($DB->error());
}
if (!$DB->tableExists(PluginProjectbridgeContractQuotaAlert::$table_name)) {
$create_table_query = "
CREATE TABLE IF NOT EXISTS `" . PluginProjectbridgeContractQuotaAlert::$table_name . "`
(
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`contract_id` INT(11) NOT NULL,
`quotaAlert` INT(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC
";
$DB->query($create_table_query) or die($DB->error());
}
// clean old crontask
if (version_compare(PLUGIN_PROJECTBRIDGE_VERSION, '2.2.3', '>')) {
$delete_crontask_table = "DELETE FROM " . Crontask::getTable() . " WHERE itemtype='PluginProjectbridgeContract' AND name='AlertContractsToRenew'";
$DB->query($delete_crontask_table) or die($DB->error());
}
if (version_compare(PLUGIN_PROJECTBRIDGE_VERSION, '2.3', '>')) {
$update_structure_query = "ALTER TABLE `" . PluginProjectbridgeConfig::$table_name . "` CHANGE `value` `value` VARCHAR(250) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL;";
$DB->query($update_structure_query) or die($DB->error());
}
// cron for alerts
CronTask::Register('PluginProjectbridgeTask', 'AlertContractsToRenew', DAY_TIMESTAMP);
// cron to process tasks (expired, quota reached, ...)
CronTask::Register('PluginProjectbridgeTask', 'ProcessTasks', DAY_TIMESTAMP);
// cron to update the percent_done counter in tasks
CronTask::Register('PluginProjectbridgeTask', 'UpdateProgressPercent', DAY_TIMESTAMP);
// cron for alert when consumntion of contract is over quota defined globally or on the contract
CronTask::Register('PluginProjectbridgeTask', 'AlertContractsOverQuota', DAY_TIMESTAMP);
return true;
}
/**
* Uninstall the plugin
*
* @return boolean
*/
function plugin_projectbridge_uninstall() {
global $DB;
// clean crontasks infos
$clear_crontaksInfos_query = "DELETE FROM " . CronTask::getTable() . " WHERE itemtype LIKE 'PluginProjectbridge%'";
$DB->query($clear_crontaksInfos_query) or die($DB->error());
//Crontask::unregister('Projectbridge');
$tables_to_drop = [
PluginProjectbridgeEntity::$table_name,
PluginProjectbridgeContract::$table_name,
PluginProjectbridgeTicket::$table_name,
PluginProjectbridgeConfig::$table_name,
PluginProjectbridgeState::$table_name,
PluginProjectbridgeContractQuotaAlert::$table_name,
];
$drop_table_query = "DROP TABLE IF EXISTS `" . implode('`, `', $tables_to_drop) . "`";
return $DB->query($drop_table_query) or die($DB->error());
}
/**
* Hook called after showing an item
*
* @param array $post_show_data
* @return void
*/
function plugin_projectbridge_post_show_item(array $post_show_data) {
if (!empty($post_show_data['item']) && is_object($post_show_data['item'])
) {
switch (get_class($post_show_data['item'])) {
case 'Entity':
PluginProjectbridgeEntity::postShow($post_show_data['item']);
break;
case 'Contract':
PluginProjectbridgeContract::postShow($post_show_data['item']);
break;
case 'Project':
PluginProjectbridgeContract::postShowProject($post_show_data['item']);
break;
default:
// nothing to do
}
}
}
/**
* Hook call when use tranfer function
*
* @param array $params
*/
function plugin_projectbridge_item_transfer(array $params) {
switch ($params['type']) {
case 'Ticket':
$forceActiveProjectTaskAfterTranfer = PluginProjectbridgeConfig::getConfValueByName('forceActiveProjectTaskAfterTranfer', true);
if($forceActiveProjectTaskAfterTranfer) {
$ticket = new Ticket();
$ticket_id = $params['id'];
$ticket->getFromDB($ticket_id);
$entity = new Entity();
$entity->getFromDB($params['entities_id']);
$bridge_entity = new PluginProjectbridgeEntity($entity);
$contract_id = $bridge_entity->getContractId();
$contract = new Contract();
$contract->getFromDB($contract_id);
$contract_bridge = new PluginProjectbridgeContract($contract);
$project_id = $contract_bridge->getProjectId();
$projectTask = $contract_bridge->getLastActiveProjectTasksForProject($project_id);
$project_task_ticket = new ProjectTask_Ticket();
// delete previous association between Ticket and projectTask
$project_task_ticket->deleteByCriteria([
'tickets_id' => $ticket_id,
]);
if($projectTask) {
// add new association between Ticket and projectTask
$project_task_ticket->add([
'tickets_id' => $ticket_id,
'projecttasks_id' => $projectTask['id'],
]);
}
}
break;
default:
// nothing to do
}
}
/**
* Hook called before the update of an entity
*
* @param Entity $entity
* @param boolean $force (optional)
* @return void|integer|boolean
*/
function plugin_projectbridge_pre_entity_update(Entity $entity, $force = false) {
if (($force === true || $entity->canUpdate()) && isset($entity->input['projectbridge_contract_id'])) {
if (empty($entity->input['projectbridge_contract_id'])) {
$selected_contract_id = 0;
} else {
$selected_contract_id = (int) $entity->input['projectbridge_contract_id'];
}
$bridge_entity = new PluginProjectbridgeEntity($entity);
$contract_id = $bridge_entity->getContractId();
$post_data = [
'entity_id' => $entity->getId(),
'contract_id' => $selected_contract_id,
];
if ($contract_id === null) {
return $bridge_entity->add($post_data);
} else if ($selected_contract_id != $contract_id) {
$post_data['id'] = $bridge_entity->getId();
return $bridge_entity->update($post_data);
}
}
}
/**
* Hook called before the update of a contract
*
* @param Contract $contract
* @return void
*/
function plugin_projectbridge_pre_contract_update(Contract $contract) {
global $DB;
$update_val = $contract->input['update'] ?? $contract->input['_update'] ?? null;
if ($contract->canUpdate() && $update_val !== null && isset($contract->input['projectbridge_project_id'])) {
if ($update_val != __('Link tickets to renewal', 'projectbridge')) {
// update contract
$nb_hours = 0;
if (empty($contract->input['projectbridge_project_id'])) {
$selected_project_id = 0;
// delete line in glpi_plugin_projectbridge_contracts
$bridge_contract = new PluginProjectbridgeContract($contract);
if ($bridge_contract && $bridge_contract->getID()) {
$projectbridge_project_id = $bridge_contract->getProjectId();
// delete line in glpi_plugin_projectbridge_contracts
$DB->delete(
$bridge_contract->getTable(),
[
'id' => $bridge_contract->getID()
]
);
if ($projectbridge_project_id) {
// delete line in table glpi_plugin_projectbridge_tickets where project_id = $projectbridge_project_id
$bridge_ticket = new PluginProjectbridgeTicket();
$DB->delete(
$bridge_ticket->getTable(),
[
'project_id' => $projectbridge_project_id
]
);
}
}
} else {
$selected_project_id = (int) $contract->input['projectbridge_project_id'];
if (!empty($contract->input['projectbridge_project_hours']) && $contract->input['projectbridge_project_hours'] > 0) {
$nb_hours = (int) $contract->input['projectbridge_project_hours'];
}
}
if ($selected_project_id > 0) {
$bridge_contract = new PluginProjectbridgeContract($contract);
$project_id = $bridge_contract->getProjectId();
$post_data = [
'contract_id' => $contract->getId(),
'project_id' => $selected_project_id,
'nb_hours' => $nb_hours,
];
if (empty($project_id)) {
$bridge_contract->add($post_data);
} else {
$post_data['id'] = $bridge_contract->getId();
$bridge_contract->update($post_data);
}
}
} else {
// renew the task of the project linked to the contract
if (empty($contract->input['_projecttask_begin_date']) || empty($contract->input['_projecttask_end_date']) || empty($contract->input['projectbridge_nb_hours_to_use'])) {
Session::addMessageAfterRedirect(__('Please complete all renewal fields', 'projectbridge'), false, ERROR);
return false;
}
$bridge_contract = new PluginProjectbridgeContract($contract);
$bridge_contract->renewProjectTask();
}
}
}
/**
* Hook called after the creation of a contract
*
* @param Contract $contract
* @param boolean $force (optional)
* @return boolean|void
*/
function plugin_projectbridge_contract_add(Contract $contract, $force = false) {
if ($force === true || ($contract->canUpdate() && isset($contract->input['projectbridge_create_project']) )) {
$nb_hours = 0;
if (!empty($contract->input['projectbridge_project_hours']) && $contract->input['projectbridge_project_hours'] > 0) {
$nb_hours = (int) $contract->input['projectbridge_project_hours'];
}
$date_creation = '';
$begin_date = '';
if (!empty($contract->fields['begin_date']) && $contract->fields['begin_date'] != 'NULL') {
$begin_date = date('Y-m-d H:i:s', strtotime($contract->fields['begin_date']));
}
if (empty($begin_date)) {
Session::addMessageAfterRedirect(__('The contract has no start date. The project could not be created.', 'projectbridge'), false, ERROR);
return false;
}
if (!empty($contract->fields['date_creation']) && $contract->fields['date_creation'] != 'NULL') {
$date_creation = $contract->fields['date_creation'];
} else if (!empty($contract->fields['date']) && $contract->fields['date'] != 'NULL') {
$date_creation = $contract->fields['date'];
} else {
$date_creation = $begin_date;
}
if (!empty($date_creation)) {
$date_creation = date('Y-m-d H:i:s', strtotime($date_creation));
}
$project_data = [
// data from contract
'name' => $contract->input['name'],
'entities_id' => $contract->fields['entities_id'],
'is_recursive' => $contract->fields['is_recursive'],
'content' => addslashes($contract->fields['comment']),
'date' => $date_creation,
'date_mod' => $date_creation,
'date_creation' => $date_creation,
'plan_start_date' => $begin_date,
// standard data to bootstrap project
'comment' => '',
'code' => '',
'priority' => 3,
'projectstates_id' => 0,
'projecttypes_id' => 0,
'users_id' => 0,
'groups_id' => 0,
'plan_end_date' => '',
'real_start_date' => '',
'real_end_date' => '',
'percent_done' => 0,
'show_on_global_gantt' => 0,
'is_deleted' => 0,
'projecttemplates_id' => 0,
'is_template' => 0,
'template_name' => '',
];
$state_in_progress_value = PluginProjectbridgeState::getProjectStateIdByStatus('in_progress');
if (empty($state_in_progress_value)) {
Session::addMessageAfterRedirect(__('The correspondence for the status "In progress" has not been defined. The project could not be created.', 'projectbridge'), false, ERROR);
return false;
}
// create the project
$project = new Project();
$project_id = $project->add($project_data);
if ($project_id) {
$bridge_data = [
'contract_id' => $contract->getId(),
'project_id' => $project_id,
'nb_hours' => $nb_hours,
];
// link the project to the contract
$bridge_contract = new PluginProjectbridgeContract($contract);
$bridge_contract->add($bridge_data);
$project_task_data = [
// data from contract
'name' => date('Y-m'),
'entities_id' => $contract->fields['entities_id'],
'is_recursive' => $contract->fields['is_recursive'],
'projects_id' => $project_id,
'content' => addslashes($contract->fields['comment']),
'plan_start_date' => $begin_date,
'plan_end_date' => (
!empty($begin_date) && !empty($contract->fields['duration']) ? date('Y-m-d H:i:s', strtotime(
Infocom::getWarrantyExpir($begin_date, $contract->fields['duration']) . ' - 1 day'
)) : ''
),
'planned_duration' => $nb_hours * 3600, // in seconds
'projectstates_id' => $state_in_progress_value, // "in progress"
// standard data to bootstrap task
'projecttasktemplates_id' => 0,
'projecttasks_id' => 0,
'projecttasktypes_id' => 0,
'percent_done' => 0,
'is_milestone' => 0,
'real_start_date' => '',
'real_end_date' => '',
'effective_duration' => 0,
'comment' => '',
];
// create the project's task
$project_task = new ProjectTask();
$project_task->add($project_task_data);
return true;
}
}
}
/**
* Hook called before the update of a ticket
* If possible, link the ticket to the project task of the entity's default contract
* If requested link the ticket to a specific project's task and set the project as default
*
* @param Ticket $ticket
* @return void
*/
function plugin_projectbridge_ticket_update(Ticket $ticket) {
$update_val = $ticket->input['update'] ?? $ticket->input['_update'] ?? null;
$isCreate = (array_key_exists('_add', $ticket->input) || !array_key_exists('id', $ticket->input) || (array_key_exists('id', $ticket->input) && $ticket->input['id'] == 0) ) ?? false;
if ($update_val == __('Make the connection', 'projectbridge') && !empty($ticket->input['projectbridge_project_id'])) {
$is_project_link_update = true;
$contract_id = null;
} else {
$is_project_link_update = false;
$entity = new Entity();
$entity->getFromDB($ticket->fields['entities_id']);
$bridge_entity = new PluginProjectbridgeEntity($entity);
$contract_id = $bridge_entity->getContractId();
}
if (array_key_exists('projectbridge_contract_id', $_POST)) {
$contract_id = $_POST['projectbridge_contract_id'];
}
// test if contrat already associate to the ticket
$haveAlreadyContractAssociate = false;
$bridge_ticket = new PluginProjectbridgeTicket($ticket);
if ($bridge_ticket->getProjectId() > 0) {
$haveAlreadyContractAssociate = true;
}
// get ticket status
$ticketStatus = $ticket->getField('status');
if (($ticketStatus != Ticket::CLOSED || $isCreate) && ($is_project_link_update || ($contract_id && !$haveAlreadyContractAssociate))) {
// default contract for the entity found or update
if (!$is_project_link_update) {
$contract = new Contract();
$contract->getFromDB($contract_id);
$contract_bridge = new PluginProjectbridgeContract($contract);
$project_id = $contract_bridge->getProjectId();
} else {
$project_id = (int) $ticket->input['projectbridge_project_id'];
}
if ($project_id && PluginProjectbridgeContract::getProjectTaskOject($project_id)) {
// project linked to contract found & task exists
PluginProjectbridgeTicket::deleteProjectLinks($ticket->getId());
$task_id = PluginProjectbridgeContract::getProjectTaskFieldValue($project_id, false, 'id');
// link the task to the ticket
$project_task_link_ticket = new ProjectTask_Ticket();
$project_task_link_ticket->add([
'projecttasks_id' => $task_id,
'tickets_id' => $ticket->getId(),
]);
$bridge_ticket = new PluginProjectbridgeTicket($ticket);
if ($is_project_link_update) {
if ($bridge_ticket->getProjectId() > 0) {
$bridge_ticket->update([
'id' => $bridge_ticket->getId(),
'projecttasks_id' => $task_id,
]);
} else {
$bridge_ticket->add([
'ticket_id' => $ticket->getId(),
'projecttasks_id' => $task_id,
]);
}
} else {
$bridge_ticket->add([
'ticket_id' => $ticket->getId(),
'projecttasks_id' => $task_id,
]);
}
}
}
}
/**
* Hook called after the creation of a ticket task
* If possible, update the linked project task's progress percentage
*
* @param TicketTask $ticket_task
* @return void
*/
function plugin_projectbridge_ticketask_add(TicketTask $ticket_task) {
if (isset($ticket_task->fields['actiontime'])) {
updateProjectTaskProgressPercent($ticket_task);
}
}
/**
* Hook called before the update of a ticket task
* If possible, update the linked project task's progress percentage
*
* @param TicketTask $ticket_task
* @return void
*/
function plugin_projectbridge_ticketask_update(TicketTask $ticket_task) {
if (isset($ticket_task->fields['actiontime']) && isset($ticket_task->input['actiontime'])) {
//$timediff = $ticket_task->input['actiontime'] - $ticket_task->fields['actiontime'];
updateProjectTaskProgressPercent($ticket_task);
}
}
/**
* this function update the progessPercent of processTask when a ticketTask is add or update with time associate.
* @param TicketTask $ticket_task
*/
function updateProjectTaskProgressPercent(TicketTask $ticket_task) {
// search if entry exist for the associate ticket
$ticketId = $ticket_task->fields['tickets_id'];
$bridge_ticket = new PluginProjectbridgeTicket();
$results = $bridge_ticket->find(['ticket_id' => $ticketId]);
foreach ($results as $result) {
if (is_array($result) && $result['projecttasks_id'] > 0) {
$projectTask = new ProjectTask();
$projectTask->getFromDB($result['projecttasks_id']);
$project_id = $projectTask->fields['projects_id'];
$pluginProjectbridgeContract = new PluginProjectbridgeContract();
$pluginProjectbridgeContracts = $pluginProjectbridgeContract->find(['project_id' => $project_id]);
foreach ($pluginProjectbridgeContracts as $pgc) {
$contract_id = $pgc['contract_id'];
PluginProjectbridgeTask::updateProjectTaskProgressPercent($result['projecttasks_id'], $contract_id);
}
}
}
}
/**
* Hook called after showing a tab
*
* @param array $tab_data
* @return void
*/
function plugin_projectbridge_post_show_tab(array $tab_data) {
if (!empty($tab_data['item']) && is_object($tab_data['item']) && !empty($tab_data['options']['itemtype'])) {
if ($tab_data['options']['itemtype'] == 'Projecttask_Ticket' || $tab_data['options']['itemtype'] == 'ProjectTask_Ticket') {
if ($tab_data['item'] instanceof Ticket) {
// add a line to allow linking ticket to a project task
PluginProjectbridgeTicket::postShow($tab_data['item']);
} else if ($tab_data['item'] instanceof ProjectTask) {
// add data to the list of tickets linked to a project task
PluginProjectbridgeTicket::postShowTask($tab_data['item']);
}
} else if ($tab_data['options']['itemtype'] == 'ProjectTask' && $tab_data['item'] instanceof Project) {
// add a link to the linked contract after showing the list of tasks in a project
PluginProjectbridgeContract::postShowProject($tab_data['item']);
// customize the duration columns
PluginProjectbridgeTask::customizeDurationColumns($tab_data['item']);
}
}
}
/**
* Add new search options
*
* @param string $itemtype
* @return array
*/
function plugin_projectbridge_getAddSearchOptionsNew($itemtype) {
$options = [];
switch ($itemtype) {
case 'Entity':
$options[] = [
'id' => 4200,
'name' => 'ProjectBridge',
];
$options[] = [
'id' => 4201,
'table' => PluginProjectbridgeEntity::$table_name,
// trick GLPI search into thinking we want the contract id so the addSelect function is called
'field' => 'contract_id',
'name' => __('Default contract', 'projectbridge'),
'massiveaction' => false,
];
$options[] = [
'id' => 4202,
'table' => PluginProjectbridgeTicket::$table_name,
'field' => 'project_id',
'name' => __('Time not affected to a project task (hours)', 'projectbridge'),
'massiveaction' => false,
];
break;
case 'Ticket':
$options[] = [
'id' => 4210,
'name' => 'ProjectBridge',
];
// $options[] = [
// 'id' => 4211,
// 'table' => PluginProjectbridgeTicket::$table_name,
// 'field' => 'project_id',
// 'name' => 'Projet',
// 'massiveaction' => false,
// ];
$options[] = [
'id' => 4212,
'table' => PluginProjectbridgeTicket::$table_name,
'field' => 'project_id',
'name' => __('Project tasks', 'projectbridge'),
'massiveaction' => false,
'datatype' => 'text'
];
$options[] = [
'id' => 4213,
'table' => PluginProjectbridgeTicket::$table_name,
'field' => 'project_id',
'name' => __('ProjectTask status', 'projectbridge'),
'massiveaction' => false,
];
$options[] = [
'id' => 4214,
'table' => PluginProjectbridgeTicket::$table_name,
'field' => 'project_id',
'name' => __('Is linked to a project task', 'projectbridge') . ' ?',
'massiveaction' => false,
'datatype' => 'bool'
];
$options[] = [
'id' => 4231,
'table' => PluginProjectbridgeTicket::$table_name,
'field' => 'project_id',
'name' => __('Effective duration (hours)', 'projectbridge'),
'massiveaction' => false,
'datatype' => 'decimal',
];
break;
case 'Contract':
$options[] = [
'id' => 4220,
'name' => 'ProjectBridge',
];
$options[] = [
'id' => 4221,
'table' => PluginProjectbridgeContract::$table_name,
'field' => 'project_id',
'name' => __('Project name', 'projectbridge'),
'massiveaction' => false,
];
$options[] = [
'id' => 4222,
'table' => PluginProjectbridgeContract::$table_name,
'field' => 'project_id',
'name' => __('ProjectBridge project tasks', 'projectbridge'),
'massiveaction' => false,
];
break;
case 'projecttask':
$options[] = [
'id' => 4230,
'name' => 'ProjectBridge',
];
$options[] = [
'id' => 4231,
'table' => PluginProjectbridgeTicket::$table_name,
'field' => 'project_id',
'name' => __('Effective duration (hours)', 'projectbridge'),
'massiveaction' => false,
];
$options[] = [
'id' => 4232,
'table' => PluginProjectbridgeTicket::$table_name,
'field' => 'project_id',
'name' => __('Planned duration (hours)', 'projectbridge'),
'massiveaction' => false,
];
$options[] = [
'id' => 4233,
'table' => PluginProjectbridgeTicket::$table_name,
'field' => 'project_id',
'name' => __('Last project task ?', 'projectbridge'),
'massiveaction' => false,
];
$options[] = [
'id' => 4234,
'table' => PluginProjectbridgeTicket::$table_name,
'field' => 'project_id',
'name' => __('Project status', 'projectbridge'),
'massiveaction' => false,
];
$options[] = [
'id' => 4235,
'table' => PluginProjectbridgeTicket::$table_name,
'field' => 'project_id',
'name' => __('Associate tickets', 'projectbridge'),
'massiveaction' => false,
];
$options[] = [
'id' => 4236,
'table' => PluginProjectbridgeTicket::$table_name,
'field' => 'project_id',
'name' => __('Comsuption', 'projectbridge'),
'massiveaction' => false,
];
break;
case 'Project':
$options[] = [
'id' => 4230,
'name' => 'ProjectBridge',
];
$options[] = [
'id' => 4231,
'table' => PluginProjectbridgeContract::$table_name,
'field' => 'project_id',
'name' => __('Number of project tasks tickets', 'projectbridge'),
'massiveaction' => false,
];
break;
default:
// nothing to do
}
return $options;
}
/**
* Add a custom select part to search
*
* @param string $itemtype
* @param string $key
* @param integer $offset
* @return string
*/
function plugin_projectbridge_addSelect($itemtype, $key, $offset) {
global $CFG_GLPI;
$select = "";
$onlypublicTasks = false;
if (!Session::haveRight("task", CommonITILTask::SEEPRIVATE) || PluginProjectbridgeConfig::getConfValueByName('CountOnlyPublicTasks')) {
$onlypublicTasks = true;
}
switch ($itemtype) {
case 'Entity':
if ($key == 4201) {
$contract_link = rtrim($CFG_GLPI['root_doc'], '/') . '/front/contract.form.php?id=';
$select = "
(CASE
WHEN `" . PluginProjectbridgeEntity::$table_name . "`.`contract_id` IS NOT NULL
THEN CONCAT(
'<!--',
`glpi_contracts`.`name`,
'-->',
'<a href=\"" . $contract_link . "',
`" . PluginProjectbridgeEntity::$table_name . "`.`contract_id`,
'\">',
`glpi_contracts`.`name`,
'</a>'
)
ELSE
NULL
END)
AS `ITEM_" . $offset . "`,
";
} else if ($key == 4202) {
// url to ticket search for tickets in the entity that are not linked to a task
$ticket_search_link = rtrim($CFG_GLPI['root_doc'], '/') . '/front/ticket.php?is_deleted=0&criteria[0][field]=4214&criteria[0][searchtype]=equals&criteria[0][value]=0&criteria[1][link]=AND&criteria[1][field]=80&criteria[1][searchtype]=equals&criteria[1][value]=';
$select = "
CONCAT(
'<!--',
COALESCE(
ROUND(`unlinked_ticket_actiontimes`.`actiontime_sum`, 2),
0
),
'-->',
'<a href=\"" . $ticket_search_link . "',
`glpi_entities`.`id`,
'\">',
COALESCE(
ROUND(`unlinked_ticket_actiontimes`.`actiontime_sum`, 2),
0
),
'</a>'
)
AS `ITEM_" . $offset . "`,
";
}
break;
case 'Ticket':
if ($key == 4211) {
// project name
$project_link = rtrim($CFG_GLPI['root_doc'], '/') . '/front/project.form.php?id=';
$select = "
GROUP_CONCAT(
DISTINCT CONCAT(
'<!--',
`glpi_projects`.`name`,
'-->',
'<a href=\"" . $project_link . "',
`glpi_projects`.`id`,
'\">',
`glpi_projects`.`name`,
'</a>'
)
SEPARATOR '$$##$$'
)
AS `ITEM_" . $offset . "`,
";
} else if ($key == 4212) {
// project task
$task_link = rtrim($CFG_GLPI['root_doc'], '/') . '/front/projecttask.form.php?id=';
$select = "
GROUP_CONCAT(
DISTINCT CONCAT(
'<!--',
`glpi_projecttasks`.`name`,
'-->',
'<a href=\"" . $task_link . "',
`glpi_projecttasks`.`id`,
'\">',
`glpi_projecttasks`.`name`,
'</a>'
)
SEPARATOR '$$##$$'
)
AS `ITEM_" . $offset . "`,
";