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