Pagination.php 6.3 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/
 */

Qiang Xue committed
8 9
namespace yii\web;

Qiang Xue committed
10 11
use Yii;

Qiang Xue committed
12
/**
Qiang Xue committed
13
 * Pagination represents information relevant to pagination of data items.
Qiang Xue committed
14
 *
Qiang Xue committed
15 16
 * When data needs to be rendered in multiple pages, Pagination can be used to
 * represent information such as [[itemCount|total item count]], [[pageSize|page size]],
Qiang Xue committed
17
 * [[page|current page]], etc. These information can be passed to [[yii\widgets\Pager|pagers]]
Qiang Xue committed
18
 * to render pagination buttons or links.
Qiang Xue committed
19
 *
Qiang Xue committed
20 21
 * The following example shows how to create a pagination object and feed it
 * to a pager.
Qiang Xue committed
22 23
 *
 * Controller action:
Qiang Xue committed
24 25 26 27
 *
 * ~~~
 * function actionIndex()
 * {
Qiang Xue committed
28 29 30 31 32 33
 *     $query = Article::find()->where(array('status' => 1));
 *     $countQuery = clone $query;
 *     $pages = new Pagination($countQuery->count());
 *     $models = $query->offset($pages->offset)
 *         ->limit($pages->limit)
 *         ->all();
Qiang Xue committed
34 35
 *
 *     $this->render('index', array(
Qiang Xue committed
36
 *          'models' => $models,
Qiang Xue committed
37
 *          'pages' => $pages,
Qiang Xue committed
38 39
 *     ));
 * }
Qiang Xue committed
40
 * ~~~
Qiang Xue committed
41 42
 *
 * View:
Qiang Xue committed
43 44
 *
 * ~~~
resurtm committed
45
 * foreach ($models as $model) {
Qiang Xue committed
46 47
 *     // display $model here
 * }
Qiang Xue committed
48 49
 *
 * // display pagination
50
 * LinkPager::widget(array(
gsd committed
51
 *     'pagination' => $pages,
Qiang Xue committed
52
 * ));
Qiang Xue committed
53
 * ~~~
Qiang Xue committed
54 55
 *
 * @property integer $pageCount Number of pages.
Qiang Xue committed
56
 * @property integer $page The zero-based index of the current page.
Qiang Xue committed
57 58 59 60 61 62
 * @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.
 * @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.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
63
 * @since 2.0
Qiang Xue committed
64
 */
Qiang Xue committed
65
class Pagination extends \yii\base\Object
Qiang Xue committed
66 67
{
	/**
Qiang Xue committed
68 69
	 * @var string name of the parameter storing the current page index. Defaults to 'page'.
	 * @see params
Qiang Xue committed
70
	 */
Qiang Xue committed
71
	public $pageVar = 'page';
Qiang Xue committed
72
	/**
Qiang Xue committed
73 74
	 * @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
75
	 */
76
	public $forcePageVar = true;
Qiang Xue committed
77
	/**
Qiang Xue committed
78
	 * @var string the route of the controller action for displaying the paged contents.
Qiang Xue committed
79
	 * If not set, it means using the currently requested route.
Qiang Xue committed
80
	 */
Qiang Xue committed
81
	public $route;
Qiang Xue committed
82
	/**
resurtm committed
83
	 * @var array parameters (name => value) that should be used to obtain the current page number
Qiang Xue committed
84
	 * and to create new pagination URLs. If not set, $_GET will be used instead.
Qiang Xue committed
85
	 *
Qiang Xue committed
86 87
	 * The array element indexed by [[pageVar]] is considered to be the current page number.
	 * If the element does not exist, the current page number is considered 0.
Qiang Xue committed
88
	 */
Qiang Xue committed
89
	public $params;
Qiang Xue committed
90
	/**
Qiang Xue committed
91 92 93 94 95
	 * @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).
	 * Because [[pageCount]] relies on the correct value of [[itemCount]] 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 [[pageVar]] in [[params]].
Qiang Xue committed
96
	 */
Qiang Xue committed
97
	public $validatePage = true;
Qiang Xue committed
98
	/**
Qiang Xue committed
99 100
	 * @var integer number of items on each page. Defaults to 10.
	 * If it is less than 1, it means the page size is infinite, and thus a single page contains all items.
Qiang Xue committed
101
	 */
Qiang Xue committed
102
	public $pageSize = 10;
Qiang Xue committed
103
	/**
Qiang Xue committed
104
	 * @var integer total number of items.
Qiang Xue committed
105
	 */
Qiang Xue committed
106
	public $itemCount;
Qiang Xue committed
107 108

	/**
Qiang Xue committed
109 110 111
	 * Constructor.
	 * @param integer $itemCount total number of items.
	 * @param array $config name-value pairs that will be used to initialize the object properties
Qiang Xue committed
112
	 */
Qiang Xue committed
113
	public function __construct($itemCount, $config = array())
Qiang Xue committed
114
	{
Qiang Xue committed
115 116
		$this->itemCount = $itemCount;
		parent::__construct($config);
Qiang Xue committed
117 118 119 120 121 122 123
	}

	/**
	 * @return integer number of pages
	 */
	public function getPageCount()
	{
Qiang Xue committed
124 125 126 127 128 129
		if ($this->pageSize < 1) {
			return $this->itemCount > 0 ? 1 : 0;
		} else {
			$itemCount = $this->itemCount < 0 ? 0 : (int)$this->itemCount;
			return (int)(($itemCount + $this->pageSize - 1) / $this->pageSize);
		}
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 142
		if ($this->_page === null || $recalculate) {
			$params = $this->params === null ? $_GET : $this->params;
Qiang Xue committed
143
			if (isset($params[$this->pageVar]) && is_scalar($params[$this->pageVar])) {
Qiang Xue committed
144 145
				$this->_page = (int)$params[$this->pageVar] - 1;
				if ($this->validatePage) {
Qiang Xue committed
146
					$pageCount = $this->getPageCount();
Qiang Xue committed
147 148
					if ($this->_page >= $pageCount) {
						$this->_page = $pageCount - 1;
Qiang Xue committed
149
					}
Qiang Xue committed
150
				}
Qiang Xue committed
151 152
				if ($this->_page < 0) {
					$this->_page = 0;
Qiang Xue committed
153 154
				}
			} else {
Qiang Xue committed
155
				$this->_page = 0;
Qiang Xue committed
156 157
			}
		}
Qiang Xue committed
158
		return $this->_page;
Qiang Xue committed
159 160 161
	}

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

	/**
Qiang Xue committed
171 172 173
	 * 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.
Qiang Xue committed
174
	 * @return string the created URL
Qiang Xue committed
175 176
	 * @see params
	 * @see forcePageVar
Qiang Xue committed
177
	 */
Qiang Xue committed
178
	public function createUrl($page)
Qiang Xue committed
179
	{
Qiang Xue committed
180
		$params = $this->params === null ? $_GET : $this->params;
Qiang Xue committed
181
		if ($page > 0 || $page >= 0 && $this->forcePageVar) {
Qiang Xue committed
182 183
			$params[$this->pageVar] = $page + 1;
		} else {
Qiang Xue committed
184
			unset($params[$this->pageVar]);
Qiang Xue committed
185
		}
Qiang Xue committed
186 187
		$route = $this->route === null ? Yii::$app->controller->route : $this->route;
		return Yii::$app->getUrlManager()->createUrl($route, $params);
Qiang Xue committed
188 189 190 191 192 193 194 195
	}

	/**
	 * @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
196
		return $this->pageSize < 1 ? 0 : $this->getPage() * $this->pageSize;
Qiang Xue committed
197 198 199 200 201
	}

	/**
	 * @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
202
	 * Note that if the page size is infinite, a value -1 will be returned.
Qiang Xue committed
203 204 205
	 */
	public function getLimit()
	{
Qiang Xue committed
206
		return $this->pageSize < 1 ? -1 : $this->pageSize;
Qiang Xue committed
207
	}
Zander Baldwin committed
208
}