-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathtask.php
128 lines (112 loc) · 4.3 KB
/
task.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
<?php
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 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/>.
*
* ---------------------------------------------------------------------
*/
/**
* @since 9.1
*/
use Glpi\Exception\Http\BadRequestHttpException;
header("Content-Type: application/json; charset=UTF-8");
Html::header_nocache();
// Mandatory parameter: tasktemplates_id
$tasktemplates_id = $_POST['tasktemplates_id'] ?? null;
if ($tasktemplates_id === null) {
throw new BadRequestHttpException("Missing or invalid parameter: 'tasktemplates_id'");
} else if ($tasktemplates_id == 0) {
// Reset form
echo json_encode([
'content' => ""
]);
return;
}
// Mandatory parameter: items_id
$parents_id = $_POST['items_id'] ?? 0;
if (!$parents_id) {
throw new BadRequestHttpException("Missing or invalid parameter: 'items_id'");
}
// Mandatory parameter: itemtype
$parents_itemtype = $_POST['itemtype'] ?? '';
if (empty($parents_itemtype) || !is_subclass_of($parents_itemtype, CommonITILObject::class)) {
throw new BadRequestHttpException("Missing or invalid parameter: 'itemtype'");
}
// Load task template
$template = new TaskTemplate();
if (!$template->getFromDB($tasktemplates_id)) {
throw new BadRequestHttpException("Unable to load template: $tasktemplates_id");
}
// Load parent item
$parent = new $parents_itemtype();
if (!$parent->getFromDB($parents_id)) {
throw new BadRequestHttpException("Unable to load parent item: $parents_itemtype $parents_id");
}
// Render template content using twig
$template->fields['content'] = $template->getRenderedContent($parent);
//load taskcategorie name (use to create OPTION dom)
//need when template is used and when GLPI preselected type if defined
$template->fields['taskcategories_name'] = "";
if ($template->fields['taskcategories_id']) {
$entityRestrict = getEntitiesRestrictCriteria(getTableForItemType(TaskCategory::getType()), "", $parent->fields['entities_id'], true);
$taskcategory = new TaskCategory();
if (
$taskcategory->getFromDBByCrit([
"id" => $template->fields['taskcategories_id'],
] + $entityRestrict)
) {
$template->fields['taskcategories_name'] = Dropdown::getDropdownName(
getTableForItemType(TaskCategory::getType()),
$template->fields['taskcategories_id'],
0,
true,
false,
//default value like "(id)" is the default behavior of GLPI when field 'name' is empty
"(" . $template->fields['taskcategories_id'] . ")"
);
}
}
if (($template->fields['pendingreasons_id'] ?? 0) > 0) {
$pendingReason = new PendingReason();
if ($pendingReason->getFromDB($template->fields['pendingreasons_id'])) {
$template->fields = array_merge($template->fields, [
'pendingreasons_name' => $pendingReason->fields['name'],
'followup_frequency' => $pendingReason->fields['followup_frequency'],
'followups_before_resolution' => $pendingReason->fields['followups_before_resolution'],
]);
}
}
if ($template->fields['groups_id_tech'] == 0) {
unset($template->fields['groups_id_tech']);
}
if ($template->fields['users_id_tech'] == 0) {
unset($template->fields['users_id_tech']);
}
// Return json response with the template fields
echo json_encode($template->fields);