| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace App;
- class Pagination {
- protected $pageSize = 25;
- protected $currentPage = 1;
- protected $itemCount = 0;
- protected $width = 3;
- protected $single;
- protected $plural;
- protected $parameters = [];
- protected $isJsHandled = false;
- /**
- * Pagination constructor.
- * @param int $item_count
- */
- public function __construct(int $item_count) {
- $this->itemCount = $item_count;
- $this->setTerms(__("%s result"), __("%s results"));
- }
- public function setItemCount(int $count): bool {
- $this->itemCount = $count;
- $this->currentPage = max(min($this->currentPage, $this->getPageCount()), 1);
- return true;
- }
- public function setTerms(string $single, string $plural): bool {
- $this->single = $single;
- $this->plural = $plural;
- return true;
- }
- public function getOffset(): int {
- return $this->pageSize * ($this->currentPage - 1);
- }
- public function slice(array $array): array {
- return array_slice($array, $this->getOffset(), $this->pageSize);
- }
- private function from(): int {
- return $this->getOffset() + 1;
- }
- private function to(): int {
- return min($this->getOffset() + $this->pageSize, $this->itemCount);
- }
- private function getCurrentPage(): int {
- return $this->currentPage;
- }
- private function getPageCount(): int {
- return (int) ceil($this->itemCount / $this->pageSize) ?: 1;
- }
- public function getPageText(): string {
- return __("Page %d of %d", $this->getCurrentPage(), $this->getPageCount());
- }
- private function hasPreviousPage(): bool {
- return $this->currentPage > 1;
- }
- private function hasNextPage(): bool {
- return $this->currentPage < $this->getPageCount();
- }
- private function urlForPage(int $page_number): string {
- $query = $_GET;
- $query = array_merge($query, $this->parameters);
- $query["page"] = $page_number;
- return $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["SERVER_NAME"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH) ."?". http_build_query($query);
- }
- public function getText(): string {
- $count_text = n__($this->single, $this->plural, $this->itemCount, number_format($this->itemCount));
- if($this->getPageCount() <= 1) {
- return sprintf("Showing %s", $count_text);
- }
- return sprintf("Showing %s-%s of %s",
- number_format($this->from()),
- number_format($this->to()),
- $count_text
- );
- }
- public function requiresPagination(): bool {
- return $this->getPageCount() > 1;
- }
- /**
- * @param array $options <b>container_class</b> Pagination container CSS class (default: pagination justify-content-center)<br />
- * <b>item_class</b> Item container CSS class (default: page-item)<br />
- * <b>next</b> Text for next link (default: Next)<br />
- * <b>previous</b> Text for previous link (default: Previous)
- * @return string
- */
- public function build(array $options = []): string {
- $container_class = $options["container_class"] ?? "pagination justify-content-center";
- $item_class = $options["item_class"] ?? "page-item";
- $first_text = '<i class="fa fa-angle-double-left"></i>';
- $previous_text = '<i class="fa fa-angle-left"></i>';
- $next_text = '<i class="fa fa-angle-right"></i>';
- $last_text = '<i class="fa fa-angle-double-right"></i>';
- $current_page = $this->getCurrentPage();
- $min_page = max($current_page - $this->width, 1);
- $max_page = min($current_page + $this->width, $this->getPageCount());
- $previous_page = $current_page - 1;
- $next_page = $current_page + 1;
- $output = "";
- // Page count is 1 or less
- if(!$this->requiresPagination()) return "";
- // Start building output
- $get_page_link_html = function(string $text, int $page) use ($item_class) {
- if($this->isJsHandled) {
- return "<li class=\"{$item_class} position-relative\"><a class=\"page-link stretched-link\" data-page=\"{$page}\" style=\"cursor: pointer\">{$text}</a></li>";
- }
- 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>";
- };
- $get_disabled_link_html = static function(string $text) use ($item_class) {
- return "<li class=\"{$item_class} disabled\"><span class=\"page-link\">{$text}</span></li>";
- };
- $get_active_link_html = function() use ($item_class) {
- return "<li class=\"{$item_class} active\"><span class=\"page-link\">{$this->getCurrentPage()}</span></li>";
- };
- $output .= "<ul class=\"{$container_class}\">";
- if($this->hasPreviousPage()) {
- $output .= $get_page_link_html($first_text, 1);
- $output .= $get_page_link_html($previous_text, $previous_page);
- }
- else {
- $output .= $get_disabled_link_html($previous_text);
- }
- for($i = $current_page - $this->width; $i <= $current_page + $this->width; $i++) {
- if(($i < $min_page) || ($i > $max_page)) {
- continue;
- }
- if($i === $this->getCurrentPage()) {
- $output .= $get_active_link_html();
- }
- else {
- $output .= $get_page_link_html($i, $i);
- }
- }
- if($this->hasNextPage()) {
- $output .= $get_page_link_html($next_text, $next_page);
- $output .= $get_page_link_html($last_text, $this->getPageCount());
- }
- else {
- $output .= $get_disabled_link_html($next_text);
- }
- $output .= "</ul>";
- return $output;
- }
- public function addParameter(string $name, ?string $value) {
- if(!$value) return;
- $this->parameters[$name] = $value;
- }
- public function setPage(?int $page) {
- if($page < 1) $page = 1;
- $this->currentPage = $page;
- }
- public function setPageSize(int $size) {
- $this->pageSize = $size;
- }
- /**
- * @param bool $js_handled
- */
- public function setIsJsHandled(bool $js_handled): void {
- $this->isJsHandled = $js_handled;
- }
- }
|