Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
50575bf
bump(1.2.0-dev): 1.2.0-dev
btry Sep 19, 2025
536ad65
refactor(CarbonIntensitySource): rename object to Source
btry Sep 19, 2025
c7e0ab8
feat(Location): move plugin specific field for location to a tab
btry Sep 19, 2025
42485e6
fix(ComputerType,MonitorType): more conststent right checks
btry Sep 23, 2025
465d819
feat(Location): associate location to a carbon intensity zone
btry Sep 29, 2025
46340a1
style(Impact\History\AbstractAsset): code cleanup
btry Oct 1, 2025
532f3ca
fix(Impact\History\AbstractAsset): test memory before calculating a c…
btry Oct 1, 2025
a2cefa3
test: replace deprecated methods
btry Oct 1, 2025
74f1772
refactor(Zone): set alias to location class
btry Oct 1, 2025
1174bd2
feat(Zone): update algorithms to find zone from location or asset
btry Oct 2, 2025
fcd54a9
test(Install): add message on failure
btry Oct 3, 2025
33e814d
tst(Location): fix test method name
btry Oct 3, 2025
d2a5926
test(Impact\History\Monitor): update out of date itemtype
btry Oct 3, 2025
1e67bca
test: update tests with location /zone relation
btry Oct 3, 2025
6f485d7
feat(Location): associate location to a carbon intensity zone
btry Sep 29, 2025
f923abc
fix(Install): avoid use of non-existing classes in migration code
btry Oct 6, 2025
6b950d8
style: php lint
btry Oct 6, 2025
9193b99
style(Zone): remove unused var
btry Oct 6, 2025
d5e2f98
style(Location): twig code style
btry Oct 6, 2025
0cdbfbf
feat(Location): update data completion diagnosis
btry Oct 6, 2025
8a699d9
fix(Install): reoder upgrade steps
btry Oct 8, 2025
a31fecb
feat(Location): remove location / carbon intensity relation based on …
btry Oct 13, 2025
8c87121
feat(Install): migrate location / zone relation
btry Oct 14, 2025
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
52 changes: 52 additions & 0 deletions ajax/dropdownZone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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 GlpiPlugin\Carbon\Source_Zone;
use GlpiPlugin\Carbon\Zone;

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

// Check if plugin is activated...
if (!Plugin::isPluginActive('carbon')) {
http_response_code(404);
die();
}

// throw new RuntimeException('Required argument missing or incorrect!');

$source_zone_table = Source_Zone::getTable();
$zone_table = Zone::getTable();
$source_id = (int) $_POST['plugin_carbon_sources_id'];
Zone::dropdown([
'condition' => Zone::getRestrictBySourceCondition($source_id),
'readonly' => ($source_id == 0)
]);
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
6 changes: 3 additions & 3 deletions front/computertype.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@

use Glpi\Exception\Http\NotFoundHttpException;
use GlpiPlugin\Carbon\ComputerType;
use ComputerType as GlpiComputerType;

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

if (!Plugin::isPluginActive('carbon')) {
throw new NotFoundHttpException();
}

Session::checkRight('config', UPDATE);
Session::checkRight(GlpiComputerType::$rightname, UPDATE);

$item = new ComputerType();

if (isset($_POST['update'])) {
// Add a new Form
Session::checkRight('entity', UPDATE);
Session::checkRight(GlpiComputerType::$rightname, UPDATE);
$item->update($_POST);
Html::back();
}
Expand Down
53 changes: 53 additions & 0 deletions front/location.form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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 Glpi\Exception\Http\NotFoundHttpException;
use GlpiPlugin\Carbon\Location;
use Location as GlpiLocation;

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

if (!Plugin::isPluginActive('carbon')) {
throw new NotFoundHttpException();
}

Session::checkRight(GlpiLocation::$rightname, UPDATE);

$item = new Location();

if (isset($_POST['update'])) {
Session::checkRight(GlpiLocation::$rightname, UPDATE);
$item->update($_POST);
Html::back();
}

Html::back();
6 changes: 3 additions & 3 deletions front/monitortype.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@

use GlpiPlugin\Carbon\MonitorType;
use Glpi\Exception\Http\NotFoundHttpException;
use MonitorType as GlpiMonitorType;

if (!Plugin::isPluginActive('carbon')) {
throw new NotFoundHttpException();
}

Session::checkRight('config', UPDATE);
Session::checkRight(GlpiMonitorType::$rightname, UPDATE);

$item = new MonitorType();

if (isset($_POST['update'])) {
// Add a new Form
Session::checkRight('entity', UPDATE);
Session::checkRight(GlpiMonitorType::$rightname, UPDATE);
$item->update($_POST);
Html::back();
}
Expand Down
6 changes: 3 additions & 3 deletions front/networkequipmenttype.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@

use Glpi\Exception\Http\NotFoundHttpException;
use GlpiPlugin\Carbon\NetworkEquipmentType;
use NetworkEquipmentType as GlpiNetworkEquipmentType;

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

if (!Plugin::isPluginActive('carbon')) {
throw new NotFoundHttpException();
}

Session::checkRight('config', UPDATE);
Session::checkRight(GlpiNetworkEquipmentType::$rightname, UPDATE);

$item = new NetworkEquipmentType();

if (isset($_POST['update'])) {
// Add a new Form
Session::checkRight('entity', UPDATE);
Session::checkRight(GlpiNetworkEquipmentType::$rightname, UPDATE);
$item->update($_POST);
Html::back();
}
Expand Down
52 changes: 26 additions & 26 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 @@ -120,7 +120,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 @@ -152,25 +152,25 @@ function plugin_carbon_getAddSearchOptionsNew($itemtype): array
return SearchOptions::getCoreSearchOptions($itemtype);
}

/**
* Callback before showing save / update button on an item form
*
* @param array $params 'item' => CommonDBTM
* 'options => array
* @return void
*/
function plugin_carbon_postItemForm(array $params)
{
switch ($params['item']->getType()) {
case GlpiLocation::class:
$location = new Location();
$location->getFromDBByCrit([
GlpiLocation::getForeignKeyField() => $params['item']->getID(),
]);
$location->showForm($location->getID());
break;
}
}
// /**
// * Callback before showing save / update button on an item form
// *
// * @param array $params 'item' => CommonDBTM
// * 'options => array
// * @return void
// */
// function plugin_carbon_postItemForm(array $params)
// {
// switch ($params['item']->getType()) {
// case GlpiLocation::class:
// $location = new Location();
// $location->getFromDBByCrit([
// GlpiLocation::getForeignKeyField() => $params['item']->getID(),
// ]);
// $location->showForm($location->getID());
// break;
// }
// }

function plugin_carbon_hook_add_asset(CommonDBTM $item)
{
Expand All @@ -188,10 +188,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 @@ -215,10 +215,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
37 changes: 23 additions & 14 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 @@ -247,16 +245,18 @@ private function getMigrationsToDo(string $current_version): array
*
* @param string $name Name of the zone
* @param int $is_fallback Is the zone a fallback zone (1) or not (0)
* @param int $is_carbon_intensity_source Does the source provide carbon intensity (1) or not (0)
* @return int ID of the zone
*/
public static function getOrCreateSource(string $name, int $is_fallback = 1): int
public static function getOrCreateSource(string $name, int $is_fallback = 1, int $is_carbon_intensity_source = 1): int
{
$source = new CarbonIntensitySource();
$source = new Source();
$source->getFromDBByCrit(['name' => $name]);
if ($source->isNewItem()) {
$source->add([
'name' => $name,
'is_fallback' => $is_fallback,
'is_carbon_intensity_source' => $is_carbon_intensity_source,
]);
/** @phpstan-ignore if.alwaysTrue */
if ($source->isNewItem()) {
Expand All @@ -280,7 +280,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 @@ -294,22 +294,31 @@ public static function getOrCreateZone(string $name, int $source_id): int
/**
* Link a carbon intensity source to a zone
*
* @param int $source_id ID of the carbon intensity source
* @param int $zone_id ID of the zone
* @return int ID of the link
* @param int $source_id ID of the carbon intensity source
* @param int $zone_id ID of the zone
* @param string $code Identifier of the zone used by the source
* @return int ID of the link
*/
public static function linkSourceZone(int $source_id, int $zone_id): int
public static function linkSourceZone(int $source_id, int $zone_id, string $code = ''): 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,
'code' => $code
]);
} else {
if ($source_zone->fields['code'] !== $code) {
$source_zone->update([
'id' => $source_zone->getID(),
'code' => $code,
]);
}
}

return $source_zone->getID();
Expand Down
Loading
Loading