From 20c24685ac6edbbd881c4b459e36ea8929df5a0e Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Wed, 22 May 2024 21:26:00 +0400 Subject: [PATCH 01/15] 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" +
    - - + +
    From 242c6d6fa485a9bafb9fc132930639673bb970d6 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Thu, 30 May 2024 19:58:44 +0400 Subject: [PATCH 07/15] use better variable names, fix bug with htmlspecialchars() --- src/views/Orders.php | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/views/Orders.php b/src/views/Orders.php index ee3839e..a51e937 100644 --- a/src/views/Orders.php +++ b/src/views/Orders.php @@ -3,24 +3,25 @@ declare(strict_types=1); /** - * @var Order $orders - * @var OrderProduct $orderproduct + * @var Order $order Current order + * @var OrderProduct[] $line_items Line items for current order */ use Steamy\Model\Order; use Steamy\Model\OrderProduct; + ?>
    -

    Order #getOrderID(), FILTER_SANITIZE_NUMBER_INT); ?>

    +

    Order #getOrderID(), FILTER_SANITIZE_NUMBER_INT); ?>

    Order Details

    -

    Order ID: getOrderID(), FILTER_SANITIZE_NUMBER_INT); ?>

    -

    Date: getCreatedDate()->format('Y-m-d H:i:s')) ?>

    -

    Status: getStatus()->value)) ?>

    -

    Total Price: $calculateTotalPrice(), 2)) ?>

    +

    Order ID: getOrderID(), FILTER_SANITIZE_NUMBER_INT); ?>

    +

    Date: getCreatedDate()->format('Y-m-d H:i:s')) ?>

    +

    Status: getStatus()->value)) ?>

    +

    Total Price: $calculateTotalPrice(), 2)) ?>

    - +

    Order Items

    @@ -29,18 +30,19 @@ - + + foreach ($line_items as $item): ?> - + - + - +
    Quantity Milk Type Cup SizePriceUnit Price
    getProductName()) ?>getQuantity()) ?>getQuantity(), FILTER_SANITIZE_NUMBER_INT) ?> getMilkType()) ?> getCupSize()) ?>$getPrice(), 2)) ?>$getUnitPrice(), 2)) ?>
    From 401db2bb77f28ff9c8a618d2462941ca0d98b7d8 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Thu, 30 May 2024 19:59:19 +0400 Subject: [PATCH 08/15] format file, update view data variable names, fetch line items appropriately --- src/controllers/Orders.php | 42 ++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/controllers/Orders.php b/src/controllers/Orders.php index ac65f75..1cc1205 100644 --- a/src/controllers/Orders.php +++ b/src/controllers/Orders.php @@ -17,27 +17,25 @@ class Orders private function validateURL(): bool { - $url = Utility::getURL(); - $parts = explode('/', $url); - // Check if the URL matches the expected pattern - $isValidURL = preg_match('/^orders\/\d+$/', $url) === 1; - return $isValidURL; + $url = Utility::getURL(); + // Check if the URL matches the expected pattern + return preg_match('/^orders\/\d+$/', $url) === 1; } private function getOrderIDFromURL(): ?int { - if ($this->validateURL()) { - $url = Utility::getURL(); - $parts = explode('/', $url); - // Check if the last part of the URL is a valid integer - $lastPart = end($parts); - if (is_numeric($lastPart)) { - return (int)$lastPart; - } else { - return null; + if ($this->validateURL()) { + $url = Utility::getURL(); + $parts = explode('/', $url); + // Check if the last part of the URL is a valid integer + $lastPart = end($parts); + if (is_numeric($lastPart)) { + return (int)$lastPart; + } else { + return null; + } } - } - return null; + return null; } @@ -56,24 +54,24 @@ public function index(): void $order_id = $this->getOrderIDFromURL(); if ($order_id === null) { (new Error())->handlePageNotFoundError(); - die(); + return; } $order = Order::getByID($order_id); if (!$order) { (new Error())->handlePageNotFoundError(); - die(); + return; } - $order_products = OrderProduct::getByID($order_id); + $order_products = Order::getOrderProducts($order->getOrderID()); - $this->view_data['orders'] = $order; - $this->view_data['orderproduct'] = $order_products; + $this->view_data['order'] = $order; + $this->view_data['line_items'] = $order_products; $this->view( 'orders', $this->view_data, - 'Order', + 'Order #' . $order_id, enableIndexing: false ); } 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 09/15] 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 From 0e8c4ddaacc668e02a34c2e8115672b449aa6a1a Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Fri, 31 May 2024 07:45:37 +0400 Subject: [PATCH 10/15] remove css for pagination --- public/styles/views/Shop.css | 48 ------------------------------------ 1 file changed, 48 deletions(-) diff --git a/public/styles/views/Shop.css b/public/styles/views/Shop.css index bd9d5dd..f82c1f1 100644 --- a/public/styles/views/Shop.css +++ b/public/styles/views/Shop.css @@ -61,52 +61,4 @@ article header { #item-grid { grid-template-columns: repeat(1, 1fr); } -} - -.pagination { - display: flex; - list-style: none; - border-radius: 0.25rem; - gap: 0.45rem; - margin-top: 2cm; -} - - -.page-item { - --bs-padding-x: 0.5rem; - --bs-padding-y: 0.25rem; -} - -.page-link { - position: relative; - display: block; - padding: var(--bs-padding-y) var(--bs-padding-x); - text-decoration: none; - transition: color .25s ease-in-out, background-color .25s ease-in-out; - outline: 1px solid #dee2e6; -} - -.page-link:hover { - z-index: 2; - background-color: var(--contrast-hover); - color: var(--contrast-inverse); -} - -.page-link:focus { - z-index: 3; - outline: 0; - box-shadow: 0 0 0.25rem rgba(0, 0, 0, 0.25); -} - -.page-item.active .page-link { - z-index: 3; - background-color: var(--contrast); - color: var(--contrast-inverse); -} - -.page-item.disabled .page-link { - color: var(--form-element-disabled-opacity); - outline-color: var(--form-element-disabled-border-color); - pointer-events: none; - background-color: var(--form-element-disabled-background-color); } \ No newline at end of file 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 11/15] 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; -} - ?> +
    -