paginator.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php namespace System;
  2. class Paginator {
  3. /**
  4. * The results for the current page.
  5. *
  6. * @var array
  7. */
  8. public $results;
  9. /**
  10. * The total number of results.
  11. *
  12. * @var int
  13. */
  14. public $total;
  15. /**
  16. * The current page.
  17. *
  18. * @var int
  19. */
  20. public $page;
  21. /**
  22. * The number of items per page.
  23. *
  24. * @var int
  25. */
  26. public $per_page;
  27. /**
  28. * The language that should be used when generating page links.
  29. *
  30. * @var string
  31. */
  32. public $language;
  33. /**
  34. * Create a new Paginator instance.
  35. *
  36. * @param array $results
  37. * @param int $total
  38. * @param int $per_page
  39. * @return void
  40. */
  41. public function __construct($results, $total, $per_page)
  42. {
  43. $this->per_page = $per_page;
  44. $this->results = $results;
  45. $this->total = $total;
  46. // Determine the current page based on the total results and per page limit.
  47. $this->page = static::page($this->total, $this->per_page);
  48. }
  49. /**
  50. * Get the current page from the request query string.
  51. *
  52. * The page will be validated and adjusted if it is less than one or greater than the last page.
  53. *
  54. * @param int $total
  55. * @param int $per_page
  56. * @return int
  57. */
  58. public static function page($total, $per_page)
  59. {
  60. $page = Input::get('page', 1);
  61. if (is_numeric($page) and $page > $last_page = ceil($total / $per_page))
  62. {
  63. return $last_page;
  64. }
  65. return ($page < 1 or filter_var($page, FILTER_VALIDATE_INT) === false) ? 1 : $page;
  66. }
  67. /**
  68. * Create the HTML pagination links.
  69. *
  70. * @param int $adjacent
  71. * @return string
  72. */
  73. public function links($adjacent = 3)
  74. {
  75. return ($this->last_page() > 1) ? '<div class="pagination">'.$this->previous().$this->numbers($adjacent).$this->next().'</div>' : '';
  76. }
  77. /**
  78. * Generate the HTML numeric page links.
  79. *
  80. * If there are not enough pages to make it worth sliding, all of the pages will be listed.
  81. *
  82. * @param int $adjacent
  83. * @return string
  84. */
  85. private function numbers($adjacent = 3)
  86. {
  87. return ($this->last_page() < 7 + ($adjacent * 2)) ? $this->range(1, $this->last_page()) : $this->slider($adjacent);
  88. }
  89. /**
  90. * Build sliding list of HTML numeric page links.
  91. *
  92. * @param int $adjacent
  93. * @return string
  94. */
  95. private function slider($adjacent)
  96. {
  97. if ($this->page <= $adjacent * 2)
  98. {
  99. return $this->range(1, 2 + ($adjacent * 2)).$this->ending();
  100. }
  101. elseif ($this->page >= $this->last_page() - ($adjacent * 2))
  102. {
  103. return $this->beginning().$this->range($this->last_page() - 2 - ($adjacent * 2), $this->last_page());
  104. }
  105. else
  106. {
  107. return $this->beginning().$this->range($this->page - $adjacent, $this->page + $adjacent).$this->ending();
  108. }
  109. }
  110. /**
  111. * Generate the "previous" HTML link.
  112. *
  113. * @return string
  114. */
  115. public function previous()
  116. {
  117. $text = Lang::line('pagination.previous')->get();
  118. return ($this->page > 1) ? $this->link($this->page - 1, $text, 'prev_page').' ' : HTML::span($text, array('class' => 'disabled prev_page')).' ';
  119. }
  120. /**
  121. * Generate the "next" HTML link.
  122. *
  123. * @return string
  124. */
  125. public function next()
  126. {
  127. $text = Lang::line('pagination.next')->get();
  128. return ($this->page < $this->last_page()) ? $this->link($this->page + 1, $text, 'next_page') : HTML::span($text, array('class' => 'disabled next_page'));
  129. }
  130. /**
  131. * Build the first two page links for a sliding page range.
  132. *
  133. * @return string
  134. */
  135. private function beginning()
  136. {
  137. return $this->range(1, 2).$this->dots();
  138. }
  139. /**
  140. * Build the last two page links for a sliding page range.
  141. *
  142. * @return string
  143. */
  144. private function ending()
  145. {
  146. return $this->dots().$this->range($this->last_page() - 1, $this->last_page());
  147. }
  148. /**
  149. * Create a HTML page link.
  150. *
  151. * @param int $page
  152. * @param string $text
  153. * @param string $attributes
  154. * @return string
  155. */
  156. private function link($page, $text, $class)
  157. {
  158. return HTML::link(Request::uri().'?page='.$page, $text, array('class' => $class), Request::is_secure());
  159. }
  160. /**
  161. * Build a "dots" HTML span element.
  162. *
  163. * @return string
  164. */
  165. private function dots()
  166. {
  167. return HTML::span('...', array('class' => 'dots')).' ';
  168. }
  169. /**
  170. * Determine the last page number based on the total pages and per page limit.
  171. *
  172. * @return int
  173. */
  174. private function last_page()
  175. {
  176. return ceil($this->total / $this->per_page);
  177. }
  178. /**
  179. * Build a range of page links.
  180. *
  181. * For the current page, an HTML span element will be generated instead of a link.
  182. *
  183. * @param int $start
  184. * @param int $end
  185. * @return string
  186. */
  187. private function range($start, $end)
  188. {
  189. $pages = '';
  190. for ($i = $start; $i <= $end; $i++)
  191. {
  192. $pages .= ($this->page == $i) ? HTML::span($i, array('class' => 'current')).' ' : $this->link($i, $i, null).' ';
  193. }
  194. return $pages;
  195. }
  196. /**
  197. * Set the language that should be used when generating page links.
  198. *
  199. * @param string $language
  200. * @return Paginator
  201. */
  202. public function lang($language)
  203. {
  204. $this->language = $language;
  205. return $this;
  206. }
  207. }