-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'creme332:main' into updatereviewtest
- Loading branch information
Showing
15 changed files
with
626 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Steamy\Controller; | ||
|
||
use Steamy\Core\Controller; | ||
use Steamy\Core\Utility; | ||
use Steamy\Model\Order; | ||
use Steamy\Model\OrderProduct; | ||
|
||
class Orders | ||
{ | ||
use Controller; | ||
|
||
private array $view_data = []; | ||
|
||
private function validateURL(): bool | ||
{ | ||
$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; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
private function handleInvalidURL(): void | ||
{ | ||
if (!$this->validateURL()) { | ||
(new Error())->handlePageNotFoundError(); | ||
die(); | ||
} | ||
} | ||
|
||
public function index(): void | ||
{ | ||
$this->handleInvalidURL(); | ||
|
||
$order_id = $this->getOrderIDFromURL(); | ||
if ($order_id === null) { | ||
(new Error())->handlePageNotFoundError(); | ||
return; | ||
} | ||
|
||
$order = Order::getByID($order_id); | ||
if (!$order) { | ||
(new Error())->handlePageNotFoundError(); | ||
return; | ||
} | ||
|
||
$order_products = Order::getOrderProducts($order->getOrderID()); | ||
|
||
$this->view_data['order'] = $order; | ||
$this->view_data['line_items'] = $order_products; | ||
|
||
$this->view( | ||
'orders', | ||
$this->view_data, | ||
'Order #' . $order_id, | ||
enableIndexing: false | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Steamy\Controller; | ||
|
||
/** | ||
* Controller for managing the pagination component | ||
*/ | ||
class Pagination | ||
{ | ||
private int $items_per_page; | ||
private int $total_items; | ||
private int $current_page_number; | ||
|
||
public function __construct(int $items_per_page = 1, int $total_items = 1, int $current_page = 1) | ||
{ | ||
$this->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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.