PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/spoon/datagrid/paging.php

http://github.com/forkcms/forkcms
PHP | 245 lines | 99 code | 37 blank | 109 comment | 21 complexity | 418e8678ffddd966af72bf4e3dc0737a MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, MIT, AGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Spoon Library
  4. *
  5. * This source file is part of the Spoon Library. More information,
  6. * documentation and tutorials can be found @ http://www.spoon-library.com
  7. *
  8. * @package spoon
  9. * @subpackage datagrid
  10. *
  11. *
  12. * @author Davy Hellemans <davy@spoon-library.com>
  13. * @since 1.0.0
  14. */
  15. /**
  16. * This interface has to be implemented by the paging-classes
  17. *
  18. * @package spoon
  19. * @subpackage datagrid
  20. *
  21. *
  22. * @author Davy Hellemans <davy@spoon-library.com>
  23. * @since 1.0.0
  24. */
  25. interface iSpoonDatagridPaging
  26. {
  27. /**
  28. * Builds & returns the pagination.
  29. *
  30. * @return string
  31. * @param string $URL The URL to use for the paging.
  32. * @param int $offset The current offset.
  33. * @param string $order The current order.
  34. * @param string $sort The current sorting method.
  35. * @param int $numResults The number of results.
  36. * @param int $numPerPage The number of results per page.
  37. * @param bool[optional] $debug Should debug-mode be enabled?
  38. * @param string[optional] $compileDirectory The path to the compile directory.
  39. */
  40. public static function getContent($URL, $offset, $order, $sort, $numResults, $numPerPage, $debug = true, $compileDirectory = null);
  41. }
  42. /**
  43. * This class is the base class for pagination
  44. *
  45. * @package spoon
  46. * @subpackage datagrid
  47. *
  48. *
  49. * @author Davy Hellemans <davy@spoon-library.com>
  50. * @since 1.0.0
  51. */
  52. class SpoonDatagridPaging implements iSpoonDatagridPaging
  53. {
  54. /**
  55. * Next label
  56. *
  57. * @var string
  58. */
  59. private static $next = 'next';
  60. /**
  61. * Previous label
  62. *
  63. * @var string
  64. */
  65. private static $previous = 'previous';
  66. /**
  67. * Builds & returns the pagination.
  68. *
  69. * @return string
  70. * @param string $URL The URL to use for the paging.
  71. * @param int $offset The current offset.
  72. * @param string $order The current order.
  73. * @param string $sort The current sorting-method.
  74. * @param int $numResults The number of results.
  75. * @param int $numPerPage The number of results per page.
  76. * @param bool[optional] $debug Should debug-mode be enabled?
  77. * @param string[optional] $compileDirectory The path to the compile-directory.
  78. */
  79. public static function getContent($URL, $offset, $order, $sort, $numResults, $numPerPage, $debug = true, $compileDirectory = null)
  80. {
  81. // current page
  82. $currentPage = ceil($offset / $numPerPage) + 1;
  83. // number of pages
  84. $numPages = ceil($numResults / $numPerPage);
  85. // load template
  86. $tpl = new SpoonTemplate();
  87. // compile directory
  88. if($compileDirectory !== null) $tpl->setCompileDirectory($compileDirectory);
  89. else $tpl->setCompileDirectory(dirname(__FILE__));
  90. // force compiling
  91. $tpl->setForceCompile((bool) $debug);
  92. // previous url
  93. if($currentPage > 1)
  94. {
  95. // label & url
  96. $previousLabel = self::$previous;
  97. $previousURL = str_replace(array('[offset]', '[order]', '[sort]'), array(($offset - $numPerPage), $order, $sort), $URL);
  98. $tpl->assign('previousLabel', $previousLabel);
  99. $tpl->assign('previousURL', $previousURL);
  100. }
  101. // next url
  102. if($currentPage < $numPages)
  103. {
  104. // label & url
  105. $nextLabel = self::$next;
  106. $nextURL = str_replace(array('[offset]', '[order]', '[sort]'), array(($offset + $numPerPage), $order, $sort), $URL);
  107. $tpl->assign('nextLabel', $nextLabel);
  108. $tpl->assign('nextURL', $nextURL);
  109. }
  110. // limit
  111. $limit = 7;
  112. $breakpoint = 4;
  113. $items = array();
  114. /**
  115. * Less than or 7 pages. We know all the keys, and we put them in the array
  116. * that we will use to generate the actual pagination.
  117. */
  118. if($numPages <= $limit)
  119. {
  120. for($i = 1; $i <= $numPages; $i++) $items[$i] = $i;
  121. }
  122. // more than 7 pages
  123. else
  124. {
  125. // first page
  126. if($currentPage == 1)
  127. {
  128. // [1] 2 3 4 5 6 7 8 9 10 11 12 13
  129. for($i = 1; $i <= $limit; $i++) $items[$i] = $i;
  130. $items[$limit + 1] = '...';
  131. }
  132. // last page
  133. elseif($currentPage == $numPages)
  134. {
  135. // 1 2 3 4 5 6 7 8 9 10 11 12 [13]
  136. $items[$numPages - $limit - 1] = '...';
  137. for($i = ($numPages - $limit); $i <= $numPages; $i++) $items[$i] = $i;
  138. }
  139. // other page
  140. else
  141. {
  142. // 1 2 3 [4] 5 6 7 8 9 10 11 12 13
  143. // define min & max
  144. $min = $currentPage - $breakpoint + 1;
  145. $max = $currentPage + $breakpoint - 1;
  146. // minimum doesnt exist
  147. while($min <= 0)
  148. {
  149. $min++;
  150. $max++;
  151. }
  152. // maximum doesnt exist
  153. while($max > $numPages)
  154. {
  155. $min--;
  156. $max--;
  157. }
  158. // create the list
  159. if($min != 1) $items[$min - 1] = '...';
  160. for($i = $min; $i <= $max; $i++) $items[$i] = $i;
  161. if($max != $numPages) $items[$max + 1] = '...';
  162. }
  163. }
  164. // init var
  165. $pages = array();
  166. // loop pages
  167. foreach($items as $item)
  168. {
  169. // counter
  170. if(!isset($i)) $i = 0;
  171. // base details
  172. $pages[$i]['page'] = false;
  173. $pages[$i]['currentPage'] = false;
  174. $pages[$i]['otherPage'] = false;
  175. $pages[$i]['noPage'] = false;
  176. $pages[$i]['url'] = '';
  177. $pages[$i]['pageNumber'] = $item;
  178. // hellips
  179. if($item == '...') $pages[$i]['noPage'] = true;
  180. // regular page
  181. else
  182. {
  183. // show page
  184. $pages[$i]['page'] = true;
  185. // current page ?
  186. if($item == $currentPage) $pages[$i]['currentPage'] = true;
  187. // other page
  188. else
  189. {
  190. // show the page
  191. $pages[$i]['otherPage'] = true;
  192. // url to this page
  193. $pages[$i]['url'] = str_replace(array('[offset]', '[order]', '[sort]'), array((($numPerPage * $item) - $numPerPage), $order, $sort), $URL);
  194. }
  195. }
  196. // update counter
  197. $i++;
  198. }
  199. // first key needs to be zero
  200. $pages = SpoonFilter::arraySortKeys($pages);
  201. // assign pages
  202. $tpl->assign('pages', $pages);
  203. // cough it up
  204. ob_start();
  205. $tpl->display(dirname(__FILE__) . '/paging.tpl');
  206. return ob_get_clean();
  207. }
  208. }