527 lines
12 KiB
PHP
Raw Normal View History

2016-07-27 00:33:56 -05:00
<?php namespace CodeIgniter\Pager;
2016-07-27 23:23:33 -05:00
/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
2018-12-26 21:49:51 -08:00
* Copyright (c) 2014-2019 British Columbia Institute of Technology
2016-07-27 23:23:33 -05:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
2018-11-10 02:57:39 -02:00
* @package CodeIgniter
* @author CodeIgniter Dev Team
2018-12-26 21:49:51 -08:00
* @copyright 2014-2019 British Columbia Institute of Technology (https://bcit.ca/)
2018-11-10 02:57:39 -02:00
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 3.0.0
2016-07-27 23:23:33 -05:00
* @filesource
*/
2018-11-10 02:57:39 -02:00
use CodeIgniter\Pager\Exceptions\PagerException;
2016-07-27 00:33:56 -05:00
use Config\Services;
2016-08-31 16:38:08 -07:00
use CodeIgniter\View\RendererInterface;
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.
*
* @package CodeIgniter\Pager
*/
2016-07-27 00:33:56 -05:00
class Pager implements PagerInterface
{
2016-07-27 00:33:56 -05:00
/**
* 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.
*
* @var \Config\Pager
*/
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;
/**
* List of only permitted queries
*
* @var array
*/
protected $only = [];
2016-07-27 00:33:56 -05:00
//--------------------------------------------------------------------
2016-08-31 16:38:08 -07:00
public function __construct($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
*
* @param string $group
* @param string $template The output template alias to render.
2016-07-27 00:33:56 -05:00
*
* @return string
*/
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
* @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.
*
2018-11-10 02:57:39 -02:00
* @param integer $page
* @param integer $perPage
* @param integer $total
* @param string $template The output template alias to render.
* @param integer $segment (if page number is provided by URI segment)
2016-07-27 00:33:56 -05:00
*
* @return string
*/
2018-09-11 19:10:37 +02:00
public function makeLinks(int $page, int $perPage, int $total, string $template = 'default_full', int $segment = 0): string
2016-07-27 00:33:56 -05:00
{
$name = time();
2018-09-11 19:10:37 +02:00
$this->store($name, $page, $perPage, $total, $segment);
2016-07-27 00:33:56 -05:00
2016-07-27 23:23:33 -05:00
return $this->displayLinks($name, $template);
}
//--------------------------------------------------------------------
/**
* Does the actual work of displaying the view file. Used internally
* by links(), simpleLinks(), and makeLinks().
*
* @param string $group
* @param string $template
*
* @return string
*/
protected function displayLinks(string $group, string $template)
{
$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
{
throw PagerException::forInvalidTemplate($template);
2016-07-27 00:33:56 -05:00
}
return $this->view->setVar('pager', $pager)
->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.
*
2018-11-10 02:57:39 -02:00
* @param string $group
* @param integer $page
* @param integer $perPage
* @param integer $total
* @param integer $segment
2016-07-27 00:33:56 -05:00
*
* @return mixed
*/
2018-09-11 19:10:37 +02:00
public function store(string $group, int $page, int $perPage, int $total, int $segment = 0)
2016-07-27 00:33:56 -05:00
{
$this->segment[$group] = $segment;
2016-07-27 00:33:56 -05:00
$this->ensureGroup($group);
$this->groups[$group]['currentPage'] = $page;
2018-11-10 02:57:39 -02:00
$this->groups[$group]['perPage'] = $perPage;
$this->groups[$group]['total'] = $total;
2018-11-10 04:29:05 -02:00
$this->groups[$group]['pageCount'] = (int)ceil($total / $perPage);
2016-07-27 00:33:56 -05:00
return $this;
}
//--------------------------------------------------------------------
/**
* Sets the path that an aliased group of links will use.
*
* @param string $group
* @param string $path
*
* @return mixed
*/
public function setPath(string $path, string $group = 'default')
{
$this->ensureGroup($group);
$this->groups[$group]['uri']->setPath($path);
return $this;
}
//--------------------------------------------------------------------
/**
* Returns the total number of pages.
*
* @param string|null $group
*
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.
*
* @param string|null $group
*
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);
return $this->groups[$group]['currentPage'];
}
//--------------------------------------------------------------------
/**
* Tells whether this group of results has any more pages of results.
*
* @param string|null $group
*
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);
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;
}
2018-11-10 04:29:05 -02: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
*/
public function getFirstPage(string $group = 'default')
{
$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
*
* @return string|\CodeIgniter\HTTP\URI
2016-07-27 00:33:56 -05:00
*/
public function getPageURI(int $page = null, string $group = 'default', $returnObject = false)
{
$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
{
$uri->addQuery('page', $page);
}
if ($this->only)
{
2018-09-11 19:15:00 +02:00
$query = array_intersect_key($_GET, array_flip($this->only));
2018-11-17 12:53:02 -02:00
if (! $segment)
2018-09-11 19:10:37 +02:00
{
$query['page'] = $page;
}
$uri->setQueryArray($query);
}
2016-07-27 00:33:56 -05: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
*/
public function getNextPageURI(string $group = 'default', $returnObject = false)
{
$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)
{
$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
*/
public function getPreviousPageURI(string $group = 'default', $returnObject = false)
{
$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)
{
$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);
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
{
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;
}
//--------------------------------------------------------------------
/**
* 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.
*
* @param string $group
*/
protected function ensureGroup(string $group)
{
if (array_key_exists($group, $this->groups))
{
return;
}
$this->groups[$group] = [
2018-11-10 02:57:39 -02:00
'uri' => clone Services::request()->uri,
'hasMore' => false,
'total' => null,
'perPage' => $this->config->perPage,
'pageCount' => 1,
2016-07-27 00:33:56 -05:00
];
2018-11-10 02:57:39 -02:00
if (array_key_exists($group, $this->segment))
2018-09-11 19:10:37 +02:00
{
try
{
$this->groups[$group]['currentPage'] = $this->groups[$group]['uri']->getSegment($this->segment[$group]);
}
catch (\CodeIgniter\HTTP\Exceptions\HTTPException $e)
{
$this->groups[$group]['currentPage'] = 1;
}
2018-09-11 19:10:37 +02:00
}
else
{
$this->groups[$group]['currentPage'] = $_GET['page_' . $group] ?? $_GET['page'] ?? 1;
}
if ($_GET)
{
$this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET);
}
2016-07-27 00:33:56 -05:00
}
//--------------------------------------------------------------------
}