update table and add listing and collection class

This commit is contained in:
envoyr
2022-02-22 17:09:36 +01:00
parent 5f2550e19c
commit 855e220d14
11 changed files with 284 additions and 131 deletions

View File

@@ -34,25 +34,13 @@ if ($page == 'admins' && $userinfo['change_serversettings'] == '1') {
$log->logAction(\Froxlor\FroxlorLogger::ADM_ACTION, LOG_NOTICE, "viewed admin_admins"); $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'; $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 { try {
// get total count // get collection
$json_result = Admins::getLocal($userinfo)->listingCount(); $collection = new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Admins::class, $userinfo);
$result = json_decode($json_result, true)['data'];
// initialize pagination and filtering // initialize pagination and filtering
$paging = new \Froxlor\UI\Pagination($userinfo, $admin_list_data['admin_list']['columns'], $result); $paging = new \Froxlor\UI\Pagination($userinfo, $admin_list_data['admin_list']['columns'], $collection->count());
// get list // get filtered collection
$json_result = Admins::getLocal($userinfo, $paging->getApiCommandParams())->listing(); $collection = new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Admins::class, $userinfo, $paging->getApiCommandParams());
} catch (Exception $e) { } catch (Exception $e) {
\Froxlor\UI\Response::dynamic_error($e->getMessage()); \Froxlor\UI\Response::dynamic_error($e->getMessage());
} }
@@ -115,7 +103,7 @@ if ($page == 'admins' && $userinfo['change_serversettings'] == '1') {
*/ */
UI::twigBuffer('user/table.html.twig', [ UI::twigBuffer('user/table.html.twig', [
'api_response' => json_decode($json_result, true)['data'], 'collection' => $collection->getData(),
'table_options' => $admin_list_data['admin_list'], 'table_options' => $admin_list_data['admin_list'],
]); ]);
UI::twigOutputBuffer(); UI::twigOutputBuffer();

View File

@@ -33,31 +33,14 @@ if ($page == 'customers' && $userinfo['customers'] != '0') {
$log->logAction(\Froxlor\FroxlorLogger::ADM_ACTION, LOG_NOTICE, "viewed admin_customers"); $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'; $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 { try {
// get total count // get collection
$json_result = Customers::getLocal($userinfo)->listingCount(); $collection = new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Customers::class, $userinfo);
$result = json_decode($json_result, true)['data']; // initialize pagination and filtering
// initialize pagination and filtering $paging = new \Froxlor\UI\Pagination($userinfo, $customer_list_data['customer_list']['columns'], $collection->count());
/* // get filtered collection
$paging = new \Froxlor\UI\Pagination($userinfo, $fields, $result); $collection = new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Customers::class, $userinfo, $paging->getApiCommandParams());
*/ $collection->has('admin', \Froxlor\Api\Commands\Admins::class, 'adminid', 'adminid');
$paging = new \Froxlor\UI\Pagination($userinfo, $customer_list_data['customer_list']['columns'], $result);
// get list
$json_result = Customers::getLocal($userinfo, $paging->getApiCommandParams())->listing();
} catch (Exception $e) { } catch (Exception $e) {
\Froxlor\UI\Response::dynamic_error($e->getMessage()); \Froxlor\UI\Response::dynamic_error($e->getMessage());
} }
@@ -156,7 +139,7 @@ if ($page == 'customers' && $userinfo['customers'] != '0') {
*/ */
UI::twigBuffer('user/table.html.twig', [ UI::twigBuffer('user/table.html.twig', [
'api_response' => json_decode($json_result, true)['data'], 'collection' => $collection->getData(),
'table_options' => $customer_list_data['customer_list'], 'table_options' => $customer_list_data['customer_list'],
]); ]);
UI::twigOutputBuffer(); UI::twigOutputBuffer();

View File

@@ -24,6 +24,7 @@ use Froxlor\Api\Commands\Customers as Customers;
use Froxlor\Api\Commands\Domains as Domains; use Froxlor\Api\Commands\Domains as Domains;
use Froxlor\Database\Database; use Froxlor\Database\Database;
use Froxlor\Settings; use Froxlor\Settings;
use Froxlor\UI\Panel\UI;
use Froxlor\UI\Request; use Froxlor\UI\Request;
$id = (int) Request::get('id'); $id = (int) Request::get('id');
@@ -34,54 +35,26 @@ if ($page == 'domains' || $page == 'overview') {
$countcustomers = json_decode($json_result, true)['data']; $countcustomers = json_decode($json_result, true)['data'];
if ($action == '') { if ($action == '') {
$log->logAction(\Froxlor\FroxlorLogger::ADM_ACTION, LOG_NOTICE, "viewed admin_domains"); $log->logAction(\Froxlor\FroxlorLogger::ADM_ACTION, LOG_NOTICE, "viewed admin_domains");
$fields = array( $domain_list_data = include_once dirname(__FILE__) . '/lib/tablelisting/admin/admin/tablelisting.domain.php';
'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'];
$domains = ''; try {
$sortcode = $paging->getHtmlSortCode($lng); // get collection
$arrowcode = $paging->getHtmlArrowCode($filename . '?page=' . $page . '&s=' . $s); $collection = new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Domains::class, $userinfo);
$searchcode = $paging->getHtmlSearchCode($lng); // initialize pagination and filtering
$pagingcode = $paging->getHtmlPagingCode($filename . '?page=' . $page . '&s=' . $s); $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; UI::twigBuffer('user/table.html.twig', [
foreach ($result['list'] as $row) { 'collection' => $collection->getData(),
formatDomainEntry($row, $idna_convert); 'table_options' => $domain_list_data['domain_list'],
$row['customername'] = \Froxlor\User::getCorrectFullUserDetails($row); ]);
$row = \Froxlor\PhpHelper::htmlentitiesArray($row); UI::twigOutputBuffer();
// 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", "<br />", $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") . "\";");
} elseif ($action == 'delete' && $id != 0) { } elseif ($action == 'delete' && $id != 0) {
try { try {

View File

@@ -0,0 +1,72 @@
<?php
namespace Froxlor\UI;
/**
* 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 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;
}
}
}
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Froxlor\UI;
/**
* 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 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;
}
}

View File

@@ -22,38 +22,46 @@ return [
'icon' => 'fa-solid fa-user', 'icon' => 'fa-solid fa-user',
'columns' => [ 'columns' => [
'adminid' => [ 'adminid' => [
'title' => '#', 'label' => '#',
'column' => 'adminid',
'sortable' => true, 'sortable' => true,
], ],
'loginname' => [ 'loginname' => [
'title' => $lng['login']['username'], 'label' => $lng['login']['username'],
'column' => 'loginname',
'sortable' => true, 'sortable' => true,
], ],
'name' => [ 'name' => [
'title' => $lng['customer']['name'], 'label' => $lng['customer']['name'],
'column' => 'name',
], ],
'diskspace' => [ 'diskspace' => [
'title' => $lng['customer']['diskspace'], 'label' => $lng['customer']['diskspace'],
'column' => 'diskspace',
'type' => 'usage', 'type' => 'usage',
], ],
'diskspace_used' => [ 'diskspace_used' => [
'title' => $lng['customer']['diskspace'] . ' (' . $lng['panel']['used'] . ')', 'label' => $lng['customer']['diskspace'] . ' (' . $lng['panel']['used'] . ')',
'column' => 'diskspace_used',
'type' => 'usage', 'type' => 'usage',
], ],
'traffic' => [ 'traffic' => [
'title' => $lng['customer']['traffic'], 'label' => $lng['customer']['traffic'],
'column' => 'traffic',
'type' => 'usage', 'type' => 'usage',
], ],
'traffic_used' => [ 'traffic_used' => [
'title' => $lng['customer']['traffic'] . ' (' . $lng['panel']['used'] . ')', 'label' => $lng['customer']['traffic'] . ' (' . $lng['panel']['used'] . ')',
'column' => 'traffic_used',
'type' => 'usage', 'type' => 'usage',
], ],
'deactivated' => [ 'deactivated' => [
'title' => $lng['admin']['deactivated'], 'label' => $lng['admin']['deactivated'],
'column' => 'deactivated',
'type' => 'boolean', 'type' => 'boolean',
], ],
], ],
'visible_columns' => getVisibleColumnsForListing('admin_list', [ 'visible_columns' => \Froxlor\UI\Listing::getVisibleColumnsForListing('admin_list', [
'loginname', 'loginname',
'name', 'name',
'diskspace', 'diskspace',
@@ -68,7 +76,7 @@ return [
'href' => '#', 'href' => '#',
], ],
'show' => [ 'show' => [
'title' => 'Show', 'text' => 'Show',
'href' => '#', '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;
}

View File

@@ -21,22 +21,61 @@ return [
'title' => $lng['admin']['customers'], 'title' => $lng['admin']['customers'],
'icon' => 'fa-solid fa-user', 'icon' => 'fa-solid fa-user',
'columns' => [ 'columns' => [
'loginname' => [ 'c.loginname' => [
'title' => $lng['login']['username'] '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', [ 'visible_columns' => \Froxlor\UI\Listing::getVisibleColumnsForListing('customer_list', [
'loginname', '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;
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* 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 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',
]),
]
];

View File

@@ -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 %} {% import "Froxlor/table/tablemacros.html.twig" as tablemacros %}
@@ -12,12 +12,12 @@
</h3> </h3>
{% endif %} {% endif %}
<div class="card"> <div class="card table-responsive">
<table class="table table-borderless table-striped mb-0"> <table class="table table-borderless table-striped mb-0">
<thead> <thead>
<tr> <tr>
{% for column in table_options.visible_columns %} {% for column in table_options.visible_columns %}
<th class="p-3">{{ table_options.columns[column].title }}</th> <th class="p-3">{{ table_options.columns[column].label }}</th>
{% endfor %} {% endfor %}
{% if table_options.actions %} {% if table_options.actions %}
<th class="p-3 text-end">Actions</th> <th class="p-3 text-end">Actions</th>
@@ -25,16 +25,16 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for columns in api_response.list %} {% for columns in collection.list %}
<tr class="{{ tablemacros.contextual_class(table_options.contextual_class, columns)|trim }}"> <tr class="{{ tablemacros.contextual_class(table_options.contextual_class, columns)|trim }}">
{% for column in table_options.visible_columns %} {% for column in table_options.visible_columns %}
<td class="p-3"> <td class="p-3">
{% if table_options.columns[column].type is empty %} {% 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' %} {% 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' %} {% elseif table_options.columns[column].type == 'usage' %}
{{ tablemacros.usage(columns[column]) }} {{ tablemacros.usage(tablemacros.column(columns, table_options.columns[column].column)|trim) }}
{% endif %} {% endif %}
</td> </td>
{% endfor %} {% endfor %}

View File

@@ -1,4 +1,5 @@
{% macro contextual_class(contextual_class, columns) %} {% macro contextual_class(contextual_class, columns) %}
{# this could be refactored as filter #}
{% for i_key,i_column in columns %} {% for i_key,i_column in columns %}
{% for c_key, c_column in contextual_class %} {% for c_key, c_column in contextual_class %}
{% if i_key == c_key %} {% if i_key == c_key %}
@@ -12,7 +13,6 @@
{{ c_column.return }} {{ c_column.return }}
{% endif %} {% endif %}
{% elseif c_column.column is not empty %} {% elseif c_column.column is not empty %}
check for {{ columns[c_column.column] }}
{# check for column #} {# check for column #}
{% if c_column.operator is empty and columns[c_column.column] == i_column %} {% if c_column.operator is empty and columns[c_column.column] == i_column %}
{{ c_column.return }} {{ c_column.return }}
@@ -37,4 +37,17 @@
{% macro usage(value) %} {% macro usage(value) %}
{{ 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 %} {% endmacro %}

View File

@@ -4,6 +4,6 @@
{% import "Froxlor/table/table.html.twig" as table %} {% 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 %} {% endblock %}