-
Notifications
You must be signed in to change notification settings - Fork 4
/
hook.php
340 lines (292 loc) · 13.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
<?php
/**
* ---------------------------------------------------------------------
* groupcategory is a plugin to customizes the list of accessible
* ticket categories for ticket requesters.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of groupcategory.
*
* groupcategory 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.
*
* groupcategory 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-groupcategory
* @link https://plugins.glpi-project.org/#/plugin/groupcategory
* ---------------------------------------------------------------------
*/
/**
* Install the plugin
*
* @return boolean
*/
function plugin_groupcategory_install()
{
global $DB;
if (!$DB->tableExists(getTableForItemType('PluginGroupcategoryGroupcategory'))) {
$create_table_query = "
CREATE TABLE IF NOT EXISTS `" . getTableForItemType('PluginGroupcategoryGroupcategory') . "`
(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`group_id` INT(11) NOT NULL,
`category_ids` TEXT NOT NULL,
PRIMARY KEY (`id`),
INDEX (`group_id`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
";
$DB->query($create_table_query) or die($DB->error());
}
return true;
}
/**
* Uninstall the plugin
*
* @return boolean
*/
function plugin_groupcategory_uninstall()
{
global $DB;
$tables_to_drop = [
getTableForItemType('PluginGroupcategoryGroupcategory'),
];
$drop_table_query = "DROP TABLE IF EXISTS `" . implode('`, `', $tables_to_drop) . "`";
return $DB->query($drop_table_query) or die($DB->error());
}
/**
* Hook callback when a group is shown
*
* @param Group $group
*/
function plugin_groupcategory_post_show_group(Group $group)
{
if ($group->getId() > 0) {
$categories = PluginGroupcategoryGroupcategory::getAllCategories();
$selected_categories = PluginGroupcategoryGroupcategory::getSelectedCategoriesForGroup($group);
$dom = '';
$dom .= '<div id="groupcategory_content">';
$dom .= '<table class="tab_cadre_fixe" >' . "\n";
$dom .= '<tbody>' . "\n";
$dom .= '<tr class="tab_bg_1">' . "\n";
$dom .= '<th colspan="2" class="subheader">';
$dom .= 'Catégories refusées';
$dom .= '</th>' . "\n";
$dom .= '<th colspan="2" class="subheader">';
$dom .= 'Catégories autorisées';
$dom .= '</th>' . "\n";
$dom .= '</tr>' . "\n";
$dom .= '<tr class="tab_bg_1">' . "\n";
$dom .= '<td colspan="2">' . "\n";
$dom .= '<input type="hidden" name="groupcategory_allowed_categories" id="groupcategory_allowed_categories_ids" value="' . implode(', ', array_keys($selected_categories)) . '" />';
$dom .= '<div>';
$dom .= '<input type="button" class="submit" id="groupcategory_allow_categories" value="Autoriser >" style="padding: 10px" />';
$dom .= '</div>' . "\n";
$dom .= '<select id="groupcategory_denied_categories" style="min-width: 150px; height: 150px; margin-top: 15px;" multiple>' . "\n";
foreach ($categories as $details) {
if (!isset($selected_categories[$details['id']])) {
$dom .= '<option value="' . $details['id'] . '">';
$dom .= $details['completename'];
$dom .= '</option>' . "\n";
}
}
$dom .= '</select>' . "\n";
$dom .= '</td>' . "\n";
$dom .= '<td colspan="2">' . "\n";
$dom .= '<div>';
$dom .= '<input type="button" class="submit" id="groupcategory_deny_categories" value="< Refuser" style="padding: 10px" />';
$dom .= '</div>' . "\n";
$dom .= '<select id="groupcategory_allowed_categories" style="min-width: 150px; height: 150px; margin-top: 15px;" multiple>' . "\n";
foreach ($selected_categories as $category_id => $completename) {
$dom .= '<option value="' . $category_id . '">';
$dom .= $completename;
$dom .= '</option>' . "\n";
}
$dom .= '</select>' . "\n";
$dom .= '</td>' . "\n";
$dom .= '</tbody>' . "\n";
$dom .= '</table>' . "\n";
$dom .= '</div>' . "\n";
echo $dom;
$js_block = '
var _groupcategory_content = $("#groupcategory_content");
$(_groupcategory_content.html()).detach().insertAfter("div#mainformtable table.tab_cadre_fixe");
_groupcategory_content.remove();
var _groupcategory_selected_categories = {
"denied": [],
"allowed": []
};
var _groupcategory_denied_categories = $("#groupcategory_denied_categories");
var _groupcategory_allowed_categories = $("#groupcategory_allowed_categories");
var _groupcategory_allowed_categories_ids_elm = $("#groupcategory_allowed_categories_ids");
var _groupcategory_allowed_categories_ids = [];
if (_groupcategory_allowed_categories_ids_elm.val()) {
_groupcategory_allowed_categories_ids = _groupcategory_allowed_categories_ids_elm.val().split(", ");
}
_groupcategory_denied_categories.on("change", function(e) {
var selection = $(this).val();
if (selection === null) {
selection = [];
}
_groupcategory_selected_categories.denied = selection;
});
_groupcategory_allowed_categories.on("change", function(e) {
var selection = $(this).val();
if (selection === null) {
selection = [];
}
_groupcategory_selected_categories.allowed = selection;
});
$("#groupcategory_allow_categories").on("click", function(e) {
if (_groupcategory_selected_categories.denied.length) {
var
current_category_id,
current_category_option
;
for (var i in _groupcategory_selected_categories.denied) {
current_category_id = _groupcategory_selected_categories.denied[i];
current_category_option = $("option[value=" + current_category_id + "]", _groupcategory_denied_categories);
_groupcategory_allowed_categories.append("<option value=\"" + current_category_id + "\">" + current_category_option.text() + "</option>");
current_category_option.remove();
_groupcategory_allowed_categories_ids.push(current_category_id);
}
_groupcategory_allowed_categories_ids_elm.val(_groupcategory_allowed_categories_ids.join(", "));
}
});
$("#groupcategory_deny_categories").on("click", function(e) {
if (_groupcategory_selected_categories.allowed.length) {
var
current_category_id,
current_category_option,
allowed_category_idx
;
for (var i in _groupcategory_selected_categories.allowed) {
current_category_id = _groupcategory_selected_categories.allowed[i];
current_category_option = $("option[value=" + current_category_id + "]", _groupcategory_allowed_categories);
_groupcategory_denied_categories.append("<option value=\"" + current_category_id + "\">" + current_category_option.text() + "</option>");
current_category_option.remove();
allowed_category_idx = _groupcategory_allowed_categories_ids.indexOf(current_category_id);
if (allowed_category_idx > -1) {
_groupcategory_allowed_categories_ids.splice(allowed_category_idx, 1);
}
}
_groupcategory_allowed_categories_ids_elm.val(_groupcategory_allowed_categories_ids.join(", "));
}
});
';
echo Html::scriptBlock($js_block);
}
}
/**
* Hook callback before a group is updated
*
* @param Group $group
*/
function plugin_groupcategory_group_update(Group $group)
{
if (isset($group->input['groupcategory_allowed_categories'])) {
$allowed_categories_ids = trim($group->input['groupcategory_allowed_categories']);
$selected_categories = PluginGroupcategoryGroupcategory::getSelectedCategoriesForGroup($group);
$selected_categories_ids = implode(', ', array_keys($selected_categories));
if ($allowed_categories_ids != $selected_categories_ids) {
$group_category = new PluginGroupcategoryGroupcategory();
$exists = $group_category->getFromDBByCrit(["group_id" => $group->getId()]);
$group_update_params = [
'group_id' => $group->getId(),
'category_ids' => $allowed_categories_ids,
];
if ($exists) {
$group_update_params['id'] = $group_category->getId();
$group_category->update($group_update_params, [], false);
} else {
$group_category->add($group_update_params, [], false);
}
}
}
}
/**
* Hook callback when a ticket is shown
*
* @param Ticket $ticket
*/
function plugin_groupcategory_post_show_ticket(Ticket $ticket)
{
global $CFG_GLPI;
$get_user_categories_url = PLUGIN_GROUPCATEGORY_WEB_DIR. '/ajax/get_user_categories.php';
$js_block = 'var requester_user_id = 0;';
$user_id = $_SESSION['glpiID'];
$js_block .= 'var requester_user_id = ' . $user_id . ';';
$js_block .= 'var glpi_csrf_token = \'' . Session::getNewCSRFToken() . '\';';
if ($_SESSION["glpiactiveprofile"]["interface"] == "central") {
$js_block .= '
var requester_user_id_input = $("select[id^=dropdown__users_id_requester]");
if (requester_user_id_input.length) {
var requester_user_id = parseInt(requester_user_id_input.val());
}
';
}
$selectedItilcategoriesId = '';
if (isset($ticket->fields['itilcategories_id'])) {
$selectedItilcategoriesId = $ticket->fields['itilcategories_id'];
}
if (isset($ticket->input['itilcategories_id'])) {
$selectedItilcategoriesId = $ticket->input['itilcategories_id'];
}
$js_block .= '
if (requester_user_id) {
loadAllowedCategories('.$selectedItilcategoriesId.');
}
function loadAllowedCategories(selectedItilcategoriesId) {
$.ajax("' . $get_user_categories_url . '", {
method: "POST",
cache: false,
data: {
requester_user_id: requester_user_id,
_glpi_csrf_token: glpi_csrf_token,
selectedItilcategoriesId : selectedItilcategoriesId
},
complete: function(responseObj, status) {
if ( status == "success" && responseObj.responseText.length)
{
try {
var allowed_categories = $.parseJSON(responseObj.responseText);
displayAllowedCategories(allowed_categories, selectedItilcategoriesId);
} catch (e) {
}
}
}
});
};
function displayAllowedCategories(allowed_categories, selectedItilcategoriesId) {
var category_container = $("#show_category_by_type");
domElementItilcategorieselement = $("select[name=itilcategories_id]");
idSelectItil = $("select[name=itilcategories_id]").attr(\'id\');
$("#"+idSelectItil).empty().select2({
data: allowed_categories,
width: "auto",
});
$("#"+idSelectItil).val(selectedItilcategoriesId) ;
if(selectedItilcategoriesId == 0){
$("#"+idSelectItil).select2("open");
}
};
$( document ).ajaxComplete(function( event, xhr, settings ) {
if ( settings.url === "/ajax/getDropdownValue.php" ) {
loadAllowedCategories('.$selectedItilcategoriesId.');
}
});
';
echo Html::scriptBlock($js_block);
}