Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
7.0 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
12 years ago
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\data;
12 years ago
use Yii;
use yii\base\Object;
use yii\web\Request;
13 years ago
/**
* Pagination represents information relevant to pagination of data items.
13 years ago
*
* When data needs to be rendered in multiple pages, Pagination can be used to
11 years ago
* represent information such as [[totalCount|total item count]], [[pageSize|page size]],
* [[page|current page]], etc. These information can be passed to [[\yii\widgets\LinkPager|pagers]]
* to render pagination buttons or links.
13 years ago
*
* The following example shows how to create a pagination object and feed it
* to a pager.
13 years ago
*
* Controller action:
12 years ago
*
* ~~~
* function actionIndex()
* {
* $query = Article::find()->where(['status' => 1]);
* $countQuery = clone $query;
* $pages = new Pagination(['totalCount' => $countQuery->count()]);
* $models = $query->offset($pages->offset)
* ->limit($pages->limit)
* ->all();
13 years ago
*
* return $this->render('index', [
* 'models' => $models,
12 years ago
* 'pages' => $pages,
* ]);
13 years ago
* }
12 years ago
* ~~~
13 years ago
*
* View:
*
* ~~~
12 years ago
* foreach ($models as $model) {
12 years ago
* // display $model here
* }
13 years ago
*
* // display pagination
* echo LinkPager::widget([
* 'pagination' => $pages,
* ]);
* ~~~
13 years ago
*
* @property integer $limit The limit of the data. This may be used to set the LIMIT value for a SQL statement
* for fetching the current page of data. Note that if the page size is infinite, a value -1 will be returned.
* This property is read-only.
* @property integer $offset The offset of the data. This may be used to set the OFFSET value for a SQL
* statement for fetching the current page of data. This property is read-only.
* @property integer $page The zero-based current page number.
* @property integer $pageCount Number of pages. This property is read-only.
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
12 years ago
* @since 2.0
13 years ago
*/
class Pagination extends Object
13 years ago
{
/**
* @var string name of the parameter storing the current page index. Defaults to 'page'.
* @see params
13 years ago
*/
public $pageParam = 'page';
13 years ago
/**
* @var boolean whether to always have the page parameter in the URL created by [[createUrl()]].
* If false and [[page]] is 0, the page parameter will not be put in the URL.
13 years ago
*/
public $forcePageParam = true;
13 years ago
/**
12 years ago
* @var string the route of the controller action for displaying the paged contents.
* If not set, it means using the currently requested route.
13 years ago
*/
public $route;
13 years ago
/**
12 years ago
* @var array parameters (name => value) that should be used to obtain the current page number
11 years ago
* and to create new pagination URLs. If not set, all parameters from $_GET will be used instead.
*
* In order to add hash to all links use `array_merge($_GET, ['#' => 'my-hash'])`.
12 years ago
*
* The array element indexed by [[pageParam]] is considered to be the current page number.
* If the element does not exist, the current page number is considered 0.
13 years ago
*/
public $params;
13 years ago
/**
* @var \yii\web\UrlManager the URL manager used for creating pagination URLs. If not set,
* the "urlManager" application component will be used.
*/
public $urlManager;
/**
* @var boolean whether to check if [[page]] is within valid range.
* When this property is true, the value of [[page]] will always be between 0 and ([[pageCount]]-1).
11 years ago
* Because [[pageCount]] relies on the correct value of [[totalCount]] which may not be available
* in some cases (e.g. MongoDB), you may want to set this property to be false to disable the page
* number validation. By doing so, [[page]] will return the value indexed by [[pageParam]] in [[params]].
13 years ago
*/
public $validatePage = true;
13 years ago
/**
* @var integer number of items on each page. Defaults to 20.
* If it is less than 1, it means the page size is infinite, and thus a single page contains all items.
13 years ago
*/
public $pageSize = 20;
13 years ago
/**
* @var integer total number of items.
13 years ago
*/
11 years ago
public $totalCount = 0;
13 years ago
/**
* @return integer number of pages
*/
public function getPageCount()
{
if ($this->pageSize < 1) {
11 years ago
return $this->totalCount > 0 ? 1 : 0;
} else {
11 years ago
$totalCount = $this->totalCount < 0 ? 0 : (int)$this->totalCount;
return (int)(($totalCount + $this->pageSize - 1) / $this->pageSize);
}
13 years ago
}
private $_page;
13 years ago
/**
* Returns the zero-based current page number.
13 years ago
* @param boolean $recalculate whether to recalculate the current page based on the page size and item count.
* @return integer the zero-based current page number.
13 years ago
*/
public function getPage($recalculate = false)
13 years ago
{
if ($this->_page === null || $recalculate) {
if (($params = $this->params) === null) {
$request = Yii::$app->getRequest();
$params = $request instanceof Request ? $request->getQueryParams() : [];
}
if (isset($params[$this->pageParam]) && is_scalar($params[$this->pageParam])) {
$this->_page = (int)$params[$this->pageParam] - 1;
if ($this->validatePage) {
12 years ago
$pageCount = $this->getPageCount();
if ($this->_page >= $pageCount) {
$this->_page = $pageCount - 1;
12 years ago
}
13 years ago
}
if ($this->_page < 0) {
$this->_page = 0;
12 years ago
}
} else {
$this->_page = 0;
13 years ago
}
}
return $this->_page;
13 years ago
}
/**
* Sets the current page number.
13 years ago
* @param integer $value the zero-based index of the current page.
*/
public function setPage($value)
13 years ago
{
$this->_page = $value;
13 years ago
}
/**
* Creates the URL suitable for pagination with the specified page number.
* This method is mainly called by pagers when creating URLs used to perform pagination.
* @param integer $page the zero-based page number that the URL should point to.
* @param boolean $absolute whether to create an absolute URL. Defaults to `false`.
13 years ago
* @return string the created URL
* @see params
* @see forcePageParam
13 years ago
*/
public function createUrl($page, $absolute = false)
13 years ago
{
if (($params = $this->params) === null) {
$request = Yii::$app->getRequest();
$params = $request instanceof Request ? $request->getQueryParams() : [];
}
if ($page > 0 || $page >= 0 && $this->forcePageParam) {
$params[$this->pageParam] = $page + 1;
12 years ago
} else {
unset($params[$this->pageParam]);
12 years ago
}
$route = $this->route === null ? Yii::$app->controller->getRoute() : $this->route;
$urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager;
if ($absolute) {
return $urlManager->createAbsoluteUrl($route, $params);
} else {
return $urlManager->createUrl($route, $params);
}
13 years ago
}
/**
* @return integer the offset of the data. This may be used to set the
* OFFSET value for a SQL statement for fetching the current page of data.
*/
public function getOffset()
{
return $this->pageSize < 1 ? 0 : $this->getPage() * $this->pageSize;
13 years ago
}
/**
* @return integer the limit of the data. This may be used to set the
* LIMIT value for a SQL statement for fetching the current page of data.
* Note that if the page size is infinite, a value -1 will be returned.
13 years ago
*/
public function getLimit()
{
return $this->pageSize < 1 ? -1 : $this->pageSize;
13 years ago
}
}