-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
139 lines (139 loc) · 6.84 KB
/
cart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
require_once('Model/BaseModel.php');
require_once('View/TemplateRenderer.php');
$model = new BaseModel();
$template = new TemplateRenderer();
foreach ($_GET as $key => $value) {
$x = explode("//", $key);
}
$userId = $x[0];
if ($userId == '{{_userId_}}') {
header('Location: ' . 'login.php');
} else {
if ($userId > 0) {
$productId = $x[1];
$decide = $x[2];
switch ($decide) {
case "ADD":
$productStock = $model->getProductStock($productId);
if (!$model->verifyCartInsertDuplicate($userId, $productId)) {
if ($productStock[0]['quantity'] > 0) {
$model->cartInsertItem($userId, $productId);
} else {
echo '
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
$(document).ready(function() {
swal({
position: "top-end",
icon: "error",
title: "Nu mai sunt produse disponibile în stoc",
timer: 2500
})
})
</script>
';
}
} else {
$newQuantity = $model->getCartStock($userId, $productId);
if ($productStock[0]['quantity'] >= $newQuantity['quantity'] + 1) {
$model->cartAddQuantityItem($userId, $productId);
} else {
echo '
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
$(document).ready(function() {
swal({
position: "top-end",
icon: "error",
title: "Nu mai sunt produse disponibile în stoc",
timer: 2500
})
})
</script>
';
}
}
break;
case "DELETE":
$model->cartDeleteItem($userId, $productId);
break;
case "DOWN1":
if($model->verifyCartQuantity($userId, $productId)) {
$model->cartDeleteQuantityItem($userId, $productId);
} else {
$model->cartDeleteItem($userId, $productId);
}
break;
case "UP1":
$productStock = $model->getProductStock($productId);
$newQuantity = $model->getCartStock($userId, $productId);
if ($productStock[0]['quantity'] >= $newQuantity['quantity'] + 1) {
$model->cartAddQuantityItem($userId, $productId);
} else {
echo '
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
$(document).ready(function() {
swal({
position: "top-end",
icon: "error",
title: "Nu mai sunt produse disponibile în stoc",
timer: 2500
})
})
</script>
';
}
break;
case "INP":
$inpValue = $_GET[$x[0]."//".$x[1]."//".$x[2]];
if (is_numeric($inpValue) && $inpValue > 0 && $inpValue == round($inpValue)) {
$productStock = $model->getProductStock($productId);
if ($productStock[0]['quantity'] >= $inpValue) {
$model->cartSetQuantityItem($userId, $productId, $inpValue);
echo '
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
$(document).ready(function() {
swal({
position: "top-end",
icon: "success",
title: "Coșul a fost actualizat!",
timer: 2500
})
})
</script>
';
} else {
echo '
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
$(document).ready(function() {
swal({
position: "top-end",
icon: "error",
title: "Cantitatea introdusă de produse nu este disponibiă în stoc",
timer: 2500
})
})
</script>
';
}
}
break;
}
}
echo $template->render($model->getProductById($productId), "view");
}
?>