move html from callbacks to twig

This commit is contained in:
envoyr 2022-02-23 16:27:13 +01:00
parent 9177273484
commit 4e4e4eca94
No known key found for this signature in database
GPG Key ID: 5A16F49AF96F462F
6 changed files with 81 additions and 50 deletions

View File

@ -1,7 +1,8 @@
<?php
namespace Froxlor\UI\Callbacks;
use Froxlor\Settings;
use Froxlor\PhpHelper;
use Froxlor\UI\Panel\UI;
/**
* This file is part of the Froxlor project.
@ -28,11 +29,7 @@ class Number
*/
public static function diskspace(string $data): string
{
if ($data < 0) {
return 'Unlimited';
}
return round($data / 1024, Settings::Get('panel.decimal_places')) . ' MB';
return $data >= 0 ? PhpHelper::sizeReadable($data * 1024, null, 'bi') : UI::getLng('panel.unlimited');
}
/**
@ -43,10 +40,6 @@ class Number
*/
public static function traffic(string $data): string
{
if ($data < 0) {
return 'Unlimited';
}
return round($data / (1024 * 1024), Settings::Get('panel.decimal_places')) . ' MB';
return $data >= 0 ? PhpHelper::sizeReadable($data * (1024 * 1024), null, 'bi') : UI::getLng('panel.unlimited');
}
}
}

View File

@ -2,6 +2,9 @@
namespace Froxlor\UI\Callbacks;
use Froxlor\PhpHelper;
use Froxlor\UI\Panel\UI;
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
@ -19,14 +22,14 @@ namespace Froxlor\UI\Callbacks;
*/
class ProgressBar
{
/**
* TODO: use twig for html templates ...
*
* @param string $data
* @param array $attributes
* @return string
*/
public static function diskspace(string $data, array $attributes): string
/**
* get progressbar data for used diskspace
*
* @param string $data
* @param array $attributes
* @return array
*/
public static function diskspace(string $data, array $attributes): array
{
$infotext = '';
if (isset($attributes['customerid'])) {
@ -48,39 +51,35 @@ class ProgressBar
];
}
$infotext = \Froxlor\UI\Panel\UI::getLng('panel.used') . ':<br>';
$infotext .= 'web: ' . \Froxlor\PhpHelper::sizeReadable($usages['webspace'] * 1024, null, 'bi') . '<br>';
$infotext .= 'mail: ' . \Froxlor\PhpHelper::sizeReadable($usages['mailspace'] * 1024, null, 'bi') . '<br>';
$infotext .= 'mysql: ' . \Froxlor\PhpHelper::sizeReadable($usages['dbspace'] * 1024, null, 'bi');
$infotext = '<i class="fa-solid fa-circle-info" title="' . $infotext . '"></i>&nbsp;';
$infotext = UI::getLng('panel.used') . ':<br>';
$infotext .= 'web: ' . PhpHelper::sizeReadable($usages['webspace'] * 1024, null, 'bi') . '<br>';
$infotext .= 'mail: ' . PhpHelper::sizeReadable($usages['mailspace'] * 1024, null, 'bi') . '<br>';
$infotext .= 'mysql: ' . PhpHelper::sizeReadable($usages['dbspace'] * 1024, null, 'bi');
}
$pbdata = self::pbData('diskspace', $attributes, 1024, (int)\Froxlor\Settings::Get('system.report_webmax'));
return '<div class="progress progress-thin"><div class="progress-bar ' . $pbdata['style'] . '" style="width: ' . $pbdata['percent'] . '%;"></div></div><div class="text-end">' . $infotext . $pbdata['text'] . '</div>';
}
return self::pbData('diskspace', $attributes, 1024, (int)\Froxlor\Settings::Get('system.report_webmax'), $infotext);
}
/**
* TODO: use twig for html templates ...
*
* @param string $data
* @param array $attributes
* @return string
*/
public static function traffic(string $data, array $attributes): string
/**
* get progressbar data for traffic
*
* @param string $data
* @param array $attributes
* @return array
*/
public static function traffic(string $data, array $attributes): array
{
$pbdata = self::pbData('traffic', $attributes, 1024 * 1024, (int)\Froxlor\Settings::Get('system.report_trafficmax'));
return '<div class="progress progress-thin"><div class="progress-bar ' . $pbdata['style'] . '" style="width: ' . $pbdata['percent'] . '%;"></div></div><div class="text-end">' . $pbdata['text'] . '</div>';
}
return self::pbData('traffic', $attributes, 1024 * 1024, (int)\Froxlor\Settings::Get('system.report_trafficmax'));
}
/**
* do needed calculations
*/
private static function pbData(string $field, array $attributes, int $size_factor = 1024, int $report_max = 90): array
private static function pbData(string $field, array $attributes, int $size_factor = 1024, int $report_max = 90, $infotext = null): array
{
$percent = 0;
$style = 'bg-info';
$text = \Froxlor\PhpHelper::sizeReadable($attributes[$field . '_used'] * $size_factor, null, 'bi') . ' / ' . \Froxlor\UI\Panel\UI::getLng('customer.unlimited');
$text = PhpHelper::sizeReadable($attributes[$field . '_used'] * $size_factor, null, 'bi') . ' / ' . UI::getLng('customer.unlimited');
if ((int) $attributes[$field] >= 0) {
if (($attributes[$field] / 100) * $report_max < $attributes[$field . '_used']) {
$style = 'bg-danger';
@ -91,13 +90,17 @@ class ProgressBar
if ($percent > 100) {
$percent = 100;
}
$text = \Froxlor\PhpHelper::sizeReadable($attributes[$field . '_used'] * $size_factor, null, 'bi') . ' / ' . \Froxlor\PhpHelper::sizeReadable($attributes[$field] * $size_factor, null, 'bi');
$text = PhpHelper::sizeReadable($attributes[$field . '_used'] * $size_factor, null, 'bi') . ' / ' . PhpHelper::sizeReadable($attributes[$field] * $size_factor, null, 'bi');
}
return [
'percent' => $percent,
'style' => $style,
'text' => $text
];
'type' => 'progressbar',
'data' => [
'percent' => $percent,
'style' => $style,
'text' => $text,
'infotext' => $infotext
]
];
}
}

View File

@ -18,8 +18,11 @@ namespace Froxlor\UI\Callbacks;
*/
class Text
{
public static function boolean(string $data): string
public static function boolean(string $data): array
{
return $data ? '<i class="fa fa-check-circle"></i>' : '<i class="fa fa-times-circle"></i>';
return [
'type' => 'boolean',
'data' => (bool) $data
];
}
}

View File

@ -40,3 +40,4 @@ $lng['menue']['logger']['logger'] = $lng['admin']['loggersystem'];
$lng['serversettings']['default_sslvhostconf']['description'] = $lng['serversettings']['default_vhostconf']['description'];
$lng['admin']['include_ownvhostsettings'] = $lng['serversettings']['includedefault_sslvhostconf'];
$lng['panel']['unlimited'] = $lng['customer']['unlimited'];

View File

@ -0,0 +1,19 @@
{% macro progressbar(data) %}
<div class="progress progress-thin">
<div class="progress-bar {{ data.style }}" style="width: {{ data.percent }}%;"></div>
</div>
<div class="text-end">
{% if data.infotext is not empty %}
<i class="fa-solid fa-circle-info" data-toggle="tooltip" data-placement="right" title="{{ data.infotext|raw }}"></i>
{% endif %}
{{ data.text }}
</div>
{% endmacro %}
{% macro boolean(data) %}
{% if (data) %}
<i class="fa fa-check-circle"></i>
{% else %}
<i class="fa fa-times-circle"></i>
{% endif %}
{% endmacro %}

View File

@ -1,5 +1,7 @@
{% macro table(listing) %}
{% import "Froxlor/table/callbacks.html.twig" as callbacks %}
<form action="{{ action|default("") }}" method="post" enctype="application/x-www-form-urlencoded" class="form">
{% if listing.title is not empty %}
<h3 class="page-header">
@ -24,7 +26,17 @@
<tr>
{% for value in td %}
<td class="p-3">
{{ value|raw }}
{% if value is iterable %}
{% if value.type == 'progressbar' %}
{{ callbacks.progressbar(value.data) }}
{% elseif value.type == 'boolean' %}
{{ callbacks.boolean(value.data) }}
{% else %}
Callback '{{ value|json_encode }}' is not implemented!
{% endif %}
{% else %}
{{ value|raw }}
{% endif %}
</td>
{% endfor %}
</tr>