diff --git a/src/controllers/Profile.php b/src/controllers/Profile.php index 6bd132b..f6c7892 100644 --- a/src/controllers/Profile.php +++ b/src/controllers/Profile.php @@ -4,12 +4,14 @@ namespace Steamy\Controller; +use Exception; 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 { @@ -23,6 +25,8 @@ 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; } @@ -100,6 +104,50 @@ private function displayProfileDetails(Client $client, string $password = "", st ); } + public function reorderOrder(): void + { + $order_id = (int)($_POST['order_id'] ?? -1); + $order = Order::getByID($order_id); + + if (empty($order)) { + $this->view_data['order_action_error'] = 'Invalid order ID'; + return; + } + + // 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 + { + $order_id = (int)($_POST['order_id'] ?? -1); + $order = Order::getByID($order_id); + + if (empty($order)) { + $this->view_data['order_action_error'] = 'Invalid order ID'; + return; + } + + if ($order->getStatus() === OrderStatus::COMPLETED) { + $this->view_data['order_action_error'] = 'Cannot cancel an order which is complete'; + return; + } + + // Cancel the order + $order->deleteOrder(); + } + private function handleProfileEditSubmission(): void { $form_data = (new Register())->getFormData(); @@ -125,9 +173,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"; } } @@ -148,7 +194,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); @@ -195,6 +240,13 @@ public function index(): void Utility::redirect('login'); } + if (isset($_POST['reorder'])) { + $this->reorderOrder(); + } + + if (isset($_POST['cancel_order'])) { + $this->cancelOrder(); + } // log out user if logout button clicked if (isset($_GET['logout_submit'])) { diff --git a/src/models/Order.php b/src/models/Order.php index 1448a12..c20cd86 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -259,7 +259,38 @@ public static function getByID(int $order_id): ?Order ); } - private static function getOrderProducts(int $order_id): array + /** + * 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; + } + } + + /** + * @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 diff --git a/src/views/Profile.php b/src/views/Profile.php index e4b95f3..09fd683 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 string $order_action_error Error when user performed action on orders tab */ use Steamy\Model\Client; @@ -19,7 +20,7 @@ if ($show_account_deletion_confirmation) : ?>
-

Deleting your account!

+

Deleting your account!

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

@@ -49,13 +50,11 @@ disabled> - -
- \ No newline at end of file +