/system/library/pagination.php

https://bitbucket.org/jjasko/opencart_serbian · PHP · 97 lines · 81 code · 16 blank · 0 comment · 17 complexity · d132d93704987076da9515b78eb5bc85 MD5 · raw file

  1. <?php
  2. final class Pagination {
  3. public $total = 0;
  4. public $page = 1;
  5. public $limit = 20;
  6. public $num_links = 10;
  7. public $url = '';
  8. public $text = 'Showing {start} to {end} of {total} ({pages} Pages)';
  9. public $text_first = '|&lt;';
  10. public $text_last = '&gt;|';
  11. public $text_next = '&gt;';
  12. public $text_prev = '&lt;';
  13. public $style_links = 'links';
  14. public $style_results = 'results';
  15. public function render() {
  16. $total = $this->total;
  17. if ($this->page < 1) {
  18. $page = 1;
  19. } else {
  20. $page = $this->page;
  21. }
  22. if (!$this->limit) {
  23. $limit = 10;
  24. } else {
  25. $limit = $this->limit;
  26. }
  27. $num_links = $this->num_links;
  28. $num_pages = ceil($total / $limit);
  29. $output = '';
  30. if ($page > 1) {
  31. $output .= ' <a href="' . str_replace('{page}', 1, $this->url) . '">' . $this->text_first . '</a> <a href="' . str_replace('{page}', $page - 1, $this->url) . '">' . $this->text_prev . '</a> ';
  32. }
  33. if ($num_pages > 1) {
  34. if ($num_pages <= $num_links) {
  35. $start = 1;
  36. $end = $num_pages;
  37. } else {
  38. $start = $page - floor($num_links / 2);
  39. $end = $page + floor($num_links / 2);
  40. if ($start < 1) {
  41. $end += abs($start) + 1;
  42. $start = 1;
  43. }
  44. if ($end > $num_pages) {
  45. $start -= ($end - $num_pages);
  46. $end = $num_pages;
  47. }
  48. }
  49. if ($start > 1) {
  50. $output .= ' .... ';
  51. }
  52. for ($i = $start; $i <= $end; $i++) {
  53. if ($page == $i) {
  54. $output .= ' <b>' . $i . '</b> ';
  55. } else {
  56. $output .= ' <a href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a> ';
  57. }
  58. }
  59. if ($end < $num_pages) {
  60. $output .= ' .... ';
  61. }
  62. }
  63. if ($page < $num_pages) {
  64. $output .= ' <a href="' . str_replace('{page}', $page + 1, $this->url) . '">' . $this->text_next . '</a> <a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a> ';
  65. }
  66. $find = array(
  67. '{start}',
  68. '{end}',
  69. '{total}',
  70. '{pages}'
  71. );
  72. $replace = array(
  73. ($total) ? (($page - 1) * $limit) + 1 : 0,
  74. ((($page - 1) * $limit) > ($total - $limit)) ? $total : ((($page - 1) * $limit) + $limit),
  75. $total,
  76. $num_pages
  77. );
  78. return ($output ? '<div class="' . $this->style_links . '">' . $output . '</div>' : '') . '<div class="' . $this->style_results . '">' . str_replace($find, $replace, $this->text) . '</div>';
  79. }
  80. }
  81. ?>