2019-03-27 10:53:21 -07:00
|
|
|
<?php
|
2016-07-27 00:33:56 -05:00
|
|
|
|
2016-07-27 23:23:33 -05:00
|
|
|
/**
|
2020-10-24 16:38:41 +08:00
|
|
|
* This file is part of the CodeIgniter 4 framework.
|
2016-07-27 23:23:33 -05:00
|
|
|
*
|
2020-10-24 16:38:41 +08:00
|
|
|
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
2016-07-27 23:23:33 -05:00
|
|
|
*
|
2020-10-24 16:38:41 +08:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2016-07-27 23:23:33 -05:00
|
|
|
*/
|
2018-11-10 02:57:39 -02:00
|
|
|
|
2019-03-27 10:53:21 -07:00
|
|
|
namespace CodeIgniter\Pager;
|
|
|
|
|
2020-10-04 00:27:56 +07:00
|
|
|
use CodeIgniter\HTTP\Exceptions\HTTPException;
|
2018-04-24 23:04:50 -05:00
|
|
|
use CodeIgniter\Pager\Exceptions\PagerException;
|
2016-08-31 16:38:08 -07:00
|
|
|
use CodeIgniter\View\RendererInterface;
|
2020-10-04 00:27:56 +07:00
|
|
|
use Config\Pager as PagerConfig;
|
2016-07-27 00:33:56 -05:00
|
|
|
|
2016-07-27 23:23:33 -05:00
|
|
|
/**
|
|
|
|
* Class Pager
|
|
|
|
*
|
|
|
|
* The Pager class provides semi-automatic and manual methods for creating
|
|
|
|
* pagination links and reading the current url's query variable, "page"
|
|
|
|
* to determine the current page. This class can support multiple
|
|
|
|
* paginations on a single page.
|
|
|
|
*/
|
2016-07-27 00:33:56 -05:00
|
|
|
class Pager implements PagerInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The group data.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $groups = [];
|
|
|
|
|
2018-09-11 19:10:37 +02:00
|
|
|
/**
|
|
|
|
* URI segment for groups if provided.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $segment = [];
|
|
|
|
|
2016-07-27 00:33:56 -05:00
|
|
|
/**
|
|
|
|
* Our configuration instance.
|
|
|
|
*
|
2020-10-04 00:27:56 +07:00
|
|
|
* @var PagerConfig
|
2016-07-27 00:33:56 -05:00
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The view engine to render the links with.
|
|
|
|
*
|
2016-08-31 16:38:08 -07:00
|
|
|
* @var RendererInterface
|
2016-07-27 00:33:56 -05:00
|
|
|
*/
|
|
|
|
protected $view;
|
|
|
|
|
2018-02-28 23:09:20 -03:00
|
|
|
/**
|
|
|
|
* List of only permitted queries
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $only = [];
|
|
|
|
|
2016-07-27 00:33:56 -05:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2019-05-05 10:12:18 -07:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2020-10-04 00:27:56 +07:00
|
|
|
* @param PagerConfig $config
|
2019-05-05 10:12:18 -07:00
|
|
|
* @param RendererInterface $view
|
|
|
|
*/
|
2020-10-04 00:27:56 +07:00
|
|
|
public function __construct(PagerConfig $config, RendererInterface $view)
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
|
|
|
$this->config = $config;
|
2018-11-10 02:57:39 -02:00
|
|
|
$this->view = $view;
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles creating and displaying the
|
|
|
|
*
|
2017-01-11 01:23:51 +07:00
|
|
|
* @param string $group
|
|
|
|
* @param string $template The output template alias to render.
|
2016-07-27 00:33:56 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-11 01:23:51 +07:00
|
|
|
public function links(string $group = 'default', string $template = 'default_full'): string
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
2016-07-27 23:23:33 -05:00
|
|
|
|
|
|
|
return $this->displayLinks($group, $template);
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates simple Next/Previous links, instead of full pagination.
|
|
|
|
*
|
|
|
|
* @param string $group
|
2017-01-11 01:23:51 +07:00
|
|
|
* @param string $template
|
2016-07-27 00:33:56 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-27 23:23:33 -05:00
|
|
|
public function simpleLinks(string $group = 'default', string $template = 'default_simple'): string
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
2016-07-27 23:23:33 -05:00
|
|
|
|
|
|
|
return $this->displayLinks($group, $template);
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows for a simple, manual, form of pagination where all of the data
|
|
|
|
* is provided by the user. The URL is the current URI.
|
|
|
|
*
|
2020-12-21 22:08:34 +01:00
|
|
|
* @param integer $page
|
|
|
|
* @param integer|null $perPage
|
|
|
|
* @param integer $total
|
|
|
|
* @param string $template The output template alias to render.
|
|
|
|
* @param integer $segment (whether page number is provided by URI segment)
|
|
|
|
* @param string|null $group optional group (i.e. if we'd like to define custom path)
|
2016-07-27 00:33:56 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-12-21 22:08:34 +01:00
|
|
|
public function makeLinks(int $page, ?int $perPage, int $total, string $template = 'default_full', int $segment = 0, ?string $group = 'default'): string
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
2020-03-23 18:48:50 +01:00
|
|
|
$group = $group === '' ? 'default' : $group;
|
2016-07-27 00:33:56 -05:00
|
|
|
|
2020-03-31 09:40:14 +07:00
|
|
|
$this->store($group, $page, $perPage ?? $this->config->perPage, $total, $segment);
|
2016-07-27 00:33:56 -05:00
|
|
|
|
2020-03-23 18:48:50 +01:00
|
|
|
return $this->displayLinks($group, $template);
|
2016-07-27 23:23:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the actual work of displaying the view file. Used internally
|
|
|
|
* by links(), simpleLinks(), and makeLinks().
|
|
|
|
*
|
|
|
|
* @param string $group
|
|
|
|
* @param string $template
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-04-19 14:14:08 +05:30
|
|
|
protected function displayLinks(string $group, string $template): string
|
2016-07-27 23:23:33 -05:00
|
|
|
{
|
|
|
|
$pager = new PagerRenderer($this->getDetails($group));
|
2016-07-27 00:33:56 -05:00
|
|
|
|
2018-11-10 02:57:39 -02:00
|
|
|
if (! array_key_exists($template, $this->config->templates))
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
2018-04-24 23:04:50 -05:00
|
|
|
throw PagerException::forInvalidTemplate($template);
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->view->setVar('pager', $pager)
|
2017-07-13 10:22:49 -07:00
|
|
|
->render($this->config->templates[$template]);
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores a set of pagination data for later display. Most commonly used
|
|
|
|
* by the model to automate the process.
|
|
|
|
*
|
2020-12-21 22:08:34 +01:00
|
|
|
* @param string $group
|
|
|
|
* @param integer $page
|
|
|
|
* @param integer|null $perPage
|
|
|
|
* @param integer $total
|
|
|
|
* @param integer $segment
|
2016-07-27 00:33:56 -05:00
|
|
|
*
|
2019-04-19 14:14:08 +05:30
|
|
|
* @return $this
|
2016-07-27 00:33:56 -05:00
|
|
|
*/
|
2020-12-21 22:08:34 +01:00
|
|
|
public function store(string $group, int $page, ?int $perPage, int $total, int $segment = 0)
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
2020-07-02 13:28:39 +02:00
|
|
|
if ($segment)
|
|
|
|
{
|
|
|
|
$this->setSegment($segment, $group);
|
|
|
|
}
|
2018-09-12 10:21:24 +02:00
|
|
|
|
2020-03-31 10:04:07 +07:00
|
|
|
$this->ensureGroup($group, $perPage);
|
2016-07-27 00:33:56 -05:00
|
|
|
|
2020-05-09 22:18:02 +07:00
|
|
|
if ($segment > 0 && $this->groups[$group]['currentPage'] > 0)
|
|
|
|
{
|
|
|
|
$page = $this->groups[$group]['currentPage'];
|
|
|
|
}
|
|
|
|
|
2020-11-01 23:44:59 +08:00
|
|
|
$perPage = $perPage ?? $this->config->perPage;
|
|
|
|
$pageCount = (int) ceil($total / $perPage);
|
|
|
|
|
2020-05-09 22:18:02 +07:00
|
|
|
$this->groups[$group]['currentPage'] = $page > $pageCount ? $pageCount : $page;
|
2018-11-10 02:57:39 -02:00
|
|
|
$this->groups[$group]['perPage'] = $perPage;
|
|
|
|
$this->groups[$group]['total'] = $total;
|
2020-04-17 00:37:14 +07:00
|
|
|
$this->groups[$group]['pageCount'] = $pageCount;
|
2016-07-27 00:33:56 -05:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2020-07-02 13:28:39 +02:00
|
|
|
/**
|
|
|
|
* Sets segment for a group.
|
|
|
|
*
|
|
|
|
* @param integer $number
|
|
|
|
* @param string $group
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function setSegment(int $number, string $group = 'default')
|
|
|
|
{
|
|
|
|
$this->segment[$group] = $number;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2016-07-27 00:33:56 -05:00
|
|
|
/**
|
|
|
|
* Sets the path that an aliased group of links will use.
|
|
|
|
*
|
|
|
|
* @param string $path
|
2019-05-05 10:12:18 -07:00
|
|
|
* @param string $group
|
2016-07-27 00:33:56 -05:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function setPath(string $path, string $group = 'default')
|
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
|
|
|
|
|
|
|
$this->groups[$group]['uri']->setPath($path);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:34:57 +04:00
|
|
|
//--------------------------------------------------------------------
|
2016-07-27 00:33:56 -05:00
|
|
|
|
2020-09-15 11:12:46 +07:00
|
|
|
/**
|
|
|
|
* Returns the total number of items in data store.
|
|
|
|
*
|
|
|
|
* @param string $group
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getTotal(string $group = 'default'): int
|
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
|
|
|
|
|
|
|
return $this->groups[$group]['total'];
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2016-07-27 00:33:56 -05:00
|
|
|
/**
|
|
|
|
* Returns the total number of pages.
|
|
|
|
*
|
2020-08-18 22:16:06 +07:00
|
|
|
* @param string $group
|
2016-07-27 00:33:56 -05:00
|
|
|
*
|
2018-11-10 02:57:39 -02:00
|
|
|
* @return integer
|
2016-07-27 00:33:56 -05:00
|
|
|
*/
|
|
|
|
public function getPageCount(string $group = 'default'): int
|
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
|
|
|
|
|
|
|
return $this->groups[$group]['pageCount'];
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of the current page of results.
|
|
|
|
*
|
2020-08-18 22:16:06 +07:00
|
|
|
* @param string $group
|
2016-07-27 00:33:56 -05:00
|
|
|
*
|
2018-11-10 02:57:39 -02:00
|
|
|
* @return integer
|
2016-07-27 00:33:56 -05:00
|
|
|
*/
|
|
|
|
public function getCurrentPage(string $group = 'default'): int
|
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
|
|
|
|
2020-07-02 13:28:39 +02:00
|
|
|
return $this->groups[$group]['currentPage'] ?: 1;
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells whether this group of results has any more pages of results.
|
|
|
|
*
|
2020-08-18 22:16:06 +07:00
|
|
|
* @param string $group
|
2016-07-27 00:33:56 -05:00
|
|
|
*
|
2018-11-10 02:57:39 -02:00
|
|
|
* @return boolean
|
2016-07-27 00:33:56 -05:00
|
|
|
*/
|
|
|
|
public function hasMore(string $group = 'default'): bool
|
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
|
|
|
|
2017-07-13 10:22:49 -07:00
|
|
|
return ($this->groups[$group]['currentPage'] * $this->groups[$group]['perPage']) < $this->groups[$group]['total'];
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the last page, if we have a total that we can calculate with.
|
|
|
|
*
|
|
|
|
* @param string $group
|
|
|
|
*
|
2018-11-10 02:57:39 -02:00
|
|
|
* @return integer|null
|
2016-07-27 00:33:56 -05:00
|
|
|
*/
|
|
|
|
public function getLastPage(string $group = 'default')
|
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
|
|
|
|
2018-11-10 02:57:39 -02:00
|
|
|
if (! is_numeric($this->groups[$group]['total']) || ! is_numeric($this->groups[$group]['perPage']))
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-01 23:44:59 +08:00
|
|
|
return (int) ceil($this->groups[$group]['total'] / $this->groups[$group]['perPage']);
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the first page # that should be shown.
|
|
|
|
*
|
|
|
|
* @param string $group
|
|
|
|
*
|
2018-11-10 02:57:39 -02:00
|
|
|
* @return integer
|
2016-07-27 00:33:56 -05:00
|
|
|
*/
|
2019-04-19 14:14:08 +05:30
|
|
|
public function getFirstPage(string $group = 'default'): int
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
|
|
|
|
|
|
|
// @todo determine based on a 'surroundCount' value
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URI for a specific page for the specified group.
|
|
|
|
*
|
2018-11-10 02:57:39 -02:00
|
|
|
* @param integer|null $page
|
|
|
|
* @param string $group
|
|
|
|
* @param boolean $returnObject
|
2016-07-27 00:33:56 -05:00
|
|
|
*
|
2018-02-28 23:09:20 -03:00
|
|
|
* @return string|\CodeIgniter\HTTP\URI
|
2016-07-27 00:33:56 -05:00
|
|
|
*/
|
2019-04-19 14:14:08 +05:30
|
|
|
public function getPageURI(int $page = null, string $group = 'default', bool $returnObject = false)
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
|
|
|
|
2018-11-17 12:53:02 -02:00
|
|
|
/**
|
|
|
|
* @var \CodeIgniter\HTTP\URI $uri
|
|
|
|
*/
|
2016-07-27 00:33:56 -05:00
|
|
|
$uri = $this->groups[$group]['uri'];
|
|
|
|
|
2018-11-10 02:57:39 -02:00
|
|
|
$segment = $this->segment[$group] ?? 0;
|
2018-09-11 19:10:37 +02:00
|
|
|
|
2018-11-17 12:53:02 -02:00
|
|
|
if ($segment)
|
|
|
|
{
|
|
|
|
$uri->setSegment($segment, $page);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-14 15:34:57 +04:00
|
|
|
$uri->addQuery($this->groups[$group]['pageSelector'], $page);
|
2018-11-17 12:53:02 -02:00
|
|
|
}
|
|
|
|
|
2018-02-28 23:09:20 -03:00
|
|
|
if ($this->only)
|
|
|
|
{
|
2018-09-11 19:15:00 +02:00
|
|
|
$query = array_intersect_key($_GET, array_flip($this->only));
|
2018-09-12 10:21:24 +02:00
|
|
|
|
2018-11-17 12:53:02 -02:00
|
|
|
if (! $segment)
|
2018-09-11 19:10:37 +02:00
|
|
|
{
|
2020-01-14 15:34:57 +04:00
|
|
|
$query[$this->groups[$group]['pageSelector']] = $page;
|
2018-09-11 19:10:37 +02:00
|
|
|
}
|
2018-02-28 23:09:20 -03:00
|
|
|
|
|
|
|
$uri->setQueryArray($query);
|
|
|
|
}
|
2016-07-27 00:33:56 -05:00
|
|
|
|
2017-07-13 10:22:49 -07:00
|
|
|
return $returnObject === true ? $uri : (string) $uri;
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the full URI to the next page of results, or null.
|
|
|
|
*
|
2018-11-10 02:57:39 -02:00
|
|
|
* @param string $group
|
|
|
|
* @param boolean $returnObject
|
2016-07-27 00:33:56 -05:00
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2019-04-19 14:14:08 +05:30
|
|
|
public function getNextPageURI(string $group = 'default', bool $returnObject = false)
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
|
|
|
|
|
|
|
$last = $this->getLastPage($group);
|
|
|
|
$curr = $this->getCurrentPage($group);
|
|
|
|
$page = null;
|
|
|
|
|
2018-11-10 02:57:39 -02:00
|
|
|
if (! empty($last) && ! empty($curr) && $last === $curr)
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($last > $curr)
|
|
|
|
{
|
2017-07-13 10:22:49 -07:00
|
|
|
$page = $curr + 1;
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getPageURI($page, $group, $returnObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the full URL to the previous page of results, or null.
|
|
|
|
*
|
2018-11-10 02:57:39 -02:00
|
|
|
* @param string $group
|
|
|
|
* @param boolean $returnObject
|
2016-07-27 00:33:56 -05:00
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2019-04-19 14:14:08 +05:30
|
|
|
public function getPreviousPageURI(string $group = 'default', bool $returnObject = false)
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
|
|
|
|
|
|
|
$first = $this->getFirstPage($group);
|
2018-11-10 02:57:39 -02:00
|
|
|
$curr = $this->getCurrentPage($group);
|
|
|
|
$page = null;
|
2016-07-27 00:33:56 -05:00
|
|
|
|
2018-11-10 02:57:39 -02:00
|
|
|
if (! empty($first) && ! empty($curr) && $first === $curr)
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($first < $curr)
|
|
|
|
{
|
2017-07-13 10:22:49 -07:00
|
|
|
$page = $curr - 1;
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getPageURI($page, $group, $returnObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of results per page that should be shown.
|
|
|
|
*
|
|
|
|
* @param string $group
|
|
|
|
*
|
2018-11-10 02:57:39 -02:00
|
|
|
* @return integer
|
2016-07-27 00:33:56 -05:00
|
|
|
*/
|
|
|
|
public function getPerPage(string $group = 'default'): int
|
|
|
|
{
|
|
|
|
$this->ensureGroup($group);
|
|
|
|
|
2017-07-13 10:22:49 -07:00
|
|
|
return (int) $this->groups[$group]['perPage'];
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array with details about the results, including
|
|
|
|
* total, per_page, current_page, last_page, next_url, prev_url, from, to.
|
|
|
|
* Does not include the actual data. This data is suitable for adding
|
|
|
|
* a 'data' object to with the result set and converting to JSON.
|
|
|
|
*
|
|
|
|
* @param string $group
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDetails(string $group = 'default'): array
|
|
|
|
{
|
2018-11-10 02:57:39 -02:00
|
|
|
if (! array_key_exists($group, $this->groups))
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
2018-04-24 23:04:50 -05:00
|
|
|
throw PagerException::forInvalidPaginationGroup($group);
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$newGroup = $this->groups[$group];
|
|
|
|
|
2018-11-10 02:57:39 -02:00
|
|
|
$newGroup['next'] = $this->getNextPageURI($group);
|
2016-07-27 00:33:56 -05:00
|
|
|
$newGroup['previous'] = $this->getPreviousPageURI($group);
|
2018-11-10 02:57:39 -02:00
|
|
|
$newGroup['segment'] = $this->segment[$group] ?? 0;
|
2016-07-27 00:33:56 -05:00
|
|
|
|
|
|
|
return $newGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2018-02-28 23:09:20 -03:00
|
|
|
/**
|
|
|
|
* Sets only allowed queries on pagination links.
|
|
|
|
*
|
|
|
|
* @param array $queries
|
|
|
|
*
|
|
|
|
* @return Pager
|
|
|
|
*/
|
|
|
|
public function only(array $queries):Pager
|
|
|
|
{
|
|
|
|
$this->only = $queries;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2016-07-27 00:33:56 -05:00
|
|
|
/**
|
|
|
|
* Ensures that an array exists for the group specified.
|
|
|
|
*
|
2020-03-31 10:04:07 +07:00
|
|
|
* @param string $group
|
|
|
|
* @param integer $perPage
|
2016-07-27 00:33:56 -05:00
|
|
|
*/
|
2020-03-31 10:04:07 +07:00
|
|
|
protected function ensureGroup(string $group, int $perPage = null)
|
2016-07-27 00:33:56 -05:00
|
|
|
{
|
|
|
|
if (array_key_exists($group, $this->groups))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->groups[$group] = [
|
2020-01-26 22:34:28 -06:00
|
|
|
'uri' => clone current_url(true),
|
2020-01-14 15:34:57 +04:00
|
|
|
'hasMore' => false,
|
|
|
|
'total' => null,
|
2020-03-31 10:04:07 +07:00
|
|
|
'perPage' => $perPage ?? $this->config->perPage,
|
2020-01-14 15:34:57 +04:00
|
|
|
'pageCount' => 1,
|
|
|
|
'pageSelector' => $group === 'default' ? 'page' : 'page_' . $group,
|
2016-07-27 00:33:56 -05:00
|
|
|
];
|
2017-03-10 17:50:25 +08:00
|
|
|
|
2020-01-14 15:34:57 +04:00
|
|
|
$this->calculateCurrentPage($group);
|
|
|
|
|
|
|
|
if ($_GET)
|
|
|
|
{
|
|
|
|
$this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2020-01-26 22:34:28 -06:00
|
|
|
|
2020-01-14 15:34:57 +04:00
|
|
|
/**
|
|
|
|
* Calculating the current page
|
|
|
|
*
|
|
|
|
* @param string $group
|
|
|
|
*/
|
|
|
|
protected function calculateCurrentPage(string $group)
|
|
|
|
{
|
2018-11-10 02:57:39 -02:00
|
|
|
if (array_key_exists($group, $this->segment))
|
2018-09-11 19:10:37 +02:00
|
|
|
{
|
2018-09-12 10:21:24 +02:00
|
|
|
try
|
|
|
|
{
|
2020-07-04 19:36:26 +02:00
|
|
|
$this->groups[$group]['currentPage'] = (int) $this->groups[$group]['uri']->setSilent(false)->getSegment($this->segment[$group]);
|
2018-09-12 10:21:24 +02:00
|
|
|
}
|
2020-10-04 00:27:56 +07:00
|
|
|
catch (HTTPException $e)
|
2018-09-12 10:21:24 +02:00
|
|
|
{
|
|
|
|
$this->groups[$group]['currentPage'] = 1;
|
|
|
|
}
|
2018-09-11 19:10:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-14 15:34:57 +04:00
|
|
|
$pageSelector = $this->groups[$group]['pageSelector'];
|
2018-09-11 19:10:37 +02:00
|
|
|
|
2020-01-14 15:42:35 +04:00
|
|
|
$page = (int) ($_GET[$pageSelector] ?? 1);
|
2020-01-14 15:34:57 +04:00
|
|
|
|
2020-01-26 22:34:28 -06:00
|
|
|
$this->groups[$group]['currentPage'] = $page < 1 ? 1 : $page;
|
2017-03-10 17:50:25 +08:00
|
|
|
}
|
2016-07-27 00:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
}
|