-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook.php
239 lines (220 loc) · 9.41 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
<?php
/*
-------------------------------------------------------------------------
Web Resources Plugin for GLPI
Copyright (C) 2019-2021 by Curtis Conard
https://github.com/cconard96/glpi-webresources-plugin
-------------------------------------------------------------------------
LICENSE
This file is part of Web Resources Plugin for GLPI.
Web Resources Plugin for GLPI 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.
Web Resources Plugin for GLPI 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 Web Resources Plugin for GLPI. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
function plugin_webresources_install()
{
global $DB;
$res_table = PluginWebresourcesResource::getTable();
$res_entity_table = PluginWebresourcesResource_Entity::getTable();
$res_profile_table = PluginWebresourcesResource_Profile::getTable();
$res_group_table = PluginWebresourcesResource_Group::getTable();
$res_user_table = PluginWebresourcesResource_User::getTable();
$clean_install = false;
if (!$DB->tableExists($res_table)) {
$query = "CREATE TABLE `{$res_table}` (
`id` int(11) NOT NULL auto_increment,
`users_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`link` varchar(255) NOT NULL,
`icon` varchar(255) DEFAULT NULL,
`color` varchar(16) NOT NULL DEFAULT '#000000',
`plugin_webresources_categories_id` int(11) DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->queryOrDie($query, 'Error creating Web Resource table' . $DB->error());
$clean_install = true;
}
if (!$DB->tableExists($res_entity_table)) {
$query = "CREATE TABLE `{$res_entity_table}` (
`id` int(11) NOT NULL auto_increment,
`plugin_webresources_resources_id` int(11) NOT NULL,
`entities_id` int(11) NOT NULL,
`is_recursive` tinyint(1) DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->queryOrDie($query, 'Error creating Web Resource Entity table' . $DB->error());
}
if (!$DB->tableExists($res_profile_table)) {
$query = "CREATE TABLE `{$res_profile_table}` (
`id` int(11) NOT NULL auto_increment,
`plugin_webresources_resources_id` int(11) NOT NULL,
`profiles_id` int(11) NOT NULL,
`entities_id` int(11) NOT NULL,
`is_recursive` tinyint(1) DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->queryOrDie($query, 'Error creating Web Resource Profile table' . $DB->error());
}
if (!$DB->tableExists($res_group_table)) {
$query = "CREATE TABLE `{$res_group_table}` (
`id` int(11) NOT NULL auto_increment,
`plugin_webresources_resources_id` int(11) NOT NULL,
`groups_id` int(11) NOT NULL,
`entities_id` int(11) NOT NULL,
`is_recursive` tinyint(1) DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->queryOrDie($query, 'Error creating Web Resource Group table' . $DB->error());
}
if (!$DB->tableExists($res_user_table)) {
$query = "CREATE TABLE `{$res_user_table}` (
`id` int(11) NOT NULL auto_increment,
`plugin_webresources_resources_id` int(11) NOT NULL,
`users_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->queryOrDie($query, 'Error creating Web Resource User table' . $DB->error());
}
$cat_table = PluginWebresourcesCategory::getTable();
if (!$DB->tableExists($cat_table)) {
$query = "CREATE TABLE `{$cat_table}` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`comment` TEXT DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->queryOrDie($query, 'Error creating Web Resource Category table' . $DB->error());
}
if (!$DB->tableExists('glpi_plugin_webresources_autoicons')) {
$query = "CREATE TABLE `glpi_plugin_webresources_autoicons` (
`itemtype` varchar(100) NOT NULL,
`items_id` int(11) NOT NULL,
`icon` varchar(255) DEFAULT NULL,
`color` varchar(16) NOT NULL DEFAULT '#000000',
PRIMARY KEY (`itemtype`, `items_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->queryOrDie($query, 'Error creating Web Resource auto-icon table' . $DB->error());
}
if (!count(Config::getConfigurationValues('plugin:Webresources'))) {
Config::setConfigurationValues('plugin:Webresources', [
'config_class' => PluginWebresourcesConfig::class,
'menu' => 'plugins',
'use_duckduckgo' => 0,
'use_google' => 0,
]);
}
$migration = new Migration(PLUGIN_WEBRESOURCES_VERSION);
if ($clean_install) {
$migration->addRight(PluginWebresourcesResource::$rightname);
}
$migration->executeMigration();
return true;
}
function plugin_webresources_uninstall()
{
global $DB;
$tables = [PluginWebresourcesResource::getTable(), PluginWebresourcesResource_Entity::getTable(),
PluginWebresourcesResource_Profile::getTable(), PluginWebresourcesResource_Group::getTable(),
PluginWebresourcesResource_User::getTable(), PluginWebresourcesCategory::getTable(), 'glpi_plugin_webresources_autoicons'];
foreach ($tables as $table) {
if ($DB->tableExists($table)) {
$DB->queryOrDie('DROP TABLE'.$DB::quoteName($table));
}
}
Config::deleteConfigurationValues('plugin:Webresources', [
'config_class',
'use_duckduckgo',
'use_google'
]);
return true;
}
function plugin_webresources_getDropdown() {
return ['PluginWebresourcesCategory' => PluginWebresourcesCategory::getTypeName(2)];
}
function plugin_webresources_showPostItemForm(array $params)
{
global $DB;
static $supported_types = [Entity::class, Supplier::class];
$item = $params['item'];
if (in_array($item::getType(), $supported_types, true)) {
if ($item::getType() === 'Entity' && $_REQUEST['_glpi_tab'] !== 'Entity$main') {
return $params;
}
$iterator = $DB->request([
'SELECT' => ['icon', 'color'],
'FROM' => 'glpi_plugin_webresources_autoicons',
'WHERE' => [
'itemtype' => $item::getType(),
'items_id' => $item->getID()
]
]);
$ico = '';
$color = '#808080';
if (count($iterator)) {
$data = $iterator->current();
$ico = $data['icon'];
$color = $data['color'];
}
$out = '<tr><td>'.__('Icon', 'webresources').'</td><td>';
$out .= Html::input('webresources_icon', [
'value' => $ico
]);
$out .= '</td><td>'.__('Icon color', 'webresources').'</td><td>';
$out .= Html::showColorField('webresources_color', [
'value' => $color,
'display' => false
]);
$out .= '</td></tr>';
echo $out;
}
return $params;
}
function plugin_webresources_preupdateitem(CommonDBTM $item)
{
global $DB;
static $supported_types = [Entity::class, Supplier::class, Appliance::class];
if (isset($item->input['webresources_icon']) && in_array($item::getType(), $supported_types, true)) {
$DB->updateOrInsert('glpi_plugin_webresources_autoicons', [
'itemtype' => $item::getType(),
'items_id' => $item->getID(),
'icon' => $item->input['webresources_icon'],
'color' => $item->input['webresources_color']
], [
'itemtype' => $item::getType(),
'items_id' => $item->getID(),
]);
unset($item->input['webresources_icon']);
unset($item->input['webresources_color']);
}
}
function plugin_webresources_preItemPurge(CommonDBTM $item)
{
global $DB;
static $supported_types = [Entity::class, Supplier::class, Appliance::class];
if (in_array($item::getType(), $supported_types, true)) {
$DB->delete('glpi_plugin_webresources_autoicons', [
'itemtype' => $item::getType(),
'items_id' => $item->getID(),
]);
}
}
function plugin_webresources_redefine_menus($menu)
{
$menu['webresources'] = [
'title' => PluginWebresourcesResource::getTypeName(Session::getPluralNumber()),
'default' => PluginWebresourcesDashboard::getSearchURL(false),
'icon' => PluginWebresourcesDashboard::getIcon(),
'links' => [
'search' => PluginWebresourcesDashboard::getSearchURL(false),
],
];
return $menu;
}