From ad4701fe95a58be879bf065d1f0aa18d40371ac9 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Fri, 17 May 2024 19:25:57 +0400 Subject: [PATCH 1/6] add reorder and cancel order functionality to Profile controller and view, and implemented deleteOrder method in Order model --- src/controllers/Profile.php | 59 +++++++++++++++++++++++++++++++++++++ src/models/Order.php | 27 +++++++++++++++++ src/views/Profile.php | 25 ++++++++++++++-- 3 files changed, 108 insertions(+), 3 deletions(-) diff --git a/src/controllers/Profile.php b/src/controllers/Profile.php index f7eca130..9dca239a 100644 --- a/src/controllers/Profile.php +++ b/src/controllers/Profile.php @@ -4,11 +4,14 @@ namespace Steamy\Controller; +use DateTime; use Steamy\Core\Controller; use Steamy\Core\Utility; use Steamy\Model\Client; use Steamy\Model\District; use Steamy\Model\Location; +use Steamy\Model\Order; +use Steamy\Model\OrderStatus; class Profile { @@ -78,6 +81,62 @@ private function displayProfileDetails(Client $client, string $password = "", st ); } + public function reorderOrder(): void + { + if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['order_id'])) { + // Handle invalid request + Utility::redirect('profile'); + } + + $order_id = (int)$_POST['order_id']; + $order = Order::getByID($order_id); + + if (!$order || $order->getStatus() !== OrderStatus::COMPLETED) { + // Order doesn't exist or not completed + Utility::redirect('profile'); + } + + // Create a new order with the same details as the previous order + $new_order = new Order( + store_id: $order->getStoreID(), + client_id: $order->getClientID(), + line_items: $order->getLineItems(), + pickup_date: null, // or set pickup date as needed + status: OrderStatus::PENDING, + created_date: new DateTime() + ); + + // Save the new order + $new_order->save(); + + // Redirect back to the profile page + Utility::redirect('profile'); + } + + + public function cancelOrder(): void + { + if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['order_id'])) { + // Handle invalid request + Utility::redirect('profile'); + } + + $order_id = (int)$_POST['order_id']; + $order = Order::getByID($order_id); + + if (!$order || $order->getStatus() === OrderStatus::COMPLETED) { + // Order doesn't exist or already completed + Utility::redirect('profile'); + } + + // Cancel the order + $order->deleteOrder(); + + // Redirect back to the profile page + Utility::redirect('profile'); + } + + private function handleProfileEditSubmission(): void { $form_data = (new Register())->getFormData(); diff --git a/src/models/Order.php b/src/models/Order.php index 5a88b5d9..d2fcaf99 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -235,6 +235,33 @@ public static function getByID(int $order_id): ?Order ); } + /** + * Deletes the order and associated line items from the database. + */ + public function deleteOrder(): void + { + $conn = self::connect(); + $conn->beginTransaction(); + + try { + // Delete line items first + $query = "DELETE FROM order_product WHERE order_id = :order_id"; + $stm = $conn->prepare($query); + $stm->execute(['order_id' => $this->order_id]); + + // Delete the order itself + $query = "DELETE FROM `order` WHERE order_id = :order_id"; + $stm = $conn->prepare($query); + $stm->execute(['order_id' => $this->order_id]); + + $conn->commit(); + } catch (PDOException $e) { + $conn->rollBack(); + } finally { + $conn = null; + } + } + private static function getOrderProducts(int $order_id): array { $query = "SELECT * diff --git a/src/views/Profile.php b/src/views/Profile.php index 10d5ad12..95a61246 100644 --- a/src/views/Profile.php +++ b/src/views/Profile.php @@ -92,11 +92,22 @@ date) - strtotime($a->date); + }); + foreach ($orders as $order) { $date = htmlspecialchars($order->date); $id = filter_var($order->id, FILTER_SANITIZE_NUMBER_INT); $cost = filter_var($order->cost, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); $status = htmlspecialchars($order->status); + + // Determine button states + $cancelDisabled = $status === 'Completed' ? 'disabled' : ''; + $reorderDisabled = $status !== 'Completed' ? 'disabled' : ''; + echo <<< EOL $date @@ -104,12 +115,20 @@ $cost $status - - +
+ + +
+
+ + +
+ - EOL; + EOL; } + ?> From 308f09894548d917d0285f7c4c1b2be69d06c463 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sun, 19 May 2024 17:53:06 +0400 Subject: [PATCH 2/6] remove order sorting code and update button state conditions --- src/views/Profile.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/views/Profile.php b/src/views/Profile.php index 95a61246..cd144b7b 100644 --- a/src/views/Profile.php +++ b/src/views/Profile.php @@ -93,11 +93,6 @@ date) - strtotime($a->date); - }); - foreach ($orders as $order) { $date = htmlspecialchars($order->date); $id = filter_var($order->id, FILTER_SANITIZE_NUMBER_INT); @@ -105,8 +100,8 @@ $status = htmlspecialchars($order->status); // Determine button states - $cancelDisabled = $status === 'Completed' ? 'disabled' : ''; - $reorderDisabled = $status !== 'Completed' ? 'disabled' : ''; + $cancelDisabled = $status === 'completed' ? 'disabled' : ''; + $reorderDisabled = $status !== 'completed' ? 'disabled' : ''; echo <<< EOL From 8a357c38f1b43458107f8e5456a6c0cd1e17772a Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Wed, 22 May 2024 20:24:42 +0400 Subject: [PATCH 3/6] Refactore Profile controller and view to handle reorder and cancel order functionality, update error handling and form submission logic --- src/controllers/Profile.php | 92 +++++++++++++++++++------------------ src/views/Profile.php | 42 +++++++---------- 2 files changed, 65 insertions(+), 69 deletions(-) diff --git a/src/controllers/Profile.php b/src/controllers/Profile.php index e39d3da3..24272351 100644 --- a/src/controllers/Profile.php +++ b/src/controllers/Profile.php @@ -26,6 +26,7 @@ public function __construct() $this->view_data['errors'] = []; $this->view_data['client'] = null; $this->view_data['show_account_deletion_confirmation'] = false; + $this->view_data['reorder_cancel'] = false; } private function handleLogOut(): void @@ -104,60 +105,62 @@ private function displayProfileDetails(Client $client, string $password = "", st public function reorderOrder(): void { - if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['order_id'])) { - // Handle invalid request + $this->view_data['reorder_cancel'] = true; + + if (isset($_POST['reorder'])){ + + $order_id = (int)$_POST['order_id']; + $order = Order::getByID($order_id); + + // Create a new order with the same details as the previous order + $new_order = new Order( + store_id: $order->getStoreID(), + client_id: $order->getClientID(), + line_items: $order->getLineItems(), + pickup_date: null, // or set pickup date as needed + status: OrderStatus::PENDING, + created_date: new DateTime() + ); + + // Save the new order + $new_order->save(); + + // Redirect back to the profile page Utility::redirect('profile'); } - $order_id = (int)$_POST['order_id']; - $order = Order::getByID($order_id); - - if (!$order || $order->getStatus() !== OrderStatus::COMPLETED) { - // Order doesn't exist or not completed - Utility::redirect('profile'); - } - - // Create a new order with the same details as the previous order - $new_order = new Order( - store_id: $order->getStoreID(), - client_id: $order->getClientID(), - line_items: $order->getLineItems(), - pickup_date: null, // or set pickup date as needed - status: OrderStatus::PENDING, - created_date: new DateTime() + $this->view( + 'Profile', + $this->view_data, + 'Reorder', + enableIndexing: false ); - - // Save the new order - $new_order->save(); - - // Redirect back to the profile page - Utility::redirect('profile'); - } - + } public function cancelOrder(): void { - if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['order_id'])) { - // Handle invalid request - Utility::redirect('profile'); - } + $this->view_data['reorder_cancel'] = true; + + if (isset($_POST['cancel'])){ - $order_id = (int)$_POST['order_id']; - $order = Order::getByID($order_id); + $order_id = (int)$_POST['order_id']; + $order = Order::getByID($order_id); - if (!$order || $order->getStatus() === OrderStatus::COMPLETED) { - // Order doesn't exist or already completed + // Cancel the order + $order->deleteOrder(); + + // Redirect back to the profile page Utility::redirect('profile'); } - // Cancel the order - $order->deleteOrder(); - - // Redirect back to the profile page - Utility::redirect('profile'); + $this->view( + 'Profile', + $this->view_data, + 'Cancel', + enableIndexing: false + ); } - private function handleProfileEditSubmission(): void { $form_data = (new Register())->getFormData(); @@ -183,9 +186,7 @@ private function handleProfileEditSubmission(): void // check if user entered a new email if (!empty($form_data['email']) && $form_data['email'] !== $this->signed_client->getEmail()) { // check if a newly typed email already exists in database - if (!empty( - Client::getByEmail($updated_client->getEmail()) - )) { + if (!empty(Client::getByEmail($updated_client->getEmail()))) { $this->view_data['errors']['email'] = "Email already in use"; } } @@ -206,7 +207,6 @@ private function handleProfileEditSubmission(): void } } - // if all data valid, update user record and redirect to login page if (empty($this->view_data['errors'])) { $success = $updated_client->updateUser($password_changed); @@ -253,6 +253,10 @@ public function index(): void Utility::redirect('login'); } + if (isset($_GET['reorder_cancel'])) { + $this->reorderOrder() || $this->cancelOrder(); + return; + } // log out user if logout button clicked if (isset($_GET['logout_submit'])) { diff --git a/src/views/Profile.php b/src/views/Profile.php index d4e8feea..d4c8be9f 100644 --- a/src/views/Profile.php +++ b/src/views/Profile.php @@ -8,6 +8,7 @@ * @var Client $client signed in client * @var Order[] $orders array of orders * @var bool $show_account_deletion_confirmation Whether to display a confirmation dialog for account deletion + * @var bool $reorder_cancel Whether to display reorder and cancel buttons */ use Steamy\Model\Client; @@ -15,11 +16,10 @@ ?> - +
-

Deleting your account!

+

Deleting your account!

Are you sure you want to delete your account? This action is irreversible.

@@ -29,8 +29,7 @@
- +

My profile

@@ -49,13 +48,11 @@ disabled> - -
- \ No newline at end of file + From e21bf78583f006c9fdd1a36c0b44fa6eeed3d258 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Thu, 23 May 2024 16:11:44 +0400 Subject: [PATCH 4/6] make getOrderProducts public --- src/models/Order.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/models/Order.php b/src/models/Order.php index 46c51737..c20cd866 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -286,7 +286,11 @@ public function deleteOrder(): void } } - private static function getOrderProducts(int $order_id): array + /** + * @param int $order_id + * @return OrderProduct[] An array of line items for current order + */ + public static function getOrderProducts(int $order_id): array { $query = "SELECT * FROM order_product From ad3926a30e3d94fbec29016dc308e44b82ced6ac Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Thu, 23 May 2024 16:12:38 +0400 Subject: [PATCH 5/6] fix bugs with order and reorder --- src/controllers/Profile.php | 84 ++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 47 deletions(-) diff --git a/src/controllers/Profile.php b/src/controllers/Profile.php index 24272351..f6c78928 100644 --- a/src/controllers/Profile.php +++ b/src/controllers/Profile.php @@ -4,7 +4,7 @@ namespace Steamy\Controller; -use DateTime; +use Exception; use Steamy\Core\Controller; use Steamy\Core\Utility; use Steamy\Model\Client; @@ -25,8 +25,9 @@ public function __construct() $this->signed_client = null; $this->view_data['errors'] = []; $this->view_data['client'] = null; + $this->view_data["orders"] = []; + $this->view_data['order_action_error'] = ""; $this->view_data['show_account_deletion_confirmation'] = false; - $this->view_data['reorder_cancel'] = false; } private function handleLogOut(): void @@ -105,60 +106,46 @@ private function displayProfileDetails(Client $client, string $password = "", st public function reorderOrder(): void { - $this->view_data['reorder_cancel'] = true; - - if (isset($_POST['reorder'])){ - - $order_id = (int)$_POST['order_id']; - $order = Order::getByID($order_id); - - // Create a new order with the same details as the previous order - $new_order = new Order( - store_id: $order->getStoreID(), - client_id: $order->getClientID(), - line_items: $order->getLineItems(), - pickup_date: null, // or set pickup date as needed - status: OrderStatus::PENDING, - created_date: new DateTime() - ); - - // Save the new order - $new_order->save(); + $order_id = (int)($_POST['order_id'] ?? -1); + $order = Order::getByID($order_id); - // Redirect back to the profile page - Utility::redirect('profile'); + if (empty($order)) { + $this->view_data['order_action_error'] = 'Invalid order ID'; + return; } - $this->view( - 'Profile', - $this->view_data, - 'Reorder', - enableIndexing: false + // Create a new order with the same details as the previous order + $new_order = new Order( + store_id: $order->getStoreID(), + client_id: $order->getClientID(), + line_items: Order::getOrderProducts($order_id), ); + + // Save the new order + try { + $new_order->save(); + } catch (Exception $e) { + $this->view_data['order_action_error'] = $e->getMessage(); + } } public function cancelOrder(): void { - $this->view_data['reorder_cancel'] = true; - - if (isset($_POST['cancel'])){ - - $order_id = (int)$_POST['order_id']; - $order = Order::getByID($order_id); + $order_id = (int)($_POST['order_id'] ?? -1); + $order = Order::getByID($order_id); - // Cancel the order - $order->deleteOrder(); + if (empty($order)) { + $this->view_data['order_action_error'] = 'Invalid order ID'; + return; + } - // Redirect back to the profile page - Utility::redirect('profile'); + if ($order->getStatus() === OrderStatus::COMPLETED) { + $this->view_data['order_action_error'] = 'Cannot cancel an order which is complete'; + return; } - $this->view( - 'Profile', - $this->view_data, - 'Cancel', - enableIndexing: false - ); + // Cancel the order + $order->deleteOrder(); } private function handleProfileEditSubmission(): void @@ -253,9 +240,12 @@ public function index(): void Utility::redirect('login'); } - if (isset($_GET['reorder_cancel'])) { - $this->reorderOrder() || $this->cancelOrder(); - return; + if (isset($_POST['reorder'])) { + $this->reorderOrder(); + } + + if (isset($_POST['cancel_order'])) { + $this->cancelOrder(); } // log out user if logout button clicked From 90677aab99212e96617ba50271a6a5d49238cbf2 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Thu, 23 May 2024 16:16:43 +0400 Subject: [PATCH 6/6] refactor code for cancel/reorder, display error message for cancel/reorder --- src/views/Profile.php | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/views/Profile.php b/src/views/Profile.php index d4c8be9f..09fd6834 100644 --- a/src/views/Profile.php +++ b/src/views/Profile.php @@ -8,7 +8,7 @@ * @var Client $client signed in client * @var Order[] $orders array of orders * @var bool $show_account_deletion_confirmation Whether to display a confirmation dialog for account deletion - * @var bool $reorder_cancel Whether to display reorder and cancel buttons + * @var string $order_action_error Error when user performed action on orders tab */ use Steamy\Model\Client; @@ -16,7 +16,8 @@ ?> - +

Deleting your account!

@@ -29,7 +30,8 @@
- +

My profile

@@ -72,6 +74,12 @@

Orders summary

+ +
ERROR 🔺: .
+ +
@@ -87,36 +95,30 @@ foreach ($orders as $order) { $date = htmlspecialchars($order->getCreatedDate()->format('Y-m-d H:i:s')); $id = filter_var($order->getOrderID(), FILTER_SANITIZE_NUMBER_INT); - $storeid = filter_var($order->getStoreID(), FILTER_SANITIZE_NUMBER_INT); + $store_id = filter_var($order->getStoreID(), FILTER_SANITIZE_NUMBER_INT); $status = htmlspecialchars(ucfirst($order->getStatus()->value)); $totalPrice = htmlspecialchars(number_format($order->calculateTotalPrice(), 2)); // Determine button states - $cancelDisabled = $status === 'completed' ? 'disabled' : ''; - $reorderDisabled = $status !== 'completed' ? 'disabled' : ''; + $cancelDisabled = $order->getStatus()->value === 'completed' ? 'disabled' : ''; echo <<< EOL - + - EOL; - if ($reorder_cancel) { - echo <<< EOL - - EOL; - } - echo ""; + + EOL; } ?>
$id$storeid$store_id $date $status \$$totalPrice -
- +
+ - - + +