forked from initiativa/roundrobin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.php
More file actions
310 lines (274 loc) · 10 KB
/
Copy pathhook.php
File metadata and controls
310 lines (274 loc) · 10 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
<?php
/**
* -------------------------------------------------------------------------
* RoundRobin plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of RoundRobin GLPI Plugin.
*
* RoundRobin 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.
*
* RoundRobin 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 RoundRobin. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2022 by initiativa s.r.l. - http://www.initiativa.it
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/initiativa/roundrobin
* -------------------------------------------------------------------------
*/
// Load dependencies in correct order using __DIR__
require_once __DIR__ . '/inc/logger.class.php';
require_once __DIR__ . '/inc/config.class.php';
require_once __DIR__ . '/inc/RRAssignmentsEntity.class.php';
require_once __DIR__ . '/inc/IHookItemHandler.php';
require_once __DIR__ . '/inc/TicketHookHandler.class.php';
require_once __DIR__ . '/inc/ITILCategoryHookHandler.class.php';
/**
* Hook Item Handlers by Item Type
*/
function plugin_roundrobin_getHookHandlers() {
$HOOK_HANDLERS = [
'Ticket' => new PluginRoundRobinTicketHookHandler(),
'ITILCategory' => new PluginRoundRobinITILCategoryHookHandler()
];
return $HOOK_HANDLERS;
}
/**
* Merge RoundRobin assignee into Ticket input (GLPI 11 `_actors`).
*
* @param array<string,mixed> $input
* @param array{user_id:int, group_id:int|null} $assignmentData
* @return array<string,mixed>
*/
function plugin_roundrobin_merge_rr_into_ticket_input(array $input, array $assignmentData): array {
if (isset($input['_users_id_assign'])) {
unset($input['_users_id_assign']);
}
if (isset($input['_groups_id_assign'])) {
unset($input['_groups_id_assign']);
}
if (!isset($input['_actors'])) {
$input['_actors'] = [];
}
if (!isset($input['_actors']['assign'])) {
$input['_actors']['assign'] = [];
}
$input['_actors']['assign'][] = [
'itemtype' => 'User',
'items_id' => (int) $assignmentData['user_id'],
'use_notification' => 1,
];
$gid = $assignmentData['group_id'] ?? null;
if ($gid !== null && (int) $gid > 0) {
$input['_actors']['assign'][] = [
'itemtype' => 'Group',
'items_id' => (int) $gid,
'use_notification' => 1,
];
}
return $input;
}
/**
* Install hook
*
* @return boolean
*/
function plugin_roundrobin_install() {
global $DB;
PluginRoundRobinLogger::addDebug(__FUNCTION__ . ' - entered...');
try {
$rrAssignmentsEntity = new PluginRoundRobinRRAssignmentsEntity();
/**
* create setting table
*/
$rrAssignmentsEntity->init();
return true;
} catch (Exception $e) {
PluginRoundRobinLogger::addError(__FUNCTION__ . ' - Error: ' . $e->getMessage());
return false;
}
}
/**
* Upgrade hook — required so GLPI updates `glpi_plugins.version` when code is newer than the DB.
*
* @param string $currentversion Previously installed version from the database.
*
* @return boolean
*/
function plugin_roundrobin_upgrade($currentversion) {
PluginRoundRobinLogger::addDebug(__FUNCTION__ . ' - from version: ' . (string) $currentversion);
try {
$rrAssignmentsEntity = new PluginRoundRobinRRAssignmentsEntity();
$rrAssignmentsEntity->init();
return true;
} catch (Exception $e) {
PluginRoundRobinLogger::addError(__FUNCTION__ . ' - Error: ' . $e->getMessage());
return false;
}
}
/**
* Uninstall hook
*
* @return boolean
*/
function plugin_roundrobin_uninstall() {
PluginRoundRobinLogger::addDebug(__FUNCTION__ . ' - entered...');
try {
$rrAssignmentsEntity = new PluginRoundRobinRRAssignmentsEntity();
/**
* drop settings
*/
$rrAssignmentsEntity->cleanUp();
return true;
} catch (Exception $e) {
PluginRoundRobinLogger::addError(__FUNCTION__ . ' - Error: ' . $e->getMessage());
return false;
}
}
/**
* hook handlers
*/
/**
* pre item add - GLPI 11 compatible
* Uses _actors array for ticket assignment
*/
function plugin_roundrobin_hook_pre_item_add_handler(CommonDBTM $item) {
PluginRoundRobinLogger::addDebug(__FUNCTION__ . " - pre add item.");
if ($item->getType() !== 'Ticket') {
return true;
}
$categoryId = isset($item->input['itilcategories_id']) ? $item->input['itilcategories_id'] : null;
if ($categoryId !== null && $categoryId > 0) {
$handler = new PluginRoundRobinTicketHookHandler();
$assignmentData = $handler->findUserIdToAssign($categoryId, true);
if ($assignmentData !== null) {
$item->input = plugin_roundrobin_merge_rr_into_ticket_input($item->input, $assignmentData);
PluginRoundRobinLogger::addDebug(__FUNCTION__ . " - assigned user: " . $assignmentData['user_id']);
}
}
return true;
}
function plugin_roundrobin_hook_itil_item_add_handler(ITILCategory $category) {
PluginRoundRobinLogger::addDebug(
__FUNCTION__ . ' - ITILCategory id=' . (int) ($category->fields['id'] ?? 0)
. ' name=' . ($category->fields['name'] ?? '')
);
$handler = new PluginRoundRobinITILCategoryHookHandler();
$handler->itemAdded($category);
return $category;
}
/**
* When a ticket's ITIL category changes, apply Round Robin if the new category is enabled
* and the ticket has no existing assignee.
*/
function plugin_roundrobin_hook_pre_item_update_handler(CommonDBTM $item) {
PluginRoundRobinLogger::addDebug(__FUNCTION__ . ' - entered');
if ($item->getType() !== 'Ticket') {
return;
}
if (!isset($item->input['itilcategories_id'])) {
return;
}
$newCat = (int) $item->input['itilcategories_id'];
$oldCat = (int) ($item->fields['itilcategories_id'] ?? 0);
if ($newCat < 1 || $newCat === $oldCat) {
return;
}
$ticketId = (int) ($item->fields['id'] ?? 0);
if ($ticketId > 0 && class_exists('Ticket')) {
$t = new Ticket();
if ($t->getFromDB($ticketId) && method_exists($t, 'countUsers')) {
$assignType = defined('CommonITILActor::ASSIGN') ? CommonITILActor::ASSIGN : 2;
if ((int) $t->countUsers($assignType) > 0) {
PluginRoundRobinLogger::addDebug(__FUNCTION__ . ' - skip: ticket already has assignee(s)');
return;
}
}
}
$handler = new PluginRoundRobinTicketHookHandler();
$assignmentData = $handler->findUserIdToAssign($newCat, true);
if ($assignmentData === null) {
return;
}
$item->input = plugin_roundrobin_merge_rr_into_ticket_input($item->input, $assignmentData);
PluginRoundRobinLogger::addDebug(__FUNCTION__ . ' - assigned user: ' . $assignmentData['user_id']);
}
/**
* ITIL category updated: ensure row exists after restore; reset group rotation when group changes.
*/
function plugin_roundrobin_hook_itil_item_update_handler(CommonDBTM $item) {
if ($item->getType() !== 'ITILCategory') {
return $item;
}
$entity = new PluginRoundRobinRRAssignmentsEntity();
$cid = (int) ($item->fields['id'] ?? 0);
if ($cid < 1) {
return $item;
}
if (isset($item->input['is_deleted']) && (int) $item->input['is_deleted'] === 0) {
$entity->insertItilCategory($cid);
PluginRoundRobinLogger::addDebug(__FUNCTION__ . ' - restored category id=' . $cid);
return $item;
}
if (isset($item->input['groups_id'])) {
$oldG = (int) ($item->fields['groups_id'] ?? 0);
$newG = (int) $item->input['groups_id'];
if ($newG > 0 && $newG !== $oldG) {
$entity->resetGroupRotationIndex($newG);
PluginRoundRobinLogger::addDebug(__FUNCTION__ . ' - groups_id changed for category id=' . $cid . ' new_group=' . $newG);
}
}
return $item;
}
function plugin_roundrobin_hook_item_pre_update_handler(CommonDBTM $item) {
PluginRoundRobinLogger::addDebug(__FUNCTION__ . " - pre update item: " . print_r($item, true));
return $item;
}
/**
* Legacy debug stub (not registered). Ticket updates use {@see plugin_roundrobin_hook_pre_item_update_handler}.
*/
function plugin_roundrobin_hook_item_update_handler(CommonDBTM $item) {
PluginRoundRobinLogger::addDebug(__FUNCTION__ . ' - legacy stub, item type: ' . $item->getType());
return $item;
}
/**
* pre item delete
*/
function plugin_roundrobin_hook_pre_item_delete_handler(CommonDBTM $item) {
PluginRoundRobinLogger::addDebug(__FUNCTION__ . " - entered with item: " . print_r($item, true));
return $item;
}
/**
* item deleted
*/
function plugin_roundrobin_hook_item_delete_handler(CommonDBTM $item) {
PluginRoundRobinLogger::addDebug( __FUNCTION__ . " - item: " . print_r($item, true));
$HOOK_HANDLERS = plugin_roundrobin_getHookHandlers();
if (array_key_exists($item->getType(), $HOOK_HANDLERS)) {
$handler = $HOOK_HANDLERS[$item->getType()];
$handler->itemDeleted($item);
}
return $item;
}
/**
* item purged
*/
function plugin_roundrobin_hook_item_purge_handler(CommonDBTM $item) {
PluginRoundRobinLogger::addDebug(__FUNCTION__ . " - entered with item: " . print_r($item, true));
$HOOK_HANDLERS = plugin_roundrobin_getHookHandlers();
if (array_key_exists($item->getType(), $HOOK_HANDLERS)) {
$handler = $HOOK_HANDLERS[$item->getType()];
$handler->itemPurged($item);
}
return $item;
}