Skip to content

Commit

Permalink
use pagination component
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed May 31, 2024
1 parent 0e8c4dd commit 09eaa01
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 226 deletions.
35 changes: 13 additions & 22 deletions src/controllers/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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;
Expand Down Expand Up @@ -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',
Expand Down
43 changes: 17 additions & 26 deletions src/controllers/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Shop
{
use Controller;

private array $data;
private array $view_data;
private static int $MAX_PRODUCTS_PER_PAGE = 4;

/**
Expand Down Expand Up @@ -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/<number>
Expand All @@ -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
Expand Down
92 changes: 3 additions & 89 deletions src/views/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
<li class="$className">
<a class="page-link" href="$page_link">$page_number</a>
</li>
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
<li class="$className">
<a class="page-link" href="$page_link">$link_content</a>
</li>
EOL;
}

?>

<dialog id="my-modal">
<article>
<a href="#"
Expand Down Expand Up @@ -452,7 +390,6 @@ class="close"
</div>
</div>


<form id="review-form" action="" method="get">
<label for="filter-by">Filter by</label>
<select id="filter-by" required>
Expand Down Expand Up @@ -481,29 +418,6 @@ class="close"
</div>
</main>

<nav class="container" style="display: flex; justify-content: center">
<ul class="pagination">
<?php
// Display previous page button
displayNavigationButton(
$current_page_number,
$total_pages,
true
);

// Display each page item
for ($page_num = 1; $page_num <= $total_pages; $page_num++) {
displayPageItem($current_page_number, $page_num);
}

// Display next page button
displayNavigationButton(
$current_page_number,
$total_pages,
false
);
?>
</ul>
</nav>
<?= $review_pagination ?>

<script src="/js/product_view.bundle.js"></script>
91 changes: 2 additions & 89 deletions src/views/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,69 +39,6 @@ function displayProduct(Product $product): void
</a>
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
<li class="$className">
<a class="page-link" href="$page_link">$page_number</a>
</li>
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
<li class="$className">
<a class="page-link" href="$page_link">$link_content</a>
</li>
EOL;
}

?>

<form method="get" class="container">
Expand Down Expand Up @@ -161,30 +97,7 @@ function displayNavigationButton(int $current_page_number, int $total_pages, boo
</div>
</main>

<nav class="container" style="display: flex; justify-content: center">
<ul class="pagination">
<?php
// Display previous page button
displayNavigationButton(
$current_page_number,
$total_pages,
true
);

// Display each page item
for ($page_num = 1; $page_num <= $total_pages; $page_num++) {
displayPageItem($current_page_number, $page_num);
}

// Display next page button
displayNavigationButton(
$current_page_number,
$total_pages,
false
);
?>
</ul>
</nav>
<?= $pagination ?>

<script>
document.addEventListener("DOMContentLoaded", function() {
Expand Down

0 comments on commit 09eaa01

Please sign in to comment.