-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.php
332 lines (298 loc) · 12.8 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
<?php
/*
* @version $Id$
LICENSE
This file is part of the simcard plugin.
Order plugin 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.
Order plugin 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 GLPI; along with Simcard. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
@package simcard
@author PGUM s.r.l, the simcard plugin team
@copyright Copyright (c) 2019 PGUM s.r.l
@license GPLv2+
http://www.gnu.org/licenses/gpl.txt
@link https://pgum.eu
@link https://github.com/pluginsglpi/simcard
@link http://www.glpi-project.org/
@since 2009
---------------------------------------------------------------------- */
/**
*
* Determine if the plugin should be installed or upgraded
*
* Returns 0 if the plugin is not yet installed
* Returns 1 if the plugin is already installed
*
* @return number|string
* @since 1.3
*
*/
function plugin_simcard_currentVersion()
{
global $DB;
// Saves the current version to not re-detect it on multiple calls
static $currentVersion = null;
if ($currentVersion === null) {
if (!$DB->TableExists('glpi_plugin_simcard_simcards_items') && !$DB->TableExists('glpi_plugin_simcard_configs')) {
// the plugin seems not installed
$currentVersion = 0;
} else {
if ($DB->TableExists('glpi_plugin_simcard_configs')) {
// plugin installed, get the current version in the plugin's configuration
$pluginSimcardConfig = new PluginSimcardConfig();
$currentVersion = $pluginSimcardConfig->getValue('Version');
}
}
}
return $currentVersion;
}
function plugin_simcard_install()
{
include_once(GLPI_ROOT . "/plugins/simcard/inc/profile.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/simcard.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/simcardsize.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/simcardvoltage.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/simcardtype.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/phoneoperator.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/simcard_item.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/config.class.php");
$migration = new Migration(PLUGIN_SIMCARD_VERSION);
if (plugin_simcard_currentVersion() == 0) {
// Installation of the plugin
PluginSimcardConfig::install($migration);
PluginSimcardProfile::install($migration);
PluginSimcardSimcard::install($migration);
PluginSimcardSimcardSize::install($migration);
PluginSimcardSimcardVoltage::install($migration);
PluginSimcardSimcardType::install($migration);
PluginSimcardPhoneOperator::install($migration);
PluginSimcardSimcard_Item::install($migration);
} else {
// Updating the plugin
PluginSimcardProfile::upgrade($migration);
PluginSimcardSimcard::upgrade($migration);
PluginSimcardSimcardSize::upgrade($migration);
PluginSimcardSimcardVoltage::upgrade($migration);
PluginSimcardSimcardType::upgrade($migration);
PluginSimcardPhoneOperator::upgrade($migration);
PluginSimcardSimcard_Item::upgrade($migration);
// UPDATE CONFIG AS LAST (so other upgrades can use the currentVersion function)
PluginSimcardConfig::upgrade($migration);
}
return true;
}
function plugin_simcard_uninstall()
{
include_once(GLPI_ROOT . "/plugins/simcard/inc/profile.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/simcard.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/simcardsize.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/simcardvoltage.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/simcardtype.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/phoneoperator.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/simcard_item.class.php");
include_once(GLPI_ROOT . "/plugins/simcard/inc/config.class.php");
PluginSimcardProfile::uninstall();
PluginSimcardSimcard::uninstall();
PluginSimcardSimcardSize::uninstall();
PluginSimcardSimcardVoltage::uninstall();
PluginSimcardSimcardType::uninstall();
PluginSimcardPhoneOperator::uninstall();
PluginSimcardSimcard_Item::uninstall();
PluginSimcardConfig::uninstall();
return true;
}
// Define dropdown relations
function plugin_simcard_getDatabaseRelations()
{
$plugin = new Plugin();
if ($plugin->isActivated("simcard")) {
return array(
"glpi_plugin_simcard_simcardsizes"
=> array("glpi_plugin_simcard_simcards" => "plugin_simcard_simcardsizes_id"),
"glpi_plugin_simcard_simcardvoltages"
=> array("glpi_plugin_simcard_simcards" => "plugin_simcard_simcardvoltages_id"),
"glpi_plugin_simcard_phoneoperators"
=> array("glpi_plugin_simcard_simcards" => "plugin_simcard_phoneoperators_id"),
"glpi_plugin_simcard_simcardtypes"
=> array("glpi_plugin_simcard_simcards" => "plugin_simcard_simcardtypes_id"),
"glpi_users" => array("glpi_plugin_simcard_simcards" => [["users_id", "users_id_tech"]]),
"glpi_groups" => array("glpi_plugin_simcard_simcards" => [["groups_id", "groups_id_tech"]]),
"glpi_manufacturers" => array("glpi_plugin_simcard_simcards" => "manufacturers_id"),
"glpi_states" => array("glpi_plugin_simcard_simcards" => "states_id"),
"glpi_locations" => array("glpi_plugin_simcard_simcards" => "locations_id")
);
//"glpi_profiles" => array ("glpi_plugin_simcard_profiles" => "profiles_id"));
} else {
return array();
}
}
// Define Dropdown tables to be manage in GLPI :
function plugin_simcard_getDropdown()
{
global $LANG;
$plugin = new Plugin();
if ($plugin->isActivated("simcard")) {
return array(
'PluginSimcardSimcardSize' => __('Size', 'simcard'),
'PluginSimcardPhoneOperator' => __('Provider', 'simcard'),
'PluginSimcardSimcardVoltage' => __('Voltage', 'simcard'),
'PluginSimcardSimcardType' => __('Type of SIM card', 'simcard')
);
} else {
return array();
}
}
function plugin_simcard_AssignToTicket($types)
{
global $LANG;
if (Session::haveRight(PluginSimcardProfile::RIGHT_SIMCARD_SIMCARD, PluginSimcardProfile::SIMCARD_ASSOCIATE_TICKET)) {
$types['PluginSimcardSimcard'] = 'SIM Cards';
}
return $types;
}
//force groupby for multible links to items
function plugin_simcard_forceGroupBy($type)
{
// return true;
switch ($type) {
case 'PluginSimcardSimcard':
return true;
}
return false;
}
function plugin_simcard_getAddSearchOptions($itemtype)
{
global $LANG;
$sopt = array();
$reservedTypeIndex = PluginSimcardConfig::RESERVED_TYPE_RANGE_MIN;
if (in_array($itemtype, PluginSimcardSimcard_Item::getClasses())) {
if (PluginSimcardSimcard::canView()) {
$sopt[$reservedTypeIndex]['table'] = 'glpi_plugin_simcard_simcards';
$sopt[$reservedTypeIndex]['field'] = 'name';
$sopt[$reservedTypeIndex]['name'] = _sn('SIM card', 'SIM cards', 2, 'simcard') . " - " . __s('Name');
$sopt[$reservedTypeIndex]['forcegroupby'] = true;
$sopt[$reservedTypeIndex]['massiveaction'] = false;
$sopt[$reservedTypeIndex]['datatype'] = 'itemlink';
$sopt[$reservedTypeIndex]['itemlink_type'] = 'PluginSimcardSimcard';
$sopt[$reservedTypeIndex]['joinparams'] = array('beforejoin'
=> array(
'table' => 'glpi_plugin_simcard_simcards_items',
'joinparams' => array('jointype' => 'itemtype_item')
));
$reservedTypeIndex++;
$sopt[$reservedTypeIndex]['table'] = 'glpi_plugin_simcard_simcards';
$sopt[$reservedTypeIndex]['field'] = 'phonenumber';
$sopt[$reservedTypeIndex]['name'] = _sn('SIM card', 'SIM cards', 2, 'simcard') . " - " . __s('Phone number', 'simcard');
$sopt[$reservedTypeIndex]['massiveaction'] = false;
$sopt[$reservedTypeIndex]['forcegroupby'] = true;
$sopt[$reservedTypeIndex]['joinparams'] = array('beforejoin'
=> array(
'table' => 'glpi_plugin_simcard_simcards_items',
'joinparams' => array('jointype' => 'itemtype_item')
));
$reservedTypeIndex++;
$sopt[$reservedTypeIndex]['table'] = 'glpi_plugin_simcard_simcards';
$sopt[$reservedTypeIndex]['field'] = 'serial';
$sopt[$reservedTypeIndex]['name'] = _sn('SIM card', 'SIM cards', 2, 'simcard') . " - " . __s('IMSI', 'simcard');
$sopt[$reservedTypeIndex]['massiveaction'] = false;
$sopt[$reservedTypeIndex]['forcegroupby'] = true;
$sopt[$reservedTypeIndex]['joinparams'] = array('beforejoin'
=> array(
'table' => 'glpi_plugin_simcard_simcards_items',
'joinparams' => array('jointype' => 'itemtype_item')
));
}
}
return $sopt;
}
// Hook done on purge item case
function plugin_item_purge_simcard($item)
{
$temp = new PluginSimcardSimcard_Item();
$temp->deleteByCriteria(array(
'itemtype' => get_class($item),
'items_id' => $item->getField('id')
));
return true;
}
function plugin_datainjection_populate_simcard()
{
global $INJECTABLE_TYPES;
$INJECTABLE_TYPES['PluginSimcardSimcardInjection'] = 'simcard';
}
/**
*
* Determine if the plugin should be installed or upgraded
*
* Returns 0 if the plugin is not yet installed
* Returns 1 if the plugin is already installed
*
* @since 1.3
*/
function plugin_simcard_postinit()
{
global $UNINSTALL_TYPES, $ORDER_TYPES, $ALL_CUSTOMFIELDS_TYPES, $DB;
$plugin = new Plugin();
if ($plugin->isInstalled('uninstall') && $plugin->isActivated('uninstall')) {
$UNINSTALL_TYPES[] = 'PluginSimcardSimcard';
}
if ($plugin->isInstalled('order') && $plugin->isActivated('order')) {
$ORDER_TYPES[] = 'PluginSimcardSimcard';
}
if ($plugin->isInstalled('customfields') && $plugin->isActivated('customfields')) {
PluginCustomfieldsItemtype::registerItemtype('PluginSimcardSimcard');
}
}
/**
* Update helpdesk_item_type in a profile if a ProfileRight changes or is created
*
* Add or remove simcard item type to match the status of "associable to tickets" in simcard's right
*
* @since 1.4.1
*/
function plugin_simcard_profileRightUpdate($item)
{
if ($_SESSION['glpiactiveprofile']['id'] == $item->fields['profiles_id']) {
if ($item->fields['name'] == PluginSimcardProfile::RIGHT_SIMCARD_SIMCARD) {
$profile = new Profile();
$profile->getFromDB($item->fields['profiles_id']);
$helpdeskItemTypes = json_decode($profile->fields['helpdesk_item_type'], true);
if (!is_array($helpdeskItemTypes)) {
$helpdeskItemTypes = array();
}
$index = array_search('PluginSimcardSimcard', $helpdeskItemTypes);
if ($item->fields['rights'] & PluginSimcardProfile::SIMCARD_ASSOCIATE_TICKET) {
if ($index === false) {
$helpdeskItemTypes[] = 'PluginSimcardSimcard';
if ($_SESSION['glpiactiveprofile']['id'] == $profile->fields['id']) {
$_SESSION['glpiactiveprofile']['helpdesk_item_type'][] = 'PluginSimcardSimcard';
}
}
} else {
if ($index !== false) {
unset($helpdeskItemTypes[$index]);
if ($_SESSION['glpiactiveprofile']['id'] == $profile->fields['id']) {
// Just in case this is not the same index in the session vars
$index = array_search('PluginSimcardSimcard', $_SESSION['glpiactiveprofile']['helpdesk_item_type']);
if ($index !== false) {
unset($_SESSION['glpiactiveprofile']['helpdesk_item_type'][$index]);
}
}
}
}
$tmp = array(
'id' => $profile->fields['id'],
'helpdesk_item_type' => json_encode($helpdeskItemTypes)
);
$profile->update($tmp, false);
}
}
}