1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
HCSF - A multilingual CMS and Shopsystem |
5
|
|
|
Copyright (C) 2014 Marcus Haase - [email protected] |
6
|
|
|
|
7
|
|
|
This program is free software: you can redistribute it and/or modify |
8
|
|
|
it under the terms of the GNU General Public License as published by |
9
|
|
|
the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
(at your option) any later version. |
11
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
GNU General Public License for more details. |
16
|
|
|
|
17
|
|
|
You should have received a copy of the GNU General Public License |
18
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace HaaseIT\HCSF\Controller\Shop; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Updatecart |
26
|
|
|
* @package HaaseIT\HCSF\Controller\Shop |
27
|
|
|
*/ |
28
|
|
|
class Updatecart extends Base |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
public function preparePage() |
34
|
|
|
{ |
35
|
|
|
$this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager); |
36
|
|
|
$this->P->cb_pagetype = 'content'; |
37
|
|
|
|
38
|
|
|
if ( |
39
|
|
|
( |
40
|
|
|
$this->config->getShop('show_pricesonlytologgedin') |
41
|
|
|
&& !$this->helperCustomer->getUserData() |
42
|
|
|
) |
43
|
|
|
|| filter_input(INPUT_SERVER, 'HTTP_REFERER') === null |
44
|
|
|
) { |
45
|
|
|
$this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T('denied_default'); |
46
|
|
|
} else { |
47
|
|
|
$iAmount = filter_input(INPUT_POST, 'amount', FILTER_SANITIZE_NUMBER_INT); |
48
|
|
|
$postitemno = filter_input(INPUT_POST, 'itemno', FILTER_SANITIZE_SPECIAL_CHARS); |
49
|
|
|
|
50
|
|
|
if (empty($postitemno) || !is_numeric($iAmount)) { |
51
|
|
|
$this->replyToCartUpdate('noitemnooramount'); |
52
|
|
|
} else { |
53
|
|
|
$iAmount = floor($iAmount); |
54
|
|
|
|
55
|
|
|
// Check if this item exists |
56
|
|
|
$aData = $this->serviceManager->get('oItem')->sortItems('', $postitemno); |
57
|
|
|
if (!isset($aData)) { |
58
|
|
|
$this->replyToCartUpdate('itemnotfound'); |
59
|
|
|
} else { |
60
|
|
|
// are there additional items to this item, if so, check if they are valid, too. |
61
|
|
|
$postadditionalitems = filter_input(INPUT_POST, 'additionalitems', FILTER_SANITIZE_SPECIAL_CHARS); |
62
|
|
|
if (!empty($postadditionalitems)) { |
63
|
|
|
|
64
|
|
|
if (strpos($postadditionalitems, '~') !== false) { |
65
|
|
|
$postadditionalitems = explode('~', $postadditionalitems); |
66
|
|
|
} else { |
67
|
|
|
$postadditionalitems = [$postadditionalitems]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$additionaldata = $this->serviceManager->get('oItem')->sortItems('', $postadditionalitems); |
71
|
|
|
|
72
|
|
|
if (count($postadditionalitems) != count($additionaldata['item'])) { |
73
|
|
|
$this->replyToCartUpdate('itemnotfound'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// build the key for this item for the shoppingcart |
78
|
|
|
$sItemno = $aData['item'][$postitemno]['itm_no']; |
79
|
|
|
$sCartKey = $sItemno; |
80
|
|
|
|
81
|
|
|
if (!empty($this->config->getShop('custom_order_fields'))) { |
82
|
|
|
foreach ($this->config->getShop('custom_order_fields') as $sValue) { |
83
|
|
|
if (isset($aData['item'][$sItemno]['itm_data'][$sValue])) { |
84
|
|
|
$aOptions = []; |
85
|
|
|
$TMP = explode('|', $aData['item'][$sItemno]['itm_data'][$sValue]); |
86
|
|
|
foreach ($TMP as $sTMPValue) { |
87
|
|
|
if (!empty($sTMPValue)) { |
88
|
|
|
$aOptions[] = $sTMPValue; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
unset($sTMP); |
92
|
|
|
|
93
|
|
|
$currentpost = filter_input(INPUT_POST, $sValue); |
94
|
|
|
if ($currentpost !== null && in_array($currentpost, $aOptions)) { |
95
|
|
|
$sCartKey .= '|'.$sValue.':'.$currentpost; |
96
|
|
|
} else { |
97
|
|
|
$this->replyToCartUpdate('requiredfieldmissing'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
// if this Items is not in cart and amount is 0, no need to do anything, return to referer |
103
|
|
|
if ($iAmount == 0 && !isset($_SESSION['cart'][$sCartKey])) { |
104
|
|
|
$this->replyToCartUpdate('noactiontaken'); |
105
|
|
|
} |
106
|
|
|
$aItem = [ |
107
|
|
|
'amount' => $iAmount, |
108
|
|
|
'price' => $this->serviceManager->get('oItem')->calcPrice($aData['item'][$sItemno]), |
109
|
|
|
'rg' => $aData['item'][$sItemno]['itm_rg'], |
110
|
|
|
'vat' => $aData['item'][$sItemno]['itm_vatid'], |
111
|
|
|
'name' => $aData['item'][$sItemno]['itm_name'], |
112
|
|
|
'img' => $aData['item'][$sItemno]['itm_img'], |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
if (filter_input(INPUT_POST, 'action') === 'add') { |
116
|
|
|
$this->addItemToCart($sCartKey, $aItem); |
117
|
|
|
|
118
|
|
|
if (!empty($postadditionalitems)) { |
119
|
|
|
foreach ($postadditionalitems as $additionalitem) { |
120
|
|
|
$this->addItemToCart( |
121
|
|
|
$additionalitem, |
122
|
|
|
[ |
123
|
|
|
'amount' => $iAmount, |
124
|
|
|
'price' => $this->serviceManager->get('oItem')->calcPrice($additionaldata['item'][$additionalitem]), |
|
|
|
|
125
|
|
|
'rg' => $additionaldata['item'][$additionalitem]['itm_rg'], |
126
|
|
|
'vat' => $additionaldata['item'][$additionalitem]['itm_vatid'], |
127
|
|
|
'name' => $additionaldata['item'][$additionalitem]['itm_name'], |
128
|
|
|
'img' => $additionaldata['item'][$additionalitem]['itm_img'], |
129
|
|
|
] |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} else { |
134
|
|
|
if (isset($_SESSION['cart'][$sCartKey])) { // if this item is already in cart, update amount |
135
|
|
|
if ($iAmount == 0) { // new amount == 0 -> remove from cart |
136
|
|
|
unset($_SESSION['cart'][$sCartKey]); |
137
|
|
|
if (count($_SESSION['cart']) == 0) { // once the last cart item is unset, we no longer need cartpricesums |
138
|
|
|
unset($_SESSION['cartpricesums']); |
139
|
|
|
} |
140
|
|
|
$this->replyToCartUpdate('removed', ['cartkey' => $sCartKey]); |
141
|
|
|
} else { // update amount |
142
|
|
|
$_SESSION['cart'][$sCartKey]['amount'] = $iAmount; |
143
|
|
|
$this->replyToCartUpdate('updated', ['cartkey' => $sCartKey, 'amount' => $iAmount]); |
144
|
|
|
} |
145
|
|
|
} else { // if this item is not in the cart yet, add it |
146
|
|
|
$_SESSION['cart'][$sCartKey] = $aItem; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
$this->replyToCartUpdate('added', ['cartkey' => $sCartKey, 'amount' => $iAmount]); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
$this->helper->terminateScript(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $sReply |
158
|
|
|
* @param array $aMore |
159
|
|
|
*/ |
160
|
|
|
private function replyToCartUpdate($sReply, $aMore = []) { |
161
|
|
|
if (filter_input(INPUT_GET, 'ajax') !== null) { |
162
|
|
|
$aAR = [ |
163
|
|
|
'cart' => $_SESSION['cart'], |
164
|
|
|
'reply' => $sReply, |
165
|
|
|
'cartsums' => $this->helperShop->calculateCartItems($_SESSION['cart']), |
166
|
|
|
'currency' => $this->config->getShop('waehrungssymbol'), |
167
|
|
|
'numberformat_decimals' => $this->config->getCore('numberformat_decimals'), |
168
|
|
|
'numberformat_decimal_point' => $this->config->getCore('numberformat_decimal_point'), |
169
|
|
|
'numberformat_thousands_seperator' => $this->config->getCore('numberformat_thousands_seperator'), |
170
|
|
|
]; |
171
|
|
|
if (count($aMore)) { |
172
|
|
|
$aAR = array_merge($aAR, $aMore); |
173
|
|
|
} |
174
|
|
|
echo $this->serviceManager->get('twig')->render('shop/update-cart.twig', $aAR); |
175
|
|
|
} else { |
176
|
|
|
$aMSG['msg'] = $sReply; |
|
|
|
|
177
|
|
|
if (count($aMore)) { |
178
|
|
|
$aMSG = array_merge($aMSG, $aMore); |
179
|
|
|
} |
180
|
|
|
header('Location: '.\HaaseIT\Toolbox\Tools::makeLinkHRefWithAddedGetVars(filter_input(INPUT_SERVER, 'HTTP_REFERER', FILTER_SANITIZE_URL), $aMSG, true, false)); |
181
|
|
|
} |
182
|
|
|
$this->helper->terminateScript(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
protected function addItemToCart($cartkey, $item) |
186
|
|
|
{ |
187
|
|
|
if (isset($_SESSION['cart'][$cartkey])) { // if this item is already in cart, add to amount |
188
|
|
|
$_SESSION['cart'][$cartkey]['amount'] += $item['amount']; |
189
|
|
|
} else { |
190
|
|
|
$_SESSION['cart'][$cartkey] = $item; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return true; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: