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

Commit 019fe1f

Browse files
Merge branch 'dev' into feature-image-product
2 parents 759c4fc + c5e010f commit 019fe1f

20 files changed

Lines changed: 818 additions & 600 deletions

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."];
Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,103 @@
11
<?php
22

33
require_once __DIR__ .'/../models/Cart.php';
4+
require_once __DIR__ .'/../models/ProductCustomization.php';
45

6+
/*
7+
* CartController is responsible for handling all cart-related actions, such as adding and removing products from the cart, viewing the cart, and saving customizations.
8+
* It interacts with the Cart model to manage the cart data stored in the session and renders the appropriate views for the cart.
9+
*/
510
class CartController{
611

712
private $cartModel;
13+
private $customizationModel;
814

9-
public function __construct(){
15+
public function __construct(PDO $dbConnection){
1016
$this->cartModel = new Cart();
17+
$this->customizationModel = new ProductCustomization($dbConnection);
1118
}
1219

13-
public function cartAction(){
20+
public function viewCart(){
21+
$cart = $this->cartModel->getCart();
22+
$total = $this->cartModel->computeTotal();
23+
24+
require_once __DIR__ . "/../views/cart.php";
25+
}
26+
27+
public function cartAction() {
1428
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
1529
header('Location: /cart');
1630
exit;
1731
}
18-
$id = $_POST['product_id'];
19-
$name = $_POST['product_name'];
20-
$price = $_POST['product_price'];
21-
$is_from_cart = $_POST['is_from_cart'];
22-
if ($_POST['action'] === 'add') {
23-
$this->addToCart($id, $name, $price);
24-
} else if ($_POST['action'] === 'remove') {
25-
$this->removeFromCart($id);
26-
}
27-
if($is_from_cart === 'True'){
28-
header('Location: /cart');
29-
exit;
30-
} else {
31-
header('Location: /product?id=' . urlencode($id));
32+
33+
$id = (int) ($_POST['product_id'] ?? 0);
34+
$name = (string) ($_POST['product_name'] ?? '');
35+
$price = (float) ($_POST['product_price'] ?? 0);
36+
$isFromCart = $_POST['is_from_cart'] ?? 'False';
37+
$lineKey = $_POST['line_key'] ?? null;
38+
$options = $_POST['customization'] ?? [];
39+
40+
if (is_string($options)) {
41+
$decodedOptions = json_decode($options, true);
42+
$options = is_array($decodedOptions) ? $decodedOptions : [];
3243
}
33-
}
3444

35-
public function viewCart(){
36-
if (!isset($_SESSION['customizations'])) {
37-
$_SESSION['customizations'] = [];
45+
if (!is_array($options)) {
46+
$options = [];
3847
}
3948

40-
$cart = $this->cartModel->getCart();
41-
$total = $this->cartModel->computeTotal();
42-
$customizations = $_SESSION['customizations'];
49+
if (!empty($options) && array_keys($options) !== range(0, count($options) - 1)) {
50+
$normalizedOptions = [];
4351

44-
require_once __DIR__ . "/../views/cart.php";
45-
}
52+
foreach ($options as $slotId => $selectedOptions) {
53+
$slot = $this->customizationModel->getCustomizationSlotById((int) $slotId);
54+
if (!$slot) {
55+
continue;
56+
}
4657

47-
public function saveCustomization() {
48-
ob_start();
49-
$data = json_decode(file_get_contents('php://input'), true);
50-
ob_clean();
51-
if (!$data || !isset($data['product_id']) || !isset($data['customization'])) {
52-
header('Content-Type: application/json');
53-
echo json_encode(['success' => false, 'message' => 'Invalid data']);
54-
exit;
55-
}
58+
$selectedChoices = [];
59+
$selectedOptions = is_array($selectedOptions) ? $selectedOptions : [$selectedOptions];
5660

57-
$productId = $data['product_id'];
58-
$customization = $data['customization'];
59-
$_SESSION['customizations'][] = [
60-
'product_id' => $productId,
61-
'customization' => $customization
62-
];
63-
$customizationIndex = count($_SESSION['customizations']) - 1;
64-
$_SESSION['cart'][$productId]['customization_index'] = $customizationIndex;
65-
66-
header('Content-Type: application/json');
67-
echo json_encode(['success' => true]);
68-
exit;
69-
}
61+
foreach ($selectedOptions as $selectedOptionId) {
62+
if ($selectedOptionId === '' || $selectedOptionId === null) {
63+
continue;
64+
}
7065

71-
public function addToCart($product_id, $quantity, $price) {
72-
$this->cartModel->addProduct($product_id, $quantity, $price);
73-
}
66+
$option = $this->customizationModel->getCustomizationOptionById((int) $selectedOptionId);
67+
if (!$option) {
68+
continue;
69+
}
7470

75-
public function removeFromCart($product_id) {
76-
$this->cartModel->removeProduct($product_id);
77-
}
71+
$selectedChoices[] = [
72+
'optionProductId' => (int) $option->option_product_id,
73+
'name' => $option->option_product_name,
74+
'priceDelta' => (float) $option->price_delta,
75+
];
76+
}
7877

79-
}
78+
$normalizedOptions[] = [
79+
'slotId' => (int) $slot->id,
80+
'categoryName' => $slot->category_name,
81+
'choices' => $selectedChoices,
82+
];
83+
}
84+
85+
$options = $normalizedOptions;
86+
}
8087

88+
if ($_POST['action'] === 'add') {
89+
$this->cartModel->addProductToCart($id, $name, $price, $options);
90+
} else if ($_POST['action'] === 'remove') {
91+
if (!$lineKey) {
92+
$lineKey = $this->cartModel->buildLineKey($id, $options);
93+
}
94+
$this->cartModel->removeProductFromCart($lineKey);
95+
}
96+
97+
if ($isFromCart === 'True'){
98+
header('Location: /cart');
99+
} else {
100+
header('Location: /product?id=' . urlencode($id));
101+
}
102+
}
103+
}

src/app/controllers/ProductController.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
<?php
22

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

68
class ProductController {
79

8-
private $dbConnection;
10+
//private $menuModel;
911
private $productModel;
12+
private $customizationModel;
13+
private $cartModel;
1014

1115
public function __construct(PDO $dbConnection) {
12-
$this->dbConnection = $dbConnection;
16+
//$this->menuModel = new Menu($dbConnection);
1317
$this->productModel = new Product($dbConnection);
18+
$this->customizationModel = new ProductCustomization($dbConnection);
19+
$this->cartModel = new Cart();
1420
}
1521

1622
public function viewProducts() {
1723
$title = "View Products";
1824
$products = $this->productModel->getAllProducts(false);
25+
//$menus = $this->menuModel->getAllMenus();
26+
1927
require_once __DIR__ . "/../views/products.php";
2028
}
2129

@@ -36,7 +44,7 @@ public function viewSingleProduct() {
3644
exit;
3745
}
3846

39-
$id = isset($_GET['id']) ? $_GET['id'] : null;
47+
$id = $_GET['id'] ?? null;
4048

4149
if(!$id) {
4250
header('Location: /products');
@@ -52,12 +60,12 @@ public function viewSingleProduct() {
5260

5361
$title = $product->name;
5462

55-
$customizationModel = new ProductCustomization($this->dbConnection);
56-
$slots = $customizationModel->getCustomizationSlotsByProductId($product->id);
63+
$slots = $this->customizationModel->getCustomizationSlotsByProductId($id);
5764
foreach($slots as $slot) {
58-
$slot->options = $customizationModel->getCustomizationOptionsBySlot($slot->id);
65+
$slot->options = $this->customizationModel->getCustomizationOptionsBySlot($slot->id);
5966
}
6067

68+
$inCartQuantity = $this->cartModel->getProductQuantityById($product->id);
6169

6270
require_once __DIR__ . "/../views/product.php";
6371
}

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

0 commit comments

Comments
 (0)