Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ajax/toggleZoneDownload.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
* -------------------------------------------------------------------------
*/

use GlpiPlugin\Carbon\CarbonIntensitySource;
use GlpiPlugin\Carbon\CarbonIntensitySource_Zone;
use GlpiPlugin\Carbon\Source;
use GlpiPlugin\Carbon\Source_Zone;

include(__DIR__ . '/../../../inc/includes.php');

Expand All @@ -41,7 +41,7 @@
die();
}

if (!CarbonIntensitySource::canView()) {
if (!Source::canView()) {
// Will die
http_response_code(403);
die();
Expand All @@ -52,7 +52,7 @@
die();
}

$source_zone = new CarbonIntensitySource_Zone();
$source_zone = new Source_Zone();
if (!$source_zone->getFromDB($_GET['id'])) {
http_response_code(403);
die();
Expand Down
14 changes: 7 additions & 7 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
use GlpiPlugin\Carbon\Install;
use GlpiPlugin\Carbon\Uninstall;
use GlpiPlugin\Carbon\UsageInfo;
use GlpiPlugin\Carbon\CarbonIntensitySource;
use GlpiPlugin\Carbon\Source;
use GlpiPlugin\Carbon\Zone;
use ComputerType as GlpiComputerType;
use GlpiPlugin\Carbon\CarbonEmission;
use GlpiPlugin\Carbon\CarbonIntensity;
use MonitorType as GlpiMonitorType;
use NetworkEquipmentType as GlpiNetworkEquipmentType;
use GlpiPlugin\Carbon\CarbonIntensitySource_Zone;
use GlpiPlugin\Carbon\Source_Zone;
use GlpiPlugin\Carbon\Config;
use GlpiPlugin\Carbon\EmbodiedImpact;
use GlpiPlugin\Carbon\Location;
Expand Down Expand Up @@ -144,7 +144,7 @@ function plugin_carbon_getDropdown()
{
return [
ComputerUsageProfile::class => ComputerUsageProfile::getTypeName(),
CarbonIntensitySource::class => CarbonIntensitySource::getTypeName(),
Source::class => Source::getTypeName(),
Zone::class => Zone::getTypeName(),
CarbonIntensity::class => CarbonIntensity::getTypeName(),
];
Expand Down Expand Up @@ -212,10 +212,10 @@ function plugin_carbon_hook_add_asset(CommonDBTM $item)
if ($zone === null) {
return;
}
$source_zone = new CarbonIntensitySource_Zone();
$source_zone = new Source_Zone();
$source_zone->getFromDBByCrit([
$zone->getForeignKeyField() => $zone->fields['id'],
CarbonIntensitySource::getForeignKeyField() => $zone->fields['plugin_carbon_carbonintensitysources_id_historical'],
Source::getForeignKeyField() => $zone->fields['plugin_carbon_sources_id_historical'],
]);
if ($source_zone->isNewItem()) {
return;
Expand All @@ -239,10 +239,10 @@ function plugin_carbon_hook_update_asset(CommonDBTM $item)
if ($zone === null) {
return;
}
$source_zone = new CarbonIntensitySource_Zone();
$source_zone = new Source_Zone();
$source_zone->getFromDBByCrit([
$zone->getForeignKeyField() => $zone->fields['id'],
CarbonIntensitySource::getForeignKeyField() => $zone->fields['plugin_carbon_carbonintensitysources_id_historical'],
Source::getForeignKeyField() => $zone->fields['plugin_carbon_sources_id_historical'],
]);
if ($source_zone->isNewItem()) {
return;
Expand Down
16 changes: 7 additions & 9 deletions install/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
use Config;
use DBmysql;
use DirectoryIterator;
use Glpi\Message\MessageType;
use Glpi\Toolbox\Sanitizer;
use Migration;
use Plugin;

Expand Down Expand Up @@ -251,7 +249,7 @@ private function getMigrationsToDo(string $current_version): array
*/
public static function getOrCreateSource(string $name, int $is_fallback = 1): int
{
$source = new CarbonIntensitySource();
$source = new Source();
$source->getFromDBByCrit(['name' => $name]);
if ($source->isNewItem()) {
$source->add([
Expand Down Expand Up @@ -280,7 +278,7 @@ public static function getOrCreateZone(string $name, int $source_id): int
if ($zone->isNewItem()) {
$zone->add([
'name' => $name,
'plugin_carbon_carbonintensitysources_id_historical' => $source_id,
'plugin_carbon_sources_id_historical' => $source_id,
]);
/** @phpstan-ignore if.alwaysTrue */
if ($zone->isNewItem()) {
Expand All @@ -300,15 +298,15 @@ public static function getOrCreateZone(string $name, int $source_id): int
*/
public static function linkSourceZone(int $source_id, int $zone_id): int
{
$source_zone = new CarbonIntensitySource_Zone();
$source_zone = new Source_Zone();
$source_zone->getFromDBByCrit([
'plugin_carbon_carbonintensitysources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id,
'plugin_carbon_sources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id,
]);
if ($source_zone->isNewItem()) {
$source_zone->add([
'plugin_carbon_carbonintensitysources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id,
'plugin_carbon_sources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id,
]);
}

Expand Down
22 changes: 16 additions & 6 deletions install/install/init_datasources.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,20 @@
use GlpiPlugin\Carbon\Install;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;
use GlpiPlugin\Carbon\Source;
use GlpiPlugin\Carbon\Source_Zone;
use GlpiPlugin\Carbon\Zone;

/** @var DBmysql $DB */
global $DB;

// This file ys executed at the end of an upgrade.
// Upgrade to 1.2.0 changed the tables modified by this code
// then we need to reset DB columns cache
$DB->listFields(getTableForItemType(Source::class), false);
$DB->listFields(getTableForItemType(Source_Zone::class), false);
$DB->listFields(getTableForItemType(Zone::class), false);

$source_id = Install::getOrCreateSource('RTE', 0);
$zone_id = Install::getOrCreateZone('France', $source_id);

Expand Down Expand Up @@ -94,13 +104,13 @@
// Insert into the database
$row_exists = (new DbUtils())->countElementsInTable($table, [
'date' => "$year-01-01 00:00:00",
'plugin_carbon_carbonintensitysources_id' => $source_id,
'plugin_carbon_sources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id
]);
if (!$row_exists) {
$success = $DB->insert($table, [
'date' => "$year-01-01 00:00:00",
'plugin_carbon_carbonintensitysources_id' => $source_id,
'plugin_carbon_sources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id,
'intensity' => $intensity,
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
Expand All @@ -111,7 +121,7 @@
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
], [
'date' => "$year-01-01 00:00:00",
'plugin_carbon_carbonintensitysources_id' => $source_id,
'plugin_carbon_sources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id
]);
}
Expand All @@ -134,13 +144,13 @@
foreach ($quebec_carbon_intensity as $year => $intensity) {
$row_exists = (new DbUtils())->countElementsInTable($table, [
'date' => "$year-01-01 00:00:00",
'plugin_carbon_carbonintensitysources_id' => $source_id,
'plugin_carbon_sources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id_quebec,
]);
if (!$row_exists) {
$success = $DB->insert($table, [
'date' => "$year-01-01 00:00:00",
'plugin_carbon_carbonintensitysources_id' => $source_id,
'plugin_carbon_sources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id_quebec,
'intensity' => $intensity,
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
Expand All @@ -151,7 +161,7 @@
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
], [
'date' => "$year-01-01 00:00:00",
'plugin_carbon_carbonintensitysources_id' => $source_id,
'plugin_carbon_carbonintensitsources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id
]);
}
Expand Down
69 changes: 69 additions & 0 deletions install/migration/update_1.1.0_to_1.2.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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/>.
*
* -------------------------------------------------------------------------
*/

function update110to120(Migration $migration)
{
/** @var DBmysql $DB */
global $DB;

$updateresult = true;
$from_version = '1.1.0';
$to_version = '1.2.0';
$update_dir = __DIR__ . "/update_{$from_version}_to_{$to_version}/";

//TRANS: %s is the number of new version
$migration->addInfoMessage(sprintf(__('Update to %s'), $to_version));
$migration->setVersion($to_version);

// New tables from enpty.sql file after the migration
// If a script requires a new table, it may create it by itself

$update_scripts = scandir($update_dir);
natcasesort($update_scripts);
foreach ($update_scripts as $update_script) {
if (preg_match('/\.php$/', $update_script) !== 1) {
continue;
}
require $update_dir . $update_script;
}

$dbFile = plugin_carbon_getSchemaPath($to_version);
if ($dbFile === null || !$DB->runFile($dbFile)) {
$migration->addWarningMessage("Error creating tables : " . $DB->error());
$updateresult = false;
}

// ************ Keep it at the end **************
$migration->executeMigration();

return $updateresult;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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/>.
*
* -------------------------------------------------------------------------
*/

/** @var DBmysql $DB */
/** @var Migration $migration */

// Rename CarbonIntensitySource
// Move data to new table
$old_table = 'glpi_plugin_carbon_carbonintensitysources';
$new_table = 'glpi_plugin_carbon_sources';
$migration->renameTable($old_table, $new_table);

// Rename CarbonIntensitySource_Zone
$old_table = 'glpi_plugin_carbon_carbonintensitysources_zones';
$new_table = 'glpi_plugin_carbon_sources_zones';
$migration->renameTable($old_table, $new_table);

$table = 'glpi_plugin_carbon_carbonintensities';
$migration->changeField($table, 'plugin_carbon_carbonintensitysources_id', 'plugin_carbon_sources_id', 'fkey');

$table = 'glpi_plugin_carbon_zones';
$migration->changeField($table, 'plugin_carbon_carbonintensitysources_id_historical', 'plugin_carbon_sources_id_historical', 'fkey');
$migration->dropKey($table, 'plugin_carbon_carbonintensitysources_id_historical');
$migration->addKey($table, 'plugin_carbon_sources_id_historical', 'plugin_carbon_sources_id_historical');


$table = 'glpi_plugin_carbon_sources_zones';
$migration->changeField($table, 'plugin_carbon_carbonintensitysources_id', 'plugin_carbon_sources_id', 'fkey');
Loading