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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 
<?php
/* --------------------------------------------------------------
   SharedShoppingCartController.inc.php 2016-07-06
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2016 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

MainFactory::load_class('HttpViewController');

/**
 * Class SharedShoppingCartController
 *
 * @category System
 * @package HttpViewControllers
 */
class SharedShoppingCartController extends HttpViewController
{
    /**
     * Adds products of a shared shopping cart to the current shopping cart of the customer .
     * 
     * @return JsonHttpControllerResponse
     */
    public function actionDefault()
    {
        $propertiesControl = MainFactory::create_object('PropertiesControl');
        $products = $this->_getSharedCart();

        foreach($products as $product)
        {
            $this->_addProductToCart($propertiesControl, $product);
        }

        return new RedirectHttpControllerResponse(xtc_href_link('shopping_cart.php'));
    }

    /**
     * Stores the current shopping cart content and creates a hash to address the shopping cart.
     * 
     * @return JsonHttpControllerResponse
     */
    public function actionStoreShoppingCart()
    {
        $products = $this->_getCart();
        $hash = $this->_storeShoppingCart($products);

        return new JsonHttpControllerResponse(array('success' => true, 'link' => xtc_href_link('shop.php', 'do=SharedShoppingCart&cart=' . $hash)));
    }

    /**
     * Extracts product information from the current shopping cart.
     * The returned array is structured as follows:
     *  array(
     *      productId => <int>,
     *      quantity => <int>,
     *      [combiId => <int>],
     *      [attributes => array(
     *          attributeId => <int>,
     *          attributeValueId => <int>
     *      )]
     *  )
     * 
     * @return array Am array of product information
     */
    protected function _getCart()
    {
        $propertiesControl = MainFactory::create_object('PropertiesControl');
        $cart = $_SESSION['cart']->get_products();
        $products = array();

        if($cart === false)
        {
            return array();
        }

        foreach($cart as $productData)
        {
            $uniqueProductId = $productData['id'];
            $productId = (int)xtc_get_prid($uniqueProductId);
            $combiId = $propertiesControl->extract_combis_id($uniqueProductId);

            if($combiId !== '' && $propertiesControl->combi_exists($productId, $combiId) === false)
            {
                continue;
            }

            $product = array();
            $product['productId'] = $productId;
            $product['quantity'] = (double)$productData['quantity'];
            if($combiId !== '')
            {
                $product['combiId'] = (int)$combiId;
            }
            if(is_array($productData['attributes']))
            {
                foreach($productData['attributes'] as $attributeId => $attributeValueId)
                {
                    $attribute = array(
                        'attributeId' => (int)$attributeId,
                        'attributeValueId' => (int)$attributeValueId
                    );
                    $product['attributes'][] = $attribute;
                }
            }

            $products[] = $product;
        }
        
        return $products;
    }

    /**
     * Stores the given product information as a shopping cart.
     * The expected array parameter has to be structured as follows:
     *  array(
     *      productId => <int>,
     *      quantity => <int>,
     *      [combiId => <int>],
     *      [attributes => array(
     *          attributeId => <int>,
     *          attributeValueId => <int>
     *      )]
     *  )
     * 
     * @param array $products The product information to store
     * @return string The hash of the stored shopping cart
     */
    protected function _storeShoppingCart(array $products)
    {
        $sharedShoppingCartService = StaticGXCoreLoader::getService('SharedShoppingCart');
        try
        {
            return $sharedShoppingCartService->storeShoppingCart($products, new IdType((int)$_SESSION['customer_id']));
        }
        catch(InvalidArgumentException $exception)
        {
            //TODO: handle exception properly
            return '';
        }
    }

    /**
     * Gathers shopping cart information from a stored cart in JSON format.
     * 
     * @return array|mixed The fetched cart information
     */
    protected function _getSharedCart()
    {
        $sharedShoppingCartService = StaticGXCoreLoader::getService('SharedShoppingCart');
        $hash = $this->_getQueryParameter('cart');
        if($hash === null)
        {
            return array();
        }
        
        try
        {
            return $sharedShoppingCartService->getShoppingCart(new StringType($hash));
        }
        catch(InvalidArgumentException $exception)
        {
            //TODO: handle exception properly
            return array();
        }
    }

    /**
     * Adds a product from given product information to the current shopping cart.
     * 
     * @param PropertiesControl $propertiesControl An instance of the PropertiesControl
     * @param stdClass $product Cart product information
     */
    protected function _addProductToCart(PropertiesControl $propertiesControl, stdClass $product)
    {
        $productId = $product->productId;
        $combiId = 0;
        if(isset($product->combiId))
        {
            $combiId = $product->combiId;

            if($propertiesControl->combi_exists($productId, $combiId) === false)
            {
                return;
            }
        }

        $attributes = '';
        if(isset($product->attributes))
        {
            $attributes = array();
            foreach($product->attributes as $attributeData)
            {
                $attributes[$attributeData->attributeId] = $attributeData->attributeValueId;
            }
        }

        $_SESSION['cart']->add_cart($productId, $product->quantity, $attributes, true, $combiId);
    }
}