paginator.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php namespace Laravel;
  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 last page available for the result set.
  29. *
  30. * @var int
  31. */
  32. public $last_page;
  33. /**
  34. * The language that should be used when generating page links.
  35. *
  36. * @var string
  37. */
  38. public $language;
  39. /**
  40. * The values that should be appended to the end of the link query strings.
  41. *
  42. * @var array
  43. */
  44. public $append = array();
  45. /**
  46. * Create a new Paginator instance.
  47. *
  48. * @param array $results
  49. * @param int $page
  50. * @param int $total
  51. * @param int $per_page
  52. * @param int $last_page
  53. * @return void
  54. */
  55. protected function __construct($results, $page, $total, $per_page, $last_page)
  56. {
  57. $this->page = $page;
  58. $this->total = $total;
  59. $this->results = $results;
  60. $this->per_page = $per_page;
  61. $this->last_page = $last_page;
  62. }
  63. /**
  64. * Create a new Paginator instance.
  65. *
  66. * @param array $results
  67. * @param int $total
  68. * @param int $per_page
  69. * @return Paginator
  70. */
  71. public static function make($results, $total, $per_page)
  72. {
  73. return new static($results, static::page($total, $per_page), $total, $per_page, ceil($total / $per_page));
  74. }
  75. /**
  76. * Get the current page from the request query string.
  77. *
  78. * The page will be validated and adjusted if it is less than one or greater than the last page.
  79. * For example, if the current page is not an integer or less than one, one will be returned.
  80. * If the current page is greater than the last page, the last page will be returned.
  81. *
  82. * @param int $total
  83. * @param int $per_page
  84. * @return int
  85. */
  86. public static function page($total, $per_page)
  87. {
  88. $page = Input::get('page', 1);
  89. if (is_numeric($page) and $page > $last_page = ceil($total / $per_page))
  90. {
  91. return ($last_page > 0) ? $last_page : 1;
  92. }
  93. return ($page < 1 or filter_var($page, FILTER_VALIDATE_INT) === false) ? 1 : $page;
  94. }
  95. /**
  96. * Create the HTML pagination links.
  97. *
  98. * @param int $adjacent
  99. * @return string
  100. */
  101. public function links($adjacent = 3)
  102. {
  103. if ($this->last_page <= 1) return '';
  104. // The hard-coded "7" is to account for all of the constant elements in a sliding range.
  105. // Namely: The the current page, the two ellipses, the two beginning pages, and the two ending pages.
  106. if ($this->last_page < 7 + ($adjacent * 2))
  107. {
  108. $numbers = $this->range(1, $this->last_page);
  109. }
  110. else
  111. {
  112. $numbers = $this->slider($adjacent);
  113. }
  114. return '<div class="pagination">'.$this->previous().$numbers.$this->next().'</div>';
  115. }
  116. /**
  117. * Build sliding list of HTML numeric page links.
  118. *
  119. * @param int $adjacent
  120. * @return string
  121. */
  122. private function slider($adjacent)
  123. {
  124. if ($this->page <= $adjacent * 2)
  125. {
  126. return $this->range(1, 2 + ($adjacent * 2)).$this->ending();
  127. }
  128. elseif ($this->page >= $this->last_page - ($adjacent * 2))
  129. {
  130. return $this->beginning().$this->range($this->last_page - 2 - ($adjacent * 2), $this->last_page);
  131. }
  132. else
  133. {
  134. return $this->beginning().$this->range($this->page - $adjacent, $this->page + $adjacent).$this->ending();
  135. }
  136. }
  137. /**
  138. * Generate the "previous" HTML link.
  139. *
  140. * @return string
  141. */
  142. public function previous()
  143. {
  144. $text = Lang::line('pagination.previous')->get($this->language);
  145. if ($this->page > 1)
  146. {
  147. return $this->link($this->page - 1, $text, 'prev_page').' ';
  148. }
  149. return HTML::span($text, array('class' => 'disabled prev_page')).' ';
  150. }
  151. /**
  152. * Generate the "next" HTML link.
  153. *
  154. * @return string
  155. */
  156. public function next()
  157. {
  158. $text = Lang::line('pagination.next')->get($this->language);
  159. if ($this->page < $this->last_page)
  160. {
  161. return $this->link($this->page + 1, $text, 'next_page');
  162. }
  163. return HTML::span($text, array('class' => 'disabled next_page'));
  164. }
  165. /**
  166. * Build the first two page links for a sliding page range.
  167. *
  168. * @return string
  169. */
  170. private function beginning()
  171. {
  172. return $this->range(1, 2).'<span class="dots">...</span>';
  173. }
  174. /**
  175. * Build the last two page links for a sliding page range.
  176. *
  177. * @return string
  178. */
  179. private function ending()
  180. {
  181. return '<span class="dots">...</span>'.$this->range($this->last_page - 1, $this->last_page);
  182. }
  183. /**
  184. * Build a range of page links.
  185. *
  186. * For the current page, an HTML span element will be generated instead of a link.
  187. *
  188. * @param int $start
  189. * @param int $end
  190. * @return string
  191. */
  192. private function range($start, $end)
  193. {
  194. $pages = '';
  195. for ($i = $start; $i <= $end; $i++)
  196. {
  197. $pages .= ($this->page == $i) ? HTML::span($i, array('class' => 'current')).' ' : $this->link($i, $i, null).' ';
  198. }
  199. return $pages;
  200. }
  201. /**
  202. * Create a HTML page link.
  203. *
  204. * @param int $page
  205. * @param string $text
  206. * @param string $attributes
  207. * @return string
  208. */
  209. private function link($page, $text, $class)
  210. {
  211. $append = '';
  212. foreach ($this->append as $key => $value)
  213. {
  214. $append .= '&'.$key.'='.$value;
  215. }
  216. return HTML::link(Request::uri().'?page='.$page.$append, $text, compact('class'), Request::secure());
  217. }
  218. /**
  219. * Set the language that should be used when generating page links.
  220. *
  221. * @param string $language
  222. * @return Paginator
  223. */
  224. public function lang($language)
  225. {
  226. $this->language = $language;
  227. return $this;
  228. }
  229. /**
  230. * Set the items that should be appended to the link query strings.
  231. *
  232. * @param array $values
  233. * @return Paginator
  234. */
  235. public function append($values)
  236. {
  237. $this->append = $values;
  238. return $this;
  239. }
  240. }