forked from InfotelGLPI/badges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.php
448 lines (371 loc) · 17.7 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
<?php
/*
* @version $Id: HEADER 15930 2011-10-30 15:47:55Z tsmr $
-------------------------------------------------------------------------
badges plugin for GLPI
Copyright (C) 2009-2016 by the badges Development Team.
https://github.com/InfotelGLPI/badges
-------------------------------------------------------------------------
LICENSE
This file is part of badges.
badges 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.
badges 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 badges. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
/**
* @return bool
*/
function plugin_badges_install() {
global $DB;
include_once(GLPI_ROOT . "/plugins/badges/inc/profile.class.php");
$install = false;
$update78 = false;
$update85 = false;
$update201 = false;
if (!$DB->tableExists("glpi_plugin_badges") && !$DB->tableExists("glpi_plugin_badges_badgetypes")) {
$install = true;
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/empty-2.4.1.sql");
} else if ($DB->tableExists("glpi_plugin_badges_users") && !$DB->tableExists("glpi_plugin_badges_default")) {
$update78 = true;
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-1.4.sql");
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-1.5.0.sql");
plugin_badges_configure15();
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-1.5.1.sql");
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-1.6.0.sql");
} else if ($DB->tableExists("glpi_plugin_badges_profiles") && $DB->fieldExists("glpi_plugin_badges_profiles", "interface")) {
$update78 = true;
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-1.5.0.sql");
plugin_badges_configure15();
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-1.5.1.sql");
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-1.6.0.sql");
} else if ($DB->tableExists("glpi_plugin_badges") && !$DB->fieldExists("glpi_plugin_badges", "date_mod")) {
$update78 = true;
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-1.5.1.sql");
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-1.6.0.sql");
} else if (!$DB->tableExists("glpi_plugin_badges_badgetypes")) {
$update78 = true;
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-1.6.0.sql");
} else if ($DB->tableExists("glpi_plugin_badges_profiles")) {
$update85 = true;
}
//version 2.4.1
if ($DB->tableExists("glpi_plugin_badges_badges") && !$DB->fieldExists("glpi_plugin_badges_badges", "is_recursive")) {
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-2.4.1.sql");
}
if (!$DB->tableExists("glpi_plugin_badges_requests")) {
$update201 = true;
$DB->runFile(GLPI_ROOT . "/plugins/badges/sql/update-2.0.1.sql");
}
if ($install || $update201) {
// Badge request notification
$query_id = "SELECT `id` FROM `glpi_notificationtemplates` WHERE `itemtype`='PluginBadgesBadge' AND `name` = 'Access Badges Request'";
$result = $DB->query($query_id) or die($DB->error());
$itemtype = $DB->result($result, 0, 'id');
if (empty($itemtype)) {
$query_id = "INSERT INTO `glpi_notificationtemplates`(`name`, `itemtype`, `date_mod`, `comment`, `css`) VALUES ('Access Badges Request','PluginBadgesBadge', NOW(),'','');";
$result = $DB->query($query_id) or die($DB->error());
$query_id = "SELECT `id` FROM `glpi_notificationtemplates` WHERE `itemtype`='PluginBadgesBadge' AND `name` = 'Access Badges Request'";
$result = $DB->query($query_id) or die($DB->error());
$itemtype = $DB->result($result, 0, 'id');
}
$query = "INSERT INTO `glpi_notificationtemplatetranslations`
VALUES(NULL, '" . $itemtype . "', '','##badge.action## : ##badge.entity##',
'##lang.badge.entity## :##badge.entity##
##FOREACHbadgerequest##
##lang.badgerequest.arrivaldate## : ##badgerequest.arrivaldate##
##lang.badgerequest.requester## : ##badgerequest.requester##
##lang.badgerequest.visitorfirstname## : ##badgerequest.visitorfirstname##
##lang.badgerequest.visitorrealname## : ##badgerequest.visitorrealname##
##lang.badgerequest.visitorsociety## : ##badgerequest.visitorsociety##
##ENDFOREACHbadgerequest##',
'<p>##lang.badge.entity## :##badge.entity##<br /> <br />
##FOREACHbadgerequest##<br />
##lang.badgerequest.arrivaldate## : ##badgerequest.arrivaldate##<br />
##lang.badgerequest.requester## : ##badgerequest.requester##<br />
##lang.badgerequest.visitorfirstname## : ##badgerequest.visitorfirstname##<br />
##lang.badgerequest.visitorrealname## : ##badgerequest.visitorrealname##<br />
##lang.badgerequest.visitorsociety## : ##badgerequest.visitorsociety##<br />
##ENDFOREACHbadgerequest##</p>');";
$DB->query($query);
$query = "INSERT INTO `glpi_notifications` (name, entities_id, itemtype, event, is_recursive, is_active)
VALUES ('Access badge request', 0, 'PluginBadgesBadge', 'AccessBadgeRequest', 1, 1);";
$DB->query($query);
//retrieve notification id
$query_id = "SELECT `id` FROM `glpi_notifications`
WHERE `name` = 'Access badge request' AND `itemtype` = 'PluginBadgesBadge' AND `event` = 'AccessBadgeRequest'";
$result = $DB->query($query_id) or die ($DB->error());
$notification = $DB->result($result, 0, 'id');
$query = "INSERT INTO `glpi_notifications_notificationtemplates` (`notifications_id`, `mode`, `notificationtemplates_id`)
VALUES (" . $notification . ", 'mailing', " . $itemtype . ");";
$DB->query($query);
// Badge expiration alert notification
$query_id = "SELECT `id` FROM `glpi_notificationtemplates` WHERE `itemtype`='PluginBadgesBadge' AND `name` = 'Access Badges Return'";
$result = $DB->query($query_id) or die($DB->error());
$itemtype = $DB->result($result, 0, 'id');
if (empty($itemtype)) {
$query_id = "INSERT INTO `glpi_notificationtemplates`(`name`, `itemtype`, `date_mod`, `comment`, `css`) VALUES ('Access Badges Return','PluginBadgesBadge', NOW(),'','');";
$result = $DB->query($query_id) or die($DB->error());
$query_id = "SELECT `id` FROM `glpi_notificationtemplates` WHERE `itemtype`='PluginBadgesBadge' AND `name` = 'Access Badges Return'";
$result = $DB->query($query_id) or die($DB->error());
$itemtype = $DB->result($result, 0, 'id');
}
$query = "INSERT INTO `glpi_notificationtemplatetranslations`
VALUES(NULL, '" . $itemtype . "', '','##badge.action## : ##badge.entity##',
'##lang.badge.entity## :##badge.entity##
##FOREACHbadgerequest##
##lang.badgerequest.arrivaldate## : ##badgerequest.arrivaldate##
##lang.badgerequest.requester## : ##badgerequest.requester##
##lang.badgerequest.visitorfirstname## : ##badgerequest.visitorfirstname##
##lang.badgerequest.visitorrealname## : ##badgerequest.visitorrealname##
##lang.badgerequest.visitorsociety## : ##badgerequest.visitorsociety##
##ENDFOREACHbadgerequest##',
'<p>##lang.badge.entity## :##badge.entity##<br /> <br />
##FOREACHbadgerequest##<br />
##lang.badgerequest.arrivaldate## : ##badgerequest.arrivaldate##<br />
##lang.badgerequest.requester## : ##badgerequest.requester##<br />
##lang.badgerequest.visitorfirstname## : ##badgerequest.visitorfirstname##<br />
##lang.badgerequest.visitorrealname## : ##badgerequest.visitorrealname##<br />
##lang.badgerequest.visitorsociety## : ##badgerequest.visitorsociety##<br />
##ENDFOREACHbadgerequest##</p>');";
$DB->query($query);
$query = "INSERT INTO `glpi_notifications` (name, entities_id, itemtype, event, is_recursive, is_active)
VALUES ('Access badge return', 0, 'PluginBadgesBadge', 'BadgesReturn', 1, 1);";
$DB->query($query);
//retrieve notification id
$query_id = "SELECT `id` FROM `glpi_notifications`
WHERE `name` = 'Access badge return' AND `itemtype` = 'PluginBadgesBadge' AND `event` = 'BadgesReturn'";
$result = $DB->query($query_id) or die ($DB->error());
$notification = $DB->result($result, 0, 'id');
$query = "INSERT INTO `glpi_notifications_notificationtemplates` (`notifications_id`, `mode`, `notificationtemplates_id`)
VALUES (" . $notification . ", 'mailing', " . $itemtype . ");";
$DB->query($query);
}
if ($update78) {
$query_ = "SELECT *
FROM `glpi_plugin_badges_profiles` ";
$result_ = $DB->query($query_);
if ($DB->numrows($result_) > 0) {
while ($data = $DB->fetch_array($result_)) {
$query = "UPDATE `glpi_plugin_badges_profiles`
SET `profiles_id` = '" . $data["id"] . "'
WHERE `id` = '" . $data["id"] . "';";
$DB->query($query);
}
}
$query = "ALTER TABLE `glpi_plugin_badges_profiles`
DROP `name` ;";
$DB->query($query);
Plugin::migrateItemType(
[1600 => 'PluginBadgesBadge'],
["glpi_savedsearches", "glpi_savedsearches_users", "glpi_displaypreferences",
"glpi_documents_items", "glpi_infocoms", "glpi_logs", "glpi_items_tickets"]);
}
if ($update85) {
$notepad_tables = ['glpi_plugin_badges_badges'];
foreach ($notepad_tables as $t) {
// Migrate data
if ($DB->fieldExists($t, 'notepad')) {
$query = "SELECT id, notepad
FROM `$t`
WHERE notepad IS NOT NULL
AND notepad <>'';";
foreach ($DB->request($query) as $data) {
$iq = "INSERT INTO `glpi_notepads`
(`itemtype`, `items_id`, `content`, `date`, `date_mod`)
VALUES ('PluginBadgesBadge', '" . $data['id'] . "',
'" . addslashes($data['notepad']) . "', NOW(), NOW())";
$DB->queryOrDie($iq, "0.85 migrate notepad data");
}
$query = "ALTER TABLE `glpi_plugin_badges_badges` DROP COLUMN `notepad`;";
$DB->query($query);
}
}
}
CronTask::Register('PluginBadgesBadge', 'BadgesAlert', DAY_TIMESTAMP);
CronTask::Register('PluginBadgesReturn', 'BadgesReturnAlert', DAY_TIMESTAMP);
PluginBadgesProfile::initProfile();
PluginBadgesProfile::createFirstAccess($_SESSION['glpiactiveprofile']['id']);
$migration = new Migration("2.2.0");
$migration->dropTable('glpi_plugin_badges_profiles');
return true;
}
function plugin_badges_configure15() {
global $DB;
// ADD FK_users
$query_old_items = "SELECT `glpi_plugin_badges_users`.`FK_users`,`glpi_plugin_badges`.`ID`
FROM `glpi_plugin_badges_users`,`glpi_plugin_badges` WHERE `glpi_plugin_badges_users`.`FK_badges` = `glpi_plugin_badges`.`ID` ";
$result_old_items = $DB->query($query_old_items);
if ($DB->numrows($result_old_items) > 0) {
while ($data_old_items = $DB->fetch_array($result_old_items)) {
if ($data_old_items["ID"]) {
$query = "UPDATE `glpi_plugin_badges` SET `FK_users` = '" . $data_old_items["FK_users"] . "' WHERE `ID` = '" . $data_old_items["ID"] . "' ";
$DB->query($query);
}
}
}
$query = "DROP TABLE IF EXISTS `glpi_plugin_badges_users` ";
$DB->query($query);
}
/**
* @return bool
*/
function plugin_badges_uninstall() {
global $DB;
include_once(GLPI_ROOT . "/plugins/badges/inc/profile.class.php");
include_once(GLPI_ROOT . "/plugins/badges/inc/menu.class.php");
$tables = ["glpi_plugin_badges_badges",
"glpi_plugin_badges_badgetypes",
"glpi_plugin_badges_configs",
"glpi_plugin_badges_notificationstates",
"glpi_plugin_badges_requests"];
foreach ($tables as $table) {
$DB->query("DROP TABLE IF EXISTS `$table`;");
}
//old versions
$tables = ["glpi_plugin_badges",
"glpi_dropdown_plugin_badges_type",
"glpi_plugin_badges_users",
"glpi_plugin_badges_profiles",
"glpi_plugin_badges_config",
"glpi_plugin_badges_mailing",
"glpi_plugin_badges_default"];
foreach ($tables as $table) {
$DB->query("DROP TABLE IF EXISTS `$table`;");
}
$notif = new Notification();
$options = ['itemtype' => 'PluginBadgesBadge',
'event' => 'ExpiredBadges',
'FIELDS' => 'id'];
foreach ($DB->request('glpi_notifications', $options) as $data) {
$notif->delete($data);
}
$options = ['itemtype' => 'PluginBadgesBadge',
'event' => 'BadgesWhichExpire',
'FIELDS' => 'id'];
foreach ($DB->request('glpi_notifications', $options) as $data) {
$notif->delete($data);
}
$options = ['itemtype' => 'PluginBadgesBadge',
'event' => 'BadgesReturn',
'FIELDS' => 'id'];
foreach ($DB->request('glpi_notifications', $options) as $data) {
$notif->delete($data);
}
$options = ['itemtype' => 'PluginBadgesBadge',
'event' => 'AccessBadgeRequest',
'FIELDS' => 'id'];
foreach ($DB->request('glpi_notifications', $options) as $data) {
$notif->delete($data);
}
//templates
$template = new NotificationTemplate();
$translation = new NotificationTemplateTranslation();
$notif_template = new Notification_NotificationTemplate();
$options = ['itemtype' => 'PluginBadgesBadge',
'FIELDS' => 'id'];
foreach ($DB->request('glpi_notificationtemplates', $options) as $data) {
$options_template = ['notificationtemplates_id' => $data['id'],
'FIELDS' => 'id'];
foreach ($DB->request('glpi_notificationtemplatetranslations', $options_template) as $data_template) {
$translation->delete($data_template);
}
$template->delete($data);
foreach ($DB->request('glpi_notifications_notificationtemplates', $options_template) as $data_template) {
$notif_template->delete($data_template);
}
}
$tables_glpi = ["glpi_displaypreferences",
"glpi_documents_items",
"glpi_savedsearches",
"glpi_logs",
"glpi_items_tickets",
"glpi_notepads",
"glpi_dropdowntranslations"];
foreach ($tables_glpi as $table_glpi) {
$DB->query("DELETE FROM `$table_glpi` WHERE `itemtype` LIKE 'PluginBadges%';");
}
if (class_exists('PluginDatainjectionModel')) {
PluginDatainjectionModel::clean(['itemtype' => 'PluginBadgesBadge']);
}
CronTask::Unregister('PluginBadgesReturn');
//Delete rights associated with the plugin
$profileRight = new ProfileRight();
foreach (PluginBadgesProfile::getAllRights() as $right) {
$profileRight->deleteByCriteria(['name' => $right['field']]);
}
PluginBadgesMenu::removeRightsFromSession();
PluginBadgesProfile::removeRightsFromSession();
return true;
}
/**
* @param $types
* @return mixed
*/
function plugin_badges_AssignToTicket($types) {
if (Session::haveRight("plugin_badges_open_ticket", "1")) {
$types['PluginBadgesBadge'] = PluginBadgesBadge::getTypeName(2);
}
return $types;
}
// Define dropdown relations
/**
* @return array
*/
function plugin_badges_getDatabaseRelations() {
$plugin = new Plugin();
if ($plugin->isActivated("badges")) {
return ["glpi_plugin_badges_badgetypes" => ["glpi_plugin_badges_badges" => "plugin_badges_badgetypes_id"],
"glpi_entities" => ["glpi_plugin_badges_badges" => "entities_id",
"glpi_plugin_badges_badgetypes" => "entities_id"],
"glpi_locations" => ["glpi_plugin_badges_badges" => "locations_id"],
"glpi_states" => ["glpi_plugin_badges_badges" => "states_id",
"glpi_plugin_badges_notificationstates" => "states_id"],
"glpi_users" => ["glpi_plugin_badges_badges" => "users_id"]];
} else {
return [];
}
}
// Define Dropdown tables to be manage in GLPI :
/**
* @return array
*/
function plugin_badges_getDropdown() {
$plugin = new Plugin();
if ($plugin->isActivated("badges")) {
return ["PluginBadgesBadgeType" => PluginBadgesBadgeType::getTypeName(2)];
} else {
return [];
}
}
/**
* @param $type
* @param $ID
* @param $data
* @param $num
* @return string
*/
function plugin_badges_displayConfigItem($type, $ID, $data, $num) {
$searchopt =& Search::getOptions($type);
$table = $searchopt[$ID]["table"];
$field = $searchopt[$ID]["field"];
switch ($table . '.' . $field) {
case "glpi_plugin_badges_badges.date_expiration" :
if ($data[$num][0]['name'] <= date('Y-m-d') && !empty($data[$num][0]['name'])) {
return " class=\"deleted\" ";
}
break;
}
return "";
}
function plugin_datainjection_populate_badges() {
global $INJECTABLE_TYPES;
$INJECTABLE_TYPES['PluginBadgesBadgeInjection'] = 'badges';
}