From 20c24685ac6edbbd881c4b459e36ea8929df5a0e Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Wed, 22 May 2024 21:26:00 +0400 Subject: [PATCH 1/7] add pagination to product reviews, including controller logic and view updates --- src/controllers/Product.php | 29 ++++++++++++ src/views/Product.php | 88 +++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/src/controllers/Product.php b/src/controllers/Product.php index 8a2d609..1e7aec9 100644 --- a/src/controllers/Product.php +++ b/src/controllers/Product.php @@ -19,6 +19,8 @@ class Product { use Controller; + private static int $MAX_REVIEWS_PER_PAGE = 2; + private ?ProductModel $product = null; // product to be displayed private array $view_data; private ?User $signed_user; // currently logged-in user @@ -231,6 +233,27 @@ private function showCommentForm(): void $this->view_data['comment_form_info'] ['quote_date'] = $comment->getCreatedDate()->format('Y'); } } + /** + * @return int Page number on shop page. Defaults to 1. + */ + public function getPageNumber(): int + { + return (int)($_GET['page'] ?? 1); + } + + /** + * @param $reviews + * @return array Reviews which should be displayed on current page + */ + public function applyPagination($reviews): array + { + // Slice the products based on pagination + return array_slice( + $reviews, + ($this->getPageNumber() - 1) * Product::$MAX_REVIEWS_PER_PAGE, + Product::$MAX_REVIEWS_PER_PAGE + ); + } private function validateURL(): bool { @@ -275,7 +298,13 @@ public function index(): void array($this, "filterReviews") ); + // Slice the reviews based on pagination + $paginated_reviews = $this->applyPagination($this->view_data['product_reviews']); + + $this->view_data['product_reviews'] = $paginated_reviews; $this->view_data['rating_distribution'] = $this->formatRatingDistribution(); + $this->view_data['current_page_number'] = $this->getPageNumber(); + $this->view_data['total_pages'] = (int)ceil(count($this->view_data['product_reviews']) / Product::$MAX_REVIEWS_PER_PAGE); $this->view( 'Product', diff --git a/src/views/Product.php b/src/views/Product.php index c1fd697..d5c7544 100644 --- a/src/views/Product.php +++ b/src/views/Product.php @@ -189,6 +189,68 @@ function printComment(StdClass $comment): void EOL; } +/** + * Returns a query string that maintains all current query string parameters and page number. + * @param int $page_number + * @return string Query string link for page item + */ +function getPageItemLink(int $page_number): string +{ + // create a string with all past query parameters except page and url + unset($_GET['page']); + unset($_GET['url']); + + $link = '?' . http_build_query($_GET); + + // add page number as query parameter + $link .= '&page=' . $page_number; + + return $link; +} + +/** + * Prints page item in HTML format. + * + * @param int $current_page_number + * @param int $page_number Page number of page item + * @return void + */ +function displayPageItem(int $current_page_number, int $page_number): void +{ + $page_link = getPageItemLink($page_number); + $className = "page-item" . ($page_number === $current_page_number ? " active" : ""); + + echo <<< EOL +
  • + $page_number +
  • + EOL; +} + +/** + * Prints navigation button in HTML format + * @param int $current_page_number + * @param int $total_pages Total number of pages + * @param bool $is_left True indicates left navigation button. + * @return void + */ +function displayNavigationButton(int $current_page_number, int $total_pages, bool $is_left): void +{ + $page_link = getPageItemLink($current_page_number + ($is_left ? -1 : 1)); + $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; +} + ?>
    @@ -388,6 +450,7 @@ class="close" +
    @@ -481,29 +418,6 @@ class="close" - + \ No newline at end of file diff --git a/src/views/Shop.php b/src/views/Shop.php index c1200bf..d3f3e0f 100644 --- a/src/views/Shop.php +++ b/src/views/Shop.php @@ -10,8 +10,7 @@ * @var string[] $selected_categories Array of selected categories * @var string $search_keyword keyword used to filter products * @var string $sort_option Sort By option selected by user - * @var int $current_page_number Current page number. - * @var int $total_pages Total number of pages + * @var string $pagination HTML code pagination */ use Steamy\Model\Product; @@ -40,69 +39,6 @@ function displayProduct(Product $product): void EOL; } - -/** - * Returns a query string that maintains all current query string parameters and page number. - * @param int $page_number - * @return string Query string link for page item - */ -function getPageItemLink(int $page_number): string -{ - // create a string with all past query parameters except page and url - unset($_GET['page']); - unset($_GET['url']); - - $link = '?' . http_build_query($_GET); - - // add page number as query parameter - $link .= '&page=' . $page_number; - - return $link; -} - -/** - * Prints page item in HTML format. - * - * @param int $current_page_number - * @param int $page_number Page number of page item - * @return void - */ -function displayPageItem(int $current_page_number, int $page_number): void -{ - $page_link = getPageItemLink($page_number); - $className = "page-item" . ($page_number === $current_page_number ? " active" : ""); - - echo <<< EOL -
  • - $page_number -
  • - EOL; -} - -/** - * Prints navigation button in HTML format - * @param int $current_page_number - * @param int $total_pages Total number of pages - * @param bool $is_left True indicates left navigation button. - * @return void - */ -function displayNavigationButton(int $current_page_number, int $total_pages, bool $is_left): void -{ - $page_link = getPageItemLink($current_page_number + ($is_left ? -1 : 1)); - $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; -} - ?> @@ -161,30 +97,7 @@ function displayNavigationButton(int $current_page_number, int $total_pages, boo - +