diff --git a/lib/Froxlor/UI/Collection.php b/lib/Froxlor/UI/Collection.php index ce32c638..96c6d911 100644 --- a/lib/Froxlor/UI/Collection.php +++ b/lib/Froxlor/UI/Collection.php @@ -2,6 +2,8 @@ namespace Froxlor\UI; +use Froxlor\Settings; + /** * This file is part of the Froxlor project. * Copyright (c) 2010 the Froxlor Team (see authors). @@ -19,105 +21,105 @@ namespace Froxlor\UI; */ class Collection { - private string $class; - private array $has = []; - private array $params; - private array $userinfo; - private ?Pagination $pagination; + private string $class; + private array $has = []; + private array $params; + private array $userinfo; + private ?Pagination $pagination; - public function __construct(string $class, array $userInfo, array $params = []) - { - $this->class = $class; - $this->params = $params; - $this->userinfo = $userInfo; - } + public function __construct(string $class, array $userInfo, array $params = []) + { + $this->class = $class; + $this->params = $params; + $this->userinfo = $userInfo; + } - private function getListing($class, $params): array - { - return json_decode($class::getLocal($this->userinfo, $params)->listing(), true); - } + private function getListing($class, $params): array + { + return json_decode($class::getLocal($this->userinfo, $params)->listing(), true); + } - public function count(): int - { - return json_decode($this->class::getLocal($this->userinfo, $this->params)->listingCount(), true)['data']; - } + public function count(): int + { + return json_decode($this->class::getLocal($this->userinfo, $this->params)->listingCount(), true)['data']; + } - public function get(): array - { - $result = $this->getListing($this->class, $this->params); + public function get(): array + { + $result = $this->getListing($this->class, $this->params); - // check if the api result contains any items (not the overall listingCount as we might be in a search-resultset) - if (count($result)) { - foreach ($this->has as $has) { - $attributes = $this->getListing($has['class'], $has['params']); + // check if the api result contains any items (not the overall listingCount as we might be in a search-resultset) + if (count($result)) { + foreach ($this->has as $has) { + $attributes = $this->getListing($has['class'], $has['params']); - foreach ($result['data']['list'] as $key => $item) { - foreach ($attributes['data']['list'] as $list) { - if ($item[$has['parentKey']] == $list[$has['childKey']]) { - $result['data']['list'][$key][$has['column']] = $list; - } - } - } - } - } + foreach ($result['data']['list'] as $key => $item) { + foreach ($attributes['data']['list'] as $list) { + if ($item[$has['parentKey']] == $list[$has['childKey']]) { + $result['data']['list'][$key][$has['column']] = $list; + } + } + } + } + } - // attach pagination if available - if ($this->pagination) { - $result = array_merge($result, $this->pagination->getApiResponseParams()); - } + // attach pagination if available + if ($this->pagination) { + $result = array_merge($result, $this->pagination->getApiResponseParams()); + } - return $result; - } + return $result; + } - public function getData(): array - { - return $this->get()['data']; - } + public function getData(): array + { + return $this->get()['data']; + } - public function getList(): array - { - return $this->getData()['list']; - } + public function getList(): array + { + return $this->getData()['list']; + } - public function getJson(): string - { - return json_encode($this->get()); - } + public function getJson(): string + { + return json_encode($this->get()); + } - public function has(string $column, string $class, string $parentKey = 'id', string $childKey = 'id', array $params = []): Collection - { - $this->has[] = [ - 'column' => $column, - 'class' => $class, - 'parentKey' => $parentKey, - 'childKey' => $childKey, - 'params' => $params - ]; + public function has(string $column, string $class, string $parentKey = 'id', string $childKey = 'id', array $params = []): Collection + { + $this->has[] = [ + 'column' => $column, + 'class' => $class, + 'parentKey' => $parentKey, + 'childKey' => $childKey, + 'params' => $params + ]; - return $this; - } + return $this; + } - public function addParam(array $keyval): Collection - { - $this->params = array_merge($this->params, $keyval); + public function addParam(array $keyval): Collection + { + $this->params = array_merge($this->params, $keyval); - return $this; - } + return $this; + } - public function withPagination(array $columns): Collection - { - // Get only searchable columns - $sortableColumns = []; - foreach ($columns as $key => $column) { - if (isset($column['sortable']) && $column['sortable']) { - $sortableColumns[$key] = $column; - } - } + public function withPagination(array $columns): Collection + { + // Get only searchable columns + $sortableColumns = []; + foreach ($columns as $key => $column) { + if (isset($column['sortable']) && $column['sortable']) { + $sortableColumns[$key] = $column; + } + } - // Prepare pagination - $this->pagination = new Pagination($this->userinfo, $sortableColumns, $this->count()); - $this->params = array_merge($this->params, $this->pagination->getApiCommandParams()); + // Prepare pagination + $this->pagination = new Pagination($sortableColumns, $this->count(), (int) Settings::Get('panel.paging')); + $this->params = array_merge($this->params, $this->pagination->getApiCommandParams()); - return $this; - } + return $this; + } } diff --git a/lib/Froxlor/UI/Pagination.php b/lib/Froxlor/UI/Pagination.php index d3b593ba..3a146ffb 100644 --- a/lib/Froxlor/UI/Pagination.php +++ b/lib/Froxlor/UI/Pagination.php @@ -1,4 +1,5 @@ fields = $fields; $this->entries = $total_entries; @@ -72,7 +72,7 @@ class Pagination if (isset($_REQUEST['searchfield']) && isset($fields[$_REQUEST['searchfield']])) { $this->searchfield = $_REQUEST['searchfield']; } - if (! empty($this->searchtext) && ! empty($this->searchfield)) { + if (!empty($this->searchtext) && !empty($this->searchfield)) { $this->addSearch($this->searchtext, $this->searchfield); } @@ -110,8 +110,8 @@ class Pagination * @return Pagination */ public function addOrderBy($field = null, $order = 'ASC'): Pagination - { - if (! isset($this->data['sql_orderby'])) { + { + if (!isset($this->data['sql_orderby'])) { $this->data['sql_orderby'] = array(); } $this->data['sql_orderby'][$field] = $order; @@ -127,7 +127,7 @@ class Pagination * @return Pagination */ public function addLimit(int $limit = 0): Pagination - { + { $this->data['sql_limit'] = $limit; return $this; } @@ -139,23 +139,23 @@ class Pagination * @return Pagination */ public function addOffset(int $offset = 0): Pagination - { + { $this->data['sql_offset'] = $offset; return $this; } - /** - * add a search operation - * - * @param string|null $searchtext - * @param string|null $field - * @param string|null $operator - * - * @return Pagination - */ + /** + * add a search operation + * + * @param string|null $searchtext + * @param string|null $field + * @param string|null $operator + * + * @return Pagination + */ public function addSearch(string $searchtext = null, string $field = null, string $operator = null): Pagination - { - if (! isset($this->data['sql_search'])) { + { + if (!isset($this->data['sql_search'])) { $this->data['sql_search'] = array(); } $this->data['sql_search'][$field] = [ @@ -177,7 +177,7 @@ class Pagination * @return number */ public function getEntries(): int - { + { return $this->entries; } @@ -186,18 +186,18 @@ class Pagination return $this->data; } - public function getApiResponseParams(): array - { - return [ - 'pagination' => [ - "total" => $this->entries, - "per_page" => $this->perPage, - "current_page" => $this->pageno, - "last_page" => ceil($this->entries / $this->perPage), - "from" => $this->data['sql_offset'] ?? null, - "to" => min($this->data['sql_offset'] + $this->perPage, $this->entries), - 'sortfields' => array_keys($this->fields), - ] - ]; - } + public function getApiResponseParams(): array + { + return [ + 'pagination' => [ + "total" => $this->entries, + "per_page" => $this->perPage, + "current_page" => $this->pageno, + "last_page" => ceil($this->entries / $this->perPage), + "from" => $this->data['sql_offset'] ?? null, + "to" => min($this->data['sql_offset'] + $this->perPage, $this->entries), + 'sortfields' => array_keys($this->fields), + ] + ]; + } } diff --git a/templates/Froxlor/table/pagination.html.twig b/templates/Froxlor/table/pagination.html.twig index 3ea1cb7f..19d6917e 100644 --- a/templates/Froxlor/table/pagination.html.twig +++ b/templates/Froxlor/table/pagination.html.twig @@ -1,10 +1,10 @@ {% macro paging(pagination) %} {% if pagination.last_page > 1 %}