| 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 |  |  | use HaaseIT\HCSF\HelperConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * Class Updatecart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @package HaaseIT\HCSF\Controller\Shop | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | class Updatecart extends Base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function preparePage() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->P->cb_pagetype = 'content'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 HelperConfig::$shop["show_pricesonlytologgedin"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 && !\HaaseIT\HCSF\Customer\Helper::getUserData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             || !isset($_SERVER["HTTP_REFERER"]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T("denied_default"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $iAmount = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             if (isset($_REQUEST["amount"])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 $iAmount = $_REQUEST["amount"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             if (empty($_REQUEST["itemno"]) || !is_numeric($iAmount)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $this->replyToCartUpdate('noitemnooramount'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 $iAmount = floor($iAmount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 // Check if this item exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 $aData = $this->serviceManager->get('oItem')->sortItems('', $_REQUEST["itemno"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 if (!isset($aData)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     $this->replyToCartUpdate('itemnotfound'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     // are there additional items to this item, if so, check if they are valid, too. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                     if (!empty($_POST['additionalitems'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                         $additionalitems = filter_input(INPUT_POST, 'additionalitems', FILTER_SANITIZE_SPECIAL_CHARS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                         if (strpos($additionalitems, '~') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                             $additionalitems = explode('~', $additionalitems); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                             $additionalitems = [$additionalitems]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                         $additionaldata = $this->serviceManager->get('oItem')->sortItems('', $additionalitems); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                         if (count($additionalitems) != count($additionaldata['item'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                             $this->replyToCartUpdate('itemnotfound'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     // build the key for this item for the shoppingcart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     $sItemno = $aData["item"][$_REQUEST["itemno"]]['itm_no']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     $sCartKey = $sItemno; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     if (isset(HelperConfig::$shop["custom_order_fields"])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                         foreach (HelperConfig::$shop["custom_order_fields"] as $sValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                             if (isset($aData["item"][$sItemno]["itm_data"][$sValue])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                                 $aOptions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                                 $TMP = explode('|', $aData["item"][$sItemno]["itm_data"][$sValue]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                                 foreach ($TMP as $sTMPValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                                     if (trim($sTMPValue) != '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                                         $aOptions[] = $sTMPValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                                 unset($sTMP); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                                 if (isset($_REQUEST[$sValue]) && in_array($_REQUEST[$sValue], $aOptions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                                     $sCartKey .= '|' . $sValue . ':' . $_REQUEST[$sValue]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                                     $this->replyToCartUpdate('requiredfieldmissing'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     // if this Items is not in cart and amount is 0, no need to do anything, return to referer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     if (!isset($_SESSION["cart"][$sCartKey]) && $iAmount == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                         $this->replyToCartUpdate('noactiontaken'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                     $aItem = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                         'amount' => $iAmount, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                         'price' => $this->serviceManager->get('oItem')->calcPrice($aData["item"][$sItemno]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                         'rg' => $aData["item"][$sItemno]['itm_rg'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                         'vat' => $aData["item"][$sItemno]['itm_vatid'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                         'name' => $aData["item"][$sItemno]['itm_name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                         'img' => $aData["item"][$sItemno]['itm_img'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                     if (!empty($_POST['action']) && $_POST['action'] === 'add') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                         if (isset($_SESSION["cart"][$sCartKey])) { // if this item is already in cart, add to amount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                             $_SESSION["cart"][$sCartKey]['amount'] += $iAmount; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                             $_SESSION["cart"][$sCartKey] = $aItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                         // todo: add additional items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                         if (isset($_SESSION["cart"][$sCartKey])) { // if this item is already in cart, update amount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                             if ($iAmount == 0) { // new amount == 0 -> remove from cart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                                 unset($_SESSION["cart"][$sCartKey]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                                 if (count($_SESSION["cart"]) == 0) { // once the last cart item is unset, we no longer need cartpricesums | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                                     unset($_SESSION["cartpricesums"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                                 $this->replyToCartUpdate('removed', ['cartkey' => $sCartKey]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                             } else { // update amount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                                 $_SESSION["cart"][$sCartKey]["amount"] = $iAmount; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                                 $this->replyToCartUpdate('updated', ['cartkey' => $sCartKey, 'amount' => $iAmount]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                         } else { // if this item is not in the cart yet, add it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                             $_SESSION["cart"][$sCartKey] = $aItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                     $this->replyToCartUpdate('added', ['cartkey' => $sCartKey, 'amount' => $iAmount]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             die(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param string $sReply | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param array $aMore | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |     private function replyToCartUpdate($sReply, $aMore = []) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |         if (isset($_REQUEST["ajax"])) { | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |             $aAR = [ | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |                 'cart' => $_SESSION["cart"], | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |                 'reply' => $sReply, | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |                 'cartsums' => \HaaseIT\HCSF\Shop\Helper::calculateCartItems($_SESSION["cart"]), | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |                 'currency' => HelperConfig::$shop["waehrungssymbol"], | 
            
                                                        
            
                                    
            
            
                | 157 |  |  |                 'numberformat_decimals' => HelperConfig::$core['numberformat_decimals'], | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |                 'numberformat_decimal_point' => HelperConfig::$core['numberformat_decimal_point'], | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |                 'numberformat_thousands_seperator' => HelperConfig::$core['numberformat_thousands_seperator'], | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |             ]; | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |             if (count($aMore)) { | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |                 $aAR = array_merge($aAR, $aMore); | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |             echo $this->serviceManager->get('twig')->render('shop/update-cart.twig', $aAR); | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |         } else { | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |             $aMSG["msg"] =  $sReply; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |             if (count($aMore)) { | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |                 $aMSG = array_merge($aMSG, $aMore); | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |             header('Location: '.\HaaseIT\Toolbox\Tools::makeLinkHRefWithAddedGetVars($_SERVER["HTTP_REFERER"], $aMSG, true, false)); | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |         die(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 175 |  |  | } | 
            
                        
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: