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.
		
		
		
		
			
				
					209 lines
				
				6.3 KiB
			
		
		
			
		
	
	
					209 lines
				
				6.3 KiB
			| 
											14 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											13 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											13 years ago
										 | namespace yii\web;
 | ||
|  | 
 | ||
| 
											13 years ago
										 | use Yii;
 | ||
|  | 
 | ||
| 
											14 years ago
										 | /**
 | ||
| 
											13 years ago
										 |  * Pagination represents information relevant to pagination of data items.
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											13 years ago
										 |  * 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]],
 | ||
| 
											13 years ago
										 |  * [[page|current page]], etc. These information can be passed to [[yii\widgets\Pager|pagers]]
 | ||
| 
											13 years ago
										 |  * to render pagination buttons or links.
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											13 years ago
										 |  * The following example shows how to create a pagination object and feed it
 | ||
|  |  * to a pager.
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * Controller action:
 | ||
| 
											13 years ago
										 |  *
 | ||
|  |  * ~~~
 | ||
|  |  * function actionIndex()
 | ||
|  |  * {
 | ||
| 
											13 years ago
										 |  *     $query = Article::find()->where(array('status' => 1));
 | ||
|  |  *     $countQuery = clone $query;
 | ||
|  |  *     $pages = new Pagination($countQuery->count());
 | ||
|  |  *     $models = $query->offset($pages->offset)
 | ||
|  |  *         ->limit($pages->limit)
 | ||
|  |  *         ->all();
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  *     $this->render('index', array(
 | ||
| 
											13 years ago
										 |  *          'models' => $models,
 | ||
| 
											13 years ago
										 |  *          'pages' => $pages,
 | ||
| 
											14 years ago
										 |  *     ));
 | ||
|  |  * }
 | ||
| 
											13 years ago
										 |  * ~~~
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * View:
 | ||
| 
											13 years ago
										 |  *
 | ||
|  |  * ~~~
 | ||
| 
											13 years ago
										 |  * foreach ($models as $model) {
 | ||
| 
											13 years ago
										 |  *     // display $model here
 | ||
|  |  * }
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * // display pagination
 | ||
| 
											13 years ago
										 |  * LinkPager::widget(array(
 | ||
| 
											13 years ago
										 |  *     'pagination' => $pages,
 | ||
| 
											13 years ago
										 |  * ));
 | ||
| 
											13 years ago
										 |  * ~~~
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * @property integer $pageCount Number of pages.
 | ||
| 
											13 years ago
										 |  * @property integer $page The zero-based index of the current page.
 | ||
| 
											14 years ago
										 |  * @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>
 | ||
| 
											13 years ago
										 |  * @since 2.0
 | ||
| 
											14 years ago
										 |  */
 | ||
| 
											13 years ago
										 | class Pagination extends \yii\base\Object
 | ||
| 
											14 years ago
										 | {
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var string name of the parameter storing the current page index. Defaults to 'page'.
 | ||
|  | 	 * @see params
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $pageVar = 'page';
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											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.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $forcePageVar = true;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var string the route of the controller action for displaying the paged contents.
 | ||
| 
											13 years ago
										 | 	 * If not set, it means using the currently requested route.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $route;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array parameters (name => value) that should be used to obtain the current page number
 | ||
| 
											13 years ago
										 | 	 * and to create new pagination URLs. If not set, $_GET will be used instead.
 | ||
| 
											13 years ago
										 | 	 *
 | ||
| 
											13 years ago
										 | 	 * 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.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $params;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @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]].
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $validatePage = true;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @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.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $pageSize = 10;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var integer total number of items.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $itemCount;
 | ||
| 
											14 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Constructor.
 | ||
|  | 	 * @param integer $itemCount total number of items.
 | ||
|  | 	 * @param array $config name-value pairs that will be used to initialize the object properties
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function __construct($itemCount, $config = array())
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$this->itemCount = $itemCount;
 | ||
|  | 		parent::__construct($config);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return integer number of pages
 | ||
|  | 	 */
 | ||
|  | 	public function getPageCount()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		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);
 | ||
|  | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	private $_page;
 | ||
|  | 
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * Returns the zero-based current page number.
 | ||
| 
											14 years ago
										 | 	 * @param boolean $recalculate whether to recalculate the current page based on the page size and item count.
 | ||
| 
											13 years ago
										 | 	 * @return integer the zero-based current page number.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function getPage($recalculate = false)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if ($this->_page === null || $recalculate) {
 | ||
|  | 			$params = $this->params === null ? $_GET : $this->params;
 | ||
| 
											13 years ago
										 | 			if (isset($params[$this->pageVar]) && is_scalar($params[$this->pageVar])) {
 | ||
| 
											13 years ago
										 | 				$this->_page = (int)$params[$this->pageVar] - 1;
 | ||
|  | 				if ($this->validatePage) {
 | ||
| 
											13 years ago
										 | 					$pageCount = $this->getPageCount();
 | ||
| 
											13 years ago
										 | 					if ($this->_page >= $pageCount) {
 | ||
|  | 						$this->_page = $pageCount - 1;
 | ||
| 
											13 years ago
										 | 					}
 | ||
| 
											14 years ago
										 | 				}
 | ||
| 
											13 years ago
										 | 				if ($this->_page < 0) {
 | ||
|  | 					$this->_page = 0;
 | ||
| 
											13 years ago
										 | 				}
 | ||
|  | 			} else {
 | ||
| 
											13 years ago
										 | 				$this->_page = 0;
 | ||
| 
											14 years ago
										 | 			}
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 		return $this->_page;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Sets the current page number.
 | ||
| 
											14 years ago
										 | 	 * @param integer $value the zero-based index of the current page.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function setPage($value)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$this->_page = $value;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											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.
 | ||
| 
											14 years ago
										 | 	 * @return string the created URL
 | ||
| 
											13 years ago
										 | 	 * @see params
 | ||
|  | 	 * @see forcePageVar
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function createUrl($page)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$params = $this->params === null ? $_GET : $this->params;
 | ||
| 
											13 years ago
										 | 		if ($page > 0 || $page >= 0 && $this->forcePageVar) {
 | ||
| 
											13 years ago
										 | 			$params[$this->pageVar] = $page + 1;
 | ||
|  | 		} else {
 | ||
| 
											14 years ago
										 | 			unset($params[$this->pageVar]);
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		$route = $this->route === null ? Yii::$app->controller->route : $this->route;
 | ||
|  | 		return Yii::$app->getUrlManager()->createUrl($route, $params);
 | ||
| 
											14 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()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->pageSize < 1 ? 0 : $this->getPage() * $this->pageSize;
 | ||
| 
											14 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.
 | ||
| 
											13 years ago
										 | 	 * Note that if the page size is infinite, a value -1 will be returned.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public function getLimit()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->pageSize < 1 ? -1 : $this->pageSize;
 | ||
| 
											14 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | }
 |