diff --git a/src/controllers/Password.php b/src/controllers/Password.php index 4f2298f..03de1de 100644 --- a/src/controllers/Password.php +++ b/src/controllers/Password.php @@ -40,11 +40,19 @@ public function __construct() */ private function sendResetEmail(string $email, string $resetLink): void { - //Implement logic to send reset email using Mailer class - $mailer = new Mailer(); $subject = "Reset Your Password | Steamy Sips"; - $htmlMessage = "Click the link below to reset your password:
$resetLink"; + + // Capture the HTML template content + ob_start(); + $userEmail = $email; + require __DIR__ . '/../views/mails/PasswordReset.php'; + $htmlMessage = ob_get_clean(); + + // Plain message as fallback $plainMessage = "Click the link below to reset your password:\n$resetLink"; + + // Send the email + $mailer = new Mailer(); $mailer->sendMail($email, $subject, $htmlMessage, $plainMessage); } 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/controllers/Register.php b/src/controllers/Register.php index 95d86fd..2fdf8e4 100644 --- a/src/controllers/Register.php +++ b/src/controllers/Register.php @@ -34,6 +34,7 @@ public function __construct() $this->view_data['errors'] = []; $this->view_data['editMode'] = false; $this->view_data['form_submitted'] = false; + $this->view_data['registration_is_successful'] = false; // get list of districts to be displayed on form $this->view_data['districts'] = District::getAll(); @@ -114,11 +115,11 @@ private function handleFormSubmission(): void $success = $client->save(); if ($success) { - Utility::redirect('login'); + $this->view_data['registration_is_successful'] = true; + } else { + (new Error())->handleUnknownError(); + die(); } - - (new Error())->handleUnknownError(); - die(); } else { $this->loadDataToForm($form_data); } 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 + diff --git a/src/views/Register.php b/src/views/Register.php index e26a637..c0cef26 100644 --- a/src/views/Register.php +++ b/src/views/Register.php @@ -19,6 +19,7 @@ * @var string $defaultConfirmPassword * @var array $errors list of errors in form after submission * @var District[] $districts list of all district objects + * @var bool $registration_is_successful */ @@ -220,3 +221,17 @@ function togglePasswordVisibility() { } } + +> +
+

Successfully registered! 🎉

+

Thank you for joining Steamy Sips. Your account has been successfully created.

+ +
+
\ No newline at end of file diff --git a/src/views/mails/PasswordReset.php b/src/views/mails/PasswordReset.php new file mode 100644 index 0000000..9cddb85 --- /dev/null +++ b/src/views/mails/PasswordReset.php @@ -0,0 +1,98 @@ + + + + + + + + Reset Your Password + + + +
+
+ Steamy Sips Logo +
+
+

Reset Your Password

+

Hello,

+

We received a request to reset the password for the account associated with . Click the button below to reset your password:

+

Reset My Password

+

If you did not request a password reset, please ignore this email. This link will expire in 24 hours.

+

Thank you,
The Steamy Sips Team

+
+ +
+ +