Pagination.php 7.06 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
<?php
/**
 * CPagination class file.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

/**
 * CPagination represents information relevant to pagination.
 *
 * When data needs to be rendered in multiple pages, we can use CPagination to
 * represent information such as {@link getItemCount total item count},
 * {@link getPageSize page size}, {@link getCurrentPage current page}, etc.
 * These information can be passed to {@link CBasePager pagers} to render
 * pagination buttons or links.
 *
 * Example:
 *
 * Controller action:
 * <pre>
 * function actionIndex(){
 *     $criteria=new CDbCriteria();
 *     $count=Article::model()->count($criteria);
 *     $pages=new CPagination($count);
 *
 *     // results per page
 *     $pages->pageSize=10;
 *     $pages->applyLimit($criteria);
 *     $models=Article::model()->findAll($criteria);
 *
 *     $this->render('index', array(
 *     'models' => $models,
 *          'pages' => $pages
 *     ));
 * }
 * </pre>
 *
 * View:
 * <pre>
 * <?php foreach($models as $model): ?>
 *     // display a model
 * <?php endforeach; ?>
 *
 * // display pagination
 * <?php $this->widget('CLinkPager', array(
 *     'pages' => $pages,
 * )) ?>
 * </pre>
 *
 * @property integer $pageSize Number of items in each page. Defaults to 10.
 * @property integer $itemCount Total number of items. Defaults to 0.
 * @property integer $pageCount Number of pages.
 * @property integer $currentPage The zero-based index of the current page. Defaults to 0.
 * @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.
 * This returns the same value as {@link pageSize}.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @version $Id$
 * @package system.web
 * @since 1.0
 */
class CPagination extends CComponent
{
	/**
	 * The default page size.
	 */
Qiang Xue committed
73
	const DEFAULT_PAGE_SIZE = 10;
Qiang Xue committed
74 75 76
	/**
	 * @var string name of the GET variable storing the current page index. Defaults to 'page'.
	 */
Qiang Xue committed
77
	public $pageVar = 'page';
Qiang Xue committed
78 79 80 81
	/**
	 * @var string the route (controller ID and action ID) for displaying the paged contents.
	 * Defaults to empty string, meaning using the current route.
	 */
Qiang Xue committed
82
	public $route = '';
Qiang Xue committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	/**
	 * @var array of parameters (name=>value) that should be used instead of GET when generating pagination URLs.
	 * Defaults to null, meaning using the currently available GET parameters.
	 */
	public $params;
	/**
	 * @var boolean whether to ensure {@link currentPage} is returning a valid page number.
	 * When this property is true, the value returned by {@link currentPage} will always be between
	 * 0 and ({@link pageCount}-1). Because {@link pageCount} relies on the correct value of {@link itemCount},
	 * it means you must have knowledge about the total number of data items when you want to access {@link currentPage}.
	 * This is fine for SQL-based queries, but may not be feasible for other kinds of queries (e.g. MongoDB).
	 * In those cases, you may set this property to be false to skip the validation (you may need to validate yourself then).
	 * Defaults to true.
	 * @since 1.1.4
	 */
Qiang Xue committed
98
	public $validateCurrentPage = true;
Qiang Xue committed
99

Qiang Xue committed
100 101
	private $_pageSize = self::DEFAULT_PAGE_SIZE;
	private $_itemCount = 0;
Qiang Xue committed
102 103 104 105 106 107
	private $_currentPage;

	/**
	 * Constructor.
	 * @param integer $itemCount total number of items.
	 */
Qiang Xue committed
108
	public function __construct($itemCount = 0)
Qiang Xue committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	{
		$this->setItemCount($itemCount);
	}

	/**
	 * @return integer number of items in each page. Defaults to 10.
	 */
	public function getPageSize()
	{
		return $this->_pageSize;
	}

	/**
	 * @param integer $value number of items in each page
	 */
	public function setPageSize($value)
	{
Qiang Xue committed
126 127 128
		if (($this->_pageSize = $value) <= 0) {
			$this->_pageSize = self::DEFAULT_PAGE_SIZE;
		}
Qiang Xue committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	}

	/**
	 * @return integer total number of items. Defaults to 0.
	 */
	public function getItemCount()
	{
		return $this->_itemCount;
	}

	/**
	 * @param integer $value total number of items.
	 */
	public function setItemCount($value)
	{
Qiang Xue committed
144 145 146
		if (($this->_itemCount = $value) < 0) {
			$this->_itemCount = 0;
		}
Qiang Xue committed
147 148 149 150 151 152 153
	}

	/**
	 * @return integer number of pages
	 */
	public function getPageCount()
	{
Qiang Xue committed
154
		return (int)(($this->_itemCount + $this->_pageSize - 1) / $this->_pageSize);
Qiang Xue committed
155 156 157 158 159 160
	}

	/**
	 * @param boolean $recalculate whether to recalculate the current page based on the page size and item count.
	 * @return integer the zero-based index of the current page. Defaults to 0.
	 */
Qiang Xue committed
161
	public function getCurrentPage($recalculate = true)
Qiang Xue committed
162
	{
Qiang Xue committed
163 164 165 166 167 168 169 170
		if ($this->_currentPage === null || $recalculate) {
			if (isset($_GET[$this->pageVar])) {
				$this->_currentPage = (int)$_GET[$this->pageVar] - 1;
				if ($this->validateCurrentPage) {
					$pageCount = $this->getPageCount();
					if ($this->_currentPage >= $pageCount) {
						$this->_currentPage = $pageCount - 1;
					}
Qiang Xue committed
171
				}
Qiang Xue committed
172 173 174 175 176
				if ($this->_currentPage < 0) {
					$this->_currentPage = 0;
				}
			} else {
				$this->_currentPage = 0;
Qiang Xue committed
177 178 179 180 181 182 183 184 185 186
			}
		}
		return $this->_currentPage;
	}

	/**
	 * @param integer $value the zero-based index of the current page.
	 */
	public function setCurrentPage($value)
	{
Qiang Xue committed
187 188
		$this->_currentPage = $value;
		$_GET[$this->pageVar] = $value + 1;
Qiang Xue committed
189 190 191 192 193 194 195 196 197 198 199 200 201
	}

	/**
	 * Creates the URL suitable for pagination.
	 * This method is mainly called by pagers when creating URLs used to
	 * perform pagination. The default implementation is to call
	 * the controller's createUrl method with the page information.
	 * You may override this method if your URL scheme is not the same as
	 * the one supported by the controller's createUrl method.
	 * @param CController $controller the controller that will create the actual URL
	 * @param integer $page the page that the URL should point to. This is a zero-based index.
	 * @return string the created URL
	 */
Qiang Xue committed
202
	public function createPageUrl($controller, $page)
Qiang Xue committed
203
	{
Qiang Xue committed
204 205 206 207 208
		$params = $this->params === null ? $_GET : $this->params;
		if ($page > 0) // page 0 is the default
		{
			$params[$this->pageVar] = $page + 1;
		} else {
Qiang Xue committed
209
			unset($params[$this->pageVar]);
Qiang Xue committed
210 211
		}
		return $controller->createUrl($this->route, $params);
Qiang Xue committed
212 213 214 215 216 217 218 219
	}

	/**
	 * Applies LIMIT and OFFSET to the specified query criteria.
	 * @param CDbCriteria $criteria the query criteria that should be applied with the limit
	 */
	public function applyLimit($criteria)
	{
Qiang Xue committed
220 221
		$criteria->limit = $this->getLimit();
		$criteria->offset = $this->getOffset();
Qiang Xue committed
222 223 224 225 226 227 228 229 230
	}

	/**
	 * @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.
	 * @since 1.1.0
	 */
	public function getOffset()
	{
Qiang Xue committed
231
		return $this->getCurrentPage() * $this->getPageSize();
Qiang Xue committed
232 233 234 235 236 237 238 239 240 241 242 243 244
	}

	/**
	 * @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.
	 * This returns the same value as {@link pageSize}.
	 * @since 1.1.0
	 */
	public function getLimit()
	{
		return $this->getPageSize();
	}
}