Pagination.php 6.98 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\data;
Qiang Xue committed
9

Qiang Xue committed
10
use Yii;
11
use yii\base\Object;
12
use yii\web\Request;
Qiang Xue committed
13

Qiang Xue committed
14
/**
Qiang Xue committed
15
 * Pagination represents information relevant to pagination of data items.
Qiang Xue committed
16
 *
Qiang Xue committed
17
 * When data needs to be rendered in multiple pages, Pagination can be used to
Qiang Xue committed
18
 * represent information such as [[totalCount|total item count]], [[pageSize|page size]],
19
 * [[page|current page]], etc. These information can be passed to [[\yii\widgets\LinkPager|pagers]]
Qiang Xue committed
20
 * to render pagination buttons or links.
Qiang Xue committed
21
 *
Qiang Xue committed
22 23
 * The following example shows how to create a pagination object and feed it
 * to a pager.
Qiang Xue committed
24 25
 *
 * Controller action:
Qiang Xue committed
26 27 28 29
 *
 * ~~~
 * function actionIndex()
 * {
Alexander Makarov committed
30
 *     $query = Article::find()->where(['status' => 1]);
Qiang Xue committed
31
 *     $countQuery = clone $query;
Alexander Makarov committed
32
 *     $pages = new Pagination(['totalCount' => $countQuery->count()]);
Qiang Xue committed
33 34 35
 *     $models = $query->offset($pages->offset)
 *         ->limit($pages->limit)
 *         ->all();
Qiang Xue committed
36
 *
Alexander Makarov committed
37
 *     return $this->render('index', [
Qiang Xue committed
38
 *          'models' => $models,
Qiang Xue committed
39
 *          'pages' => $pages,
Alexander Makarov committed
40
 *     ]);
Qiang Xue committed
41
 * }
Qiang Xue committed
42
 * ~~~
Qiang Xue committed
43 44
 *
 * View:
Qiang Xue committed
45 46
 *
 * ~~~
resurtm committed
47
 * foreach ($models as $model) {
Qiang Xue committed
48 49
 *     // display $model here
 * }
Qiang Xue committed
50 51
 *
 * // display pagination
Alexander Makarov committed
52
 * echo LinkPager::widget([
gsd committed
53
 *     'pagination' => $pages,
Alexander Makarov committed
54
 * ]);
Qiang Xue committed
55
 * ~~~
Qiang Xue committed
56
 *
57 58 59 60 61
 * @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.
62
 * @property integer $page The zero-based current page number.
63
 * @property integer $pageCount Number of pages. This property is read-only.
Qiang Xue committed
64 65
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
66
 * @since 2.0
Qiang Xue committed
67
 */
68
class Pagination extends Object
Qiang Xue committed
69 70
{
	/**
Qiang Xue committed
71 72
	 * @var string name of the parameter storing the current page index. Defaults to 'page'.
	 * @see params
Qiang Xue committed
73
	 */
74
	public $pageParam = 'page';
Qiang Xue committed
75
	/**
Qiang Xue committed
76 77
	 * @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.
Qiang Xue committed
78
	 */
79
	public $forcePageParam = true;
Qiang Xue committed
80
	/**
Qiang Xue committed
81
	 * @var string the route of the controller action for displaying the paged contents.
Qiang Xue committed
82
	 * If not set, it means using the currently requested route.
Qiang Xue committed
83
	 */
Qiang Xue committed
84
	public $route;
Qiang Xue committed
85
	/**
resurtm committed
86
	 * @var array parameters (name => value) that should be used to obtain the current page number
egorpromo committed
87
	 * and to create new pagination URLs. If not set, all parameters from $_GET will be used instead.
88
	 *
89
	 * In order to add hash to all links use `array_merge($_GET, ['#' => 'my-hash'])`.
Qiang Xue committed
90
	 *
91
	 * The array element indexed by [[pageParam]] is considered to be the current page number.
Qiang Xue committed
92
	 * If the element does not exist, the current page number is considered 0.
Qiang Xue committed
93
	 */
Qiang Xue committed
94
	public $params;
95 96 97 98 99
	/**
	 * @var \yii\web\UrlManager the URL manager used for creating pagination URLs. If not set,
	 * the "urlManager" application component will be used.
	 */
	public $urlManager;
Qiang Xue committed
100
	/**
Qiang Xue committed
101 102
	 * @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).
Qiang Xue committed
103
	 * Because [[pageCount]] relies on the correct value of [[totalCount]] which may not be available
Qiang Xue committed
104
	 * in some cases (e.g. MongoDB), you may want to set this property to be false to disable the page
105
	 * number validation. By doing so, [[page]] will return the value indexed by [[pageParam]] in [[params]].
Qiang Xue committed
106
	 */
Qiang Xue committed
107
	public $validatePage = true;
Qiang Xue committed
108
	/**
109
	 * @var integer number of items on each page. Defaults to 20.
Qiang Xue committed
110
	 * If it is less than 1, it means the page size is infinite, and thus a single page contains all items.
Qiang Xue committed
111
	 */
112
	public $pageSize = 20;
Qiang Xue committed
113
	/**
Qiang Xue committed
114
	 * @var integer total number of items.
Qiang Xue committed
115
	 */
Qiang Xue committed
116
	public $totalCount = 0;
Qiang Xue committed
117 118 119 120 121 122 123


	/**
	 * @return integer number of pages
	 */
	public function getPageCount()
	{
Qiang Xue committed
124
		if ($this->pageSize < 1) {
Qiang Xue committed
125
			return $this->totalCount > 0 ? 1 : 0;
Qiang Xue committed
126
		} else {
Qiang Xue committed
127 128
			$totalCount = $this->totalCount < 0 ? 0 : (int)$this->totalCount;
			return (int)(($totalCount + $this->pageSize - 1) / $this->pageSize);
Qiang Xue committed
129
		}
Qiang Xue committed
130 131
	}

Qiang Xue committed
132 133
	private $_page;

Qiang Xue committed
134
	/**
Qiang Xue committed
135
	 * Returns the zero-based current page number.
Qiang Xue committed
136
	 * @param boolean $recalculate whether to recalculate the current page based on the page size and item count.
Qiang Xue committed
137
	 * @return integer the zero-based current page number.
Qiang Xue committed
138
	 */
Qiang Xue committed
139
	public function getPage($recalculate = false)
Qiang Xue committed
140
	{
Qiang Xue committed
141
		if ($this->_page === null || $recalculate) {
142
			if (($params = $this->params) === null) {
Qiang Xue committed
143
				$request = Yii::$app->getRequest();
144
				$params = $request instanceof Request ? $request->getQueryParams() : [];
145
			}
146 147
			if (isset($params[$this->pageParam]) && is_scalar($params[$this->pageParam])) {
				$this->_page = (int)$params[$this->pageParam] - 1;
Qiang Xue committed
148
				if ($this->validatePage) {
Qiang Xue committed
149
					$pageCount = $this->getPageCount();
Qiang Xue committed
150 151
					if ($this->_page >= $pageCount) {
						$this->_page = $pageCount - 1;
Qiang Xue committed
152
					}
Qiang Xue committed
153
				}
Qiang Xue committed
154 155
				if ($this->_page < 0) {
					$this->_page = 0;
Qiang Xue committed
156 157
				}
			} else {
Qiang Xue committed
158
				$this->_page = 0;
Qiang Xue committed
159 160
			}
		}
Qiang Xue committed
161
		return $this->_page;
Qiang Xue committed
162 163 164
	}

	/**
Qiang Xue committed
165
	 * Sets the current page number.
Qiang Xue committed
166 167
	 * @param integer $value the zero-based index of the current page.
	 */
Qiang Xue committed
168
	public function setPage($value)
Qiang Xue committed
169
	{
Qiang Xue committed
170
		$this->_page = $value;
Qiang Xue committed
171 172 173
	}

	/**
Qiang Xue committed
174 175 176
	 * 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.
177
	 * @param boolean $absolute whether to create an absolute URL. Defaults to `false`.
Qiang Xue committed
178
	 * @return string the created URL
Qiang Xue committed
179
	 * @see params
180
	 * @see forcePageParam
Qiang Xue committed
181
	 */
182
	public function createUrl($page, $absolute = false)
Qiang Xue committed
183
	{
184
		if (($params = $this->params) === null) {
Qiang Xue committed
185
			$request = Yii::$app->getRequest();
186
			$params = $request instanceof Request ? $request->getQueryParams() : [];
187
		}
188 189
		if ($page > 0 || $page >= 0 && $this->forcePageParam) {
			$params[$this->pageParam] = $page + 1;
Qiang Xue committed
190
		} else {
191
			unset($params[$this->pageParam]);
Qiang Xue committed
192
		}
193
		$params[0] = $this->route === null ? Yii::$app->controller->getRoute() : $this->route;
194 195
		$urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager;
		if ($absolute) {
196
			return $urlManager->createAbsoluteUrl($params);
197
		} else {
198
			return $urlManager->createUrl($params);
199
		}
Qiang Xue committed
200 201 202 203 204 205 206 207
	}

	/**
	 * @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()
	{
Qiang Xue committed
208
		return $this->pageSize < 1 ? 0 : $this->getPage() * $this->pageSize;
Qiang Xue committed
209 210 211 212 213
	}

	/**
	 * @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.
Qiang Xue committed
214
	 * Note that if the page size is infinite, a value -1 will be returned.
Qiang Xue committed
215 216 217
	 */
	public function getLimit()
	{
Qiang Xue committed
218
		return $this->pageSize < 1 ? -1 : $this->pageSize;
Qiang Xue committed
219
	}
Zander Baldwin committed
220
}