PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/environment/classes/dataaccess/DataPagination.class.php

https://github.com/fb83/Project-Pier
PHP | 297 lines | 99 code | 32 blank | 166 comment | 17 complexity | 594c637451a6465002fafd8e42d53a7e MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, AGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * Data pagination object holds data that calculate per page output
  4. * (start, end, number of pages, first and last page etc)
  5. *
  6. * @version 1.0
  7. * @http://www.projectpier.org/
  8. */
  9. class DataPagination {
  10. /**
  11. * Total number of items
  12. *
  13. * @var integer
  14. */
  15. private $total_items = 0;
  16. /**
  17. * Number of items per page
  18. *
  19. * @var integer
  20. */
  21. private $items_per_page = 10;
  22. /**
  23. * Number of current page
  24. *
  25. * @var integer
  26. */
  27. private $current_page = 1;
  28. /**
  29. * Cached total pages value. If null this value will be calculated by getTotalPages()
  30. * method and cached
  31. *
  32. * @var integer
  33. */
  34. private $total_pages = null;
  35. /**
  36. * Construct the DataPagination
  37. *
  38. * @access public
  39. * @param integer $total_items Number of items
  40. * @param integer $items_per_page Number of items per page. Default is 10
  41. * @param integer $current_page Current page. Default is 1
  42. * @return DataPagination
  43. */
  44. function __construct($total_items = null, $items_per_page = null, $current_page = null) {
  45. if (!is_null($total_items)) {
  46. $this->setTotalItems($total_items);
  47. } // if
  48. if (!is_null($items_per_page)) {
  49. $this->setItemsPerPage($items_per_page);
  50. } // if
  51. if (!is_null($current_page)) {
  52. $this->setCurrentPage($current_page);
  53. } // if
  54. } // __construct
  55. // ---------------------------------------------------
  56. // Check and get
  57. // ---------------------------------------------------
  58. /**
  59. * Check if specific page is current page. If $page is null function will use
  60. * current page
  61. *
  62. * @access public
  63. * @param integer $page Page that need to be checked. If null function will
  64. * use current page
  65. * @return boolean
  66. */
  67. function isCurrent($page = null) {
  68. $page = is_null($page) ? $this->getCurrentPage() : (integer) $page;
  69. return $page == $this->getCurrentPage();
  70. } // isCurrent
  71. /**
  72. * Check if specific page is first page. If $page is null function will use
  73. * current page
  74. *
  75. * @access public
  76. * @param integer $page Page that need to be checked. If null function will
  77. * use current page
  78. * @return boolean
  79. */
  80. function isFirst($page = null) {
  81. $page = is_null($page) ? $this->getCurrentPage() : (integer) $page;
  82. return $page == 1;
  83. } // isFirst
  84. /**
  85. * Check if specific page is last page. If $page is null function will use
  86. * current page
  87. *
  88. * @access public
  89. * @param integer $page Page that need to be checked. If null function will
  90. * use current page
  91. * @return boolean
  92. */
  93. function isLast($page = null) {
  94. $page = is_null($page) ? $this->getCurrentPage() : (integer) $page;
  95. if (is_null($last = $this->getTotalPages())) {
  96. return false;
  97. } // if
  98. return $page == $last;
  99. } // isLast
  100. /**
  101. * Return previous page. If there is some kind of error function will return
  102. * current page. Check existance of prev page using hasPrevious() function
  103. *
  104. * @access public
  105. * @param void
  106. * @return integer
  107. */
  108. function getPrevious() {
  109. return $this->hasPrevious() ? $this->getCurrentPage() - 1 : $this->getCurrentPage();
  110. } // getPreviousPage
  111. /**
  112. * Check if specific page has previous page. If $page is null function will use
  113. * current page
  114. *
  115. * @access public
  116. * @param integer $page Page that need to be checked. If null function will
  117. * use current page
  118. * @return boolean
  119. */
  120. function hasPrevious($page = null) {
  121. $page = is_null($page) ? $this->getCurrentPage() : (integer) $page;
  122. return $page > 1;
  123. } // hasPrevious
  124. /**
  125. * Return next page number. In case of an error this function will return current
  126. * page number. Check if next page exists using hasNext() function
  127. *
  128. * @access public
  129. * @param void
  130. * @return integer
  131. */
  132. function getNext() {
  133. return $this->hasNext() ? $this->getCurrentPage() + 1 : $this->getCurrentPage();
  134. } // getNext
  135. /**
  136. * Check if specific page has next page. If $page is null function will use
  137. * current page
  138. *
  139. * @access public
  140. * @param integer $page Page that need to be checked. If null function will
  141. * use current page
  142. * @return boolean
  143. */
  144. function hasNext($page = null) {
  145. $page = is_null($page) ? $this->getCurrentPage() : (integer) $page;
  146. if (is_null($last = $this->getTotalPages())) {
  147. return false;
  148. } // if
  149. return $page < $last;
  150. } // hasNext
  151. /**
  152. * Return total number of pages
  153. *
  154. * @access public
  155. * @param void
  156. * @return integer
  157. */
  158. function getTotalPages() {
  159. if (is_int($this->total_pages)) {
  160. return $this->total_pages;
  161. } // if
  162. if (($this->getItemsPerPage() < 1) || ($this->getTotalItems() < 1)) return 1; // there must be one page
  163. if (($this->getTotalItems() % $this->getItemsPerPage()) == 0) {
  164. $this->total_pages = (integer) ($this->getTotalItems() / $this->getItemsPerPage());
  165. } else {
  166. $this->total_pages = (integer) ($this->getTotalItems() / $this->getItemsPerPage()) + 1;
  167. } // if
  168. return $this->total_pages;
  169. } // getTotalPages
  170. /**
  171. * Return number of items on specific page
  172. *
  173. * @param integer $page
  174. * @return integer
  175. */
  176. function countItemsOnPage($page) {
  177. $page = (integer) $page;
  178. if ($page < 1) {
  179. $page = 1;
  180. } // if
  181. if (($page + 1) * $this->getItemsPerPage() > $this->getTotalItems()) {
  182. return $this->getTotalItems() - (($page - 1) * $this->getItemsPerPage());
  183. } else {
  184. return $this->getItemsPerPage();
  185. } // if
  186. } // countItemsOnPage
  187. /**
  188. * Return first param for LIMIT in queries. Second one is number of items per page
  189. *
  190. * @access public
  191. * @param integer $page On witch page? If null current will be used
  192. * @return integer
  193. */
  194. function getLimitStart($page = null) {
  195. $page = is_null($page) ? $this->getCurrentPage() : (integer) $page;
  196. $page -= 1; // Start is one page down...
  197. return ($page * $this->getItemsPerPage());
  198. } // getLimitStart
  199. // ---------------------------------------------------
  200. // Getters and setters
  201. // ---------------------------------------------------
  202. /**
  203. * Get total_items
  204. *
  205. * @access public
  206. * @param null
  207. * @return integer
  208. */
  209. function getTotalItems() {
  210. return $this->total_items;
  211. } // getTotalItems
  212. /**
  213. * Set total_items value
  214. *
  215. * @access public
  216. * @param integer $value
  217. * @return null
  218. */
  219. function setTotalItems($value) {
  220. $this->total_pages = null;
  221. $this->total_items = (integer) $value > 0 ? (integer) $value : 0;
  222. } // setTotalItems
  223. /**
  224. * Get items_per_page
  225. *
  226. * @access public
  227. * @param null
  228. * @return integer
  229. */
  230. function getItemsPerPage() {
  231. return $this->items_per_page;
  232. } // getItemsPerPage
  233. /**
  234. * Set items_per_page value
  235. *
  236. * @access public
  237. * @param integer $value
  238. * @return null
  239. */
  240. function setItemsPerPage($value) {
  241. $this->total_pages = null;
  242. $this->items_per_page = (integer) $value > 0 ? (integer) $value : 10;
  243. } // setItemsPerPage
  244. /**
  245. * Get current_page
  246. *
  247. * @access public
  248. * @param null
  249. * @return integer
  250. */
  251. function getCurrentPage() {
  252. return $this->current_page;
  253. } // getCurrentPage
  254. /**
  255. * Set current_page value
  256. *
  257. * @access public
  258. * @param integer $value
  259. * @return null
  260. */
  261. function setCurrentPage($value) {
  262. $this->current_page = (integer) $value > 0 ? (integer) $value : 1;
  263. } // setCurrentPage
  264. } // DataPagination
  265. ?>