-
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 pull request #211 from Divyesh000/detailedorder
create a detailed order page
- Loading branch information
Showing
7 changed files
with
184 additions
and
35 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
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
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @var Order $order Current order | ||
* @var OrderProduct[] $line_items Line items for current order | ||
*/ | ||
|
||
use Steamy\Model\Order; | ||
use Steamy\Model\OrderProduct; | ||
|
||
?> | ||
|
||
<main class="container"> | ||
<h1>Order #<?= filter_var($order->getOrderID(), FILTER_SANITIZE_NUMBER_INT); ?></h1> | ||
<section> | ||
<h2>Order Details</h2> | ||
<p><strong>Order ID:</strong> <?= filter_var($order->getOrderID(), FILTER_SANITIZE_NUMBER_INT); ?></p> | ||
<p><strong>Date:</strong> <?= htmlspecialchars($order->getCreatedDate()->format('Y-m-d H:i:s')) ?></p> | ||
<p><strong>Status:</strong> <?= htmlspecialchars(ucfirst($order->getStatus()->value)) ?></p> | ||
<p><strong>Total Price:</strong> $<?= htmlspecialchars(number_format($order->calculateTotalPrice(), 2)) ?></p> | ||
</section> | ||
|
||
<section> | ||
<h2>Order Items</h2> | ||
<table> | ||
<tr> | ||
<th>Product Name</th> | ||
<th>Quantity</th> | ||
<th>Milk Type</th> | ||
<th>Cup Size</th> | ||
<th>Unit Price</th> | ||
</tr> | ||
<?php | ||
foreach ($line_items as $item): ?> | ||
<tr> | ||
<td><?= htmlspecialchars($item->getProductName()) ?></td> | ||
<td><?= filter_var($item->getQuantity(), FILTER_SANITIZE_NUMBER_INT) ?></td> | ||
<td><?= htmlspecialchars($item->getMilkType()) ?></td> | ||
<td><?= htmlspecialchars($item->getCupSize()) ?></td> | ||
<td>$<?= (number_format($item->getUnitPrice(), 2)) ?></td> | ||
</tr> | ||
<?php | ||
endforeach; ?> | ||
</table> | ||
</section> | ||
</main> |
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