From 855e220d14f267dda33f2f982051032e4e19c930 Mon Sep 17 00:00:00 2001 From: envoyr Date: Tue, 22 Feb 2022 17:09:36 +0100 Subject: [PATCH] update table and add listing and collection class --- admin_admins.php | 24 ++----- admin_customers.php | 33 +++------ admin_domains.php | 63 +++++----------- lib/Froxlor/UI/Collection.php | 72 +++++++++++++++++++ lib/Froxlor/UI/Listing.php | 29 ++++++++ .../admin/admin/tablelisting.admin.php | 38 +++++----- .../admin/admin/tablelisting.customer.php | 67 +++++++++++++---- .../admin/admin/tablelisting.domain.php | 58 +++++++++++++++ templates/Froxlor/table/table.html.twig | 14 ++-- templates/Froxlor/table/tablemacros.html.twig | 15 +++- templates/Froxlor/user/table.html.twig | 2 +- 11 files changed, 284 insertions(+), 131 deletions(-) create mode 100644 lib/Froxlor/UI/Collection.php create mode 100644 lib/Froxlor/UI/Listing.php create mode 100644 lib/tablelisting/admin/admin/tablelisting.domain.php diff --git a/admin_admins.php b/admin_admins.php index 03c2fa91..5f3fab75 100644 --- a/admin_admins.php +++ b/admin_admins.php @@ -34,25 +34,13 @@ if ($page == 'admins' && $userinfo['change_serversettings'] == '1') { $log->logAction(\Froxlor\FroxlorLogger::ADM_ACTION, LOG_NOTICE, "viewed admin_admins"); $admin_list_data = include_once dirname(__FILE__) . '/lib/tablelisting/admin/admin/tablelisting.admin.php'; - /* - $fields = array( - 'loginname' => $lng['login']['username'], - 'name' => $lng['customer']['name'], - 'diskspace' => $lng['customer']['diskspace'], - 'diskspace_used' => $lng['customer']['diskspace'] . ' (' . $lng['panel']['used'] . ')', - 'traffic' => $lng['customer']['traffic'], - 'traffic_used' => $lng['customer']['traffic'] . ' (' . $lng['panel']['used'] . ')', - 'deactivated' => $lng['admin']['deactivated'] - ); - */ try { - // get total count - $json_result = Admins::getLocal($userinfo)->listingCount(); - $result = json_decode($json_result, true)['data']; + // get collection + $collection = new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Admins::class, $userinfo); // initialize pagination and filtering - $paging = new \Froxlor\UI\Pagination($userinfo, $admin_list_data['admin_list']['columns'], $result); - // get list - $json_result = Admins::getLocal($userinfo, $paging->getApiCommandParams())->listing(); + $paging = new \Froxlor\UI\Pagination($userinfo, $admin_list_data['admin_list']['columns'], $collection->count()); + // get filtered collection + $collection = new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Admins::class, $userinfo, $paging->getApiCommandParams()); } catch (Exception $e) { \Froxlor\UI\Response::dynamic_error($e->getMessage()); } @@ -115,7 +103,7 @@ if ($page == 'admins' && $userinfo['change_serversettings'] == '1') { */ UI::twigBuffer('user/table.html.twig', [ - 'api_response' => json_decode($json_result, true)['data'], + 'collection' => $collection->getData(), 'table_options' => $admin_list_data['admin_list'], ]); UI::twigOutputBuffer(); diff --git a/admin_customers.php b/admin_customers.php index e9b3efd7..6b01d8ea 100644 --- a/admin_customers.php +++ b/admin_customers.php @@ -33,31 +33,14 @@ if ($page == 'customers' && $userinfo['customers'] != '0') { $log->logAction(\Froxlor\FroxlorLogger::ADM_ACTION, LOG_NOTICE, "viewed admin_customers"); $customer_list_data = include_once dirname(__FILE__) . '/lib/tablelisting/admin/admin/tablelisting.customer.php'; - /* - $fields = array( - 'c.loginname' => $lng['login']['username'], - 'a.loginname' => $lng['admin']['admin'], - 'c.name' => $lng['customer']['name'], - 'c.email' => $lng['customer']['email'], - 'c.firstname' => $lng['customer']['firstname'], - 'c.company' => $lng['customer']['company'], - 'c.diskspace' => $lng['customer']['diskspace'], - 'c.diskspace_used' => $lng['customer']['diskspace'] . ' (' . $lng['panel']['used'] . ')', - 'c.traffic' => $lng['customer']['traffic'], - 'c.traffic_used' => $lng['customer']['traffic'] . ' (' . $lng['panel']['used'] . ')' - ); - */ try { - // get total count - $json_result = Customers::getLocal($userinfo)->listingCount(); - $result = json_decode($json_result, true)['data']; - // initialize pagination and filtering - /* - $paging = new \Froxlor\UI\Pagination($userinfo, $fields, $result); - */ - $paging = new \Froxlor\UI\Pagination($userinfo, $customer_list_data['customer_list']['columns'], $result); - // get list - $json_result = Customers::getLocal($userinfo, $paging->getApiCommandParams())->listing(); + // get collection + $collection = new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Customers::class, $userinfo); + // initialize pagination and filtering + $paging = new \Froxlor\UI\Pagination($userinfo, $customer_list_data['customer_list']['columns'], $collection->count()); + // get filtered collection + $collection = new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Customers::class, $userinfo, $paging->getApiCommandParams()); + $collection->has('admin', \Froxlor\Api\Commands\Admins::class, 'adminid', 'adminid'); } catch (Exception $e) { \Froxlor\UI\Response::dynamic_error($e->getMessage()); } @@ -156,7 +139,7 @@ if ($page == 'customers' && $userinfo['customers'] != '0') { */ UI::twigBuffer('user/table.html.twig', [ - 'api_response' => json_decode($json_result, true)['data'], + 'collection' => $collection->getData(), 'table_options' => $customer_list_data['customer_list'], ]); UI::twigOutputBuffer(); diff --git a/admin_domains.php b/admin_domains.php index 6d215d62..caad37ab 100644 --- a/admin_domains.php +++ b/admin_domains.php @@ -24,6 +24,7 @@ use Froxlor\Api\Commands\Customers as Customers; use Froxlor\Api\Commands\Domains as Domains; use Froxlor\Database\Database; use Froxlor\Settings; +use Froxlor\UI\Panel\UI; use Froxlor\UI\Request; $id = (int) Request::get('id'); @@ -34,54 +35,26 @@ if ($page == 'domains' || $page == 'overview') { $countcustomers = json_decode($json_result, true)['data']; if ($action == '') { - $log->logAction(\Froxlor\FroxlorLogger::ADM_ACTION, LOG_NOTICE, "viewed admin_domains"); - $fields = array( - 'd.domain_ace' => $lng['domains']['domainname'], - 'c.name' => $lng['customer']['name'], - 'c.firstname' => $lng['customer']['firstname'], - 'c.company' => $lng['customer']['company'], - 'c.loginname' => $lng['login']['username'], - 'd.aliasdomain' => $lng['domains']['aliasdomain'] - ); - try { - // get total count - $json_result = Domains::getLocal($userinfo)->listingCount(); - $result = json_decode($json_result, true)['data']; - // initialize pagination and filtering - $paging = new \Froxlor\UI\Pagination($userinfo, $fields, $result); - // get list - $json_result = Domains::getLocal($userinfo, $paging->getApiCommandParams())->listing(); - } catch (Exception $e) { - \Froxlor\UI\Response::dynamic_error($e->getMessage()); - } - $result = json_decode($json_result, true)['data']; + $domain_list_data = include_once dirname(__FILE__) . '/lib/tablelisting/admin/admin/tablelisting.domain.php'; - $domains = ''; - $sortcode = $paging->getHtmlSortCode($lng); - $arrowcode = $paging->getHtmlArrowCode($filename . '?page=' . $page . '&s=' . $s); - $searchcode = $paging->getHtmlSearchCode($lng); - $pagingcode = $paging->getHtmlPagingCode($filename . '?page=' . $page . '&s=' . $s); + try { + // get collection + $collection = new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Domains::class, $userinfo); + // initialize pagination and filtering + $paging = new \Froxlor\UI\Pagination($userinfo, $domain_list_data['domain_list']['columns'], $collection->count()); + // get filtered collection + $collection = new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Domains::class, $userinfo, $paging->getApiCommandParams()); + $collection->has('customer', \Froxlor\Api\Commands\Customers::class, 'customerid', 'customerid'); + } catch (Exception $e) { + \Froxlor\UI\Response::dynamic_error($e->getMessage()); + } - $count = 0; - foreach ($result['list'] as $row) { - formatDomainEntry($row, $idna_convert); - $row['customername'] = \Froxlor\User::getCorrectFullUserDetails($row); - $row = \Froxlor\PhpHelper::htmlentitiesArray($row); - // display a nice list of IP's if it's not an alias for another domain - if (isset($row['aliasdomainid']) && $row['aliasdomainid'] != null && isset($row['aliasdomain']) && $row['aliasdomain'] != '') { - $row['ipandport'] = sprintf($lng['domains']['isaliasdomainof'], $row['aliasdomain']); - } else { - $row['ipandport'] = str_replace("\n", "
", $row['ipandport']); - } - eval("\$domains.=\"" . \Froxlor\UI\Template::getTemplate("domains/domains_domain") . "\";"); - $count++; - } - - $domainscount = $result['count'] . " / " . $paging->getEntries(); - - // Display the list - eval("echo \"" . \Froxlor\UI\Template::getTemplate("domains/domains") . "\";"); + UI::twigBuffer('user/table.html.twig', [ + 'collection' => $collection->getData(), + 'table_options' => $domain_list_data['domain_list'], + ]); + UI::twigOutputBuffer(); } elseif ($action == 'delete' && $id != 0) { try { diff --git a/lib/Froxlor/UI/Collection.php b/lib/Froxlor/UI/Collection.php new file mode 100644 index 00000000..24f88dd0 --- /dev/null +++ b/lib/Froxlor/UI/Collection.php @@ -0,0 +1,72 @@ + (2010-) + * @author Maurice Preuß + * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt + * @package Collection + * + */ +class Collection +{ + private array $items; + private array $userInfo; + private array $params; + private string $class; + + public function __construct($class, $userInfo, $params = []) + { + $this->class = $class; + $this->params = $params; + $this->userInfo = $userInfo; + + $this->items = $this->getListing($this->class, $this->params); + } + + private function getListing($class, $params) + { + return json_decode($class::getLocal($this->userInfo, $params)->listing(), true); + } + + public function count() + { + return $this->get()['data']['count']; + } + + public function get() + { + return $this->items; + } + + public function getData() + { + return $this->get()['data']; + } + + public function getJson() + { + return json_encode($this->get()); + } + + public function has($column, $class, $parentKey = 'id', $childKey = 'id', $params = []) + { + $attributes = $this->getListing($class, $params); + + foreach ($this->items['data']['list'] as $key => $item) { + foreach ($attributes['data']['list'] as $list) { + if ($item[$parentKey] == $list[$childKey]) { + $this->items['data']['list'][$key][$column] = $list; + } + } + } + } +} \ No newline at end of file diff --git a/lib/Froxlor/UI/Listing.php b/lib/Froxlor/UI/Listing.php new file mode 100644 index 00000000..6d72981c --- /dev/null +++ b/lib/Froxlor/UI/Listing.php @@ -0,0 +1,29 @@ + (2010-) + * @author Maurice Preuß + * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt + * @package Listing + * + */ +class Listing +{ + public static function getVisibleColumnsForListing($listing, $default_columns) + { + // Hier käme dann die Logik, die das aus der DB zieht ... + // alternativ nimmt er die $default_columns, wenn kein Eintrag + // in der DB definiert ist + + return $default_columns; + } +} \ No newline at end of file diff --git a/lib/tablelisting/admin/admin/tablelisting.admin.php b/lib/tablelisting/admin/admin/tablelisting.admin.php index 6e7585c3..ca23ffde 100644 --- a/lib/tablelisting/admin/admin/tablelisting.admin.php +++ b/lib/tablelisting/admin/admin/tablelisting.admin.php @@ -22,38 +22,46 @@ return [ 'icon' => 'fa-solid fa-user', 'columns' => [ 'adminid' => [ - 'title' => '#', + 'label' => '#', + 'column' => 'adminid', 'sortable' => true, ], 'loginname' => [ - 'title' => $lng['login']['username'], + 'label' => $lng['login']['username'], + 'column' => 'loginname', 'sortable' => true, ], 'name' => [ - 'title' => $lng['customer']['name'], + 'label' => $lng['customer']['name'], + 'column' => 'name', ], 'diskspace' => [ - 'title' => $lng['customer']['diskspace'], + 'label' => $lng['customer']['diskspace'], + 'column' => 'diskspace', 'type' => 'usage', ], 'diskspace_used' => [ - 'title' => $lng['customer']['diskspace'] . ' (' . $lng['panel']['used'] . ')', + 'label' => $lng['customer']['diskspace'] . ' (' . $lng['panel']['used'] . ')', + 'column' => 'diskspace_used', 'type' => 'usage', ], 'traffic' => [ - 'title' => $lng['customer']['traffic'], + 'label' => $lng['customer']['traffic'], + 'column' => 'traffic', 'type' => 'usage', ], 'traffic_used' => [ - 'title' => $lng['customer']['traffic'] . ' (' . $lng['panel']['used'] . ')', + 'label' => $lng['customer']['traffic'] . ' (' . $lng['panel']['used'] . ')', + 'column' => 'traffic_used', 'type' => 'usage', ], 'deactivated' => [ - 'title' => $lng['admin']['deactivated'], + 'label' => $lng['admin']['deactivated'], + 'column' => 'deactivated', 'type' => 'boolean', ], ], - 'visible_columns' => getVisibleColumnsForListing('admin_list', [ + 'visible_columns' => \Froxlor\UI\Listing::getVisibleColumnsForListing('admin_list', [ 'loginname', 'name', 'diskspace', @@ -68,7 +76,7 @@ return [ 'href' => '#', ], 'show' => [ - 'title' => 'Show', + 'text' => 'Show', 'href' => '#', ] ], @@ -90,13 +98,3 @@ return [ ] ] ]; - -// Das müsste dann irgendwie als Klasse ausgelagert werden -function getVisibleColumnsForListing($listing, $default_columns) -{ - // Hier käme dann die Logik, die das aus der DB zieht ... - // alternativ nimmt er die $default_columns, wenn kein Eintrag - // in der DB definiert ist - - return $default_columns; -} diff --git a/lib/tablelisting/admin/admin/tablelisting.customer.php b/lib/tablelisting/admin/admin/tablelisting.customer.php index e861131d..a2dc4105 100644 --- a/lib/tablelisting/admin/admin/tablelisting.customer.php +++ b/lib/tablelisting/admin/admin/tablelisting.customer.php @@ -21,22 +21,61 @@ return [ 'title' => $lng['admin']['customers'], 'icon' => 'fa-solid fa-user', 'columns' => [ - 'loginname' => [ - 'title' => $lng['login']['username'] + 'c.loginname' => [ + 'label' => $lng['login']['username'], + 'column' => 'loginname', + ], + 'a.loginname' => [ + 'label' => $lng['admin']['admin'], + 'column' => 'admin.loginname', + ], + 'c.name' => [ + 'label' => $lng['customer']['name'], + 'column' => 'name', + ], + 'c.email' => [ + 'label' => $lng['customer']['email'], + 'column' => 'email', + ], + 'c.firstname' => [ + 'label' => $lng['customer']['firstname'], + 'column' => 'firstname', + ], + 'c.company' => [ + 'label' => $lng['customer']['company'], + 'column' => 'company', + ], + 'c.diskspace' => [ + 'label' => $lng['customer']['diskspace'], + 'column' => 'diskspace', + 'type' => 'usage' + ], + 'c.diskspace_used' => [ + 'label' => $lng['customer']['diskspace'] . ' (' . $lng['panel']['used'] . ')', + 'column' => 'diskspace_used', + 'type' => 'usage' + ], + 'c.traffic' => [ + 'label' => $lng['customer']['traffic'], + 'column' => 'traffic', + 'type' => 'usage' + ], + 'c.traffic_used' => [ + 'label' => $lng['customer']['traffic'] . ' (' . $lng['panel']['used'] . ')', + 'column' => 'traffic_used', + 'type' => 'usage' ], ], - 'visible_columns' => getVisibleColumnsForListing('admin_list', [ - 'loginname', + 'visible_columns' => \Froxlor\UI\Listing::getVisibleColumnsForListing('customer_list', [ + 'c.loginname', + 'a.loginname', + 'c.email', + 'c.firstname', + 'c.company', + 'c.diskspace', + 'c.diskspace_used', + 'c.traffic', + 'c.traffic_used', ]), ] ]; - -// Das müsste dann irgendwie als Klasse ausgelagert werden -function getVisibleColumnsForListing($listing, $default_columns) -{ - // Hier käme dann die Logik, die das aus der DB zieht ... - // alternativ nimmt er die $default_columns, wenn kein Eintrag - // in der DB definiert ist - - return $default_columns; -} diff --git a/lib/tablelisting/admin/admin/tablelisting.domain.php b/lib/tablelisting/admin/admin/tablelisting.domain.php new file mode 100644 index 00000000..0ba3d01b --- /dev/null +++ b/lib/tablelisting/admin/admin/tablelisting.domain.php @@ -0,0 +1,58 @@ + (2010-) + * @author Maurice Preuß + * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt + * @package Tabellisting + * + */ + +return [ + 'domain_list' => [ + 'title' => $lng['admin']['domains'], + 'icon' => 'fa-solid fa-user', + 'columns' => [ + 'd.domain_ace' => [ + 'label' => $lng['domains']['domainname'], + 'column' => 'domain_ace', + ], + 'c.name' => [ + 'label' => $lng['customer']['name'], + 'column' => 'customer.name', + ], + 'c.firstname' => [ + 'label' => $lng['customer']['firstname'], + 'column' => 'customer.firstname', + ], + 'c.company' => [ + 'label' => $lng['customer']['company'], + 'column' => 'customer.company', + ], + 'c.loginname' => [ + 'label' => $lng['login']['username'], + 'column' => 'customer.loginname', + ], + 'd.aliasdomain' => [ + 'label' => $lng['domains']['aliasdomain'], + 'column' => 'aliasdomain', + ], + ], + 'visible_columns' => \Froxlor\UI\Listing::getVisibleColumnsForListing('domain_list', [ + 'd.domain_ace', + 'c.name', + 'c.firstname', + 'c.company', + 'c.loginname', + 'd.aliasdomain', + ]), + ] +]; diff --git a/templates/Froxlor/table/table.html.twig b/templates/Froxlor/table/table.html.twig index 2ff84e36..09cc654a 100644 --- a/templates/Froxlor/table/table.html.twig +++ b/templates/Froxlor/table/table.html.twig @@ -1,4 +1,4 @@ -{% macro table(table_options, api_response, action, title = "") %} +{% macro table(table_options, collection, action, title = "") %} {% import "Froxlor/table/tablemacros.html.twig" as tablemacros %} @@ -12,12 +12,12 @@ {% endif %} -
+
{% for column in table_options.visible_columns %} - + {% endfor %} {% if table_options.actions %} @@ -25,16 +25,16 @@ - {% for columns in api_response.list %} + {% for columns in collection.list %} {% for column in table_options.visible_columns %} {% endfor %} diff --git a/templates/Froxlor/table/tablemacros.html.twig b/templates/Froxlor/table/tablemacros.html.twig index 7a131b57..b8af8ba8 100644 --- a/templates/Froxlor/table/tablemacros.html.twig +++ b/templates/Froxlor/table/tablemacros.html.twig @@ -1,4 +1,5 @@ {% macro contextual_class(contextual_class, columns) %} + {# this could be refactored as filter #} {% for i_key,i_column in columns %} {% for c_key, c_column in contextual_class %} {% if i_key == c_key %} @@ -12,7 +13,6 @@ {{ c_column.return }} {% endif %} {% elseif c_column.column is not empty %} - check for {{ columns[c_column.column] }} {# check for column #} {% if c_column.operator is empty and columns[c_column.column] == i_column %} {{ c_column.return }} @@ -37,4 +37,17 @@ {% macro usage(value) %} {{ value }} +{% endmacro %} + +{% macro column(arr, str) %} + {# this could be refactored as filter #} + {% set strarr = str|split('.') %} + + {% if strarr|length == 1 %} + {{ arr[strarr[0]] }} + {% elseif strarr|length == 2 %} + {{ arr[strarr[0]][strarr[1]] }} + {% else %} + column depth not supported + {% endif %} {% endmacro %} \ No newline at end of file diff --git a/templates/Froxlor/user/table.html.twig b/templates/Froxlor/user/table.html.twig index c0f6c35f..c47cdeeb 100644 --- a/templates/Froxlor/user/table.html.twig +++ b/templates/Froxlor/user/table.html.twig @@ -4,6 +4,6 @@ {% import "Froxlor/table/table.html.twig" as table %} - {{ table.table(table_options, api_response, '#', table_options.title) }} + {{ table.table(table_options, collection, '#', table_options.title) }} {% endblock %}
{{ table_options.columns[column].title }}{{ table_options.columns[column].label }}Actions
{% if table_options.columns[column].type is empty %} - {{ columns[column] }} + {{ tablemacros.column(columns, table_options.columns[column].column)|trim }} {% elseif table_options.columns[column].type == 'boolean' %} - {{ tablemacros.boolean(columns[column]) }} + {{ tablemacros.boolean(tablemacros.column(columns, table_options.columns[column].column)|trim) }} {% elseif table_options.columns[column].type == 'usage' %} - {{ tablemacros.usage(columns[column]) }} + {{ tablemacros.usage(tablemacros.column(columns, table_options.columns[column].column)|trim) }} {% endif %}