-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhook.php
More file actions
398 lines (363 loc) · 11.9 KB
/
hook.php
File metadata and controls
398 lines (363 loc) · 11.9 KB
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
<?php
/**
* -------------------------------------------------------------------------
* Carbon plugin for GLPI
*
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
* @link https://github.com/pluginsGLPI/carbon
*
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Carbon plugin for GLPI.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/
use Computer as GlpiComputer;
use Monitor as GlpiMonitor;
use NetworkEquipment as GlpiNetworkEquipment;
use Glpi\Dashboard\Right as GlpiDashboardRight;
use GlpiPlugin\Carbon\ComputerType;
use GlpiPlugin\Carbon\ComputerUsageProfile;
use GlpiPlugin\Carbon\Install;
use GlpiPlugin\Carbon\Uninstall;
use GlpiPlugin\Carbon\UsageInfo;
use GlpiPlugin\Carbon\Source;
use GlpiPlugin\Carbon\Zone;
use ComputerType as GlpiComputerType;
use GlpiPlugin\Carbon\CarbonEmission;
use GlpiPlugin\Carbon\CarbonIntensity;
use GlpiPlugin\Carbon\CommonAsset;
use MonitorType as GlpiMonitorType;
use NetworkEquipmentType as GlpiNetworkEquipmentType;
use GlpiPlugin\Carbon\Source_Zone;
use GlpiPlugin\Carbon\Config;
use GlpiPlugin\Carbon\EmbodiedImpact;
use GlpiPlugin\Carbon\Location;
use GlpiPlugin\Carbon\MonitorType;
use GlpiPlugin\Carbon\NetworkEquipmentType;
use GlpiPlugin\Carbon\SearchOptions;
use GlpiPlugin\Carbon\Toolbox;
use Location as GlpiLocation;
use Profile as GlpiProfile;
use Toolbox as GlpiToolbox;
/**
* Plugin install process
* supported arguments for upgrade process
* -p force-upgrade : force execution of upgrade from the previous version
* or from the version specified wih version argument
* -p version : specifi the version to begin a forced upgrade
* -p reset-report-dashboard : delete then recreate the dashboard of the reporting page
*
* @return boolean
*/
function plugin_carbon_install(array $args = []): bool
{
if (!is_readable(__DIR__ . '/install/Install.php')) {
return false;
}
require_once(__DIR__ . '/install/Install.php');
$version = Install::detectVersion();
$install = new Install(new Migration(PLUGIN_CARBON_VERSION));
$success = true;
$silent = !isCommandLine() && $_SESSION['glpi_use_mode'] !== Session::DEBUG_MODE;
if ($silent) {
// do not output messages
ob_start();
}
try {
if ($version === '0.0.0') {
$success = $install->install($args);
} else {
$success = $install->upgrade($version, $args);
}
} catch (\RuntimeException $e) {
if (isCommandLine()) {
throw $e;
}
$error_footer = '<br />' . __('Please check the logs for more details. Fill an issue in the repository of the plugin.', 'carbon');
Session::addMessageAfterRedirect($e->getMessage() . $error_footer, false, ERROR);
$success = false;
}
if ($silent) {
// do not output messages
ob_end_clean();
}
return $success;
}
/**
* Plugin uninstall process
*
* @return boolean
*/
function plugin_carbon_uninstall(): bool
{
if (!is_readable(__DIR__ . '/install/Uninstall.php')) {
return false;
}
require_once(__DIR__ . '/install/Uninstall.php');
$uninstall = new Uninstall();
try {
$uninstall->uninstall();
} catch (\Exception $e) {
$backtrace = GlpiToolbox::backtrace('');
trigger_error($e->getMessage() . PHP_EOL . $backtrace, E_USER_WARNING);
return false;
}
return true;
}
function plugin_carbon_getDropdown()
{
return [
ComputerUsageProfile::class => ComputerUsageProfile::getTypeName(),
Source::class => Source::getTypeName(),
Zone::class => Zone::getTypeName(),
CarbonIntensity::class => CarbonIntensity::getTypeName(),
];
}
function plugin_carbon_postShowTab(array $param)
{
if ($param['options']['itemtype'] !== UsageInfo::class) {
return;
}
$asset_itemtype = $param['item']::getType();
if (!in_array($asset_itemtype, PLUGIN_CARBON_TYPES)) {
return;
}
$history_class = 'GlpiPlugin\\Carbon\\Impact\\History\\' . $asset_itemtype;
$history_class::showHistorizableDiagnosis($param['item']);
UsageInfo::showCharts($param['item']);
}
/**
* Add search options to core itemtypes
*
* @template T of CommonDBTM
* @param class-string<T> $itemtype
* @return array
*/
function plugin_carbon_getAddSearchOptionsNew($itemtype): array
{
return SearchOptions::getCoreSearchOptions($itemtype);
}
/**
* Callback when an object is added into the database
*
* @param CommonDBTM $item item being added
* @return void
*/
function plugin_carbon_hook_add_asset(CommonDBTM $item)
{
if (!in_array($item::getType(), PLUGIN_CARBON_TYPES)) {
return;
}
$location_fk = GlpiLocation::getForeignKeyField();
if (!in_array($location_fk, array_keys($item->fields))) {
return;
}
if (GlpiLocation::isNewID($item->fields[$location_fk])) {
return;
}
$source_zone = new Source_Zone();
if ($source_zone->getFromDbByItem($item) === false) {
return;
}
$source_zone->toggleZone(true);
}
/**
* Callback when an item is being updated in the database
*
* @param CommonDBTM $item item being updated
* @return void
*/
function plugin_carbon_hook_update_asset(CommonDBTM $item)
{
if (!in_array($item::getType(), PLUGIN_CARBON_TYPES)) {
return;
}
$item_table = $item::getTable();
$location_table = getTableForItemType(Location::class);
$source_zone_table = getTableForItemType(Source_Zone::class);
$request = [
'INNER JOIN' => [
$location_table => [
'ON' => [
$source_zone_table => 'id',
$location_table => 'plugin_carbon_sources_zones_id',
]
],
$item_table => [
'ON' => [
$item_table => 'locations_id',
$location_table => 'locations_id',
]
],
],
'WHERE' => [
$item::getTableField('id') => $item->getID(),
]
];
$source_zone = new Source_Zone();
if (!$source_zone->getFromDBByRequest($request)) {
return;
}
$source_zone->toggleZone(true);
}
/**
* Callback when an asset is being purged from the database
*
* @param CommonDBTM $item
* @return void
*/
function plugin_carbon_hook_pre_purge_asset(CommonDBTM $item)
{
if (!in_array($item::getType(), PLUGIN_CARBON_TYPES)) {
return;
}
$itemtype = $item->getType();
$item_id = $item->getID();
$carbon_emission = new CarbonEmission();
$carbon_emission->deleteByCriteria([
'itemtype' => $itemtype,
'items_id' => $item_id
]);
$embodied_impact = new EmbodiedImpact();
$embodied_impact->deleteByCriteria([
'itemtype' => $itemtype,
'items_id' => $item_id
]);
$usage_info = new UsageInfo();
$usage_info->deleteByCriteria([
'itemtype' => $itemtype,
'items_id' => $item_id
]);
}
/**
* Delete plugin's data linked to asset types
*
* @param CommonDBTM $item
* @return void
*/
function plugin_carbon_hook_pre_purge_assettype(CommonDBTM $item)
{
$itemtype = $item->getType();
$pos = strrpos($itemtype, 'Type');
if ($pos !== strlen($itemtype) - 4) { // 4 is length of 'Type'
return;
}
$asset_itemtype = substr($itemtype, 0, $pos);
if (!in_array($asset_itemtype, PLUGIN_CARBON_TYPES)) {
return;
}
$carbon_type_itemtype = 'GlpiPlugin\\Carbon\\' . $itemtype;
$carbon_type = new $carbon_type_itemtype();
$carbon_type->deleteByCriteria([
$item->getForeignKeyField() => $item->getID(),
]);
}
function plugin_carbon_MassiveActions($itemtype)
{
switch ($itemtype) {
case GlpiComputer::class:
return [
ComputerUsageProfile::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'MassAssociateItems' => __('Associate to an usage profile', 'carbon'),
CommonAsset::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'MassDeleteAllImpacts' => __('Delete all calculated environmental impacts', 'carbon'),
];
case GlpiMonitor::class:
case GlpiNetworkEquipment::class:
return [
CommonAsset::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'MassDeleteAllImpacts' => __('Delete all calculated environmental impacts', 'carbon'),
];
case GlpiComputerType::class:
return [
ComputerType::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'MassUpdatePower' => __('Update type power consumption', 'carbon'),
ComputerType::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'MassUpdateCategory' => __('Update category', 'carbon'),
];
case GlpiMonitorType::class:
return [
MonitorType::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'MassUpdatePower' => __('Update type power consumption', 'carbon'),
];
case GlpiNetworkEquipmentType::class:
return [
NetworkEquipmentType::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'MassUpdatePower' => __('Update type power consumption', 'carbon'),
];
case GlpiLocation::class:
return [
Location::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'MassUpdateBoaviztaZone' => __('Update zone for Boavizta engine', 'carbon'),
];
}
return [];
}
function plugin_carbon_profileAdd(CommonDBTM $item)
{
if (!isset($item->input['_carbon:report']['1_0'])) {
// Access to reporting not affected
return;
}
if (($dashboard_id = Toolbox::getDashboardId()) === null) {
// Dashboard of the plugin notfound (should not happen)
return;
}
$dashboard_right = new GlpiDashboardRight();
$grant_access = ($item->input['_carbon:report']['1_0'] == 1);
$dashboard_right->getFromDBByCrit([
'itemtype' => GlpiProfile::class,
'items_id' => $item->getID(),
'dashboards_dashboards_id' => $dashboard_id,
]);
if ($grant_access) {
// Create right for profile if not exists
if ($dashboard_right->isNewItem()) {
$dashboard_right->add([
'itemtype' => GlpiProfile::class,
'items_id' => $item->getID(),
'dashboards_dashboards_id' => $dashboard_id,
]);
}
return;
}
// delete right if exists
if (!$dashboard_right->isNewItem()) {
$dashboard_right->delete([
'id' => $dashboard_right->getID(),
]);
}
}
function plugin_carbon_profileUpdate(CommonDBTM $item)
{
plugin_carbon_profileAdd($item);
}
function plugin_carbon_locationAdd(CommonDBTM $item)
{
$location = new Location();
$location->onGlpiLocationAdd($item, Config::getGeocoder());
}
function plugin_carbon_locationPreUpdate(CommonDBTM $item)
{
$location = new Location();
$location->onGlpiLocationPreUpdate($item, Config::getGeocoder());
}
function plugin_carbon_locationUpdate(CommonDBTM $item)
{
$location = new Location();
$location->onGlpiLocationUpdate($item);
}
function plugin_carbon_locationPrePurge(CommonDBTM $item)
{
$location = new Location();
$location->onGlpiLocationPrePurge($item);
}