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 
<?php
/* --------------------------------------------------------------
   CreateRegistreeController.inc.php 2018-04-19
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2018 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 CreateRegistreeController
 *
 * @category System
 * @package  HttpViewControllers
 */
class CreateRegistreeController extends HttpViewController
{
    /**
     * @return HttpControllerResponse|RedirectHttpControllerResponse
     */
    public function actionDefault()
    {
        if(isset($_SESSION['customer_id']))
        {
            return MainFactory::create('RedirectHttpControllerResponse', xtc_href_link(FILENAME_ACCOUNT, '', 'SSL'));
        }
        
        return $this->_getResponse($this->_getMainContentHtml());
    }
    
    
    /**
     * TODO use of MainFactory is only an interim solution
     *
     * @return HttpControllerResponse|RedirectHttpControllerResponse
     */
    public function actionProceed()
    {
        if(isset($_SESSION['customer_id']))
        {
            return MainFactory::create('RedirectHttpControllerResponse', xtc_href_link(FILENAME_ACCOUNT, '', 'SSL'));
        }
        
        // proceed with standard page, if actionProcees is not called by a POST request
        if(!count($this->_getPostDataCollection()->getArray()))
        {
            return $this->actionDefault();
        }
        
        /**
         * @var CountryService $countryService
         */
        $countryService = StaticGXCoreLoader::getService('Country');
        
        try
        {
            $inputTransformer     = MainFactory::create('CustomerInputToCollectionTransformer');
            $customerCollection   = $inputTransformer->getRegistreeCollectionFromInputArray($this->_getPostDataCollection()
                                                                                                 ->getArray(),
                                                                                            $countryService);
            $createAccountProcess = MainFactory::create('CreateAccountProcess',
                                                        StaticGXCoreLoader::getService('CustomerWrite'),
                                                        $countryService);
            
            $createAccountProcess->proceedRegistree($customerCollection,
                                                    MainFactory::create('GMLogoManager', 'gm_logo_mail'));
            
            $redirectUrl = FILENAME_SHOPPING_CART;
            
            if($this->_getQueryParameter('checkout_started') === '1')
            {
                $redirectUrl = FILENAME_CHECKOUT_SHIPPING;
            }
            
            return MainFactory::create('RedirectHttpControllerResponse', xtc_href_link($redirectUrl, '', 'SSL'));
        }
        catch(InvalidCustomerDataException $e)
        {
            return $this->_getResponse($this->_getMainContentHtml($customerCollection, $e->getErrorMessageCollection(),
                                                                  true));
        }
        catch(InvalidArgumentException $e)
        {
            // Catches forged requests with invalid content to prevent that the error log will pile up if a bot goes rouge
            
            // TODO replace new KeyValueCollection(array()) with something meaningful
            return $this->_getResponse($this->_getMainContentHtml($customerCollection, new KeyValueCollection(array()),
                                                                  true));
        }
    }
    
    
    /**
     * @param string $p_mainContentHtml
     *
     * @return HttpControllerResponse|RedirectHttpControllerResponse
     */
    protected function _getResponse($p_mainContentHtml)
    {
        $GLOBALS['breadcrumb']->add(NAVBAR_TITLE_CREATE_ACCOUNT, xtc_href_link('shop.php',
                                                                               xtc_get_all_get_params(array('do'))
                                                                               . '&do=CreateRegistree', 'SSL'));
        
        $layoutContentControl = MainFactory::create_object('LayoutContentControl');
        $layoutContentControl->set_data('GET', $this->_getQueryParametersCollection()->getArray());
        $layoutContentControl->set_data('POST', $this->_getPostDataCollection()->getArray());
        $layoutContentControl->set_('coo_breadcrumb', $GLOBALS['breadcrumb']);
        $layoutContentControl->set_('coo_product', $GLOBALS['product']);
        $layoutContentControl->set_('coo_xtc_price', $GLOBALS['xtPrice']);
        $layoutContentControl->set_('c_path', $GLOBALS['cPath']);
        $layoutContentControl->set_('main_content', $p_mainContentHtml);
        $layoutContentControl->set_('request_type', $GLOBALS['request_type']);
        $layoutContentControl->proceed();
        
        $redirectUrl = $layoutContentControl->get_redirect_url();
        if(!empty($redirectUrl))
        {
            return MainFactory::create('RedirectHttpControllerResponse', $redirectUrl);
        }
        
        return MainFactory::create('HttpControllerResponse', $layoutContentControl->get_response());
    }
    
    
    /**
     * @param KeyValueCollection $customerCollection
     * @param KeyValueCollection $errorMessageCollection
     * @param bool               $p_process
     *
     * @return string
     */
    protected function _getMainContentHtml(KeyValueCollection $customerCollection = null,
                                           KeyValueCollection $errorMessageCollection = null,
                                           $p_process = false)
    {
        $customerArray = array();
        if($customerCollection !== null)
        {
            $customerArray = $customerCollection->getArray();
        }
        
        $errorMessages = array();
        if($errorMessageCollection !== null)
        {
            $errorMessages = $errorMessageCollection->getArray();
        }
        
        $createAccountContentView = MainFactory::create_object('CreateAccountContentView');
        
        $createAccountContentView->set_('guest_account', false);
        $createAccountContentView->set_('customer_data_array', $customerArray);
        $createAccountContentView->set_('error_array', $errorMessages);
        $createAccountContentView->set_('process', $p_process);
        $createAccountContentView->set_('checkout_started', (int)$this->_getQueryParameter('checkout_started'));
        
        return $createAccountContentView->get_html();
    }
}