CPagination.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * CPagination class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CPagination represents information relevant to pagination.
  12. *
  13. * When data needs to be rendered in multiple pages, we can use CPagination to
  14. * represent information such as {@link getItemCount total item count},
  15. * {@link getPageSize page size}, {@link getCurrentPage current page}, etc.
  16. * These information can be passed to {@link CBasePager pagers} to render
  17. * pagination buttons or links.
  18. *
  19. * Example:
  20. *
  21. * Controller action:
  22. * <pre>
  23. * function actionIndex(){
  24. * $criteria=new CDbCriteria();
  25. * $count=Article::model()->count($criteria);
  26. * $pages=new CPagination($count);
  27. *
  28. * // results per page
  29. * $pages->pageSize=10;
  30. * $pages->applyLimit($criteria);
  31. * $models=Article::model()->findAll($criteria);
  32. *
  33. * $this->render('index', array(
  34. * 'models' => $models,
  35. * 'pages' => $pages
  36. * ));
  37. * }
  38. * </pre>
  39. *
  40. * View:
  41. * <pre>
  42. * <?php foreach($models as $model): ?>
  43. * // display a model
  44. * <?php endforeach; ?>
  45. *
  46. * // display pagination
  47. * <?php $this->widget('CLinkPager', array(
  48. * 'pages' => $pages,
  49. * )) ?>
  50. * </pre>
  51. *
  52. * @property integer $pageSize Number of items in each page. Defaults to 10.
  53. * @property integer $itemCount Total number of items. Defaults to 0.
  54. * @property integer $pageCount Number of pages.
  55. * @property integer $currentPage The zero-based index of the current page. Defaults to 0.
  56. * @property integer $offset The offset of the data. This may be used to set the
  57. * OFFSET value for a SQL statement for fetching the current page of data.
  58. * @property integer $limit The limit of the data. This may be used to set the
  59. * LIMIT value for a SQL statement for fetching the current page of data.
  60. * This returns the same value as {@link pageSize}.
  61. *
  62. * @author Qiang Xue <qiang.xue@gmail.com>
  63. * @package system.web
  64. * @since 1.0
  65. */
  66. class CPagination extends CComponent
  67. {
  68. /**
  69. * The default page size.
  70. */
  71. const DEFAULT_PAGE_SIZE=10;
  72. /**
  73. * @var string name of the GET variable storing the current page index. Defaults to 'page'.
  74. */
  75. public $pageVar='page';
  76. /**
  77. * @var string the route (controller ID and action ID) for displaying the paged contents.
  78. * Defaults to empty string, meaning using the current route.
  79. */
  80. public $route='';
  81. /**
  82. * @var array of parameters (name=>value) that should be used instead of GET when generating pagination URLs.
  83. * Defaults to null, meaning using the currently available GET parameters.
  84. */
  85. public $params;
  86. /**
  87. * @var boolean whether to ensure {@link currentPage} is returning a valid page number.
  88. * When this property is true, the value returned by {@link currentPage} will always be between
  89. * 0 and ({@link pageCount}-1). Because {@link pageCount} relies on the correct value of {@link itemCount},
  90. * it means you must have knowledge about the total number of data items when you want to access {@link currentPage}.
  91. * This is fine for SQL-based queries, but may not be feasible for other kinds of queries (e.g. MongoDB).
  92. * In those cases, you may set this property to be false to skip the validation (you may need to validate yourself then).
  93. * Defaults to true.
  94. * @since 1.1.4
  95. */
  96. public $validateCurrentPage=true;
  97. private $_pageSize=self::DEFAULT_PAGE_SIZE;
  98. private $_itemCount=0;
  99. private $_currentPage;
  100. public $pageSize = self::DEFAULT_PAGE_SIZE;
  101. public $rowsCount = 0;
  102. public $pagesCount = 0;
  103. /**
  104. * Constructor.
  105. * @param integer $itemCount total number of items.
  106. */
  107. public function __construct($itemCount=0, $pageSize = 0)
  108. {
  109. $this->setItemCount($itemCount);
  110. $this->setPageSize($pageSize);
  111. if($this->pageSize != 0){
  112. $this->pagesCount = ceil($this->rowsCount / $this->pageSize);
  113. }
  114. }
  115. /**
  116. * @return integer number of items in each page. Defaults to 10.
  117. */
  118. public function getPageSize()
  119. {
  120. return $this->_pageSize;
  121. }
  122. /**
  123. * @param integer $value number of items in each page
  124. */
  125. public function setPageSize($value)
  126. {
  127. if(($this->_pageSize = $this->pageSize = $value)<=0)
  128. $this->_pageSize = $this->pageSize = self::DEFAULT_PAGE_SIZE;
  129. }
  130. /**
  131. * @return integer total number of items. Defaults to 0.
  132. */
  133. public function getItemCount()
  134. {
  135. return $this->_itemCount;
  136. }
  137. /**
  138. * @param integer $value total number of items.
  139. */
  140. public function setItemCount($value)
  141. {
  142. if(($this->_itemCount = $this->rowsCount =$value)<0)
  143. $this->_itemCount = $this->rowsCount=0;
  144. }
  145. /**
  146. * @return integer number of pages
  147. */
  148. public function getPageCount()
  149. {
  150. return (int)(($this->_itemCount+$this->_pageSize-1)/$this->_pageSize);
  151. }
  152. /**
  153. * @param boolean $recalculate whether to recalculate the current page based on the page size and item count.
  154. * @return integer the zero-based index of the current page. Defaults to 0.
  155. */
  156. public function getCurrentPage($recalculate=true)
  157. {
  158. if($this->_currentPage===null || $recalculate)
  159. {
  160. if(isset($_GET[$this->pageVar]))
  161. {
  162. $this->_currentPage=(int)$_GET[$this->pageVar]-1;
  163. if($this->validateCurrentPage)
  164. {
  165. $pageCount=$this->getPageCount();
  166. if($this->_currentPage>=$pageCount)
  167. $this->_currentPage=$pageCount-1;
  168. }
  169. if($this->_currentPage<0)
  170. $this->_currentPage=0;
  171. }
  172. else
  173. $this->_currentPage=0;
  174. }
  175. return $this->_currentPage;
  176. }
  177. /**
  178. * @param integer $value the zero-based index of the current page.
  179. */
  180. public function setCurrentPage($value)
  181. {
  182. $this->_currentPage=$value;
  183. $_GET[$this->pageVar]=$value+1;
  184. }
  185. /**
  186. * Creates the URL suitable for pagination.
  187. * This method is mainly called by pagers when creating URLs used to
  188. * perform pagination. The default implementation is to call
  189. * the controller's createUrl method with the page information.
  190. * You may override this method if your URL scheme is not the same as
  191. * the one supported by the controller's createUrl method.
  192. * @param CController $controller the controller that will create the actual URL
  193. * @param integer $page the page that the URL should point to. This is a zero-based index.
  194. * @return string the created URL
  195. */
  196. public function createPageUrl($controller,$page)
  197. {
  198. $params=$this->params===null ? $_GET : $this->params;
  199. if($page>0) // page 0 is the default
  200. $params[$this->pageVar]=$page+1;
  201. else
  202. unset($params[$this->pageVar]);
  203. return $controller->createUrl($this->route,$params);
  204. }
  205. /**
  206. * Applies LIMIT and OFFSET to the specified query criteria.
  207. * @param CDbCriteria $criteria the query criteria that should be applied with the limit
  208. */
  209. public function applyLimit($criteria)
  210. {
  211. $criteria->limit=$this->getLimit();
  212. $criteria->offset=$this->getOffset();
  213. }
  214. /**
  215. * @return integer the offset of the data. This may be used to set the
  216. * OFFSET value for a SQL statement for fetching the current page of data.
  217. * @since 1.1.0
  218. */
  219. public function getOffset()
  220. {
  221. return $this->getCurrentPage()*$this->getPageSize();
  222. }
  223. /**
  224. * @return integer the limit of the data. This may be used to set the
  225. * LIMIT value for a SQL statement for fetching the current page of data.
  226. * This returns the same value as {@link pageSize}.
  227. * @since 1.1.0
  228. */
  229. public function getLimit()
  230. {
  231. return $this->getPageSize();
  232. }
  233. }