From 09eaa0115e89efcfca675673f0a1d8df3b28a8ff Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Fri, 31 May 2024 07:47:27 +0400 Subject: [PATCH] use pagination component --- src/controllers/Product.php | 35 ++++++-------- src/controllers/Shop.php | 43 +++++++---------- src/views/Product.php | 92 ++----------------------------------- src/views/Shop.php | 91 +----------------------------------- 4 files changed, 35 insertions(+), 226 deletions(-) diff --git a/src/controllers/Product.php b/src/controllers/Product.php index ee45971..25270dd 100644 --- a/src/controllers/Product.php +++ b/src/controllers/Product.php @@ -6,7 +6,6 @@ use Steamy\Core\Controller; use Steamy\Core\Utility; -use Steamy\Model\Client; use Steamy\Model\Comment; use Steamy\Model\Review; use Steamy\Model\User; @@ -233,7 +232,7 @@ 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. */ @@ -242,20 +241,6 @@ 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 { return preg_match("/^shop\/products\/[0-9]+$/", Utility::getURL()) === 1; @@ -294,18 +279,24 @@ public function index(): void $this->handleCommentSubmission(); } - $this->view_data['product_reviews'] = array_filter( + // get all reviews that match criteria + $all_matching_reviews = array_filter( $this->view_data['product_reviews'], array($this, "filterReviews") ); - // Slice the reviews based on pagination - $paginated_reviews = $this->applyPagination($this->view_data['product_reviews']); + $pagination_controller = new Pagination( + Product::$MAX_REVIEWS_PER_PAGE, + count($all_matching_reviews), + $this->getPageNumber() + ); + + // get html code for displaying pagination + $this->view_data['review_pagination'] = $pagination_controller->getHTML(); + + $this->view_data['product_reviews'] = $pagination_controller->getCurrentItems($all_matching_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/controllers/Shop.php b/src/controllers/Shop.php index 69a4c38..d04ca5b 100644 --- a/src/controllers/Shop.php +++ b/src/controllers/Shop.php @@ -16,7 +16,7 @@ class Shop { use Controller; - private array $data; + private array $view_data; private static int $MAX_PRODUCTS_PER_PAGE = 4; /** @@ -154,20 +154,6 @@ public function getPageNumber(): int return (int)($_GET['page'] ?? 1); } - /** - * @param $products - * @return array Products which should be displayed on current page - */ - public function applyPagination($products): array - { - // Slice the products based on pagination - return array_slice( - $products, - ($this->getPageNumber() - 1) * Shop::$MAX_PRODUCTS_PER_PAGE, - Shop::$MAX_PRODUCTS_PER_PAGE - ); - } - public function index(): void { // check if URL follows format /shop/products/ @@ -187,22 +173,27 @@ public function index(): void // get all products that match user criteria $filtered_products = $this->getMatchingProducts(); - // Slice the products based on pagination - $paginated_products = $this->applyPagination($filtered_products); + // get html for pagination + $pagination_controller = new Pagination( + Shop::$MAX_PRODUCTS_PER_PAGE, + count($filtered_products), + $this->getPageNumber() + ); + + $this->view_data['pagination'] = $pagination_controller->getHTML(); + $this->view_data['products'] = $pagination_controller->getCurrentItems($filtered_products); - // Initialize view variables (existing functionality) - $this->data['products'] = $paginated_products; - $this->data['search_keyword'] = $_GET['keyword'] ?? ""; - $this->data['categories'] = Product::getCategories(); - $this->data['sort_option'] = $_GET['sort'] ?? ""; - $this->data['selected_categories'] = $_GET['categories'] ?? []; - $this->data['current_page_number'] = $this->getPageNumber(); - $this->data['total_pages'] = (int)ceil(count($filtered_products) / Shop::$MAX_PRODUCTS_PER_PAGE); + // Initialize view variables + $this->view_data['search_keyword'] = $_GET['keyword'] ?? ""; + $this->view_data['categories'] = Product::getCategories(); + $this->view_data['sort_option'] = $_GET['sort'] ?? ""; + $this->view_data['selected_categories'] = $_GET['categories'] ?? []; + $this->view_data['current_page_number'] = $this->getPageNumber(); // Render the view with pagination information $this->view( 'Shop', - $this->data, + $this->view_data, 'Shop', template_tags: $this->getLibrariesTags(['aos']), template_meta_description: "Explore a delightful selection of aromatic coffees, teas, and delectable diff --git a/src/views/Product.php b/src/views/Product.php index 564cd4f..c177424 100644 --- a/src/views/Product.php +++ b/src/views/Product.php @@ -3,8 +3,6 @@ declare(strict_types=1); /** - * @var $current_page_number Current page number. - * @var $total_pages Total number of pages * @var $product Product product information * @var $product_reviews Review[] List of product reviews to be displayed with any filters applied * @var $current_review_filter string @@ -13,6 +11,7 @@ * @var $default_rating int default rating in form * @var $rating_distribution string An array containing the percentages of ratings * @var $comment_form_info ?array Array with information to be displayed on comment form + * @var $review_pagination string HTML code for review pagination */ use Steamy\Core\Utility; @@ -191,69 +190,8 @@ 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; -} - ?> +
    -