11<?php
22
33require_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+ */
510class 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+ }
0 commit comments