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 
<?php
/* --------------------------------------------------------------
   CartShippingCostsController.inc.php 2016-07-04
   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]
   --------------------------------------------------------------
*/

/**
 * Class CartShippingCostsController
 *
 * @extends    HttpViewController
 * @category   System
 * @package    HttpViewControllers
 */
class CartShippingCostsController extends HttpViewController
{
    /**
     * @var array
     */
    protected $status = array();
    
    /**
     * @var array
     */
    protected $shippingCostsInformation = array();
    
    /**
     * @var CartShippingCostsAjaxHandler
     */
    protected $cartShippingCostsAjaxHandler;
    
    
    /**
     * @todo get rid of old AjaxHandler
     * @todo use GET and POST REST-API like
     *
     * @return HttpControllerResponse
     */
    public function actionDefault()
    {
        $this->cartShippingCostsAjaxHandler = MainFactory::create('CartShippingCostsAjaxHandler');
        
        $this->_setStatusAndShippingInformation();
        $this->_setShippingModulesSelection();
        $this->_setShippingWeightInformation();
        
        $result = $this->_getResponseArray();
        
        return MainFactory::create('JsonHttpControllerResponse', $result);
    }
    
    
    protected function _setStatusAndShippingInformation()
    {
        $getData = array('action' => 'get_shipping_costs');
        $this->cartShippingCostsAjaxHandler->set_data('GET', $getData);
        $this->cartShippingCostsAjaxHandler->set_data('POST', $this->_getPostDataCollection()->getArray());
        $this->cartShippingCostsAjaxHandler->proceed();
        $result = json_decode($this->cartShippingCostsAjaxHandler->get_response(), true);
        
        $this->status['success']       = true;
        $this->status['error_message'] = $this->status['success']
            ? ''
            : $result['error_message'];
        
        $this->shippingCostsInformation['cart_ot_gambioultra_costs'] = isset($result['cart_ot_gambioultra_costs'])
            ? strip_tags($result['cart_ot_gambioultra_costs'])
            : '';
        $this->shippingCostsInformation['cart_shipping_costs']       = $result['status'] === 'success'
            ? $result['cart_shipping_costs']
            : '';
    }
    
    
    protected function _setShippingModulesSelection()
    {
        $getData = array('action' => 'get_shipping_modules');
        $this->cartShippingCostsAjaxHandler->set_data('GET', $getData);
        $this->cartShippingCostsAjaxHandler->proceed();
        $result = json_decode($this->cartShippingCostsAjaxHandler->get_response(), true);
        
        $this->shippingCostsInformation['cart_shipping_modules'] = $result['html'];
    }
    
    
    protected function _setShippingWeightInformation()
    {
        $getData = array('action' => 'get_shipping_weight');
        $this->cartShippingCostsAjaxHandler->set_data('GET', $getData);
        $this->cartShippingCostsAjaxHandler->proceed();
        $result = json_decode($this->cartShippingCostsAjaxHandler->get_response(), true);
        
        $this->shippingCostsInformation['cart_shipping_weight'] = $result['html'];
    }
    
    
    /**
     * @return array
     */
    protected function _getResponseArray()
    {
        $result = array(
            'success'     => $this->status['success'],
            'status_code' => 1,
            'content'     => array(
                'gambioultra' => array(
                    'selector' => 'gambioUltraCosts',
                    'type'     => 'text',
                    'value'    => $this->shippingCostsInformation['cart_ot_gambioultra_costs']
                ),
                'weight'      => array(
                    'selector' => 'shippingWeight',
                    'type'     => 'replace',
                    'value'    => $this->shippingCostsInformation['cart_shipping_weight']
                ),
                'costs'       => array(
                    'selector' => 'shippingCost',
                    'type'     => 'text',
                    'value'    => $this->shippingCostsInformation['cart_shipping_costs']
                ),
                'modules'     => array(
                    'selector' => 'shippingCalculator',
                    'type'     => 'replace',
                    'value'    => $this->shippingCostsInformation['cart_shipping_modules']
                ),
                'error'       => array(
                    'selector' => 'invalidCombinationError',
                    'type'     => 'text',
                    'value'    => $this->status['error_message']
                )
            )
        );
        
        return $result;
    }
}