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 
<?php

/* --------------------------------------------------------------
 JSWidgetsAjaxController.inc.php 2017-08-10
 Gambio GmbH
 http://www.gambio.de
 Copyright (c) 2017 Gambio GmbH
 Released under the GNU General Public License (Version 2)
 [http://www.gnu.org/licenses/gpl-2.0.html]
 --------------------------------------------------------------
 */

MainFactory::load_class('AdminHttpViewController');

/**
 * Class JSWidgetsAjaxController
 *
 * @category   System
 * @package    AdminHttpViewControllers
 * @extends    AdminHttpViewController
 */
class JSWidgetsAjaxController extends AdminHttpViewController
{
    public function actionIconInput()
    {
        $languageProvider = MainFactory::create('LanguageProvider', StaticGXCoreLoader::getDatabaseQueryBuilder());
        $languageId       = $this->_getQueryParameter('language_id');
        $langId           = new IdType($languageId);
        if(defined('ENABLE_SSL_CATALOG') && (string)ENABLE_SSL_CATALOG === 'true')
        {
            $urlPrefix = HTTPS_CATALOG_SERVER;
        }
        else
        {
            $urlPrefix = HTTP_CATALOG_SERVER;
        }
        $iconUrl = $urlPrefix . DIR_WS_LANGUAGES . $languageProvider->getDirectoryById($langId) . '/admin/images/'
                   . $languageProvider->getIconFilenameByCode($languageProvider->getCodeById($langId));
        
        return new JsonHttpControllerResponse([
                                                  'iconUrl' => $iconUrl
                                              ]);
    }
    
    
    /**
     * Detects if the file manager is installed or not.
     *
     * @return \JsonHttpControllerResponse
     */
    public function actionResponsiveFileManagerExists()
    {
        return MainFactory::create('JsonHttpControllerResponse',
                                   ['exists' => is_dir(DIR_FS_CATALOG . 'ResponsiveFilemanager')]);
    }
    
    
    /**
     * Checks if the group check is enabled.
     * If true, the response provide information about the existing customer groups.
     *
     * @return \JsonHttpControllerResponse
     */
    public function actionIsGroupCheckEnabled()
    {
        $db = StaticGXCoreLoader::getDatabaseQueryBuilder();
        
        $status = $db->from('configuration')
                     ->where('configuration_key', 'GROUP_CHECK')
                     ->get()
                     ->row_array()['configuration_value'] === 'true';
        
        $response = ['status' => $status];
        if($status)
        {
            /** @var \CustomerGroupReadServiceInterface $customerGroupReadService */
            $customerGroupReadService   = StaticGXCoreLoader::getService('CustomerGroupRead');
            $customerGroups             = $customerGroupReadService->getAll();
            $response['customerGroups'] = $this->_serializeCustomerGroupCollection($customerGroups);
        }
        
        return MainFactory::create('JsonHttpControllerResponse', $response);
    }
    
    
    /**
     * Serializes customer group collections.
     * @TODO: Replace the methods with the customer group serializer, once it is finished.
     *
     * @param \CustomerGroupCollection $customerGroupCollection Customer group collection to be serialized.
     *
     * @return array Serialized customer group collection array.
     */
    protected function _serializeCustomerGroupCollection(CustomerGroupCollection $customerGroupCollection)
    {
        $data = [];
        foreach($customerGroupCollection->getArray() as $customerGroup)
        {
            $data[] = $this->_serializeCustomerGroup($customerGroup);
        }
        
        return $data;
    }
    
    
    /**
     * Serializes customer group entities.
     * @TODO: Replace the methods with the customer group serializer, once it is finished.
     *
     * @param \CustomerGroupInterface $customerGroup Customer group entity to be serialized.
     *
     * @return array Serialized customer group array.
     */
    protected function _serializeCustomerGroup(CustomerGroupInterface $customerGroup)
    {
        return [
            'id'             => $customerGroup->getId(),
            'names'          => $this->_serializeCustomerGroupNames($customerGroup->getNames()),
            'settings'       => $this->_serializeCustomerGroupSettings($customerGroup->getSettings()),
            'configurations' => $this->_serializeCustomerGroupConfigurations($customerGroup->getConfigurations())
        ];
    }
    
    
    /**
     * Serializes customer group names array.
     * @TODO: Replace the methods with the customer group serializer, once it is finished.
     *
     * @param array $customerGroupNames Customer group names array to be serialized.
     *
     * @return array Serialized customer group names array.
     */
    protected function _serializeCustomerGroupNames(array $customerGroupNames)
    {
        $data = [];
        foreach($customerGroupNames as $languageCode => $name)
        {
            $data[$languageCode] = $name;
        }
        
        return $data;
    }
    
    
    /**
     * Serializes customer group settings value objects.
     * @TODO: Replace the methods with the customer group serializer, once it is finished.
     *
     * @param \CustomerGroupSettingsInterface $settings Customer group settings object to be serialized.
     *
     * @return array Serialized customer group settings array.
     */
    protected function _serializeCustomerGroupSettings(CustomerGroupSettingsInterface $settings)
    {
        return [
            'public'             => $settings->isPublic(),
            'otDiscountFlag'     => $settings->isOtDiscountFlag(),
            'graduatedPrices'    => $settings->isGraduatedPrices(),
            'showPrice'          => $settings->isShowPrice(),
            'showPriceTax'       => $settings->isShowPriceTax(),
            'addTaxOt'           => $settings->isAddTaxOt(),
            'discountAttributes' => $settings->isDiscountAttributes(),
            'fsk18'              => $settings->isFsk18(),
            'fsk18Display'       => $settings->isFsk18Display(),
            'writeReviews'       => $settings->isWriteReviews(),
            'readReviews'        => $settings->isReadReviews()
        ];
    }
    
    
    /**
     * Serializes customer group configuration value objects.
     * @TODO: Replace the methods with the customer group serializer, once it is finished.
     *
     * @param \CustomerGroupConfigurationsInterface $configurations Customer group configuration object to be
     *                                                              serialized.
     *
     * @return array Serialized customer group configuration array.
     */
    protected function _serializeCustomerGroupConfigurations(CustomerGroupConfigurationsInterface $configurations)
    {
        return [
            'minOrder'                 => $configurations->getMinOrder(),
            'maxOrder'                 => $configurations->getMaxOrder(),
            'discount'                 => $configurations->getDiscount(),
            'otDiscount'               => $configurations->getOtDiscount(),
            'unallowedPaymentModules'  => $configurations->getUnallowedPaymentModules(),
            'unallowedShippingModules' => $configurations->getUnallowedShippingModules()
        ];
    }
}