forked from pluginsGLPI/formcreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.php
459 lines (402 loc) · 15.3 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
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator 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 2 of the License, or
* (at your option) any later version.
*
* Formcreator 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 © 2011 - 2020 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/
/**
* Install all necessary elements for the plugin
* @return boolean True if success
*/
function plugin_formcreator_install() {
spl_autoload_register('plugin_formcreator_autoload');
$version = plugin_version_formcreator();
$migration = new Migration($version['version']);
require_once(__DIR__ . '/install/install.php');
$install = new PluginFormcreatorInstall();
if (!$install->isPluginInstalled()
|| isset($_SESSION['plugin_formcreator']['cli'])
&& $_SESSION['plugin_formcreator']['cli'] == 'force-install') {
return $install->install($migration);
}
return $install->upgrade($migration);
}
/**
* Uninstall previously installed elements of the plugin
*
* @return boolean True if success
*/
function plugin_formcreator_uninstall() {
require_once(__DIR__ . '/install/install.php');
$install = new PluginFormcreatorInstall();
$install->uninstall();
}
/**
* Define Dropdown tables to be manage in GLPI :
*/
function plugin_formcreator_getDropdown() {
return [
'PluginFormcreatorCategory' => _n('Form category', 'Form categories', 2, 'formcreator'),
];
}
function plugin_formcreator_addDefaultSelect($itemtype) {
switch ($itemtype) {
case PluginFormcreatorIssue::class:
return "`glpi_plugin_formcreator_issues`.`sub_itemtype`, ";
}
return "";
}
function plugin_formcreator_addDefaultJoin($itemtype, $ref_table, &$already_link_tables) {
$join = "";
switch ($itemtype) {
case PluginFormcreatorIssue::class:
$join = Search::addDefaultJoin("Ticket", "glpi_tickets", $already_link_tables);
$join = str_replace('`glpi_tickets`.`id`', '`glpi_plugin_formcreator_issues`.`original_id`', $join);
$join = str_replace('`glpi_tickets`', '`glpi_plugin_formcreator_issues`', $join);
$join = str_replace('`users_id_recipient`', '`requester_id`', $join);
}
return $join;
}
function plugin_formcreator_canValidate() {
return Session::haveRight('ticketvalidation', TicketValidation::VALIDATEINCIDENT)
|| Session::haveRight('ticketvalidation', TicketValidation::VALIDATEREQUEST);
}
function plugin_formcreator_getCondition($itemtype) {
$table = $itemtype::getTable();
if ($itemtype == PluginFormcreatorFormAnswer::class) {
if (Session::haveRight('config', UPDATE)) {
return "";
}
if (plugin_formcreator_canValidate()) {
$groupUser = new Group_User();
$groups = $groupUser->getUserGroups($_SESSION['glpiID']);
$condition = " (`$table`.`users_id_validator` =". $_SESSION['glpiID'];
if (count($groups) < 1) {
$condition .= ")";
} else {
$groupIDs = [];
foreach ($groups as $group) {
$groupIDs[] = $group['id'];
}
$groupIDs = implode(',', $groupIDs);
$condition .= " OR `$table`.`groups_id_validator` IN ($groupIDs) )";
}
return $condition;
}
}
return " `$table`.`requester_id` = " . $_SESSION['glpiID'];
}
/**
* Define specific search request
*
* @param String $itemtype Itemtype for the search engine
* @return String Specific search request
*/
function plugin_formcreator_addDefaultWhere($itemtype) {
$condition = '';
switch ($itemtype) {
case PluginFormcreatorIssue::class:
$condition = Search::addDefaultWhere(Ticket::class);
$condition = str_replace('`glpi_tickets`', '`glpi_plugin_formcreator_issues`', $condition);
$condition = str_replace('`users_id_recipient`', '`requester_id`', $condition);
$condition = "($condition OR `glpi_plugin_formcreator_issues`.`validator_id` = '" . Session::getLoginUserID() . "')";
break;
case PluginFormcreatorFormAnswer::class:
if (isset($_SESSION['formcreator']['form_search_answers'])
&& $_SESSION['formcreator']['form_search_answers']) {
// Context is displaying the answers for a given form
$table = $itemtype::getTable();
$formFk = PluginFormcreatorForm::getForeignKeyField();
$condition = "`$table`.`$formFk` = ".
$_SESSION['formcreator']['form_search_answers'];
} else {
$condition = plugin_formcreator_getCondition($itemtype);
}
break;
}
return $condition;
}
function plugin_formcreator_addLeftJoin($itemtype, $ref_table, $new_table, $linkfield, &$already_link_tables) {
$join = "";
switch ($itemtype) {
case PluginFormcreatorIssue::class:
if ($new_table == 'glpi_ticketvalidations') {
foreach ($already_link_tables as $table) {
if (strpos($table, $new_table) === 0) {
$AS = $table;
}
}
$join = " LEFT JOIN `$new_table` AS `$AS` ON (`$ref_table`.`tickets_id` = `$AS`.`tickets_id`) ";
}
if ($new_table == 'glpi_groups') {
foreach ($already_link_tables as $table) {
if (strpos($table, $new_table) === 0) {
$AS = $table;
}
}
$join = " LEFT JOIN `$new_table` AS `$AS` ON (`glpi_groups_tickets_original_id`.`groups_id` = `$AS`.`id`) ";
}
if ($new_table == 'glpi_users' && $linkfield == 'users_id') {
foreach ($already_link_tables as $table) {
if (strpos($table, $new_table) === 0) {
$AS = $table;
}
}
$join = " LEFT JOIN `$new_table` AS `$AS` ON (`glpi_tickets_users_original_id`.`users_id` = `$AS`.`id`) ";
}
break;
}
return $join;
}
function plugin_formcreator_addWhere($link, $nott, $itemtype, $ID, $val, $searchtype) {
$searchopt = &Search::getOptions($itemtype);
$table = $searchopt[$ID]["table"];
$field = $searchopt[$ID]["field"];
switch ($table.".".$field) {
case "glpi_plugin_formcreator_issues.status" :
$tocheck = [];
if ($item = getItemForItemtype($itemtype)) {
switch ($val) {
case 'all':
$tocheck = array_keys($item->getAllStatusArray());
break;
case Ticket::INCOMING:
$tocheck = $item->getNewStatusArray();
break;
case 'process' :
$tocheck = $item->getProcessStatusArray();
break;
case 'notclosed' :
$tocheck = $item->getAllStatusArray();
foreach ($item->getClosedStatusArray() as $status) {
unset($tocheck[$status]);
}
$tocheck = array_keys($tocheck);
break;
case 'old' :
$tocheck = array_merge($item->getSolvedStatusArray(),
$item->getClosedStatusArray());
break;
case 'notold' :
$tocheck = $item->getAllStatusArray();
foreach ($item->getSolvedStatusArray() as $status) {
unset($tocheck[$status]);
}
foreach ($item->getClosedStatusArray() as $status) {
unset($tocheck[$status]);
}
unset($tocheck[PluginFormcreatorFormAnswer::STATUS_REFUSED]);
$tocheck = array_keys($tocheck);
break;
}
}
if (count($tocheck) == 0) {
$statuses = $item->getAllStatusArray();
if (isset($statuses[$val])) {
$tocheck = [$val];
}
}
if (count($tocheck)) {
if ($nott) {
return $link." `$table`.`$field` NOT IN ('".implode("','", $tocheck)."')";
}
return $link." `$table`.`$field` IN ('".implode("','", $tocheck)."')";
}
break;
}
}
function plugin_formcreator_AssignToTicket($types) {
$types[PluginFormcreatorFormAnswer::class] = PluginFormcreatorFormAnswer::getTypeName();
return $types;
}
function plugin_formcreator_MassiveActions($itemtype) {
switch ($itemtype) {
case PluginFormcreatorForm::class:
return [
'PluginFormcreatorForm' . MassiveAction::CLASS_ACTION_SEPARATOR . 'Duplicate' => _x('button', 'Duplicate'),
'PluginFormcreatorForm' . MassiveAction::CLASS_ACTION_SEPARATOR . 'Transfert' => __('Transfer'),
'PluginFormcreatorForm' . MassiveAction::CLASS_ACTION_SEPARATOR . 'Export' => _sx('button', 'Export'),
];
}
return [];
}
function plugin_formcreator_giveItem($itemtype, $ID, $data, $num) {
switch ($itemtype) {
case PluginFormcreatorIssue::class:
return PluginFormcreatorIssue::giveItem($itemtype, $ID, $data, $num);
break;
}
return "";
}
function plugin_formcreator_hook_add_ticket(CommonDBTM $item) {
global $CFG_GLPI;
if ($item instanceof Ticket) {
if (!isset($CFG_GLPI['plugin_formcreator_disable_hook_create_ticket'])) {
// run this hook only if the plugin is not generating tickets
$issue = new PluginFormcreatorIssue();
$issue->add([
'original_id' => $item->getID(),
'sub_itemtype' => 'Ticket',
'name' => addslashes($item->fields['name']),
'status' => $item->fields['status'],
'date_creation' => $item->fields['date'],
'date_mod' => $item->fields['date_mod'],
'entities_id' => $item->fields['entities_id'],
'is_recursive' => '0',
'requester_id' => $item->fields['users_id_recipient'],
'validator_id' => '0',
'comment' => addslashes($item->fields['content']),
]);
}
}
}
function plugin_formcreator_hook_update_ticket(CommonDBTM $item) {
if ($item instanceof Ticket) {
$id = $item->getID();
$issue = new PluginFormcreatorIssue();
$issue->getFromDBByCrit([
'AND' => [
'sub_itemtype' => Ticket::class,
'original_id' => $id
]
]);
$issue->update([
'id' => $issue->getID(),
'original_id' => $id,
'display_id' => "t_$id",
'sub_itemtype' => 'Ticket',
'name' => addslashes($item->fields['name']),
'status' => $item->fields['status'],
'date_creation' => $item->fields['date'],
'date_mod' => $item->fields['date_mod'],
'entities_id' => $item->fields['entities_id'],
'is_recursive' => '0',
'requester_id' => $item->fields['users_id_recipient'],
'validator_id' => '0',
'comment' => addslashes($item->fields['content']),
]);
}
}
function plugin_formcreator_hook_delete_ticket(CommonDBTM $item) {
global $DB;
if ($item instanceof Ticket) {
$id = $item->getID();
// mark formanswers as deleted
$iterator = $DB->request([
'SELECT' => ['id'],
'FROM' => Item_Ticket::getTable(),
'WHERE' => [
'itemtype' => 'PluginFormcreatorFormAnswer',
'tickets_id' => $id,
]
]);
foreach ($iterator as $row) {
$form_answer = new PluginFormcreatorFormAnswer();
$form_answer->update([
'id' => $row['id'],
'is_deleted' => 1,
]);
}
// delete issue
$issue = new PluginFormcreatorIssue();
$issue->deleteByCriteria([
'display_id' => "t_$id",
'sub_itemtype' => 'Ticket'
], 1);
}
}
function plugin_formcreator_hook_restore_ticket(CommonDBTM $item) {
global $DB;
if ($item instanceof Ticket) {
$id = $item->getID();
// Restore deletes form_answers
$iterator = $DB->request([
'SELECT' => ['id'],
'FROM' => Item_Ticket::getTable(),
'WHERE' => [
'itemtype' => 'PluginFormcreatorFormAnswer',
'tickets_id' => $id,
]
]);
foreach ($iterator as $row) {
$form_answer = new PluginFormcreatorFormAnswer();
$form_answer->update([
'id' => $row['id'],
'is_deleted' => 0,
]);
}
$issue = new PluginFormcreatorIssue();
$issue->add([
'original_id' => $item->getID(),
'sub_itemtype' => 'Ticket',
'name' => addslashes($item->fields['name']),
'status' => $item->fields['status'],
'date_creation' => $item->fields['date'],
'date_mod' => $item->fields['date_mod'],
'entities_id' => $item->fields['entities_id'],
'is_recursive' => '0',
'requester_id' => $item->fields['users_id_recipient'],
'validator_id' => '0',
'comment' => '',
]);
}
}
function plugin_formcreator_hook_purge_ticket(CommonDBTM $item) {
if ($item instanceof Ticket) {
$id = $item->getID();
$issue = new PluginFormcreatorIssue();
$issue->deleteByCriteria([
'display_id' => "t_$id",
'sub_itemtype' => 'Ticket'
], 1);
}
}
function plugin_formcreator_hook_pre_purge_targetTicket(CommonDBTM $item) {
$item->pre_purgeItem();
}
function plugin_formcreator_hook_pre_purge_targetChange(CommonDBTM $item) {
$item->pre_purgeItem();
}
function plugin_formcreator_dynamicReport($params) {
switch ($params['item_type']) {
case PluginFormcreatorFormAnswer::class;
if ($url = parse_url($_SERVER['HTTP_REFERER'])) {
if (strpos($url['path'],
Toolbox::getItemTypeFormURL("PluginFormcreatorForm")) !== false) {
parse_str($url['query'], $query);
if (isset($query['id'])) {
$item = new PluginFormcreatorForm;
$item->getFromDB($query['id']);
PluginFormcreatorFormAnswer::showForForm($item, $params);
return true;
}
}
}
break;
}
return false;
}