Skip to content

Commit

Permalink
Merge branch 'updatereviewtest' of https://github.com/Divyesh000/stea…
Browse files Browse the repository at this point in the history
…my-sips into updatereviewtest
  • Loading branch information
Divyeshhhh committed Jun 1, 2024
2 parents 99fe71b + 0a9e511 commit 2c52658
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 28 deletions.
14 changes: 11 additions & 3 deletions src/controllers/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:<br><a href='$resetLink'>$resetLink</a>";

// 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);
}

Expand Down
60 changes: 56 additions & 4 deletions src/controllers/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
Expand All @@ -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";
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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'])) {
Expand Down
9 changes: 5 additions & 4 deletions src/controllers/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
33 changes: 32 additions & 1 deletion src/models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 22 additions & 16 deletions src/views/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +20,7 @@
if ($show_account_deletion_confirmation) : ?>
<dialog open>
<article>
<h3>Deleting your account! </h3>
<h3>Deleting your account!</h3>
<p>Are you sure you want to delete your account? This action is irreversible.</p>
<footer>
<form method="post" class="grid">
Expand Down Expand Up @@ -49,13 +50,11 @@
disabled>
</label>


<label class="grid">
Email
<input value="<?= htmlspecialchars($client->getEmail()) ?>" type="email" disabled>
</label>


<label class="grid">
Address
<input value="<?= htmlspecialchars($client->getAddress()->getFormattedAddress()) ?>" type="text"
Expand All @@ -70,14 +69,17 @@
<a href="/profile/edit">
<button>Edit</button>
</a>

</div>


<div id="Orders" class="tabcontent">

<h2>Orders summary</h2>

<?php
if (!empty($order_action_error)): ?>
<blockquote><strong> ERROR 🔺: <?= $order_action_error ?>.</strong></blockquote>
<?php
endif ?>

<figure>
<table>
<tr>
Expand All @@ -93,29 +95,34 @@
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 = $order->getStatus()->value === 'completed' ? 'disabled' : '';

echo <<< EOL
<tr>
<td>$id</td>
<td>$storeid</td>
<td>$store_id</td>
<td>$date</td>
<td>$status</td>
<td>\$$totalPrice</td>
<td class="grid">
<button>cancel</button>
<td>
<form style="display: flex; gap:1em;" method="post">
<input type="hidden" name="order_id" value="$id">
<button type="submit" name="cancel_order" $cancelDisabled>Cancel</button>
<button type="submit" name="reorder">Reorder</button>
</form>
</td>
</tr>
EOL;
}

?>


</table>
</figure>

</div>

<div id="Settings" class="tabcontent">
Expand Down Expand Up @@ -162,10 +169,9 @@
<form>
<button type="submit" name="account_delete_submit">Delete</button>
</form>

</article>
</div>
</div>
</main>

<script src="/js/profile_view.bundle.js"></script>
<script src="/js/profile_view.bundle.js"></script>
15 changes: 15 additions & 0 deletions src/views/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/


Expand Down Expand Up @@ -220,3 +221,17 @@ function togglePasswordVisibility() {
}
}
</script>

<dialog <?= $registration_is_successful ? "open" : "" ?>>
<article>
<h3>Successfully registered! 🎉</h3>
<p>Thank you for joining Steamy Sips. Your account has been successfully created.</p>
<footer>
<a href="/login"
role="button"
>
Return to sign in
</a>
</footer>
</article>
</dialog>
Loading

0 comments on commit 2c52658

Please sign in to comment.