fix pagination back links
Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Froxlor\UI;
|
namespace Froxlor\UI;
|
||||||
|
|
||||||
|
use Froxlor\Settings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This file is part of the Froxlor project.
|
* This file is part of the Froxlor project.
|
||||||
* Copyright (c) 2010 the Froxlor Team (see authors).
|
* Copyright (c) 2010 the Froxlor Team (see authors).
|
||||||
@@ -19,105 +21,105 @@ namespace Froxlor\UI;
|
|||||||
*/
|
*/
|
||||||
class Collection
|
class Collection
|
||||||
{
|
{
|
||||||
private string $class;
|
private string $class;
|
||||||
private array $has = [];
|
private array $has = [];
|
||||||
private array $params;
|
private array $params;
|
||||||
private array $userinfo;
|
private array $userinfo;
|
||||||
private ?Pagination $pagination;
|
private ?Pagination $pagination;
|
||||||
|
|
||||||
public function __construct(string $class, array $userInfo, array $params = [])
|
public function __construct(string $class, array $userInfo, array $params = [])
|
||||||
{
|
{
|
||||||
$this->class = $class;
|
$this->class = $class;
|
||||||
$this->params = $params;
|
$this->params = $params;
|
||||||
$this->userinfo = $userInfo;
|
$this->userinfo = $userInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getListing($class, $params): array
|
private function getListing($class, $params): array
|
||||||
{
|
{
|
||||||
return json_decode($class::getLocal($this->userinfo, $params)->listing(), true);
|
return json_decode($class::getLocal($this->userinfo, $params)->listing(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function count(): int
|
public function count(): int
|
||||||
{
|
{
|
||||||
return json_decode($this->class::getLocal($this->userinfo, $this->params)->listingCount(), true)['data'];
|
return json_decode($this->class::getLocal($this->userinfo, $this->params)->listingCount(), true)['data'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(): array
|
public function get(): array
|
||||||
{
|
{
|
||||||
$result = $this->getListing($this->class, $this->params);
|
$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)
|
// check if the api result contains any items (not the overall listingCount as we might be in a search-resultset)
|
||||||
if (count($result)) {
|
if (count($result)) {
|
||||||
foreach ($this->has as $has) {
|
foreach ($this->has as $has) {
|
||||||
$attributes = $this->getListing($has['class'], $has['params']);
|
$attributes = $this->getListing($has['class'], $has['params']);
|
||||||
|
|
||||||
foreach ($result['data']['list'] as $key => $item) {
|
foreach ($result['data']['list'] as $key => $item) {
|
||||||
foreach ($attributes['data']['list'] as $list) {
|
foreach ($attributes['data']['list'] as $list) {
|
||||||
if ($item[$has['parentKey']] == $list[$has['childKey']]) {
|
if ($item[$has['parentKey']] == $list[$has['childKey']]) {
|
||||||
$result['data']['list'][$key][$has['column']] = $list;
|
$result['data']['list'][$key][$has['column']] = $list;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// attach pagination if available
|
// attach pagination if available
|
||||||
if ($this->pagination) {
|
if ($this->pagination) {
|
||||||
$result = array_merge($result, $this->pagination->getApiResponseParams());
|
$result = array_merge($result, $this->pagination->getApiResponseParams());
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getData(): array
|
public function getData(): array
|
||||||
{
|
{
|
||||||
return $this->get()['data'];
|
return $this->get()['data'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getList(): array
|
public function getList(): array
|
||||||
{
|
{
|
||||||
return $this->getData()['list'];
|
return $this->getData()['list'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getJson(): string
|
public function getJson(): string
|
||||||
{
|
{
|
||||||
return json_encode($this->get());
|
return json_encode($this->get());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function has(string $column, string $class, string $parentKey = 'id', string $childKey = 'id', array $params = []): Collection
|
public function has(string $column, string $class, string $parentKey = 'id', string $childKey = 'id', array $params = []): Collection
|
||||||
{
|
{
|
||||||
$this->has[] = [
|
$this->has[] = [
|
||||||
'column' => $column,
|
'column' => $column,
|
||||||
'class' => $class,
|
'class' => $class,
|
||||||
'parentKey' => $parentKey,
|
'parentKey' => $parentKey,
|
||||||
'childKey' => $childKey,
|
'childKey' => $childKey,
|
||||||
'params' => $params
|
'params' => $params
|
||||||
];
|
];
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addParam(array $keyval): Collection
|
public function addParam(array $keyval): Collection
|
||||||
{
|
{
|
||||||
$this->params = array_merge($this->params, $keyval);
|
$this->params = array_merge($this->params, $keyval);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function withPagination(array $columns): Collection
|
public function withPagination(array $columns): Collection
|
||||||
{
|
{
|
||||||
// Get only searchable columns
|
// Get only searchable columns
|
||||||
$sortableColumns = [];
|
$sortableColumns = [];
|
||||||
foreach ($columns as $key => $column) {
|
foreach ($columns as $key => $column) {
|
||||||
if (isset($column['sortable']) && $column['sortable']) {
|
if (isset($column['sortable']) && $column['sortable']) {
|
||||||
$sortableColumns[$key] = $column;
|
$sortableColumns[$key] = $column;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare pagination
|
// Prepare pagination
|
||||||
$this->pagination = new Pagination($this->userinfo, $sortableColumns, $this->count());
|
$this->pagination = new Pagination($sortableColumns, $this->count(), (int) Settings::Get('panel.paging'));
|
||||||
$this->params = array_merge($this->params, $this->pagination->getApiCommandParams());
|
$this->params = array_merge($this->params, $this->pagination->getApiCommandParams());
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Froxlor\UI;
|
namespace Froxlor\UI;
|
||||||
|
|
||||||
use Froxlor\Settings;
|
use Froxlor\Settings;
|
||||||
@@ -26,33 +27,32 @@ class Pagination
|
|||||||
{
|
{
|
||||||
private array $data = array();
|
private array $data = array();
|
||||||
|
|
||||||
private ?array $fields = null;
|
private ?array $fields = null;
|
||||||
|
|
||||||
public string $sortorder = 'ASC';
|
public string $sortorder = 'ASC';
|
||||||
|
|
||||||
public $sortfield = null;
|
public $sortfield = null;
|
||||||
|
|
||||||
private ?string $searchtext = null;
|
private ?string $searchtext = null;
|
||||||
|
|
||||||
private $searchfield = null;
|
private $searchfield = null;
|
||||||
|
|
||||||
private bool $is_search = false;
|
private bool $is_search = false;
|
||||||
|
|
||||||
private int $pageno = 0;
|
private int $pageno = 0;
|
||||||
|
|
||||||
private int $entries = 0;
|
private int $entries = 0;
|
||||||
|
|
||||||
private int $perPage;
|
private int $perPage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create new pagination object to search/filter, limit and sort Api-listing() calls
|
* Create new pagination object to search/filter, limit and sort Api-listing() calls
|
||||||
*
|
*
|
||||||
* @param array $userinfo
|
* @param array $fields
|
||||||
* @param array $fields
|
* @param int $total_entries
|
||||||
* @param int $total_entries
|
* @param int $perPage
|
||||||
* @param int $perPage
|
*/
|
||||||
*/
|
public function __construct(array $fields = array(), int $total_entries = 0, int $perPage = 20)
|
||||||
public function __construct(array $userinfo, array $fields = array(), int $total_entries = 0, int $perPage = 20)
|
|
||||||
{
|
{
|
||||||
$this->fields = $fields;
|
$this->fields = $fields;
|
||||||
$this->entries = $total_entries;
|
$this->entries = $total_entries;
|
||||||
@@ -72,7 +72,7 @@ class Pagination
|
|||||||
if (isset($_REQUEST['searchfield']) && isset($fields[$_REQUEST['searchfield']])) {
|
if (isset($_REQUEST['searchfield']) && isset($fields[$_REQUEST['searchfield']])) {
|
||||||
$this->searchfield = $_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);
|
$this->addSearch($this->searchtext, $this->searchfield);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,8 +110,8 @@ class Pagination
|
|||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function addOrderBy($field = null, $order = 'ASC'): 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'] = array();
|
||||||
}
|
}
|
||||||
$this->data['sql_orderby'][$field] = $order;
|
$this->data['sql_orderby'][$field] = $order;
|
||||||
@@ -127,7 +127,7 @@ class Pagination
|
|||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function addLimit(int $limit = 0): Pagination
|
public function addLimit(int $limit = 0): Pagination
|
||||||
{
|
{
|
||||||
$this->data['sql_limit'] = $limit;
|
$this->data['sql_limit'] = $limit;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -139,23 +139,23 @@ class Pagination
|
|||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function addOffset(int $offset = 0): Pagination
|
public function addOffset(int $offset = 0): Pagination
|
||||||
{
|
{
|
||||||
$this->data['sql_offset'] = $offset;
|
$this->data['sql_offset'] = $offset;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* add a search operation
|
* add a search operation
|
||||||
*
|
*
|
||||||
* @param string|null $searchtext
|
* @param string|null $searchtext
|
||||||
* @param string|null $field
|
* @param string|null $field
|
||||||
* @param string|null $operator
|
* @param string|null $operator
|
||||||
*
|
*
|
||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function addSearch(string $searchtext = null, string $field = null, string $operator = null): 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'] = array();
|
||||||
}
|
}
|
||||||
$this->data['sql_search'][$field] = [
|
$this->data['sql_search'][$field] = [
|
||||||
@@ -177,7 +177,7 @@ class Pagination
|
|||||||
* @return number
|
* @return number
|
||||||
*/
|
*/
|
||||||
public function getEntries(): int
|
public function getEntries(): int
|
||||||
{
|
{
|
||||||
return $this->entries;
|
return $this->entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,18 +186,18 @@ class Pagination
|
|||||||
return $this->data;
|
return $this->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getApiResponseParams(): array
|
public function getApiResponseParams(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'pagination' => [
|
'pagination' => [
|
||||||
"total" => $this->entries,
|
"total" => $this->entries,
|
||||||
"per_page" => $this->perPage,
|
"per_page" => $this->perPage,
|
||||||
"current_page" => $this->pageno,
|
"current_page" => $this->pageno,
|
||||||
"last_page" => ceil($this->entries / $this->perPage),
|
"last_page" => ceil($this->entries / $this->perPage),
|
||||||
"from" => $this->data['sql_offset'] ?? null,
|
"from" => $this->data['sql_offset'] ?? null,
|
||||||
"to" => min($this->data['sql_offset'] + $this->perPage, $this->entries),
|
"to" => min($this->data['sql_offset'] + $this->perPage, $this->entries),
|
||||||
'sortfields' => array_keys($this->fields),
|
'sortfields' => array_keys($this->fields),
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
{% macro paging(pagination) %}
|
{% macro paging(pagination) %}
|
||||||
{% if pagination.last_page > 1 %}
|
{% if pagination.last_page > 1 %}
|
||||||
<div class="card-footer border-top">
|
<div class="card-footer border-top">
|
||||||
{% if pagination.current_page >= pagination.last_page %}
|
{% if pagination.current_page > 2 %}
|
||||||
<a href="?s={{ s }}&page={{ page }}&action={{ action }}&pageno=1">«</a>
|
<a href="?s={{ s }}&page={{ page }}&action={{ action }}&pageno=1">«</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if pagination.current_page >= pagination.last_page %}
|
{% if pagination.current_page > 1 %}
|
||||||
<a href="?s={{ s }}&page={{ page }}&action={{ action }}&pageno={{ pagination.current_page - 1 }}">‹</a>
|
<a href="?s={{ s }}&page={{ page }}&action={{ action }}&pageno={{ pagination.current_page - 1 }}">‹</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if pagination.current_page < pagination.last_page %}
|
{% if pagination.current_page < pagination.last_page %}
|
||||||
@@ -18,13 +18,13 @@
|
|||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% macro titlesorting(pagination, key, th) %}
|
{% macro titlesorting(pagination, key, th) %}
|
||||||
{% if pagination is defined and key in pagination.sortfields %}
|
{% if pagination is defined and key in pagination.sortfields %}
|
||||||
<th class="p-3 {{ th.class }}">
|
<th class="p-3 {{ th.class }}">
|
||||||
{{ th.text }}
|
{{ th.text }}
|
||||||
<a href="?s={{ s }}&page={{ page }}&action={{ action }}&pageno={{ pagination.current_page }}&sortfield={{ key }}&sortorder=desc">↓</a>
|
<a href="?s={{ s }}&page={{ page }}&action={{ action }}&pageno={{ pagination.current_page }}&sortfield={{ key }}&sortorder=desc">↓</a>
|
||||||
<a href="?s={{ s }}&page={{ page }}&action={{ action }}&pageno={{ pagination.current_page }}&sortfield={{ key }}&sortorder=asc">↑</a>
|
<a href="?s={{ s }}&page={{ page }}&action={{ action }}&pageno={{ pagination.current_page }}&sortfield={{ key }}&sortorder=asc">↑</a>
|
||||||
</th>
|
</th>
|
||||||
{% else %}
|
{% else %}
|
||||||
<th class="p-3 {{ th.class }}">{{ th.text }}</th>
|
<th class="p-3 {{ th.class }}">{{ th.text }}</th>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|||||||
Reference in New Issue
Block a user