|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * ------------------------------------------------------------------------- |
| 5 | + * Carbon plugin for GLPI |
| 6 | + * |
| 7 | + * @copyright Copyright (C) 2024-2025 Teclib' and contributors. |
| 8 | + * @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+ |
| 9 | + * @link https://github.com/pluginsGLPI/carbon |
| 10 | + * |
| 11 | + * ------------------------------------------------------------------------- |
| 12 | + * |
| 13 | + * LICENSE |
| 14 | + * |
| 15 | + * This file is part of Carbon plugin for GLPI. |
| 16 | + * |
| 17 | + * This program is free software: you can redistribute it and/or modify |
| 18 | + * it under the terms of the GNU General Public License as published by |
| 19 | + * the Free Software Foundation, either version 3 of the License, or |
| 20 | + * (at your option) any later version. |
| 21 | + * |
| 22 | + * This program is distributed in the hope that it will be useful, |
| 23 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | + * GNU General Public License for more details. |
| 26 | + * |
| 27 | + * You should have received a copy of the GNU General Public License |
| 28 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 29 | + * |
| 30 | + * ------------------------------------------------------------------------- |
| 31 | + */ |
| 32 | + |
| 33 | +use GlpiPlugin\Carbon\CarbonEmission; |
| 34 | +use GlpiPlugin\Carbon\CarbonIntensitySource; |
| 35 | +use GlpiPlugin\Carbon\CarbonIntensitySource_Zone; |
| 36 | +use GlpiPlugin\Carbon\Zone; |
| 37 | + |
| 38 | +/** @var DBmysql $DB */ |
| 39 | +global $DB; |
| 40 | + |
| 41 | +$db_utils = new DbUtils(); |
| 42 | + |
| 43 | +// Update and fix the bad relation |
| 44 | +$source_table = $db_utils->getTableForItemType(CarbonIntensitySource::class); |
| 45 | +$zone_table = $db_utils->getTableForItemType(Zone::class); |
| 46 | +$source_zone_table = $db_utils->getTableForItemType(CarbonIntensitySource_Zone::class); |
| 47 | +$source_iterator = $DB->request([ |
| 48 | + 'SELECT' => 'id', |
| 49 | + 'FROM' => $source_table, |
| 50 | + 'WHERE' => ['name' => 'Hydro Quebec'] |
| 51 | +]); |
| 52 | +$zone_iterator = $DB->request([ |
| 53 | + 'SELECT' => 'id', |
| 54 | + 'FROM' => $zone_table, |
| 55 | + 'WHERE' => ['name' => 'Quebec'] |
| 56 | +]); |
| 57 | +if ($source_iterator->count() && $zone_iterator->count()) { |
| 58 | + $DB->update($source_zone_table, [ |
| 59 | + 'plugin_carbon_zones_id' => $zone_iterator->current()['id'], |
| 60 | + ], [ |
| 61 | + 'plugin_carbon_carbonintensitysources_id' => $source_iterator->current()['id'], |
| 62 | + ]); |
| 63 | +} |
| 64 | + |
| 65 | +// delete carbon intensity results for computers in Quebec |
| 66 | +$itemtypes = [ |
| 67 | + Computer::class, |
| 68 | + Monitor::class, |
| 69 | + NetworkEquipment::class |
| 70 | +]; |
| 71 | + |
| 72 | +$carbon_emission_table = $db_utils->getTableForItemType(CarbonEmission::class); |
| 73 | +$location_table = $db_utils->getTableForItemType(Location::class); |
| 74 | +foreach ($itemtypes as $itemtype) { |
| 75 | + $item_table = $db_utils->getTableForItemType($itemtype); |
| 76 | + $request = [ |
| 77 | + 'SELECT' => $item_table . '.id', |
| 78 | + 'FROM' => $item_table, |
| 79 | + 'INNER JOIN' => [ |
| 80 | + $location_table => [ |
| 81 | + 'FKEY' => [ |
| 82 | + $location_table => 'id', |
| 83 | + $item_table => 'locations_id' |
| 84 | + ] |
| 85 | + ] |
| 86 | + ], |
| 87 | + 'WHERE' => [ |
| 88 | + Location::getTableField('state') => 'Quebec', |
| 89 | + ] |
| 90 | + ]; |
| 91 | + $subquery = new QuerySubQuery($request); |
| 92 | + $DB->delete( |
| 93 | + $carbon_emission_table, |
| 94 | + [ |
| 95 | + 'itemtype' => $itemtype, |
| 96 | + 'items_id' => $subquery |
| 97 | + ] |
| 98 | + ); |
| 99 | +} |
0 commit comments