Skip to content

Commit 5768a5f

Browse files
authored
Progress bar Twig helper
1 parent 90a84f5 commit 5768a5f

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/Glpi/Application/View/Extension/DataHelpersExtension.php

+21
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Toolbox;
4242
use Twig\Extension\AbstractExtension;
4343
use Twig\TwigFilter;
44+
use Twig\TwigFunction;
4445
use Twig\TwigTest;
4546

4647
/**
@@ -69,6 +70,13 @@ public function getFilters(): array
6970
];
7071
}
7172

73+
public function getFunctions(): array
74+
{
75+
return [
76+
new TwigFunction('progress_bar', [$this, 'getProgressBar'], ['is_safe' => ['html']]),
77+
];
78+
}
79+
7280
public function getTests(): array
7381
{
7482
return [
@@ -307,4 +315,17 @@ public function truncateLeft(string $string = "", int $length = 30, string $sepa
307315

308316
return $separator . mb_substr($string, -$length);
309317
}
318+
319+
/**
320+
* Returns a static progress bar HTML snippet.
321+
*
322+
* @param float $percentage
323+
* @param string $label
324+
*
325+
* @return string
326+
*/
327+
public function getProgressBar(float $percentage, ?string $label = null): string
328+
{
329+
return Html::getProgressBar($percentage, $label);
330+
}
310331
}

src/Html.php

+27
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,33 @@ public static function displayProgressBar($width, $percent, $options = [])
980980
}
981981
}
982982

983+
/**
984+
* Returns a static progress bar HTML snippet.
985+
*
986+
* @param float $percentage
987+
* @param string $label
988+
*
989+
* @return string
990+
*/
991+
public static function getProgressBar(float $percentage, ?string $label = null): string
992+
{
993+
if ($label === null) {
994+
$label = floor($percentage) . ' %';
995+
}
996+
997+
return TemplateRenderer::getInstance()->renderFromStringTemplate(
998+
<<<TWIG
999+
<div class="progress" style="height: 15px; min-width: 50px;">
1000+
<div class="progress-bar bg-info" role="progressbar" style="width: {{ percentage }}%;"
1001+
aria-valuenow="{{ percentage }}" aria-valuemin="0" aria-valuemax="100">{{ label }}</div>
1002+
</div>
1003+
TWIG,
1004+
[
1005+
'percentage' => $percentage,
1006+
'label' => $label,
1007+
]
1008+
);
1009+
}
9831010

9841011
/**
9851012
* Include common HTML headers

templates/pages/setup/general/performance.html.twig

+2-8
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@
7575
<th class="fw-normal">{{ _n('Memory', 'Memories', 1) }}</th>
7676
<td>{{ __('%1$s / %2$s')|format(used|formatted_size, max|formatted_size) }}</td>
7777
<td>
78-
{{ call('Html::displayProgressBar', [100, rate, {
79-
simple: true,
80-
forcepadding: false
81-
}]) }}
78+
{{ progress_bar(rate) }}
8279
</td>
8380
{% if rate > 5 and rate < 75 %}
8481
{{ _self.success_icon_msg(__('%1$s memory usage is correct')|format(opcache_ext)) }}
@@ -94,10 +91,7 @@
9491
<th class="fw-normal">{{ __('Hits rate') }}</th>
9592
<td>{{ __('%1$s / %2$s')|format(hits, num_requests) }}</td>
9693
<td>
97-
{{ call('Html::displayProgressBar', [100, hit_rate, {
98-
simple: true,
99-
forcepadding: false
100-
}]) }}
94+
{{ progress_bar(hit_rate) }}
10195
</td>
10296
{% if hit_rate > 90 %}
10397
{{ _self.success_icon_msg(__('%1$s hits rate is correct')|format(opcache_ext)) }}

0 commit comments

Comments
 (0)