Skip to content
This repository was archived by the owner on Jul 20, 2026. It is now read-only.

Commit b5b706e

Browse files
committed
...
1 parent 55f75b7 commit b5b706e

12 files changed

Lines changed: 184 additions & 40 deletions

File tree

src/app/controllers/AuthController.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,12 @@ public function signIn() {
9090

9191
if ($user && password_verify($password, $user->password)) {
9292
$this->accountModel->updateLastLogin($user->id);
93+
9394
$_SESSION['user_id'] = $user->id;
9495
$_SESSION['user_first_name'] = $user->first_name;
9596
$_SESSION['user_last_name'] = $user->last_name;
96-
$_SESSION['user_email'] = $user->email;
97-
$_SESSION['user_phone'] = $user->phone;
97+
$_SESSION['user_email'] = $user->email;
98+
$_SESSION['user_phone'] = $user->phone;
9899

99100
$role = $this->accountModel->getAccountRole($user->id);
100101
if ($role === 'admin') {
@@ -142,18 +143,21 @@ public function updateAccount() {
142143
$firstName = trim($_POST["first_name"] ?? '');
143144
$lastName = trim($_POST["last_name"] ?? '');
144145
$phone = trim($_POST["phone"] ?? '');
145-
$email = $_SESSION['user_email'];
146+
$email = trim($_POST["email"] ?? '');
147+
148+
$id = $_SESSION['user_id'];
146149

147150
if (empty($firstName) || empty($lastName)) {
148151
$_SESSION['errors'] = ["First name and Last name are required."];
149152
header('Location: /account');
150153
exit;
151154
}
152155

153-
if ($this->accountModel->updateAccountInfo($firstName, $lastName, $email, $phone)) {
156+
if ($this->accountModel->updateAccountInfo($id, $firstName, $lastName, $email, $phone)) {
154157
$_SESSION['user_first_name'] = $firstName;
155158
$_SESSION['user_last_name'] = $lastName;
156159
$_SESSION['user_phone'] = $phone;
160+
$_SESSION['user_email'] = $email;
157161
$_SESSION['success'] = "Profile updated!";
158162
} else {
159163
$_SESSION['errors'] = ["Error updating account."];

src/app/controllers/ProductController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<?php
22

3+
require __DIR__ .'/../models/Menu.php';
34
require __DIR__ .'/../models/Product.php';
45
require __DIR__ .'/../models/ProductCustomization.php';
56
require __DIR__ .'/../models/Cart.php';
67

78
class ProductController {
89

10+
//private $menuModel;
911
private $productModel;
1012
private $customizationModel;
1113
private $cartModel;
1214

1315
public function __construct(PDO $dbConnection) {
16+
//$this->menuModel = new Menu($dbConnection);
1417
$this->productModel = new Product($dbConnection);
1518
$this->customizationModel = new ProductCustomization($dbConnection);
1619
$this->cartModel = new Cart();
@@ -19,6 +22,7 @@ public function __construct(PDO $dbConnection) {
1922
public function viewProducts() {
2023
$title = "View Products";
2124
$products = $this->productModel->getAllProducts(false);
25+
//$menus = $this->menuModel->getAllMenus();
2226

2327
require_once __DIR__ . "/../views/products.php";
2428
}

src/app/models/Account.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,25 @@ public function updateAccount($id, $firstName, $lastName, $email, $phone, $roleI
153153
/**
154154
* Updates the account information for a user based on their email address.
155155
* This is typically used for users to update their own profile information.
156+
* @param int $id The ID of the account to update.
156157
* @param string $firstName The new first name.
157158
* @param string $lastName The new last name.
158159
* @param string $email The email address of the account to update (used as identifier).
159160
* @param string|null $phone The new phone number.
160161
* @return bool True if the update was successful, false otherwise.
161162
*/
162-
public function updateAccountInfo($firstName, $lastName, $email, $phone) {
163+
public function updateAccountInfo($id, $firstName, $lastName, $email, $phone) {
163164

164165
$query = "UPDATE account
165166
SET first_name = :firstName,
166167
last_name = :lastName,
168+
email = :email,
167169
phone = :phone
168-
WHERE email = :email";
170+
WHERE id = :id";
169171

170172
$stmt = $this->conn->prepare($query);
171173

174+
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
172175
$stmt->bindValue(':firstName', $firstName, PDO::PARAM_STR);
173176
$stmt->bindValue(':lastName', $lastName, PDO::PARAM_STR);
174177
$stmt->bindValue(':phone', $phone, PDO::PARAM_STR);

src/app/models/Cart.php

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ private function normalizeOptions($options) {
5151
return $normalized;
5252
}
5353

54-
public function buildLineKey($product_id, $options = []) {
55-
return $product_id . ':' . md5(json_encode($this->normalizeOptions($options)));
54+
public function buildLineKey($id, $options = []) {
55+
return $id . ':' . md5(json_encode($this->normalizeOptions($options)));
5656
}
5757

5858
public function addProductToCart($product_id, $name, $price, $options = []) {
@@ -94,6 +94,45 @@ public function removeProductFromCart($lineKey) {
9494
}
9595
}
9696

97+
public function addMenuToCart($menu_id, $name, $price, $selections = []) {
98+
if (!isset($_SESSION['cart']['menus'])) {
99+
$_SESSION['cart']['menus'] = [];
100+
}
101+
102+
$normalizedSelections = $this->normalizeOptions($selections);
103+
$lineKey = $this->buildLineKey($menu_id, $normalizedSelections);
104+
105+
if (isset($_SESSION['cart']['menus'][$lineKey])) {
106+
$_SESSION['cart']['menus'][$lineKey]['quantity']++;
107+
} else {
108+
$_SESSION['cart']['menus'][$lineKey] = [
109+
'line_key' => $lineKey,
110+
'menu_id' => $menu_id,
111+
'name' => $name,
112+
'quantity' => 1,
113+
'price' => $price,
114+
'selections' => $normalizedSelections,
115+
];
116+
}
117+
118+
return $lineKey;
119+
}
120+
121+
public function removeMenuFromCart($lineKey) {
122+
if (!isset($_SESSION['cart']['menus'])) {
123+
$_SESSION['cart']['menus'] = [];
124+
return;
125+
}
126+
127+
if (isset($_SESSION['cart']['menus'][$lineKey])) {
128+
if ($_SESSION['cart']['menus'][$lineKey]['quantity'] - 1 === 0) {
129+
unset($_SESSION['cart']['menus'][$lineKey]);
130+
} else {
131+
$_SESSION['cart']['menus'][$lineKey]['quantity'] --;
132+
}
133+
}
134+
}
135+
97136
public function getProductQuantityById($productId) {
98137
if (!isset($_SESSION['cart']['products'])) {
99138
return 0;
@@ -110,6 +149,22 @@ public function getProductQuantityById($productId) {
110149
return $quantity;
111150
}
112151

152+
public function getMenuQuantityById($menuId) {
153+
if (!isset($_SESSION['cart']['menus'])) {
154+
return 0;
155+
}
156+
157+
$quantity = 0;
158+
159+
foreach ($_SESSION['cart']['menus'] as $menu) {
160+
if ((int) ($menu['menu_id'] ?? 0) === (int) $menuId) {
161+
$quantity += (int) ($menu['quantity'] ?? 0);
162+
}
163+
}
164+
165+
return $quantity;
166+
}
167+
113168
public function getTotalCount() {
114169
if (!isset($_SESSION['cart']) || !isset($_SESSION['cart']['products']) || !isset($_SESSION['cart']['menus'])) {
115170
return 0;
@@ -130,28 +185,51 @@ public function getTotalCount() {
130185

131186
public function computeTotal() {
132187
$total = 0;
133-
if (!isset($_SESSION['cart']['products'])) {
134-
return $total;
135-
}
136188

137-
foreach ($_SESSION['cart']['products'] as $product) {
138-
$quantity = (int) ($product['quantity'] ?? 0);
139-
$price = (float) ($product['price'] ?? 0);
189+
// Add products total
190+
if (isset($_SESSION['cart']['products'])) {
191+
foreach ($_SESSION['cart']['products'] as $product) {
192+
$quantity = (int) ($product['quantity'] ?? 0);
193+
$price = (float) ($product['price'] ?? 0);
140194

141-
$total += $quantity * $price;
195+
$total += $quantity * $price;
142196

143-
if (isset($product['options']) && is_array($product['options'])) {
144-
foreach ($product['options'] as $slot) {
145-
if (!isset($slot['choices']) || !is_array($slot['choices'])) {
146-
continue;
197+
if (isset($product['options']) && is_array($product['options'])) {
198+
foreach ($product['options'] as $slot) {
199+
if (!isset($slot['choices']) || !is_array($slot['choices'])) {
200+
continue;
201+
}
202+
203+
foreach ($slot['choices'] as $choice) {
204+
$total += $quantity * (float) ($choice['priceDelta'] ?? 0);
205+
}
147206
}
207+
}
208+
}
209+
}
210+
211+
// Add menus total
212+
if (isset($_SESSION['cart']['menus'])) {
213+
foreach ($_SESSION['cart']['menus'] as $menu) {
214+
$quantity = (int) ($menu['quantity'] ?? 0);
215+
$price = (float) ($menu['price'] ?? 0);
148216

149-
foreach ($slot['choices'] as $choice) {
150-
$total += $quantity * (float) ($choice['priceDelta'] ?? 0);
217+
$total += $quantity * $price;
218+
219+
if (isset($menu['selections']) && is_array($menu['selections'])) {
220+
foreach ($menu['selections'] as $slot) {
221+
if (!isset($slot['choices']) || !is_array($slot['choices'])) {
222+
continue;
223+
}
224+
225+
foreach ($slot['choices'] as $choice) {
226+
$total += $quantity * (float) ($choice['priceDelta'] ?? 0);
227+
}
151228
}
152229
}
153230
}
154231
}
232+
155233
return $total;
156234
}
157235

src/app/models/Product.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public function getProductsFiltered($search, $sort) {
119119
$query .= " ORDER BY name DESC";
120120
} else if ($sort == "name_asc") {
121121
$query .= " ORDER BY name ASC";
122+
} else if ($sort == "price_asc") {
123+
$query .= " ORDER BY price ASC";
124+
} else if ($sort == "price_desc") {
125+
$query .= " ORDER BY price DESC";
122126
}
123127
}
124128

src/app/views/account.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,41 @@
2424

2525
<div id="account-content" style="display: none;" class="card shadow-sm p-4 mb-4">
2626
<div class="account-details row">
27-
<div class="col-md-4">
27+
<div class="col-md-3">
2828
<label class="fw-bold">First name</label>
2929
<p id="user-first-name" class="border-bottom pb-1"></p>
3030
</div>
31-
<div class="col-md-4">
31+
<div class="col-md-3">
3232
<label class="fw-bold">Last name</label>
3333
<p id="user-last-name" class="border-bottom pb-1"></p>
3434
</div>
35-
<div class="col-md-4">
35+
<div class="col-md-3">
3636
<label class="fw-bold">Email</label>
3737
<p id="user-email" class="border-bottom pb-1"></p>
3838
</div>
39+
<div class="col-md-3">
40+
<label class="fw-bold">Phone</label>
41+
<p id="user-phone" class="border-bottom pb-1"></p>
42+
</div>
3943
</div>
4044
</div>
4145

4246
<h2 class="text-pourpre border-bottom pb-2 mb-3">Update account</h2>
4347
<form action="/update-account" method="POST" class="card p-4 shadow-sm">
4448
<div class="account-details row g-3">
45-
<div class="col-md-4">
49+
<div class="col-md-3">
4650
<label class="form-label">First name</label>
4751
<input type="text" name="first_name" class="form-control"/>
4852
</div>
49-
<div class="col-md-4">
53+
<div class="col-md-3">
5054
<label class="form-label">Last name</label>
5155
<input type="text" name="last_name" class="form-control"/>
5256
</div>
53-
<div class="col-md-4">
57+
<div class="col-md-3">
58+
<label class="form-label">Email</label>
59+
<input type="email" name="email" class="form-control"/>
60+
</div>
61+
<div class="col-md-3">
5462
<label class="form-label">Phone</label>
5563
<input type="text" name="phone" class="form-control"/>
5664
</div>
@@ -59,6 +67,7 @@
5967
</div>
6068
</div>
6169
</form>
70+
6271
<form action="/update-password" method="POST" class="card p-4 shadow-sm">
6372
<div class="account-details row g-3">
6473
<div class="col-md-4">
@@ -96,6 +105,7 @@
96105
document.getElementById('user-first-name').textContent = data.first_name;
97106
document.getElementById('user-last-name').textContent = data.last_name;
98107
document.getElementById('user-email').textContent = data.email;
108+
document.getElementById('user-phone').textContent = data.phone;
99109

100110
document.getElementById('loading').style.display = 'none';
101111
document.getElementById('account-content').style.display = 'block';

src/app/views/cart.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
<h2>Total: <?= htmlspecialchars((string) $total) ?> €</h2>
55

66
<?php $products = $cart['products'] ?? []; ?>
7+
<!-- <?php $menus = $cart['menus'] ?? []; ?> -->
78

8-
<?php if (empty($products)) { ?>
9+
<?php if (empty($products) && empty($menus)) { ?>
910
<p>Your cart is empty.</p>
1011
<?php } else { ?>
1112
<ul>
13+
<!-- Display Products -->
1214
<?php foreach ($products as $lineKey => $item): ?>
1315
<li>
1416
<strong><?= htmlspecialchars($item['name'] ?? '') ?></strong>
@@ -47,7 +49,8 @@
4749
</form>
4850
</li>
4951
<?php endforeach; ?>
50-
</ul>
52+
53+
<!-- TODO: Display Menus -->
5154
<?php } ?>
5255

53-
<?php include 'partials/footer.php'; ?>
56+
<?php include 'partials/footer.php'; ?>

src/app/views/product.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<select name="customization[<?= (int) $slot->id ?>][]" <?= $row < (int) $slot->min_select ? 'required' : '' ?>>
5252
<option value="">-- Select an option --</option>
5353
<?php foreach ($slot->options as $option): ?>
54-
<option value="<?= (int) $option->option_product_id ?>" <?= $row === $option->display_order && !empty($option->is_default) ? 'selected' : '' ?>>
54+
<option value="<?= (int) $option->option_product_id ?>" <?= $row === 0 && !empty($option->is_default) ? 'selected' : '' ?>>
5555
<?= htmlspecialchars($option->option_product_name) ?>
5656
<?= ((float) $option->price_delta !== 0.0) ? ' (' . htmlspecialchars((string) $option->price_delta) . ' €)' : ' (free)' ?>
5757
</option>
@@ -64,7 +64,6 @@
6464
<?php } ?>
6565

6666
<button type="submit" name="action" value="add">Add to Cart</button>
67-
<button type="submit" name="action" value="remove" id="remove-btn">Remove from Cart</button>
6867
<?php } else { ?>
6968
<p style="color: red; font-weight: bold;">Please sign in to add this product to your cart.</p>
7069
<?php } ?>

0 commit comments

Comments
 (0)