Source for file Paginator.php

Documentation is available at Paginator.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: /**
  24. 24:  * Paginating math.
  25. 25:  *
  26. 26:  * @author     David Grudl
  27. 27:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  28. 28:  * @package    Nette
  29. 29:  */
  30. 30: class Paginator extends Object
  31. 31: {
  32. 32:     /** @var int */
  33. 33:     private $base 1;
  34. 34:  
  35. 35:     /** @var int */
  36. 36:     private $itemsPerPage;
  37. 37:  
  38. 38:     /** @var int */
  39. 39:     private $page;
  40. 40:  
  41. 41:     /** @var int */
  42. 42:     private $itemCount 0;
  43. 43:  
  44. 44:  
  45. 45:  
  46. 46:     /**
  47. 47:      * Sets current page number.
  48. 48:      * @param  int 
  49. 49:      * @return void 
  50. 50:      */
  51. 51:     public function setPage($page)
  52. 52:     {
  53. 53:         $this->page = (int) $page;
  54. 54:     }
  55. 55:  
  56. 56:  
  57. 57:  
  58. 58:     /**
  59. 59:      * Returns current page number.
  60. 60:      * @return int 
  61. 61:      */
  62. 62:     public function getPage()
  63. 63:     {
  64. 64:         return $this->base $this->getPageIndex();
  65. 65:     }
  66. 66:  
  67. 67:  
  68. 68:  
  69. 69:     /**
  70. 70:      * Returns first page number.
  71. 71:      * @return int 
  72. 72:      */
  73. 73:     public function getFirstPage()
  74. 74:     {
  75. 75:         return $this->base;
  76. 76:     }
  77. 77:  
  78. 78:  
  79. 79:  
  80. 80:     /**
  81. 81:      * Returns last page number.
  82. 82:      * @return int 
  83. 83:      */
  84. 84:     public function getLastPage()
  85. 85:     {
  86. 86:         return $this->base max(0$this->getPageCount(1);
  87. 87:     }
  88. 88:  
  89. 89:  
  90. 90:  
  91. 91:     /**
  92. 92:      * Sets first page (base) number.
  93. 93:      * @param  int 
  94. 94:      * @return void 
  95. 95:      */
  96. 96:     public function setBase($base)
  97. 97:     {
  98. 98:         $this->base = (int) $base;
  99. 99:     }
  100. 100:  
  101. 101:  
  102. 102:  
  103. 103:     /**
  104. 104:      * Returns first page (base) number.
  105. 105:      * @return int 
  106. 106:      */
  107. 107:     public function getBase()
  108. 108:     {
  109. 109:         return $this->base;
  110. 110:     }
  111. 111:  
  112. 112:  
  113. 113:  
  114. 114:     /**
  115. 115:      * Returns zero-based page number.
  116. 116:      * @return int 
  117. 117:      */
  118. 118:     protected function getPageIndex()
  119. 119:     {
  120. 120:         return min(max(0$this->page $this->base)max(0$this->getPageCount(1));
  121. 121:     }
  122. 122:  
  123. 123:  
  124. 124:  
  125. 125:     /**
  126. 126:      * Is the current page the first one?
  127. 127:      * @return bool 
  128. 128:      */
  129. 129:     public function isFirst()
  130. 130:     {
  131. 131:         return $this->getPageIndex(=== 0;
  132. 132:     }
  133. 133:  
  134. 134:  
  135. 135:  
  136. 136:     /**
  137. 137:      * Is the current page the last one?
  138. 138:      * @return bool 
  139. 139:      */
  140. 140:     public function isLast()
  141. 141:     {
  142. 142:         return $this->getPageIndex(=== $this->getPageCount(1;
  143. 143:     }
  144. 144:  
  145. 145:  
  146. 146:  
  147. 147:     /**
  148. 148:      * Returns the total number of pages.
  149. 149:      * @return int 
  150. 150:      */
  151. 151:     public function getPageCount()
  152. 152:     {
  153. 153:         return (int) ceil($this->itemCount $this->itemsPerPage);
  154. 154:     }
  155. 155:  
  156. 156:  
  157. 157:  
  158. 158:     /**
  159. 159:      * Sets the number of items to display on a single page.
  160. 160:      * @param  int 
  161. 161:      * @return void 
  162. 162:      */
  163. 163:     public function setItemsPerPage($itemsPerPage)
  164. 164:     {
  165. 165:         $this->itemsPerPage max(1(int) $itemsPerPage);
  166. 166:     }
  167. 167:  
  168. 168:  
  169. 169:  
  170. 170:     /**
  171. 171:      * Returns the number of items to display on a single page.
  172. 172:      * @return int 
  173. 173:      */
  174. 174:     public function getItemsPerPage()
  175. 175:     {
  176. 176:         return $this->itemsPerPage;
  177. 177:     }
  178. 178:  
  179. 179:  
  180. 180:  
  181. 181:     /**
  182. 182:      * Sets the total number of items.
  183. 183:      * @param  int (or FALSE as infinity)
  184. 184:      * @return void 
  185. 185:      */
  186. 186:     public function setItemCount($itemCount)
  187. 187:     {
  188. 188:         $this->itemCount $itemCount === FALSE PHP_INT_MAX max(0(int) $itemCount);
  189. 189:     }
  190. 190:  
  191. 191:  
  192. 192:  
  193. 193:     /**
  194. 194:      * Returns the total number of items.
  195. 195:      * @return int 
  196. 196:      */
  197. 197:     public function getItemCount()
  198. 198:     {
  199. 199:         return $this->itemCount;
  200. 200:     }
  201. 201:  
  202. 202:  
  203. 203:  
  204. 204:     /**
  205. 205:      * Returns the absolute index of the first item on current page.
  206. 206:      * @return int 
  207. 207:      */
  208. 208:     public function getOffset()
  209. 209:     {
  210. 210:         return $this->getPageIndex($this->itemsPerPage;
  211. 211:     }
  212. 212:  
  213. 213:  
  214. 214:  
  215. 215:     /**
  216. 216:      * Returns the absolute index of the first item on current page in countdown paging.
  217. 217:      * @return int 
  218. 218:      */
  219. 219:     public function getCountdownOffset()
  220. 220:     {
  221. 221:         return max(0$this->itemCount ($this->getPageIndex(1$this->itemsPerPage);
  222. 222:     }
  223. 223:  
  224. 224:  
  225. 225:  
  226. 226:     /**
  227. 227:      * Returns the number of items on current page.
  228. 228:      * @return int 
  229. 229:      */
  230. 230:     public function getLength()
  231. 231:     {
  232. 232:         return min($this->itemsPerPage$this->itemCount $this->getPageIndex($this->itemsPerPage);
  233. 233:     }
  234. 234:  
  235. 235:  
  236. 236:  
  237. 237:     /**
  238. 238:      * Generates list of pages used for visual control. (experimental)
  239. 239:      * @return array 
  240. 240:      * @deprecated
  241. 241:      */
  242. 242:     public function getSteps($steps 5$surround 3)
  243. 243:     {
  244. 244:         trigger_error('Paginator::getSteps() is deprecated; use template helper instead.'E_USER_WARNING);
  245. 245:         $lastPage $this->getPageCount(1;
  246. 246:         $page $this->getPageIndex();
  247. 247:         if ($lastPage 1return array($page $this->base);
  248. 248:  
  249. 249:         $surround max(0$surround);
  250. 250:         $arr range(max(0$page $surround$this->basemin($lastPage$page $surround$this->base);
  251. 251:  
  252. 252:         $steps max(1$steps 1);
  253. 253:         for ($i 0$i <= $steps$i++$arr[round($lastPage $steps $i$this->base;
  254. 254:         sort($arr);
  255. 255:         return array_values(array_unique($arr));
  256. 256:     }
  257. 257: