Skip to content

Commit

Permalink
Merge branch 'creme332:main' into emailtemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 authored May 24, 2024
2 parents 35ce640 + 7188fbc commit 180ba1f
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 21 deletions.
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
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>

0 comments on commit 180ba1f

Please sign in to comment.