Skip to content

Commit 89a88e3

Browse files
committed
feat: show all individual impacts
1 parent f356f2b commit 89a88e3

6 files changed

Lines changed: 126 additions & 77 deletions

File tree

src/AbstractImpact.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
namespace GlpiPlugin\Carbon;
34+
35+
use CommonDBChild;
36+
use GlpiPlugin\Carbon\Impact\Type;
37+
38+
abstract class AbstractImpact extends CommonDBChild
39+
{
40+
public static $rightname = 'carbon:report';
41+
42+
/**
43+
* Get impact value in a human r eadable format, selecting the best unit
44+
*
45+
* @param string $field the field of the impact
46+
*/
47+
public function getHumanReadableImpact(string $field): string
48+
{
49+
return Toolbox::getHumanReadableValue(
50+
$this->fields[$field],
51+
Type::getImpactUnit($field)
52+
);
53+
}
54+
}

src/EmbodiedImpact.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@
4343
*
4444
* Embodied impact is the impact of the asset while it is built and destroyed or recycled
4545
*/
46-
class EmbodiedImpact extends CommonDBTM
46+
class EmbodiedImpact extends AbstractImpact
4747
{
48-
public static $rightname = 'carbon:report';
49-
5048
public function canEdit($ID): bool
5149
{
5250
return false;
@@ -193,21 +191,4 @@ public function calculateImpact(string $lca_type, int $limit = 0): int
193191

194192
return $iterator->count();
195193
}
196-
197-
/**
198-
* Get impact value in a human r eadable format, selecting the best unit
199-
*/
200-
public function getHumanReadableImpact(string $field): string
201-
{
202-
switch ($field) {
203-
case 'gwp':
204-
return Toolbox::getWeight($this->fields[$field]) . 'CO2eq';
205-
case 'adp':
206-
return Toolbox::getWeight($this->fields[$field]) . 'Sbeq';
207-
case 'pe':
208-
return Toolbox::getEnergy($this->fields[$field] / 3600); // Convert J into Wh
209-
}
210-
211-
return '';
212-
}
213194
}

src/Impact/Type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public static function getCriteriaInfoLink(string $impact_type): string
392392
);
393393
switch ($impact_type) {
394394
case 'gwp':
395-
return "$base_url/carbon/types_of_impact.html#carbon-dioxyde-equivalent";
395+
return "$base_url/types_of_impact.html#carbon-dioxyde-equivalent";
396396
case 'adp':
397397
return "$base_url/types_of_impact.html#antimony-equivalent";
398398
case 'pe':

src/UsageImpact.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,13 @@
3232

3333
namespace GlpiPlugin\Carbon;
3434

35-
use CommonDBChild;
36-
use DateInterval;
37-
use DateTimeInterface;
38-
use Entity;
39-
use Location;
35+
use GlpiPlugin\Carbon\Impact\Type;
4036

41-
class UsageImpact extends CommonDBChild
37+
class UsageImpact extends AbstractImpact
4238
{
4339
public static $itemtype = 'itemtype';
4440
public static $items_id = 'items_id';
4541

46-
public static $rightname = 'carbon:report';
47-
4842
public static function getTypeName($nb = 0)
4943
{
5044
return _n("Usage impact", "Usage impacts", $nb, 'carbon');
@@ -165,21 +159,4 @@ public function rawSearchOptions()
165159

166160
return $tab;
167161
}
168-
169-
/**
170-
* Get impact value in a human r eadable format, selecting the best unit
171-
*/
172-
public function getHumanReadableImpact(string $field): string
173-
{
174-
switch ($field) {
175-
case 'gwp':
176-
return Toolbox::getWeight($this->fields[$field]) . 'CO2eq';
177-
case 'adp':
178-
return Toolbox::getWeight($this->fields[$field]) . 'Sbeq';
179-
case 'pe':
180-
return Toolbox::getEnergy($this->fields[$field] / 3600); // Convert J into Wh
181-
}
182-
183-
return '';
184-
}
185162
}

src/UsageInfo.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,30 @@ public static function showCharts(CommonDBTM $asset)
243243
'applyto' => 'embodied_primary_energy_tip',
244244
]);
245245

246+
$tooltips = [];
247+
$embodied_labels = [];
248+
$usage_labels = [];
249+
foreach (Type::getImpactTypes() as $impact_type) {
250+
$tooltip = Type::getCriteriaTooltip($impact_type);
251+
$url = Type::getCriteriaInfoLink($impact_type);
252+
if ($url !== '') {
253+
$tooltip = sprintf(
254+
$tooltip . __('%s More information %s', 'carbon'),
255+
'<br /><a target="_blank" href="' . $url . '">',
256+
'</a>'
257+
);
258+
}
259+
$tooltips[$impact_type] = '';
260+
if ($tooltip !== '') {
261+
$tooltips[$impact_type] = Html::showToolTip($tooltip, [
262+
'display' => false,
263+
'applyto' => 'embodied_' . $impact_type . '_tip',
264+
]);
265+
}
266+
$embodied_labels[$impact_type] = Type::getEmbodiedImpactLabel($impact_type);
267+
$usage_labels[$impact_type] = Type::getUsageImpactLabel($impact_type);
268+
}
269+
246270
$usage_imapct_action_url = $CFG_GLPI['root_doc'] . '/plugins/carbon/front/usageimpact.form.php';
247271
$embodied_impact_action_url = $CFG_GLPI['root_doc'] . '/plugins/carbon/front/embodiedimpact.form.php';
248272
TemplateRenderer::getInstance()->display('@carbon/environmentalimpact-item.html.twig', [
@@ -251,6 +275,9 @@ public static function showCharts(CommonDBTM $asset)
251275
'usage_carbon_emission_count' => $usage_carbon_emission_count,
252276
'embodied_impact' => $embodied_impact,
253277
'usage_impact' => $usage_impact,
278+
'embodied_labels' => $embodied_labels,
279+
'usage_labels' => $usage_labels,
280+
'tooltips' => $tooltips,
254281
'usage_carbon_emission_graph' => Widget::DisplayGraphUsageCarbonEmissionPerMonth($data),
255282
'carbon_emission_tooltip_html' => $carbon_emission_tooltip_html,
256283
'usage_abiotic_depletion_tooltip_html' => $usage_abiotic_depletion_tooltip_html,

templates/environmentalimpact-item.html.twig

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,27 @@
3737
) }}
3838

3939
{{ usage_carbon_emission_graph|raw }}
40-
<div class="card-body" id="plugin_carbon_usage_impacts">
40+
<div class="card-body d-flex flex-wrap" id="plugin_carbon_usage_impacts">
41+
<div class="col-12 col-xxl-12 flex-column">
42+
<div class="d-flex flex-row flex-wrap flex-xl-nowrap">
43+
<div class="row flex-row align-items-start flex-grow-1 col-12 col-sm-6 mb-2">
44+
{% for criteria, label in usage_labels %}
45+
{% if usage_impact.fields[criteria] ?? null is not null %}
46+
<div class="col-xxl-7">
47+
{{ label }}
48+
</div>
49+
<div class="col-xxl-5">
50+
<span id="embodied_{{criteria}}_tip">
51+
{{ usage_impact.getHumanReadableImpact(criteria)|raw }}
52+
{# <i class="fa-solid fa-circle-info"></i> #}
53+
</span>
54+
{{ tooltips[criteria]|raw }}
55+
</div>
56+
{% endif %}
57+
{% endfor %}
58+
</div>
59+
</div>
60+
</div>
4161
{% if usage_impact.fields.gwp ?? null is not null %}
4262
<div class="h1 mb-3">{{ usage_impact.getHumanReadableImpact('gwp')|raw }}</div>
4363
{% endif %}
@@ -71,37 +91,27 @@
7191
'fas fa-chart-pie'
7292
) }}
7393

74-
<div class="card-body" id="plugin_carbon_embodied_impacts">
75-
{% if embodied_impact.fields.gwp ?? null is not null %}
76-
<div class="h1 mb-3">
77-
<span id="embodied_carbon_emission_tip">
78-
{{ embodied_impact.getHumanReadableImpact('gwp')|raw }}
79-
{# <i class="fa-solid fa-circle-info"></i> #}
80-
</span>
81-
{{ carbon_emission_tooltip_html|raw }}
82-
</div>
83-
{% endif %}
84-
85-
{% if embodied_impact.fields.adp ?? null is not null %}
86-
<div class="h1 mb-3">
87-
<span id="embodied_abiotic_depletion_tip">
88-
{{ embodied_impact.getHumanReadableImpact('adp')|raw }}
89-
{# <i class="fa-solid fa-circle-info"></i> #}
90-
</span>
91-
{{ embodied_abiotic_depletion_tooltip_html|raw }}
92-
</div>
93-
{% endif %}
94-
95-
{% if embodied_impact.fields.pe ?? null is not null %}
96-
<div class="h1 mb-3">
97-
<span id="embodied_primary_energy_tip">
98-
{{ embodied_impact.getHumanReadableImpact('pe')|raw }}
99-
{# <i class="fa-solid fa-circle-info"></i> #}
100-
</span>
101-
{{ embodied_primary_energy_tooltip_html|raw }}
94+
<div class="card-body d-flex flex-wrap" id="plugin_carbon_embodied_impacts">
95+
<div class="col-12 col-xxl-12 flex-column">
96+
<div class="d-flex flex-row flex-wrap flex-xl-nowrap">
97+
<div class="row flex-row align-items-start flex-grow-1 col-12 col-sm-6 mb-2">
98+
{% for criteria, label in embodied_labels %}
99+
{% if embodied_impact.fields[criteria] ?? null is not null %}
100+
<div class="col-xxl-7">
101+
{{ label }}
102+
</div>
103+
<div class="col-xxl-5">
104+
<span id="embodied_{{criteria}}_tip">
105+
{{ embodied_impact.getHumanReadableImpact(criteria)|raw }}
106+
{# <i class="fa-solid fa-circle-info"></i> #}
107+
</span>
108+
{{ tooltips[criteria]|raw }}
109+
</div>
110+
{% endif %}
111+
{% endfor %}
112+
</div>
102113
</div>
103-
{% endif %}
104-
114+
</div>
105115
<div>
106116
{% if not embodied_impact.isNewItem() %}
107117
{% set reset_args = "{_glpi_csrf_token: '" ~ csrf_token() ~ "', reset: '', id: '" ~ embodied_impact.getID() ~ "'}" %}

0 commit comments

Comments
 (0)