CPagination.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. /**
  101. * Constructor.
  102. * @param integer $itemCount total number of items.
  103. */
  104. public function __construct($itemCount=0)
  105. {
  106. $this->setItemCount($itemCount);
  107. }
  108. /**
  109. * @return integer number of items in each page. Defaults to 10.
  110. */
  111. public function getPageSize()
  112. {
  113. return $this->_pageSize;
  114. }
  115. /**
  116. * @param integer $value number of items in each page
  117. */
  118. public function setPageSize($value)
  119. {
  120. if(($this->_pageSize=$value)<=0)
  121. $this->_pageSize=self::DEFAULT_PAGE_SIZE;
  122. }
  123. /**
  124. * @return integer total number of items. Defaults to 0.
  125. */
  126. public function getItemCount()
  127. {
  128. return $this->_itemCount;
  129. }
  130. /**
  131. * @param integer $value total number of items.
  132. */
  133. public function setItemCount($value)
  134. {
  135. if(($this->_itemCount=$value)<0)
  136. $this->_itemCount=0;
  137. }
  138. /**
  139. * @return integer number of pages
  140. */
  141. public function getPageCount()
  142. {
  143. return (int)(($this->_itemCount+$this->_pageSize-1)/$this->_pageSize);
  144. }
  145. /**
  146. * @param boolean $recalculate whether to recalculate the current page based on the page size and item count.
  147. * @return integer the zero-based index of the current page. Defaults to 0.
  148. */
  149. public function getCurrentPage($recalculate=true)
  150. {
  151. if($this->_currentPage===null || $recalculate)
  152. {
  153. if(isset($_GET[$this->pageVar]))
  154. {
  155. $this->_currentPage=(int)$_GET[$this->pageVar]-1;
  156. if($this->validateCurrentPage)
  157. {
  158. $pageCount=$this->getPageCount();
  159. if($this->_currentPage>=$pageCount)
  160. $this->_currentPage=$pageCount-1;
  161. }
  162. if($this->_currentPage<0)
  163. $this->_currentPage=0;
  164. }
  165. else
  166. $this->_currentPage=0;
  167. }
  168. return $this->_currentPage;
  169. }
  170. /**
  171. * @param integer $value the zero-based index of the current page.
  172. */
  173. public function setCurrentPage($value)
  174. {
  175. $this->_currentPage=$value;
  176. $_GET[$this->pageVar]=$value+1;
  177. }
  178. /**
  179. * Creates the URL suitable for pagination.
  180. * This method is mainly called by pagers when creating URLs used to
  181. * perform pagination. The default implementation is to call
  182. * the controller's createUrl method with the page information.
  183. * You may override this method if your URL scheme is not the same as
  184. * the one supported by the controller's createUrl method.
  185. * @param CController $controller the controller that will create the actual URL
  186. * @param integer $page the page that the URL should point to. This is a zero-based index.
  187. * @return string the created URL
  188. */
  189. public function createPageUrl($controller,$page)
  190. {
  191. $params=$this->params===null ? $_GET : $this->params;
  192. if($page>0) // page 0 is the default
  193. $params[$this->pageVar]=$page+1;
  194. else
  195. unset($params[$this->pageVar]);
  196. return $controller->createUrl($this->route,$params);
  197. }
  198. /**
  199. * Applies LIMIT and OFFSET to the specified query criteria.
  200. * @param CDbCriteria $criteria the query criteria that should be applied with the limit
  201. */
  202. public function applyLimit($criteria)
  203. {
  204. $criteria->limit=$this->getLimit();
  205. $criteria->offset=$this->getOffset();
  206. }
  207. /**
  208. * @return integer the offset of the data. This may be used to set the
  209. * OFFSET value for a SQL statement for fetching the current page of data.
  210. * @since 1.1.0
  211. */
  212. public function getOffset()
  213. {
  214. return $this->getCurrentPage()*$this->getPageSize();
  215. }
  216. /**
  217. * @return integer the limit of the data. This may be used to set the
  218. * LIMIT value for a SQL statement for fetching the current page of data.
  219. * This returns the same value as {@link pageSize}.
  220. * @since 1.1.0
  221. */
  222. public function getLimit()
  223. {
  224. return $this->getPageSize();
  225. }
  226. }