refactoring callbacks, add class for table rows

This commit is contained in:
envoyr
2022-02-27 16:36:23 +01:00
parent d9f909150d
commit 1a6b899c67
17 changed files with 222 additions and 143 deletions

View File

@@ -2,6 +2,7 @@
namespace Froxlor\UI\Callbacks;
use Froxlor\FileDir;
use Froxlor\UI\Panel\UI;
/**
@@ -15,33 +16,32 @@ use Froxlor\UI\Panel\UI;
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
* @package Froxlor\UI\Callbacks
*
*/
class Domain
{
public static function domainTarget(string $data, array $attributes): mixed
public static function domainTarget(array $attributes)
{
if (empty($attributes['aliasdomain'])) {
if (empty($attributes['fields']['aliasdomain'])) {
// path or redirect
if (preg_match('/^https?\:\/\//', $attributes['documentroot'])) {
if (preg_match('/^https?\:\/\//', $attributes['fields']['documentroot'])) {
return [
'type' => 'link',
'data' => [
'text' => $attributes['documentroot'],
'href' => $attributes['documentroot'],
'text' => $attributes['fields']['documentroot'],
'href' => $attributes['fields']['documentroot'],
'target' => '_blank'
]
];
} else {
// show docroot nicely
if (strpos($attributes['documentroot'], UI::getCurrentUser()['documentroot']) === 0) {
$attributes['documentroot'] = \Froxlor\FileDir::makeCorrectDir(str_replace(UI::getCurrentUser()['documentroot'], "/", $attributes['documentroot']));
if (strpos($attributes['fields']['documentroot'], UI::getCurrentUser()['documentroot']) === 0) {
$attributes['fields']['documentroot'] = FileDir::makeCorrectDir(str_replace(UI::getCurrentUser()['documentroot'], "/", $attributes['fields']['documentroot']));
}
return $attributes['documentroot'];
return $attributes['fields']['documentroot'];
}
}
return UI::getLng('domains.aliasdomain') . ' ' . $attributes['aliasdomain'];
return UI::getLng('domains.aliasdomain') . ' ' . $attributes['fields']['aliasdomain'];
}
}

View File

@@ -2,8 +2,8 @@
namespace Froxlor\UI\Callbacks;
use Froxlor\Settings;
use Froxlor\PhpHelper;
use Froxlor\Settings;
/**
* This file is part of the Froxlor project.
@@ -16,19 +16,18 @@ use Froxlor\PhpHelper;
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
* @package Froxlor\UI\Callbacks
*
*/
class Email
{
public static function account(string $data, array $attributes): mixed
public static function account(array $attributes)
{
return [
'type' => 'booleanWithInfo',
'data' => [
'checked' => $data != 0,
'info' => $data != 0 ? PhpHelper::sizeReadable($attributes['mboxsize'], 'GiB', 'bi', '%01.' . (int) Settings::Get('panel.decimal_places') . 'f %s') : ''
'checked' => $attributes['data'] != 0,
'info' => $attributes['data'] != 0 ? PhpHelper::sizeReadable($attributes['fields']['mboxsize'], 'GiB', 'bi', '%01.' . (int)Settings::Get('panel.decimal_places') . 'f %s') : ''
]
];
}

View File

@@ -2,6 +2,7 @@
namespace Froxlor\UI\Callbacks;
use Froxlor\FileDir;
use Froxlor\UI\Panel\UI;
/**
@@ -15,19 +16,18 @@ use Froxlor\UI\Panel\UI;
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
* @package Froxlor\UI\Callbacks
*
*/
class Ftp
{
public static function pathRelative(string $data, array $attributes): string
public static function pathRelative(array $attributes): string
{
if (strpos($data, UI::getCurrentUser()['documentroot']) === 0) {
$data = str_replace(UI::getCurrentUser()['documentroot'], "/", $data);
if (strpos($attributes['data'], UI::getCurrentUser()['documentroot']) === 0) {
$attributes['data'] = str_replace(UI::getCurrentUser()['documentroot'], "/", $attributes['data']);
}
$data = \Froxlor\FileDir::makeCorrectDir($data);
$attributes['data'] = FileDir::makeCorrectDir($attributes['data']);
return $data;
return $attributes['data'];
}
}

View File

@@ -16,45 +16,44 @@ use Froxlor\UI\Panel\UI;
* @author Froxlor team <team@froxlor.org> (2010-)
* @author Maurice Preuß <hello@envoyr.com>
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
* @package Froxlor\UI\Callbacks
*
*/
class Impersonate
{
public static function admin(string $data, array $attributes): mixed
public static function admin(array $attributes)
{
if (UI::getCurrentUser()['adminid'] != $attributes['adminid']) {
if (UI::getCurrentUser()['adminid'] != $attributes['fields']['adminid']) {
$linker = UI::getLinker();
return [
'type' => 'link',
'data' => [
'text' => $data,
'text' => $attributes['data'],
'href' => $linker->getLink([
'section' => 'admins',
'page' => 'admins',
'action' => 'su',
'id' => $attributes['adminid'],
'id' => $attributes['fields']['adminid'],
]),
]
];
}
return $data;
return $attributes['data'];
}
public static function customer(string $data, array $attributes): array
public static function customer(array $attributes): array
{
$linker = UI::getLinker();
return [
'type' => 'link',
'data' => [
'text' => $data,
'text' => $attributes['data'],
'href' => $linker->getLink([
'section' => 'customers',
'page' => 'customers',
'action' => 'su',
'sort' => $attributes['loginname'],
'id' => $attributes['customerid'],
'sort' => $attributes['fields']['loginname'],
'id' => $attributes['fields']['customerid'],
]),
]
];

View File

@@ -15,16 +15,15 @@ use Froxlor\Database\Database;
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
* @package Froxlor\UI\Callbacks
*
*/
class Mysql
{
public static function dbserver(string $data, array $attributes): string
public static function dbserver(array $attributes): string
{
// get sql-root access data
Database::needRoot(true, (int) $data);
Database::needRoot(true, (int)$attributes['data']);
Database::needSqlData();
$sql_root = Database::getSqlData();
Database::needRoot(false);

View File

@@ -3,6 +3,7 @@
namespace Froxlor\UI\Callbacks;
use Froxlor\PhpHelper;
use Froxlor\Settings;
use Froxlor\UI\Panel\UI;
/**
@@ -17,42 +18,40 @@ use Froxlor\UI\Panel\UI;
* @author Froxlor team <team@froxlor.org> (2010-)
* @author Maurice Preuß <hello@envoyr.com>
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
* @package Froxlor\UI\Callbacks
*
*/
class ProgressBar
{
/**
* get progressbar data for used diskspace
*
* @param string $data
* @param array $attributes
* @return array
*/
public static function diskspace(string $data, array $attributes): array
/**
* get progressbar data for used diskspace
*
* @param array $attributes
* @return array
*/
public static function diskspace(array $attributes): array
{
$infotext = null;
if (isset($attributes['webspace_used']) && isset($attributes['mailspace_used']) && isset($attributes['dbspace_used'])) {
$infotext = null;
if (isset($attributes['fields']['webspace_used']) && isset($attributes['fields']['mailspace_used']) && isset($attributes['fields']['dbspace_used'])) {
$infotext = UI::getLng('panel.used') . ':<br>';
$infotext .= 'web: ' . PhpHelper::sizeReadable($attributes['webspace_used'] * 1024, null, 'bi') . '<br>';
$infotext .= 'mail: ' . PhpHelper::sizeReadable($attributes['mailspace_used'] * 1024, null, 'bi') . '<br>';
$infotext .= 'mysql: ' . PhpHelper::sizeReadable($attributes['dbspace_used'] * 1024, null, 'bi');
$infotext .= 'web: ' . PhpHelper::sizeReadable($attributes['fields']['webspace_used'] * 1024, null, 'bi') . '<br>';
$infotext .= 'mail: ' . PhpHelper::sizeReadable($attributes['fields']['mailspace_used'] * 1024, null, 'bi') . '<br>';
$infotext .= 'mysql: ' . PhpHelper::sizeReadable($attributes['fields']['dbspace_used'] * 1024, null, 'bi');
}
return self::pbData('diskspace', $attributes, 1024, (int)\Froxlor\Settings::Get('system.report_webmax'), $infotext);
}
return self::pbData('diskspace', $attributes['fields'], 1024, (int)Settings::Get('system.report_webmax'), $infotext);
}
/**
* get progressbar data for traffic
*
* @param string $data
* @param array $attributes
* @return array
*/
public static function traffic(string $data, array $attributes): array
/**
* get progressbar data for traffic
*
* @param array $attributes['fields']
* @return array
*/
public static function traffic(array $attributes): array
{
return self::pbData('traffic', $attributes, 1024 * 1024, (int)\Froxlor\Settings::Get('system.report_trafficmax'));
}
return self::pbData('traffic', $attributes['fields'], 1024 * 1024, (int)Settings::Get('system.report_trafficmax'));
}
/**
* do needed calculations
@@ -62,7 +61,7 @@ class ProgressBar
$percent = 0;
$style = 'bg-info';
$text = PhpHelper::sizeReadable($attributes[$field . '_used'] * $size_factor, null, 'bi') . ' / ' . UI::getLng('panel.unlimited');
if ((int) $attributes[$field] >= 0) {
if ((int)$attributes[$field] >= 0) {
if (($attributes[$field] / 100) * $report_max < $attributes[$field . '_used']) {
$style = 'bg-danger';
} elseif (($attributes[$field] / 100) * ($report_max - 15) < $attributes[$field . '_used']) {
@@ -76,13 +75,13 @@ class ProgressBar
}
return [
'type' => 'progressbar',
'data' => [
'percent' => $percent,
'style' => $style,
'text' => $text,
'infotext' => $infotext
]
];
'type' => 'progressbar',
'data' => [
'percent' => $percent,
'style' => $style,
'text' => $text,
'infotext' => $infotext
]
];
}
}

View File

@@ -0,0 +1,32 @@
<?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 Froxlor\UI\Callbacks
*
*/
class SSLCertificates
{
public static function domainWithSan(array $attributes): array
{
return [
'type' => 'domainWithSan',
'data' => [
'domain' => $attributes['data'],
'san' => implode(', ', $attributes['fields']['san'] ?? []),
]
];
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Froxlor\UI\Callbacks;
use Froxlor\PhpHelper;
use Froxlor\Settings;
use Froxlor\UI\Panel\UI;
use Froxlor\User;
/**
* 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 Froxlor\UI\Callbacks
*
*/
class Style
{
public static function deactivated(array $attributes): string
{
return $attributes['fields']['deactivated'] ? 'bg-danger' : '';
}
public static function diskspaceWarning(array $attributes): string
{
return self::getWarningStyle('diskspace', $attributes['fields'], (int)Settings::Get('system.report_webmax'));
}
public static function trafficWarning(array $attributes): string
{
return self::getWarningStyle('traffic', $attributes['fields'], (int)Settings::Get('system.report_trafficmax'));
}
private static function getWarningStyle(string $field, array $attributes, int $report_max = 90): string
{
$style = '';
if ((int)$attributes[$field] >= 0) {
if (($attributes[$field] / 100) * $report_max < $attributes[$field . '_used']) {
$style = 'bg-danger';
} elseif (($attributes[$field] / 100) * ($report_max - 15) < $attributes[$field . '_used']) {
$style = 'bg-warning';
}
}
return $style;
}
}

View File

@@ -3,6 +3,9 @@
namespace Froxlor\UI\Callbacks;
use Froxlor\PhpHelper;
use Froxlor\Settings;
use Froxlor\UI\Panel\UI;
use Froxlor\User;
/**
* This file is part of the Froxlor project.
@@ -16,37 +19,26 @@ use Froxlor\PhpHelper;
* @author Froxlor team <team@froxlor.org> (2010-)
* @author Maurice Preuß <hello@envoyr.com>
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
* @package Froxlor\UI\Callbacks
*
*/
class Text
{
public static function boolean(?string $data): array
public static function boolean(array $attributes): array
{
return [
'type' => 'boolean',
'data' => (bool) $data
'data' => (bool)$attributes['data']
];
}
public static function domainWithSan(string $data, array $attributes): array
public static function customerfullname(array $attributes): string
{
return [
'type' => 'domainWithSan',
'data' => [
'domain' => $data,
'san' => implode(', ', $attributes['san'] ?? []),
]
];
return User::getCorrectFullUserDetails($attributes['fields']);
}
public static function customerfullname(string $data, array $attributes): string
public static function size(array $attributes): string
{
return \Froxlor\User::getCorrectFullUserDetails($attributes);
}
public static function size(string $data, array $attributes): string
{
return PhpHelper::sizeReadable($data, null, 'bi');
return PhpHelper::sizeReadable($attributes['data'], null, 'bi');
}
}

View File

@@ -75,11 +75,11 @@ class Listing
}
$format_callback = $tabellisting['columns'][$visible_column]['format_callback'] ?? null;
$column = $tabellisting['columns'][$visible_column]['column'];
$column = $tabellisting['columns'][$visible_column]['field'];
$data = self::getMultiArrayFromString($item, $column);
if ($format_callback) {
$rows[$row]['td'][$col]['data'] = call_user_func($format_callback, $data, $item);
$rows[$row]['td'][$col]['data'] = call_user_func($format_callback, ['data' => $data, 'fields' => $item]);
} else {
$rows[$row]['td'][$col]['data'] = $data;
}
@@ -87,10 +87,14 @@ class Listing
$rows[$row]['td'][$col]['class'] = $tabellisting['columns'][$visible_column]['class'] ?? null;
}
// TODO: contextual_class ...
//if (...) {
// $rows[$key]['class'] = '...';
//}
// Set row classes from format_callback
if (isset($tabellisting['format_callback'])) {
$class = [];
foreach ($tabellisting['format_callback'] as $format_callback) {
$class[] = call_user_func($format_callback, ['fields' => $item]);
}
$rows[$row]['class'] = implode(' ', $class);
}
// Set all actions for row
if (isset($tabellisting['actions'])) {

View File

@@ -17,6 +17,7 @@
*/
use Froxlor\UI\Callbacks\ProgressBar;
use Froxlor\UI\Callbacks\Style;
use Froxlor\UI\Callbacks\Text;
use Froxlor\UI\Listing;
@@ -27,36 +28,36 @@ return [
'columns' => [
'adminid' => [
'label' => '#',
'column' => 'adminid',
'field' => 'adminid',
'sortable' => true,
],
'loginname' => [
'label' => $lng['login']['username'],
'column' => 'loginname',
'field' => 'loginname',
'sortable' => true,
],
'name' => [
'label' => $lng['customer']['name'],
'column' => 'name',
'field' => 'name',
],
'customers_used' => [
'label' => $lng['admin']['customers'],
'column' => 'customers_used',
'field' => 'customers_used',
'class' => 'text-center',
],
'diskspace' => [
'label' => $lng['customer']['diskspace'],
'column' => 'diskspace',
'field' => 'diskspace',
'format_callback' => [ProgressBar::class, 'diskspace'],
],
'traffic' => [
'label' => $lng['customer']['traffic'],
'column' => 'traffic',
'field' => 'traffic',
'format_callback' => [ProgressBar::class, 'traffic'],
],
'deactivated' => [
'label' => $lng['admin']['deactivated'],
'column' => 'deactivated',
'field' => 'deactivated',
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean'],
],
@@ -90,11 +91,10 @@ return [
],
],
],
'contextual_class' => [
'deactivated' => [
'value' => true,
'return' => 'bg-secondary'
]
'format_callback' => [
[Style::class, 'deactivated'],
[Style::class, 'diskspaceWarning'],
[Style::class, 'trafficWarning']
]
]
];

View File

@@ -28,31 +28,31 @@ return [
'columns' => [
'c.name' => [
'label' => $lng['customer']['name'],
'column' => 'name',
'field' => 'name',
'format_callback' => [Text::class, 'customerfullname'],
],
'c.loginname' => [
'label' => $lng['login']['username'],
'column' => 'loginname',
'field' => 'loginname',
'format_callback' => [Impersonate::class, 'customer'],
],
'a.loginname' => [
'label' => $lng['admin']['admin'],
'column' => 'admin.loginname',
'field' => 'admin.loginname',
'format_callback' => [Impersonate::class, 'admin'],
],
'c.email' => [
'label' => $lng['customer']['email'],
'column' => 'email',
'field' => 'email',
],
'c.diskspace' => [
'label' => $lng['customer']['diskspace'],
'column' => 'diskspace',
'field' => 'diskspace',
'format_callback' => [ProgressBar::class, 'diskspace'],
],
'c.traffic' => [
'label' => $lng['customer']['traffic'],
'column' => 'traffic',
'field' => 'traffic',
'format_callback' => [ProgressBar::class, 'traffic'],
],
],

View File

@@ -27,21 +27,21 @@ return [
'columns' => [
'd.domain_ace' => [
'label' => $lng['domains']['domainname'],
'column' => 'domain_ace',
'field' => 'domain_ace',
],
'c.name' => [
'label' => $lng['customer']['name'],
'column' => 'customer.name',
'field' => 'customer.name',
'format_callback' => [Text::class, 'customerfullname'],
],
'c.loginname' => [
'label' => $lng['login']['username'],
'column' => 'customer.loginname',
'field' => 'customer.loginname',
'format_callback' => [Impersonate::class, 'customer'],
],
'd.aliasdomain' => [
'label' => $lng['domains']['aliasdomain'],
'column' => 'aliasdomain',
'field' => 'aliasdomain',
],
],
'visible_columns' => Listing::getVisibleColumnsForListing('domain_list', [

View File

@@ -27,49 +27,49 @@ return [
'columns' => [
'ip' => [
'label' => $lng['admin']['ipsandports']['ip'],
'column' => 'ip',
'field' => 'ip',
],
'port' => [
'label' => $lng['admin']['ipsandports']['port'],
'column' => 'port',
'field' => 'port',
'class' => 'text-center',
],
'listen' => [
'label' => 'Listen',
'column' => 'listen_statement',
'field' => 'listen_statement',
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean'],
'visible' => Settings::Get('system.webserver') != 'nginx'
],
'namevirtualhost' => [
'label' => 'NameVirtualHost',
'column' => 'namevirtualhost_statement',
'field' => 'namevirtualhost_statement',
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean'],
'visible' => Settings::Get('system.webserver') == 'apache2' && (int) Settings::Get('system.apache24') == 0
],
'vhostcontainer' => [
'label' => 'vHost-Container',
'column' => 'vhostcontainer',
'field' => 'vhostcontainer',
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean']
],
'specialsettings' => [
'label' => 'Specialsettings',
'column' => 'specialsettings',
'field' => 'specialsettings',
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean']
],
'servername' => [
'label' => 'ServerName',
'column' => 'vhostcontainer_servername_statement',
'field' => 'vhostcontainer_servername_statement',
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean'],
'visible' => Settings::Get('system.webserver') == 'apache2'
],
'ssl' => [
'label' => 'SSL',
'column' => 'ssl',
'field' => 'ssl',
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean']
],

View File

@@ -26,19 +26,19 @@ return [
'columns' => [
'p.name' => [
'label' => $lng['admin']['plans']['name'],
'column' => 'name',
'field' => 'name',
],
'p.description' => [
'label' => $lng['admin']['plans']['description'],
'column' => 'description',
'field' => 'description',
],
'p.adminname' => [
'label' => $lng['admin']['admin'],
'column' => 'adminname',
'field' => 'adminname',
],
'p.ts' => [
'label' => $lng['admin']['plans']['last_update'],
'column' => 'ts',
'field' => 'ts',
],
],
'visible_columns' => Listing::getVisibleColumnsForListing('sslcertificates_list', [

View File

@@ -17,6 +17,7 @@
*/
use Froxlor\Settings;
use Froxlor\UI\Callbacks\SSLCertificates;
use Froxlor\UI\Callbacks\Text;
use Froxlor\UI\Listing;
@@ -27,28 +28,28 @@ return [
'columns' => [
'd.domain' => [
'label' => $lng['domains']['domainname'],
'column' => 'domain',
'field' => 'domain',
],
'c.domain' => [
'label' => $lng['ssl_certificates']['certificate_for'],
'column' => 'domain',
'format_callback' => [Text::class, 'domainWithSan'],
'field' => 'domain',
'format_callback' => [SSLCertificates::class, 'domainWithSan'],
],
'c.issuer' => [
'label' => $lng['ssl_certificates']['issuer'],
'column' => 'issuer',
'field' => 'issuer',
],
'c.validfromdate' => [
'label' => $lng['ssl_certificates']['valid_from'],
'column' => 'validfromdate',
'field' => 'validfromdate',
],
'c.validtodate' => [
'label' => $lng['ssl_certificates']['valid_until'],
'column' => 'validtodate',
'field' => 'validtodate',
],
'c.letsencrypt' => [
'label' => $lng['panel']['letsencrypt'],
'column' => 'letsencrypt',
'field' => 'letsencrypt',
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean'],
'visible' => Settings::Get('system.le_froxlor_enabled'),

View File

@@ -27,20 +27,20 @@ return [
'columns' => [
'databasename' => [
'label' => $lng['mysql']['databasename'],
'column' => 'databasename',
'field' => 'databasename',
],
'description' => [
'label' => $lng['mysql']['databasedescription'],
'column' => 'description'
'field' => 'description'
],
'size' => [
'label' => $lng['mysql']['size'],
'column' => 'size',
'field' => 'size',
'format_callback' => [Text::class, 'size']
],
'dbserver' => [
'label' => $lng['mysql']['mysql_server'],
'column' => 'dbserver',
'field' => 'dbserver',
'format_callback' => [Mysql::class, 'dbserver'],
'visible' => $count_mysqlservers > 1
]