update table listing and add callbacks

This commit is contained in:
envoyr
2022-02-22 19:07:04 +01:00
parent 855e220d14
commit 8f7876b850
13 changed files with 189 additions and 192 deletions

View File

@@ -0,0 +1,50 @@
<?php
namespace Froxlor\UI\Callbacks;
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @author Maurice Preuß <hello@envoyr.com>
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
*
*/
class Number
{
/**
* Formats the diskspace to human-readable number
*
* @param string $data
* @return string
*/
public static function diskspace(string $data): string
{
if ($data < 0) {
return 'Unlimited';
}
return round($data / 1024, 3) . ' MB';
}
/**
* Formats the traffic to human-readable number
*
* @param string $data
* @return string
*/
public static function traffic(string $data): string
{
if ($data < 0) {
return 'Unlimited';
}
return round($data / (1024 * 1024), 3) . ' MB';
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Froxlor\UI\Callbacks;
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @author Maurice Preuß <hello@envoyr.com>
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
*
*/
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
{
$percentage = $attributes['diskspace_used'] ? round(100 * $attributes['diskspace_used'] / $attributes['diskspace']) : 0;
$text = Number::diskspace($attributes['diskspace_used']) . ' / ' . Number::diskspace($attributes['diskspace']);
return '<div class="progress progress-thin"><div class="progress-bar bg-info" style="width: ' . $percentage . '%;"></div></div><div class="text-end">' . $text . '</div>';
}
/**
* TODO: use twig for html templates ...
*
* @param string $data
* @param array $attributes
* @return string
*/
public static function traffic(string $data, array $attributes): string
{
$percentage = $attributes['traffic_used'] ? round(100 * $attributes['traffic_used'] / $attributes['traffic']) : 0;
$text = Number::traffic($attributes['traffic_used']) . ' / ' . Number::traffic($attributes['traffic']);
return '<div class="progress progress-thin"><div class="progress-bar bg-info" style="width: ' . $percentage . '%;"></div></div><div class="text-end">' . $text . '</div>';
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Froxlor\UI\Callbacks;
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @author Maurice Preuß <hello@envoyr.com>
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
*
*/
class Text
{
public static function boolean(string $data): string
{
return $data ? '<i class="fa fa-check-circle"></i>' : '<i class="fa fa-times-circle"></i>';
}
}

View File

@@ -39,7 +39,7 @@ class Collection
public function count()
{
return $this->get()['data']['count'];
return json_decode($this->class::getLocal($this->userInfo, $this->params)->listingCount(), true);
}
public function get()

View File

@@ -18,6 +18,37 @@ namespace Froxlor\UI;
*/
class Listing
{
public static function format(Collection $collection, array $tabellisting): array
{
$items = $collection->getData()['list'];
$table = [];
foreach ($tabellisting['visible_columns'] as $visible_column) {
$table['th'][] = $tabellisting['columns'][$visible_column]['label'];
}
foreach ($items as $key => $item) {
foreach ($tabellisting['visible_columns'] as $visible_column) {
$format_callback = $tabellisting['columns'][$visible_column]['format_callback'] ?? null;
$column = $tabellisting['columns'][$visible_column]['column'];
$data = self::getMultiArrayFromString($item, $column);
// TODO: contextual_class ...
if ($format_callback) {
$table['tr'][$key][] = call_user_func($format_callback, $data, $item);
} else {
$table['tr'][$key][] = $data;
}
}
}
return [
'table' => $table,
'pagination' => null, // TODO: write some logic
];
}
public static function getVisibleColumnsForListing($listing, $default_columns)
{
// Hier käme dann die Logik, die das aus der DB zieht ...
@@ -26,4 +57,16 @@ class Listing
return $default_columns;
}
public static function getMultiArrayFromString($arr, $str)
{
foreach (explode('.', $str) as $key) {
if (!array_key_exists($key, $arr)) {
return null;
}
$arr = $arr[$key];
}
return $arr;
}
}