-
Notifications
You must be signed in to change notification settings - Fork 18
/
hook.php
298 lines (263 loc) · 9.51 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
<?php
/**
* -------------------------------------------------------------------------
* Tag plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Tag.
*
* Tag 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.
*
* Tag 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 Tag. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2014-2023 by Teclib'.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/tag
* -------------------------------------------------------------------------
*/
// Plugin hook after *Uninstall*
function plugin_uninstall_after_tag($item)
{
$tagitem = new PluginTagTagItem();
$tagitem->deleteByCriteria([
'itemtype' => $item->getType(),
'items_id' => $item->getID()
]);
}
function plugin_datainjection_populate_tag()
{
/** @var array $INJECTABLE_TYPES */
global $INJECTABLE_TYPES;
$INJECTABLE_TYPES['PluginTagTagInjection'] = 'tag';
}
function plugin_tag_getAddSearchOptionsNew($itemtype)
{
if (!PluginTagTag::canView() || !PluginTagTag::canItemtype($itemtype)) {
return [];
}
$options = [
[
'id' => PluginTagTag::S_OPTION,
'table' => PluginTagTag::getTable(),
'field' => 'name',
'name' => PluginTagTag::getTypeName(2),
'datatype' => 'dropdown',
'searchtype' => ['equals','notequals','contains'],
'massiveaction' => false,
'forcegroupby' => true,
'joinparams' => [
'beforejoin' => [
'table' => 'glpi_plugin_tag_tagitems',
'joinparams' => [
'jointype' => 'itemtype_item'
]
]
]
]
];
if ($itemtype != 'AllAssets') {
$item = new $itemtype();
if ($item->isEntityAssign()) {
$options [] = [
'id' => (PluginTagTag::S_OPTION + 1),
'table' => PluginTagTag::getTable(),
'field' => 'name',
'name' => PluginTagTag::getTypeName(2) . " - " . __("Entity"),
'datatype' => 'string',
'searchtype' => 'contains',
'massiveaction' => false,
'forcegroupby' => true,
'usehaving' => true,
'joinparams' => [
'condition' => "AND 1=1", // to force distinct complex id than the previous option
'beforejoin' => [
'table' => 'glpi_plugin_tag_tagitems',
'joinparams' => [
'jointype' => 'itemtype_item',
'specific_itemtype' => 'Entity',
'beforejoin' => [
'table' => 'glpi_entities',
]
]
]
]
];
}
}
return $options;
}
function plugin_tag_giveItem($type, $field, $data, $num, $linkfield = "")
{
switch ($field) {
case PluginTagTag::S_OPTION:
case PluginTagTag::S_OPTION + 1:
$out = '<div class="tag_select select2-container" style="width: 100%;">
<div class="select2-choices no-negative-margin">';
$separator = '';
foreach ($data[$num] as $tag) {
if (isset($tag['id']) && isset($tag['name'])) {
$out .= PluginTagTag::getSingleTag($tag['id'], $separator);
//For export (CSV, PDF) of GLPI core
$separator = '<span style="display:none">, </span>';
}
}
$out .= '</div></div>';
return $out;
}
return "";
}
function plugin_tag_addHaving($link, $nott, $itemtype, $id, $val, $num)
{
$searchopt = &Search::getOptions($itemtype);
$table = $searchopt[$id]["table"];
$field = $searchopt[$id]["field"];
if ($table . "." . $field == "glpi_plugin_tag_tags.type_menu") {
$values = explode(",", $val);
$where = "$link `ITEM_$num` LIKE '%" . $values[0] . "%'";
array_shift($values);
foreach ($values as $value) {
$value = trim($value);
$where .= " OR `ITEM_$num` LIKE '%$value%'";
}
return $where;
}
}
function plugin_tag_addWhere($link, $nott, $itemtype, $id, $val, $searchtype)
{
$searchopt = &Search::getOptions($itemtype);
$table = $searchopt[$id]["table"];
$field = $searchopt[$id]["field"];
if ($table . "." . $field == "glpi_plugin_tag_tags.type_menu") {
switch ($searchtype) {
case 'equals':
return "`glpi_plugin_tag_tags`.`type_menu` LIKE '%\"$val\"%'";
case 'notequals':
return "`glpi_plugin_tag_tags`.`type_menu` NOT LIKE '%\"$val\"%'";
}
}
return "";
}
/**
* Define Dropdown managed in GLPI
*
* @return array the list of dropdowns (label => class)
*/
function plugin_tag_getDropdown()
{
return ['PluginTagTag' => PluginTagTag::getTypeName(2)];
}
/**
* Define massive actions for other itemtype
*
* @param string $itemtype
* @return array the massive action list
*/
function plugin_tag_MassiveActions($itemtype = '')
{
if (PluginTagTag::canItemtype($itemtype) && is_a($itemtype, CommonDBTM::class, true) && $itemtype::canUpdate()) {
return [
'PluginTagTagItem' . MassiveAction::CLASS_ACTION_SEPARATOR . 'addTag'
=> __("Add tags", 'tag'),
'PluginTagTagItem' . MassiveAction::CLASS_ACTION_SEPARATOR . 'removeTag'
=> __("Remove tags", 'tag'),
];
}
return [];
}
/**
* Plugin install process
*
* @return boolean
*/
function plugin_tag_install()
{
$version = plugin_version_tag();
$migration = new Migration($version['version']);
// Parse inc directory
foreach (glob(dirname(__FILE__) . '/inc/*') as $filepath) {
// Load *.class.php files and get the class name
if (preg_match("/inc.(.+)\.class.php/", $filepath, $matches)) {
$classname = 'PluginTag' . ucfirst($matches[1]);
// Don't load Datainjection mapping lass (no install + bug if datainjection is not installed and activated)
if ($classname == 'PluginTagTaginjection') {
continue;
}
include_once($filepath);
// If the install method exists, load it
if (method_exists($classname, 'install')) {
$classname::install($migration);
}
}
}
return true;
}
/**
* Plugin uninstall process
*
* @return boolean
*/
function plugin_tag_uninstall()
{
// Parse inc directory
foreach (glob(dirname(__FILE__) . '/inc/*') as $filepath) {
// Load *.class.php files and get the class name
if (preg_match("/inc.(.+)\.class.php/", $filepath, $matches)) {
$classname = 'PluginTag' . ucfirst($matches[1]);
// Don't load Datainjection mapping lass (no uninstall + bug if datainjection is not installed and activated)
if ($classname == 'PluginTagTaginjection') {
continue;
}
include_once($filepath);
// If the uninstall method exists, load it
if (method_exists($classname, 'uninstall')) {
$classname::uninstall();
}
}
}
return true;
}
function plugin_tag_post_init()
{
/** @var array $PLUGIN_HOOKS */
global $PLUGIN_HOOKS;
// hook on object changes
if ($itemtype = PluginTagTag::getCurrentItemtype()) {
if (PluginTagTag::canItemtype($itemtype)) {
$PLUGIN_HOOKS['item_add']['tag'][$itemtype] = ['PluginTagTagItem', 'updateItem'];
$PLUGIN_HOOKS['pre_item_update']['tag'][$itemtype] = ['PluginTagTagItem', 'updateItem'];
$PLUGIN_HOOKS['pre_item_purge']['tag'][$itemtype] = ['PluginTagTagItem', 'purgeItem'];
}
}
// Always define hook for tickets
// Needed for rules to function properly when a ticket is created from a mail
// collector
$PLUGIN_HOOKS['item_add']['tag'][Ticket::getType()] = ['PluginTagTagItem', 'updateItem'];
$PLUGIN_HOOKS['pre_item_update']['tag'][Ticket::getType()] = ['PluginTagTagItem', 'updateItem'];
$PLUGIN_HOOKS['pre_item_purge']['tag'][Ticket::getType()] = ['PluginTagTagItem', 'purgeItem'];
}
function plugin_tag_getRuleActions($params = [])
{
$actions = [];
switch ($params['rule_itemtype']) {
case "RuleTicket":
$actions['_plugin_tag_tag_values'] = [
'name' => __("Add tags", 'tag'),
'type' => 'dropdown',
'table' => PluginTagTag::getTable(),
'condition' => ['type_menu' => ['LIKE', '%\"Ticket\"%']],
];
break;
}
return $actions;
}