Pagination.php 5.9 KB

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