PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/laravel/paginator.php

http://github.com/ericbarnes/Status-Board
PHP | 424 lines | 141 code | 46 blank | 237 comment | 11 complexity | 5dd38ce370c2972a23ccf4523aa8d875 MD5 | raw file
Possible License(s): MIT
  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 current page.
  11. *
  12. * @var int
  13. */
  14. public $page;
  15. /**
  16. * The last page available for the result set.
  17. *
  18. * @var int
  19. */
  20. public $last;
  21. /**
  22. * The total number of results.
  23. *
  24. * @var int
  25. */
  26. public $total;
  27. /**
  28. * The number of items per page.
  29. *
  30. * @var int
  31. */
  32. public $per_page;
  33. /**
  34. * The values that should be appended to the end of the link query strings.
  35. *
  36. * @var array
  37. */
  38. protected $appends;
  39. /**
  40. * The compiled appendage that will be appended to the links.
  41. *
  42. * This consists of a sprintf format with a page place-holder and query string.
  43. *
  44. * @var string
  45. */
  46. protected $appendage;
  47. /**
  48. * The language that should be used when creating the pagination links.
  49. *
  50. * @var string
  51. */
  52. protected $language;
  53. /**
  54. * The "dots" element used in the pagination slider.
  55. *
  56. * @var string
  57. */
  58. protected $dots = '<span class="dots">...</span>';
  59. /**
  60. * Create a new Paginator instance.
  61. *
  62. * @param array $results
  63. * @param int $page
  64. * @param int $total
  65. * @param int $per_page
  66. * @param int $last
  67. * @return void
  68. */
  69. protected function __construct($results, $page, $total, $per_page, $last)
  70. {
  71. $this->page = $page;
  72. $this->last = $last;
  73. $this->total = $total;
  74. $this->results = $results;
  75. $this->per_page = $per_page;
  76. }
  77. /**
  78. * Create a new Paginator instance.
  79. *
  80. * @param array $results
  81. * @param int $total
  82. * @param int $per_page
  83. * @return Paginator
  84. */
  85. public static function make($results, $total, $per_page)
  86. {
  87. $page = static::page($total, $per_page);
  88. $last = ceil($total / $per_page);
  89. return new static($results, $page, $total, $per_page, $last);
  90. }
  91. /**
  92. * Get the current page from the request query string.
  93. *
  94. * @param int $total
  95. * @param int $per_page
  96. * @return int
  97. */
  98. public static function page($total, $per_page)
  99. {
  100. $page = Input::get('page', 1);
  101. // The page will be validated and adjusted if it is less than one or greater
  102. // than the last page. For example, if the current page is not an integer or
  103. // less than one, one will be returned. If the current page is greater than
  104. // the last page, the last page will be returned.
  105. if (is_numeric($page) and $page > $last = ceil($total / $per_page))
  106. {
  107. return ($last > 0) ? $last : 1;
  108. }
  109. return (static::valid($page)) ? $page : 1;
  110. }
  111. /**
  112. * Determine if a given page number is a valid page.
  113. *
  114. * A valid page must be greater than or equal to one and a valid integer.
  115. *
  116. * @param int $page
  117. * @return bool
  118. */
  119. protected static function valid($page)
  120. {
  121. return $page >= 1 and filter_var($page, FILTER_VALIDATE_INT) !== false;
  122. }
  123. /**
  124. * Create the HTML pagination links.
  125. *
  126. * Typically, an intelligent, "sliding" window of links will be rendered based
  127. * on the total number of pages, the current page, and the number of adjacent
  128. * pages that should rendered. This creates a beautiful paginator similar to
  129. * that of Google's.
  130. *
  131. * Example: 1 2 ... 23 24 25 [26] 27 28 29 ... 51 52
  132. *
  133. * If you wish to render only certain elements of the pagination control,
  134. * explore some of the other public methods available on the instance.
  135. *
  136. * <code>
  137. * // Render the pagination links
  138. * echo $paginator->links();
  139. *
  140. * // Render the pagination links using a given window size
  141. * echo $paginator->links(5);
  142. * </code>
  143. *
  144. * @param int $adjacent
  145. * @return string
  146. */
  147. public function links($adjacent = 3)
  148. {
  149. if ($this->last <= 1) return '';
  150. // The hard-coded seven is to account for all of the constant elements in a
  151. // sliding range, such as the current page, the two ellipses, and the two
  152. // beginning and ending pages.
  153. //
  154. // If there are not enough pages to make the creation of a slider possible
  155. // based on the adjacent pages, we will simply display all of the pages.
  156. // Otherwise, we will create a "truncating" slider which displays a
  157. // nice window of pages based on the current page.
  158. if ($this->last < 7 + ($adjacent * 2))
  159. {
  160. $links = $this->range(1, $this->last);
  161. }
  162. else
  163. {
  164. $links = $this->slider($adjacent);
  165. }
  166. $content = $this->previous().' '.$links.' '.$this->next();
  167. return '<div class="pagination">'.$content.'</div>';
  168. }
  169. /**
  170. * Build sliding list of HTML numeric page links.
  171. *
  172. * This method is very similar to the "links" method, only it does not
  173. * render the "first" and "last" pagination links, but only the pages.
  174. *
  175. * <code>
  176. * // Render the pagination slider
  177. * echo $paginator->slider();
  178. *
  179. * // Render the pagination slider using a given window size
  180. * echo $paginator->slider(5);
  181. * </code>
  182. *
  183. * @param int $adjacent
  184. * @return string
  185. */
  186. public function slider($adjacent = 3)
  187. {
  188. $window = $adjacent * 2;
  189. // If the current page is so close to the beginning that we do not have
  190. // room to create a full sliding window, we will only show the first
  191. // several pages, followed by the ending section of the slider.
  192. //
  193. // Likewise, if the page is very close to the end, we will create the
  194. // beginning of the slider, but just show the last several pages at
  195. // the end of the slider.
  196. //
  197. // Example: 1 [2] 3 4 5 6 ... 23 24
  198. if ($this->page <= $window)
  199. {
  200. return $this->range(1, $window + 2).' '.$this->ending();
  201. }
  202. // Example: 1 2 ... 32 33 34 35 [36] 37
  203. elseif ($this->page >= $this->last - $window)
  204. {
  205. return $this->beginning().' '.$this->range($this->last - $window - 2, $this->last);
  206. }
  207. // Example: 1 2 ... 23 24 25 [26] 27 28 29 ... 51 52
  208. $content = $this->range($this->page - $adjacent, $this->page + $adjacent);
  209. return $this->beginning().' '.$content.' '.$this->ending();
  210. }
  211. /**
  212. * Generate the "previous" HTML link.
  213. *
  214. * <code>
  215. * // Create the "previous" pagination element
  216. * echo $paginator->previous();
  217. *
  218. * // Create the "previous" pagination element with custom text
  219. * echo $paginator->previous('Go Back');
  220. * </code>
  221. *
  222. * @param string $text
  223. * @return string
  224. */
  225. public function previous($text = null)
  226. {
  227. $disabled = function($page) { return $page <= 1; };
  228. return $this->element(__FUNCTION__, $this->page - 1, $text, $disabled);
  229. }
  230. /**
  231. * Generate the "next" HTML link.
  232. *
  233. * <code>
  234. * // Create the "next" pagination element
  235. * echo $paginator->next();
  236. *
  237. * // Create the "next" pagination element with custom text
  238. * echo $paginator->next('Skip Forwards');
  239. * </code>
  240. *
  241. * @param string $text
  242. * @return string
  243. */
  244. public function next($text = null)
  245. {
  246. $disabled = function($page, $last) { return $page >= $last; };
  247. return $this->element(__FUNCTION__, $this->page + 1, $text, $disabled);
  248. }
  249. /**
  250. * Create a chronological pagination element, such as a "previous" or "next" link.
  251. *
  252. * @param string $element
  253. * @param int $page
  254. * @param string $text
  255. * @param Closure $disabled
  256. * @return string
  257. */
  258. protected function element($element, $page, $text, $disabled)
  259. {
  260. $class = "{$element}_page";
  261. if (is_null($text))
  262. {
  263. $text = Lang::line("pagination.{$element}")->get($this->language);
  264. }
  265. // Each consumer of this method provides a "disabled" Closure which can
  266. // be used to determine if the element should be a span element or an
  267. // actual link. For example, if the current page is the first page,
  268. // the "first" element should be a span instead of a link.
  269. if ($disabled($this->page, $this->last))
  270. {
  271. return HTML::span($text, array('class' => "{$class} disabled"));
  272. }
  273. else
  274. {
  275. return $this->link($page, $text, $class);
  276. }
  277. }
  278. /**
  279. * Build the first two page links for a sliding page range.
  280. *
  281. * @return string
  282. */
  283. protected function beginning()
  284. {
  285. return $this->range(1, 2).' '.$this->dots;
  286. }
  287. /**
  288. * Build the last two page links for a sliding page range.
  289. *
  290. * @return string
  291. */
  292. protected function ending()
  293. {
  294. return $this->dots.' '.$this->range($this->last - 1, $this->last);
  295. }
  296. /**
  297. * Build a range of numeric pagination links.
  298. *
  299. * For the current page, an HTML span element will be generated instead of a link.
  300. *
  301. * @param int $start
  302. * @param int $end
  303. * @return string
  304. */
  305. protected function range($start, $end)
  306. {
  307. $pages = array();
  308. // To generate the range of page links, we will iterate through each page
  309. // and, if the current page matches the page, we will generate a span,
  310. // otherwise we will generate a link for the page. The span elements
  311. // will be assigned the "current" CSS class for convenient styling.
  312. for ($page = $start; $page <= $end; $page++)
  313. {
  314. if ($this->page == $page)
  315. {
  316. $pages[] = HTML::span($page, array('class' => 'current'));
  317. }
  318. else
  319. {
  320. $pages[] = $this->link($page, $page, null);
  321. }
  322. }
  323. return implode(' ', $pages);
  324. }
  325. /**
  326. * Create a HTML page link.
  327. *
  328. * @param int $page
  329. * @param string $text
  330. * @param string $class
  331. * @return string
  332. */
  333. protected function link($page, $text, $class)
  334. {
  335. $query = '?page='.$page.$this->appendage($this->appends);
  336. return HTML::link(URI::current().$query, $text, compact('class'), Request::secure());
  337. }
  338. /**
  339. * Create the "appendage" to be attached to every pagination link.
  340. *
  341. * @param array $appends
  342. * @return string
  343. */
  344. protected function appendage($appends)
  345. {
  346. // The developer may assign an array of values that will be converted to a
  347. // query string and attached to every pagination link. This allows simple
  348. // implementation of sorting or other things the developer may need.
  349. if ( ! is_null($this->appendage)) return $this->appendage;
  350. if (count($appends) <= 0)
  351. {
  352. return $this->appendage = '';
  353. }
  354. return $this->appendage = '&'.http_build_query($appends);
  355. }
  356. /**
  357. * Set the items that should be appended to the link query strings.
  358. *
  359. * @param array $values
  360. * @return Paginator
  361. */
  362. public function appends($values)
  363. {
  364. $this->appends = $values;
  365. return $this;
  366. }
  367. /**
  368. * Set the language that should be used when creating the pagination links.
  369. *
  370. * @param string $language
  371. * @return Paginator
  372. */
  373. public function speaks($language)
  374. {
  375. $this->language = $language;
  376. return $this;
  377. }
  378. }