forked from pluginsGLPI/fields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.php
323 lines (261 loc) · 9.17 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
<?php
/*
-------------------------------------------------------------------------
Fields plugin for GLPI
Copyright (C) 2016 by the Fields Development Team.
https://github.com/pluginsGLPI/fields
-------------------------------------------------------------------------
LICENSE
This file is part of Fields.
Fields 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.
Fields 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 Fields. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
/**
* Plugin install process
*
* @return boolean
*/
function plugin_fields_install() {
global $CFG_GLPI;
set_time_limit(900);
ini_set('memory_limit', '2048M');
$plugin_fields = new Plugin;
$plugin_fields->getFromDBbyDir('fields');
$version = $plugin_fields->fields['version'];
$classesToInstall = [
'PluginFieldsField',
'PluginFieldsDropdown',
'PluginFieldsLabelTranslation',
'PluginFieldsContainer',
'PluginFieldsContainer_Field',
'PluginFieldsValue',
'PluginFieldsProfile',
'PluginFieldsMigration'
];
$migration = new Migration($version);
echo "<center>";
echo "<table class='tab_cadre_fixe'>";
echo "<tr><th>".__("MySQL tables installation", "fields")."<th></tr>";
echo "<tr class='tab_bg_1'>";
echo "<td align='center'>";
//load all classes
foreach ($classesToInstall as $class) {
if ($plug = isPluginItemType($class)) {
$dir = GLPI_ROOT . "/plugins/fields/inc/";
$item = strtolower($plug['class']);
if (file_exists("$dir$item.class.php")) {
include_once ("$dir$item.class.php");
}
}
}
//install
foreach ($classesToInstall as $class) {
if ($plug = isPluginItemType($class)) {
$dir = GLPI_ROOT . "/plugins/fields/inc/";
$item =strtolower($plug['class']);
if (file_exists("$dir$item.class.php")) {
if (!call_user_func(array($class,'install'), $migration, $version)) {
return false;
}
}
}
}
echo "</td>";
echo "</tr>";
echo "</table></center>";
return true;
}
/**
* Plugin uninstall process
*
* @return boolean
*/
function plugin_fields_uninstall() {
global $DB;
if (!class_exists('PluginFieldsProfile')) {
Session::addMessageAfterRedirect(__("The plugin can't be uninstalled when the plugin is disabled", 'fields'),
true, WARNING, true);
return false;
}
$_SESSION['uninstall_fields'] = true;
echo "<center>";
echo "<table class='tab_cadre_fixe'>";
echo "<tr><th>".__("MySQL tables uninstallation", "fields")."<th></tr>";
echo "<tr class='tab_bg_1'>";
echo "<td align='center'>";
$classesToUninstall = [
'PluginFieldsDropdown',
'PluginFieldsContainer',
'PluginFieldsContainer_Field',
'PluginFieldsLabelTranslation',
'PluginFieldsField',
'PluginFieldsValue',
'PluginFieldsProfile',
'PluginFieldsMigration'
];
foreach ($classesToUninstall as $class) {
if ($plug = isPluginItemType($class)) {
$dir = GLPI_ROOT . "/plugins/fields/inc/";
$item = strtolower($plug['class']);
if (file_exists("$dir$item.class.php")) {
include_once ("$dir$item.class.php");
if (!call_user_func(array($class,'uninstall'))) {
return false;
}
}
}
}
echo "</td>";
echo "</tr>";
echo "</table></center>";
unset($_SESSION['uninstall_fields']);
// clean display preferences
$pref = new DisplayPreference;
$pref->deleteByCriteria([
'itemtype' => ['LIKE' , 'PluginFields%']
]);
return true;
}
function regenerateFiles() {
$container = new PluginFieldsContainer;
$found_container = $container->find();
foreach ($found_container as $current_container) {
$containers_id = $current_container['id'];
$container->getFromDB($containers_id);
$container->post_addItem();
}
}
function plugin_fields_getAddSearchOptions($itemtype) {
if (isset($_SESSION['glpiactiveentities'])
&& is_array($_SESSION['glpiactiveentities'])
&& count($_SESSION['glpiactiveentities']) > 0) {
$itemtypes = PluginFieldsContainer::getEntries('all');
if ($itemtypes !== false && in_array($itemtype, $itemtypes)) {
return PluginFieldsContainer::getAddSearchOptions($itemtype);
}
}
return null;
}
// Define Dropdown tables to be manage in GLPI :
function plugin_fields_getDropdown() {
$dropdowns = [];
$field_obj = new PluginFieldsField;
$fields = $field_obj->find("`type` = 'dropdown'");
foreach ($fields as $field) {
$field['itemtype'] = PluginFieldsField::getType();
$label = PluginFieldsLabelTranslation::getLabelFor($field);
$dropdowns["PluginFields".ucfirst($field['name'])."Dropdown"] = $label;
}
asort($dropdowns);
return $dropdowns;
}
/**** MASSIVE ACTIONS ****/
// Display specific massive actions for plugin fields
function plugin_fields_MassiveActionsFieldsDisplay($options = []) {
$itemtypes = PluginFieldsContainer::getEntries('all');
if (in_array($options['itemtype'], $itemtypes)) {
PluginFieldsField::showSingle($options['itemtype'], $options['options'], true);
return true;
}
// Need to return false on non display item
return false;
}
/**** RULES ENGINE ****/
/**
*
* Actions for rules
* @since 0.84
* @param $params input data
* @return an array of actions
*/
function plugin_fields_getRuleActions($params = []) {
$actions = [];
switch ($params['rule_itemtype']) {
case "PluginFusioninventoryTaskpostactionRule":
$options = PluginFieldsContainer::getAddSearchOptions("Computer");
foreach ($options as $num => $option) {
$actions[$option['linkfield']]['name'] = $option['name'];
$actions[$option['linkfield']]['type'] = $option['pfields_type'];
if ($option['pfields_type'] == 'dropdown') {
$actions[$option['linkfield']]['table'] = $option['table'];
}
}
break;
}
return $actions;
}
function plugin_fields_rule_matched($params = []) {
global $DB;
$container = new PluginFieldsContainer;
switch ($params['sub_type']) {
case "PluginFusioninventoryTaskpostactionRule":
$agent = new PluginFusioninventoryAgent;
if (isset($params['input']['plugin_fusioninventory_agents_id'])) {
foreach ($params['output'] as $field => $value) {
// check if current field is in a tab container
$query = "SELECT c.id
FROM glpi_plugin_fields_fields f
LEFT JOIN glpi_plugin_fields_containers c
ON c.id = f.plugin_fields_containers_id
WHERE f.name = '$field'";
$res = $DB->query($query);
if ($DB->numrows($res) > 0) {
$data = $DB->fetch_assoc($res);
//retrieve computer
$agents_id = $params['input']['plugin_fusioninventory_agents_id'];
$agent->getFromDB($agents_id);
// update current field
$container->updateFieldsValues(
array(
'plugin_fields_containers_id' => $data['id'],
$field => $value,
'items_id' => $agent->fields['computers_id']
),
Computer::getType()
);
}
}
}
break;
}
}
function plugin_fields_giveItem($itemtype, $ID, $data, $num) {
$searchopt = &Search::getOptions($itemtype);
$table = $searchopt[$ID]["table"];
$field = $searchopt[$ID]["field"];
//fix glpi default Search::giveItem who for empty date display "--"
if (strpos($table, "glpi_plugin_fields") !== false
&& isset($searchopt[$ID]["datatype"])
&& strpos($searchopt[$ID]["datatype"], "date") !== false
&& empty($data['raw']["ITEM_$num"])) {
return " ";
}
return false;
}
/**
* Load Fields classes in datainjection.
* Called by Setup.php:44 if Datainjection is installed and active
**/
function plugin_datainjection_populate_fields() {
global $INJECTABLE_TYPES;
$container = new PluginFieldsContainer();
$found = $container->find("`is_active` = 1");
foreach ($found as $id => $values) {
$types = json_decode($values['itemtypes']);
foreach ($types as $type) {
$classname = "PluginFields"
. ucfirst($type. preg_replace('/s$/', '', $values['name']))
. 'Injection';
$INJECTABLE_TYPES[$classname] = 'fields';
}
}
}