From 8cdc565df187eeb1b2677d8234ccd6372b5b90ff Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Fri, 31 May 2024 07:44:43 +0400 Subject: [PATCH] create view and controller for pagination component --- src/controllers/Pagination.php | 76 ++++++++++++++++++ src/views/Pagination.php | 136 +++++++++++++++++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 src/controllers/Pagination.php create mode 100644 src/views/Pagination.php diff --git a/src/controllers/Pagination.php b/src/controllers/Pagination.php new file mode 100644 index 0000000..4f5bec7 --- /dev/null +++ b/src/controllers/Pagination.php @@ -0,0 +1,76 @@ +items_per_page = $items_per_page; + $this->total_items = $total_items; + $this->current_page_number = $current_page; + } + + /** + * Returns a query string that maintains all current query string parameters, except page number. + * @return string Query string + */ + private function getCurrentQueryString(): string + { + // create a string with all past query parameters except page and url + unset($_GET['page']); + unset($_GET['url']); + + return '?' . http_build_query($_GET); + } + + /** + * @param array $array + * @return array New array containing only elements to be displayed on current page + */ + public function getCurrentItems(array $array): array + { + return array_slice( + $array, + ($this->current_page_number - 1) * $this->items_per_page, + $this->items_per_page + ); + } + + /** + * Returns HTML code need to display pagination items + * @return string + */ + public function getHTML(): string + { + $current_page_number = $this->current_page_number; + $total_pages = (int)ceil((float)$this->total_items / $this->items_per_page); + $query_string = $this->getCurrentQueryString(); + + $view_file_path = __DIR__ . '/../views/Pagination.php'; + $html = ''; + + // get content from view file + ob_start(); + include $view_file_path; + $html = ob_get_contents(); + ob_end_clean(); + + return $html; + } + + public function index(): void + { + // we don't want the page /pagination to be accessible + (new Error())->handlePageNotFoundError(); + } +} diff --git a/src/views/Pagination.php b/src/views/Pagination.php new file mode 100644 index 0000000..729d1cd --- /dev/null +++ b/src/views/Pagination.php @@ -0,0 +1,136 @@ + + $page_number + + EOL; +} + +/** + * Prints navigation button in HTML format + * @param int $current_page_number + * @param int $total_pages Total number of pages + * @param string $query_string + * @param bool $is_left True indicates left navigation button. + * @return void + */ +function displayNavigationButton(int $current_page_number, int $total_pages, string $query_string, bool $is_left): void +{ + $page_number = $current_page_number + ($is_left ? -1 : 1); + $page_link = $query_string . "&page=$page_number"; + + $link_content = htmlspecialchars($is_left ? "<" : ">"); + $className = "page-item"; + + if (($current_page_number === 1 && $is_left) || ($current_page_number === $total_pages && !$is_left)) { + $className .= " disabled"; + } + + echo <<< EOL +
  • + $link_content +
  • + EOL; +} + +?> + + + + \ No newline at end of file