forked from pluginsGLPI/tag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.php
235 lines (194 loc) · 7.63 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
<?php
// Plugin hook after *Uninstall*
function plugin_uninstall_after_tag($item) {
$tagitem = new PluginTagTagItem();
$tagitem->deleteByCriteria(array('itemtype' => $item->getType(),
'items_id' => $item->getID()
)
);
}
function plugin_datainjection_populate_tag() {
global $INJECTABLE_TYPES;
$INJECTABLE_TYPES['PluginTagTagInjection'] = 'tag';
}
function plugin_pre_item_update_tag($parm) {
global $DB;
if (isset($_REQUEST['plugin_tag_tag_id']) && isset($_REQUEST['plugin_tag_tag_itemtype'])) {
$already_present = array();
$itemtype = PluginTagTag::getItemtype($_REQUEST['plugin_tag_tag_itemtype'], $_REQUEST['plugin_tag_tag_id']);
$query_part = "`items_id`=".$_REQUEST['plugin_tag_tag_id']."
AND `itemtype` = '".$itemtype."'";
$item = new PluginTagTagItem();
foreach ($item->find($query_part) as $indb) {
if (isset($_REQUEST["_plugin_tag_tag_values"]) &&
in_array($indb["plugin_tag_tags_id"], $_REQUEST["_plugin_tag_tag_values"])) {
$already_present[] = $indb["plugin_tag_tags_id"];
} else {
$item->delete(array("id" => $indb['id']));
}
}
if (isset($_REQUEST["_plugin_tag_tag_values"])) {
foreach ($_REQUEST["_plugin_tag_tag_values"] as $tag_id) {
if (!in_array($tag_id, $already_present)) {
$item->add(array(
'plugin_tag_tags_id' => $tag_id,
'items_id' => $_REQUEST['plugin_tag_tag_id'],
'itemtype' => ucfirst($itemtype), //get_class($parm)
));
}
}
}
}
return $parm;
}
function plugin_pre_item_purge_tag($object) {
if (isset($object->input["plugin_tag_tag_itemtype"])) { // Example : TicketTask no have tag
$tagitem = new PluginTagTagItem();
$result = $tagitem->deleteByCriteria(array(
"items_id" => $object->fields["id"],
"itemtype" => ucfirst($object->input["plugin_tag_tag_itemtype"]),
));
}
}
function plugin_tag_getAddSearchOptions($itemtype) {
$sopt = array();
if (! Session::haveRight("itilcategory", READ)) {
return array();
}
if ($itemtype === 'PluginTagTag'
|| $itemtype === 'CronTask' //Because no have already tag in CronTask interface
|| $itemtype === 'PluginFormcreatorFormanswer' //No have tag associated
|| $itemtype === 'QueuedMail'
|| strpos($itemtype, 'PluginPrintercounters') !== false) {
return array();
}
$rng1 = PluginTagTag::TAG_SEARCH_NUM;
//$sopt[strtolower($itemtype)] = ''; //self::getTypeName(2);
$sopt[$rng1]['table'] = getTableForItemType('PluginTagTag');
$sopt[$rng1]['field'] = 'name';
$sopt[$rng1]['name'] = PluginTagTag::getTypeName(2);
$sopt[$rng1]['datatype'] = 'string';
$sopt[$rng1]['searchtype'] = "contains";
$sopt[$rng1]['massiveaction'] = false;
$sopt[$rng1]['forcegroupby'] = true;
$sopt[$rng1]['usehaving'] = true;
$sopt[$rng1]['joinparams'] = array('beforejoin' => array('table' => 'glpi_plugin_tag_tagitems',
'joinparams' => array('jointype' => "itemtype_item")));
//array('jointype' => "itemtype_item");
return $sopt;
}
function plugin_tag_giveItem($type, $field, $data, $num, $linkfield = "") {
switch ($field) {
case PluginTagTag::TAG_SEARCH_NUM: //Note : can declare a const for "10500"
$out = '<div id="s2id_tag_select" class="select2-container select2-container-multi chosen-select-no-results" style="width: 100%;">
<ul class="select2-choices">';
$separator = '';
$plugintagtag = new PluginTagTag();
foreach ($data[$num] as $tag) {
if (isset($tag['id']) && isset($tag['name'])) {
$id = $tag['id'];
$name = $tag['name'];
$plugintagtag->getFromDB($id);
$color = $plugintagtag->fields["color"];
$style = "";
if (!empty($color)) {
$style .= "border-width:2px; border-color:$color;";
}
$out .= '<li class="select2-search-choice" style="padding-left:5px;'.$style.'">'.$separator.$name.'</li>';
$separator = '<span style="display:none">, </span>'; //For export (CSV, PDF) of GLPI core
}
}
$out .= '</ul></div>';
return $out;
break;
case 6: //Type de tag
if ($type == 'PluginTagTag') { //for future
$key = $data[$num][0]['name'];
if (! is_array(json_decode($key))) { //for when $key value is "0"
return __("None");
}
$itemtype_names = array();
foreach (json_decode($key) as $itemtype) {
$item = getItemForItemtype($itemtype);
$itemtype_names[] = $item->getTypeName();
}
$out = implode(", ", $itemtype_names);
return $out;
}
}
return "";
}
function plugin_tag_addHaving($link, $nott, $type, $id, $val, $num) {
$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) {
// "Types d'élément associé"
if ($itemtype == 'PluginTagTag' && $ID == 6) {
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 tables to be manage in GLPI :
*/
function plugin_tag_getDropdown() {
return array('PluginTagTag' => PluginTagTag::getTypeName(2));
}
/**
* Install all necessary elements for the plugin
*
* @return boolean (True if success)
*/
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;
}
/**
* Uninstall previously installed elements of the plugin
*
* @return boolean True if success
*/
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 install method exists, load it
if (method_exists($classname, 'uninstall')) {
$classname::uninstall();
}
}
}
return true;
}