Pagination.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App;
  3. class Pagination {
  4. protected $pageSize = 25;
  5. protected $currentPage = 1;
  6. protected $itemCount = 0;
  7. protected $width = 3;
  8. protected $single;
  9. protected $plural;
  10. protected $parameters = [];
  11. protected $isJsHandled = false;
  12. /**
  13. * Pagination constructor.
  14. * @param int $item_count
  15. */
  16. public function __construct(int $item_count) {
  17. $this->itemCount = $item_count;
  18. $this->setTerms(__("%s result"), __("%s results"));
  19. }
  20. public function setItemCount(int $count): bool {
  21. $this->itemCount = $count;
  22. $this->currentPage = max(min($this->currentPage, $this->getPageCount()), 1);
  23. return true;
  24. }
  25. public function setTerms(string $single, string $plural): bool {
  26. $this->single = $single;
  27. $this->plural = $plural;
  28. return true;
  29. }
  30. public function getOffset(): int {
  31. return $this->pageSize * ($this->currentPage - 1);
  32. }
  33. public function slice(array $array): array {
  34. return array_slice($array, $this->getOffset(), $this->pageSize);
  35. }
  36. private function from(): int {
  37. return $this->getOffset() + 1;
  38. }
  39. private function to(): int {
  40. return min($this->getOffset() + $this->pageSize, $this->itemCount);
  41. }
  42. private function getCurrentPage(): int {
  43. return $this->currentPage;
  44. }
  45. private function getPageCount(): int {
  46. return (int) ceil($this->itemCount / $this->pageSize) ?: 1;
  47. }
  48. public function getPageText(): string {
  49. return __("Page %d of %d", $this->getCurrentPage(), $this->getPageCount());
  50. }
  51. private function hasPreviousPage(): bool {
  52. return $this->currentPage > 1;
  53. }
  54. private function hasNextPage(): bool {
  55. return $this->currentPage < $this->getPageCount();
  56. }
  57. private function urlForPage(int $page_number): string {
  58. $query = $_GET;
  59. $query = array_merge($query, $this->parameters);
  60. $query["page"] = $page_number;
  61. return $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["SERVER_NAME"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH) ."?". http_build_query($query);
  62. }
  63. public function getText(): string {
  64. $count_text = n__($this->single, $this->plural, $this->itemCount, number_format($this->itemCount));
  65. if($this->getPageCount() <= 1) {
  66. return sprintf("Showing %s", $count_text);
  67. }
  68. return sprintf("Showing %s-%s of %s",
  69. number_format($this->from()),
  70. number_format($this->to()),
  71. $count_text
  72. );
  73. }
  74. public function requiresPagination(): bool {
  75. return $this->getPageCount() > 1;
  76. }
  77. /**
  78. * @param array $options <b>container_class</b> Pagination container CSS class (default: pagination justify-content-center)<br />
  79. * <b>item_class</b> Item container CSS class (default: page-item)<br />
  80. * <b>next</b> Text for next link (default: Next)<br />
  81. * <b>previous</b> Text for previous link (default: Previous)
  82. * @return string
  83. */
  84. public function build(array $options = []): string {
  85. $container_class = $options["container_class"] ?? "pagination justify-content-center";
  86. $item_class = $options["item_class"] ?? "page-item";
  87. $first_text = '<i class="fa fa-angle-double-left"></i>';
  88. $previous_text = '<i class="fa fa-angle-left"></i>';
  89. $next_text = '<i class="fa fa-angle-right"></i>';
  90. $last_text = '<i class="fa fa-angle-double-right"></i>';
  91. $current_page = $this->getCurrentPage();
  92. $min_page = max($current_page - $this->width, 1);
  93. $max_page = min($current_page + $this->width, $this->getPageCount());
  94. $previous_page = $current_page - 1;
  95. $next_page = $current_page + 1;
  96. $output = "";
  97. // Page count is 1 or less
  98. if(!$this->requiresPagination()) return "";
  99. // Start building output
  100. $get_page_link_html = function(string $text, int $page) use ($item_class) {
  101. if($this->isJsHandled) {
  102. return "<li class=\"{$item_class} position-relative\"><a class=\"page-link stretched-link\" data-page=\"{$page}\" style=\"cursor: pointer\">{$text}</a></li>";
  103. }
  104. return "<li class=\"{$item_class} position-relative\"><a class=\"page-link stretched-link\" href=\"{$this->urlForPage($page)}\" data-page=\"{$page}\" data-ajax-load>{$text}</a></li>";
  105. };
  106. $get_disabled_link_html = static function(string $text) use ($item_class) {
  107. return "<li class=\"{$item_class} disabled\"><span class=\"page-link\">{$text}</span></li>";
  108. };
  109. $get_active_link_html = function() use ($item_class) {
  110. return "<li class=\"{$item_class} active\"><span class=\"page-link\">{$this->getCurrentPage()}</span></li>";
  111. };
  112. $output .= "<ul class=\"{$container_class}\">";
  113. if($this->hasPreviousPage()) {
  114. $output .= $get_page_link_html($first_text, 1);
  115. $output .= $get_page_link_html($previous_text, $previous_page);
  116. }
  117. else {
  118. $output .= $get_disabled_link_html($previous_text);
  119. }
  120. for($i = $current_page - $this->width; $i <= $current_page + $this->width; $i++) {
  121. if(($i < $min_page) || ($i > $max_page)) {
  122. continue;
  123. }
  124. if($i === $this->getCurrentPage()) {
  125. $output .= $get_active_link_html();
  126. }
  127. else {
  128. $output .= $get_page_link_html($i, $i);
  129. }
  130. }
  131. if($this->hasNextPage()) {
  132. $output .= $get_page_link_html($next_text, $next_page);
  133. $output .= $get_page_link_html($last_text, $this->getPageCount());
  134. }
  135. else {
  136. $output .= $get_disabled_link_html($next_text);
  137. }
  138. $output .= "</ul>";
  139. return $output;
  140. }
  141. public function addParameter(string $name, ?string $value) {
  142. if(!$value) return;
  143. $this->parameters[$name] = $value;
  144. }
  145. public function setPage(?int $page) {
  146. if($page < 1) $page = 1;
  147. $this->currentPage = $page;
  148. }
  149. public function setPageSize(int $size) {
  150. $this->pageSize = $size;
  151. }
  152. /**
  153. * @param bool $js_handled
  154. */
  155. public function setIsJsHandled(bool $js_handled): void {
  156. $this->isJsHandled = $js_handled;
  157. }
  158. }