3
* @link https://www.yiiframework.com/
4
* @copyright Copyright (c) 2008 Yii Software LLC
5
* @license https://www.yiiframework.com/license/
11
use yii\base\BaseObject;
17
* Pagination represents information relevant to pagination of data items.
19
* When data needs to be rendered in multiple pages, Pagination can be used to
20
* represent information such as [[totalCount|total item count]], [[pageSize|page size]],
21
* [[page|current page]], etc. These information can be passed to [[\yii\widgets\LinkPager|pagers]]
22
* to render pagination buttons or links.
24
* The following example shows how to create a pagination object and feed it
30
* public function actionIndex()
32
* $query = Article::find()->where(['status' => 1]);
33
* $countQuery = clone $query;
34
* $pages = new Pagination(['totalCount' => $countQuery->count()]);
35
* $models = $query->offset($pages->offset)
36
* ->limit($pages->limit)
39
* return $this->render('index', [
40
* 'models' => $models,
49
* foreach ($models as $model) {
50
* // display $model here
53
* // display pagination
54
* echo LinkPager::widget([
55
* 'pagination' => $pages,
59
* For more details and usage information on Pagination, see the [guide article on pagination](guide:output-pagination).
61
* @property-read int $limit The limit of the data. This may be used to set the LIMIT value for a SQL
62
* statement for fetching the current page of data. Note that if the page size is infinite, a value -1 will be
64
* @property-read array $links The links for navigational purpose. The array keys specify the purpose of the
65
* links (e.g. [[LINK_FIRST]]), and the array values are the corresponding URLs.
66
* @property-read int $offset The offset of the data. This may be used to set the OFFSET value for a SQL
67
* statement for fetching the current page of data.
68
* @property int $page The zero-based current page number.
69
* @property-read int $pageCount Number of pages.
70
* @property int $pageSize The number of items per page. If it is less than 1, it means the page size is
71
* infinite, and thus a single page contains all items.
73
* @author Qiang Xue <qiang.xue@gmail.com>
76
class Pagination extends BaseObject implements Linkable
78
const LINK_NEXT = 'next';
79
const LINK_PREV = 'prev';
80
const LINK_FIRST = 'first';
81
const LINK_LAST = 'last';
84
* @var string name of the parameter storing the current page index.
87
public $pageParam = 'page';
89
* @var string name of the parameter storing the page size.
92
public $pageSizeParam = 'per-page';
94
* @var bool whether to always have the page parameter in the URL created by [[createUrl()]].
95
* If false and [[page]] is 0, the page parameter will not be put in the URL.
97
public $forcePageParam = true;
99
* @var string|null the route of the controller action for displaying the paged contents.
100
* If not set, it means using the currently requested route.
104
* @var array|null parameters (name => value) that should be used to obtain the current page number
105
* and to create new pagination URLs. If not set, all parameters from $_GET will be used instead.
107
* In order to add hash to all links use `array_merge($_GET, ['#' => 'my-hash'])`.
109
* The array element indexed by [[pageParam]] is considered to be the current page number (defaults to 0);
110
* while the element indexed by [[pageSizeParam]] is treated as the page size (defaults to [[defaultPageSize]]).
114
* @var \yii\web\UrlManager|null the URL manager used for creating pagination URLs. If not set,
115
* the "urlManager" application component will be used.
119
* @var bool whether to check if [[page]] is within valid range.
120
* When this property is true, the value of [[page]] will always be between 0 and ([[pageCount]]-1).
121
* Because [[pageCount]] relies on the correct value of [[totalCount]] which may not be available
122
* in some cases (e.g. MongoDB), you may want to set this property to be false to disable the page
123
* number validation. By doing so, [[page]] will return the value indexed by [[pageParam]] in [[params]].
125
public $validatePage = true;
127
* @var int total number of items.
129
public $totalCount = 0;
131
* @var int the default page size. This property will be returned by [[pageSize]] when page size
132
* cannot be determined by [[pageSizeParam]] from [[params]].
134
public $defaultPageSize = 20;
136
* @var array|false the page size limits. The first array element defines the minimum page size, and the second
137
* the maximum page size. If this is false, it means [[pageSize]] should always return the value of [[defaultPageSize]].
139
public $pageSizeLimit = [1, 50];
142
* @var int number of items on each page.
143
* If it is less than 1, it means the page size is infinite, and thus a single page contains all items.
149
* @return int number of pages
151
public function getPageCount()
153
$pageSize = $this->getPageSize();
155
return $this->totalCount > 0 ? 1 : 0;
158
$totalCount = $this->totalCount < 0 ? 0 : (int) $this->totalCount;
160
return (int) (($totalCount + $pageSize - 1) / $pageSize);
166
* Returns the zero-based current page number.
167
* @param bool $recalculate whether to recalculate the current page based on the page size and item count.
168
* @return int the zero-based current page number.
170
public function getPage($recalculate = false)
172
if ($this->_page === null || $recalculate) {
173
$page = (int) $this->getQueryParam($this->pageParam, 1) - 1;
174
$this->setPage($page, true);
181
* Sets the current page number.
182
* @param int $value the zero-based index of the current page.
183
* @param bool $validatePage whether to validate the page number. Note that in order
184
* to validate the page number, both [[validatePage]] and this parameter must be true.
186
public function setPage($value, $validatePage = false)
188
if ($value === null) {
191
$value = (int) $value;
192
if ($validatePage && $this->validatePage) {
193
$pageCount = $this->getPageCount();
194
if ($value >= $pageCount) {
195
$value = $pageCount - 1;
201
$this->_page = $value;
206
* Returns the number of items per page.
207
* By default, this method will try to determine the page size by [[pageSizeParam]] in [[params]].
208
* If the page size cannot be determined this way, [[defaultPageSize]] will be returned.
209
* @return int the number of items per page. If it is less than 1, it means the page size is infinite,
210
* and thus a single page contains all items.
213
public function getPageSize()
215
if ($this->_pageSize === null) {
216
if (empty($this->pageSizeLimit) || !isset($this->pageSizeLimit[0], $this->pageSizeLimit[1])) {
217
$pageSize = $this->defaultPageSize;
218
$this->setPageSize($pageSize);
220
$pageSize = (int) $this->getQueryParam($this->pageSizeParam, $this->defaultPageSize);
221
$this->setPageSize($pageSize, true);
225
return $this->_pageSize;
229
* @param int $value the number of items per page.
230
* @param bool $validatePageSize whether to validate page size.
232
public function setPageSize($value, $validatePageSize = false)
234
if ($value === null) {
235
$this->_pageSize = null;
237
$value = (int) $value;
238
if ($validatePageSize && isset($this->pageSizeLimit[0], $this->pageSizeLimit[1])) {
239
if ($value < $this->pageSizeLimit[0]) {
240
$value = $this->pageSizeLimit[0];
241
} elseif ($value > $this->pageSizeLimit[1]) {
242
$value = $this->pageSizeLimit[1];
245
$this->_pageSize = $value;
250
* Creates the URL suitable for pagination with the specified page number.
251
* This method is mainly called by pagers when creating URLs used to perform pagination.
252
* @param int $page the zero-based page number that the URL should point to.
253
* @param int|null $pageSize the number of items on each page. If not set, the value of [[pageSize]] will be used.
254
* @param bool $absolute whether to create an absolute URL. Defaults to `false`.
255
* @return string the created URL
257
* @see forcePageParam
259
public function createUrl($page, $pageSize = null, $absolute = false)
262
$pageSize = (int) $pageSize;
263
if (($params = $this->params) === null) {
264
$request = Yii::$app->getRequest();
265
$params = $request instanceof Request ? $request->getQueryParams() : [];
267
if ($page > 0 || $page == 0 && $this->forcePageParam) {
268
$params[$this->pageParam] = $page + 1;
270
unset($params[$this->pageParam]);
272
if ($pageSize <= 0) {
273
$pageSize = $this->getPageSize();
275
if ($pageSize != $this->defaultPageSize) {
276
$params[$this->pageSizeParam] = $pageSize;
278
unset($params[$this->pageSizeParam]);
280
$params[0] = $this->route === null ? Yii::$app->controller->getRoute() : $this->route;
281
$urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager;
283
return $urlManager->createAbsoluteUrl($params);
286
return $urlManager->createUrl($params);
290
* @return int the offset of the data. This may be used to set the
291
* OFFSET value for a SQL statement for fetching the current page of data.
293
public function getOffset()
295
$pageSize = $this->getPageSize();
297
return $pageSize < 1 ? 0 : $this->getPage() * $pageSize;
301
* @return int the limit of the data. This may be used to set the
302
* LIMIT value for a SQL statement for fetching the current page of data.
303
* Note that if the page size is infinite, a value -1 will be returned.
305
public function getLimit()
307
$pageSize = $this->getPageSize();
309
return $pageSize < 1 ? -1 : $pageSize;
313
* Returns a whole set of links for navigating to the first, last, next and previous pages.
314
* @param bool $absolute whether the generated URLs should be absolute.
315
* @return array the links for navigational purpose. The array keys specify the purpose of the links (e.g. [[LINK_FIRST]]),
316
* and the array values are the corresponding URLs.
318
public function getLinks($absolute = false)
320
$currentPage = $this->getPage();
321
$pageCount = $this->getPageCount();
323
$links = [Link::REL_SELF => $this->createUrl($currentPage, null, $absolute)];
324
if ($pageCount > 0) {
325
$links[self::LINK_FIRST] = $this->createUrl(0, null, $absolute);
326
$links[self::LINK_LAST] = $this->createUrl($pageCount - 1, null, $absolute);
327
if ($currentPage > 0) {
328
$links[self::LINK_PREV] = $this->createUrl($currentPage - 1, null, $absolute);
330
if ($currentPage < $pageCount - 1) {
331
$links[self::LINK_NEXT] = $this->createUrl($currentPage + 1, null, $absolute);
339
* Returns the value of the specified query parameter.
340
* This method returns the named parameter value from [[params]]. Null is returned if the value does not exist.
341
* @param string $name the parameter name
342
* @param string|null $defaultValue the value to be returned when the specified parameter does not exist in [[params]].
343
* @return string|null the parameter value
345
protected function getQueryParam($name, $defaultValue = null)
347
if (($params = $this->params) === null) {
348
$request = Yii::$app->getRequest();
349
$params = $request instanceof Request ? $request->getQueryParams() : [];
352
return isset($params[$name]) && is_scalar($params[$name]) ? $params[$name] : $defaultValue;