-
Notifications
You must be signed in to change notification settings - Fork 22
/
hook.php
360 lines (321 loc) · 12.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
<?php
/**
* -------------------------------------------------------------------------
* News plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of News.
*
* News 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.
*
* News 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 News. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2015-2023 by News plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/news
* -------------------------------------------------------------------------
*/
function plugin_news_install()
{
/** @var DBmysql $DB */
global $DB;
$migration = new Migration(Plugin::getInfo('news', 'version'));
$default_charset = DBConnection::getDefaultCharset();
$default_collation = DBConnection::getDefaultCollation();
$default_key_sign = DBConnection::getDefaultPrimaryKeySignOption();
$alert_table = 'glpi_plugin_news_alerts';
if (!$DB->tableExists($alert_table)) {
$white = PluginNewsAlert::WHITE;
$dark = PluginNewsAlert::DARK;
$medium = PluginNewsAlert::MEDIUM;
$DB->query("
CREATE TABLE IF NOT EXISTS `$alert_table` (
`id` INT {$default_key_sign} NOT NULL AUTO_INCREMENT,
`date_mod` TIMESTAMP NOT NULL,
`name` VARCHAR(255) NOT NULL,
`message` TEXT NOT NULL,
`date_start` TIMESTAMP NULL DEFAULT NULL,
`date_end` TIMESTAMP NULL DEFAULT NULL,
`type` INT NOT NULL,
`is_deleted` TINYINT NOT NULL DEFAULT 0,
`is_displayed_onlogin` TINYINT NOT NULL,
`is_displayed_oncentral` TINYINT NOT NULL,
`display_dates` TINYINT NOT NULL DEFAULT 1,
`background_color` VARCHAR(255) NOT NULL DEFAULT '$white',
`text_color` VARCHAR(255) NOT NULL DEFAULT '$dark',
`emphasis_color` VARCHAR(255) NOT NULL DEFAULT '$dark',
`size` VARCHAR(255) NOT NULL DEFAULT '$medium',
`icon` VARCHAR(255) NOT NULL,
`entities_id` INT {$default_key_sign} NOT NULL,
`is_recursive` TINYINT NOT NULL DEFAULT 1,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;
");
} else {
$DB->updateOrDie(
PluginNewsAlert_User::getTable(),
[
'state' => PluginNewsAlert_User::VISIBLE,
],
[
'AND' => [
'state' => PluginNewsAlert_User::HIDDEN,
PluginNewsAlert::getTable() . '.is_close_allowed' => 0,
],
],
'',
[
'JOIN' => [
PluginNewsAlert::getTable() => [
'FKEY' => [
PluginNewsAlert_User::getTable() => 'plugin_news_alerts_id',
PluginNewsAlert::getTable() => 'id',
],
],
],
],
);
}
if (!$DB->tableExists('glpi_plugin_news_alerts_users')) {
$DB->query("
CREATE TABLE IF NOT EXISTS `glpi_plugin_news_alerts_users` (
`id` INT {$default_key_sign} NOT NULL AUTO_INCREMENT,
`plugin_news_alerts_id` INT {$default_key_sign} NOT NULL,
`users_id` INT {$default_key_sign} NOT NULL,
`state` TINYINT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `state_for_user`
(`plugin_news_alerts_id`,`users_id`,`state`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;
");
}
if (!$DB->tableExists('glpi_plugin_news_alerts_targets')) {
$DB->query("
CREATE TABLE IF NOT EXISTS `glpi_plugin_news_alerts_targets` (
`id` INT {$default_key_sign} NOT NULL AUTO_INCREMENT,
`plugin_news_alerts_id` INT {$default_key_sign} NOT NULL,
`itemtype` VARCHAR(255) NOT NULL,
`items_id` INT {$default_key_sign} NOT NULL,
`all_items` TINYINT NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `alert_itemtype_items_id`
(`plugin_news_alerts_id`, `itemtype`,`items_id`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;
");
}
/* Remove old table */
if ($DB->tableExists('glpi_plugin_news_profiles')) {
$DB->query('DROP TABLE IF EXISTS `glpi_plugin_news_profiles`;');
}
// add displayed on login flag
if (!$DB->fieldExists($alert_table, 'is_displayed_onlogin')) {
$migration->addField($alert_table, 'is_displayed_onlogin', 'bool');
}
// add displayed on helpdesk flag
if (!$DB->fieldExists($alert_table, 'is_displayed_onhelpdesk')) {
$migration->addField($alert_table, 'is_displayed_onhelpdesk', 'bool');
}
if (!$DB->fieldExists($alert_table, 'date_creation')) {
if ($migration->addField($alert_table, 'date_creation', 'date')) {
$migration->addKey($alert_table, 'date_creation');
}
}
// add close allowed flag
if (!$DB->fieldExists($alert_table, 'is_close_allowed')) {
$migration->addField($alert_table, 'is_close_allowed', 'bool');
}
// add type field on alert (to display icons)
if (!$DB->fieldExists($alert_table, 'type')) {
$migration->addField($alert_table, 'type', 'integer');
}
// add activity flag
if (!$DB->fieldExists($alert_table, 'is_active')) {
if ($migration->addField($alert_table, 'is_active', 'bool')) {
$migration->addKey($alert_table, 'is_active');
}
}
// fix is_default default value
$alert_fields = $DB->listFields($alert_table);
if ($alert_fields['is_deleted']['Default'] !== '0') {
$migration->changeField(
$alert_table,
'is_deleted',
'is_deleted',
'TINYINT NOT NULL DEFAULT 0',
);
}
// end/start dates can be null
$migration->changeField(
$alert_table,
'date_end',
'date_end',
'TIMESTAMP NULL DEFAULT NULL',
);
$migration->changeField(
$alert_table,
'date_start',
'date_start',
'TIMESTAMP NULL DEFAULT NULL',
);
if ($DB->fieldExists($alert_table, 'profiles_id')) {
// migration of direct profiles into targets table
$query_targets = "INSERT INTO glpi_plugin_news_alerts_targets
(plugin_news_alerts_id, itemtype, items_id)
SELECT id, 'Profile', profiles_id
FROM $alert_table";
$DB->query($query_targets) or die('fail to migration targets');
//drop old field
$migration->dropField($alert_table, 'profiles_id');
}
// Replace -1 value usage in items_id foreign key
if (!$DB->fieldExists('glpi_plugin_news_alerts_targets', 'all_items')) {
$migration->addField('glpi_plugin_news_alerts_targets', 'all_items', 'bool');
$migration->addPostQuery(
$DB->buildUpdate(
'glpi_plugin_news_alerts_targets',
['items_id' => '0', 'all_items' => '1'],
['items_id' => '-1'],
),
);
}
// install default display preferences
$dpreferences = new DisplayPreference();
$found_dpref = $dpreferences->find(['itemtype' => ['LIKE', '%PluginNews%']]);
if (count($found_dpref) == 0) {
$DB->query("INSERT INTO `glpi_displaypreferences`
(`itemtype`, `num`, `rank`, `users_id`)
VALUES
('PluginNewsAlert', 2, 1, 0),
('PluginNewsAlert', 3, 2, 0),
('PluginNewsAlert', 6, 4, 0)");
}
// add displayed on central flag
if (!$DB->fieldExists($alert_table, 'is_displayed_oncentral')) {
$migration->addField(
$alert_table,
'is_displayed_oncentral',
'bool',
['value' => true],
);
}
// Add background_color field
if (!$DB->fieldExists($alert_table, 'background_color')) {
$migration->addField(
$alert_table,
'background_color',
'string',
['value' => PluginNewsAlert::WHITE],
);
}
// Add text_color field
if (!$DB->fieldExists($alert_table, 'text_color')) {
$migration->addField(
$alert_table,
'text_color',
'string',
['value' => PluginNewsAlert::DARK],
);
}
// Add emphasis_color field
if (!$DB->fieldExists($alert_table, 'emphasis_color')) {
$migration->addField(
$alert_table,
'emphasis_color',
'string',
['value' => PluginNewsAlert::DARK],
);
}
// Add size field
if (!$DB->fieldExists($alert_table, 'size')) {
$migration->addField(
$alert_table,
'size',
'string',
['value' => PluginNewsAlert::MEDIUM],
);
}
// Add icon field
if (!$DB->fieldExists($alert_table, 'icon')) {
$migration->addField(
$alert_table,
'icon',
'string',
['value' => ''],
);
}
// Add display_dates field
if (!$DB->fieldExists($alert_table, 'display_dates')) {
$migration->addField(
$alert_table,
'display_dates',
'bool',
['value' => '1'],
);
}
// Build or rebuild templates data
// -> Will fill new columns (colors + icon) with the expected values for each
// templates
// -> Will update template values if changed in future updates
foreach (array_keys(PluginNewsAlert::getTypes()) as $type) {
$migration->addPostQuery(
$DB->buildUpdate(
$alert_table,
PluginNewsAlert::getTemplatesValues()[$type],
['type' => $type],
),
);
}
// $migration->addRight() does not allow to copy an existing right, we must write some custom code
$right_exist = countElementsInTable(
'glpi_profilerights',
['name' => PluginNewsAlert::$rightname],
) > 0;
// Add the same standard rights on alerts as the rights already granted on
// public reminders
if (!$right_exist) {
$reminder_rights = $DB->request([
'SELECT' => ['profiles_id', 'rights'],
'FROM' => 'glpi_profilerights',
'WHERE' => ['name' => 'reminder_public'],
]);
foreach ($reminder_rights as $row) {
$profile_id = $row['profiles_id'];
$right_value = $row['rights'] & ALLSTANDARDRIGHT;
$migration->addPostQuery($DB->buildInsert('glpi_profilerights', [
'profiles_id' => $profile_id,
'rights' => $right_value,
'name' => PluginNewsAlert::$rightname,
]));
if (($_SESSION['glpiactiveprofile']['id'] ?? null) === $profile_id) {
// Ensure menu will be displayed as soon as right is added.
$_SESSION['glpiactiveprofile'][PluginNewsAlert::$rightname] = $right_value;
unset($_SESSION['glpimenu']);
}
}
}
$migration->executeMigration();
return true;
}
function plugin_news_uninstall()
{
/** @var DBmysql $DB */
global $DB;
$DB->query('DROP TABLE IF EXISTS `glpi_plugin_news_alerts`;');
$DB->query('DROP TABLE IF EXISTS `glpi_plugin_news_profiles`;');
$DB->query('DROP TABLE IF EXISTS `glpi_plugin_news_alerts_users`;');
$DB->query('DROP TABLE IF EXISTS `glpi_plugin_news_alerts_targets`;');
$DB->query("DELETE FROM `glpi_profiles` WHERE `name` LIKE '%plugin_news%';");
$DB->query("DELETE FROM `glpi_displaypreferences` WHERE `itemtype` LIKE '%PluginNews%';");
return true;
}