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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 
<?php
/* --------------------------------------------------------------
  JanolawModuleCenterModuleController.inc.php 2018-06-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]
  --------------------------------------------------------------
*/

/**
 * Class JanolawModuleCenterModuleController
 * @extends    AbstractModuleCenterModuleController
 * @category   System
 * @package    Modules
 * @subpackage Controllers
 */
class JanolawModuleCenterModuleController extends AbstractModuleCenterModuleController
{
    /**
     * @var CI_DB_query_builder
     */
    protected $db;

    /**
     * @var array $configurationKeys
     */
    protected $configurationKeys = array();


    protected function _init()
    {
        $gxCoreLoader = MainFactory::create('GXCoreLoader', MainFactory::create('GXCoreLoaderSettings'));
        $this->db     = $gxCoreLoader->getDatabaseQueryBuilder();

        $this->configurationKeys = array(
            'MODULE_GAMBIO_JANOLAW_STATUS',
            'MODULE_GAMBIO_JANOLAW_USER_ID',
            'MODULE_GAMBIO_JANOLAW_SHOP_ID',
            'MODULE_GAMBIO_JANOLAW_USE_IN_PDF'
        );

        $this->redirectUrl = xtc_href_link('gm_janolaw.php');

        $this->pageTitle = $this->languageTextManager->get_text('janolaw_title');
    }


    /**
     * Returns an AdminPageHttpControllerResponse with the janolaw configuration template
     *
     * @return AdminPageHttpControllerResponse
     */
    public function actionConfig()
    {
        $this->contentView->set_template_dir(DIR_FS_ADMIN . 'html/content/module_center/');

        $versionInfo = array(
            'version' => 0,
            'multilang' => false,
        );
        if(MODULE_GAMBIO_JANOLAW_STATUS !== 'False')
        {
            $janolaw = MainFactory::create('GMJanolaw');
            $versionInfo = $janolaw->versionCheck();
        }

        $html = $this->_render('janolaw_configuration.html', array(
            'configuration'  => $this->_getConfiguration(),
            'info_page_link' => xtc_href_link('gm_janolaw.php'),
            'version_info' => $versionInfo,
        ));

        return MainFactory::create('AdminPageHttpControllerResponse', $this->pageTitle, $html);
    }


    /**
     * Save janolaw configuration
     *
     * @return RedirectHttpControllerResponse
     */
    public function actionStore()
    {
        if($this->_getPostData('configure_contents') !== null)
        {
            $this->configureContents();
            isset($GLOBALS['messageStack']) or $GLOBALS['messageStack'] = new messageStack();
            $GLOBALS['messageStack']->add_session($this->languageTextManager->get_text('janolaw_contents_configured'), 'info');
        }
        else
        {
            $versioninfo_cache_file = DIR_FS_CATALOG.'cache/janolaw-versioninfo.pdc';
            @unlink($versioninfo_cache_file);
            $this->_store($this->_getPostDataCollection());
        }

        $url = xtc_href_link('admin.php', 'do=JanolawModuleCenterModule/Config');
        return MainFactory::create('RedirectHttpControllerResponse', $url);
    }

    /**
     * configures content manager entries to use Janolaw media
     */
    protected function configureContents()
    {
        $db          = StaticGXCoreLoader::getDatabaseQueryBuilder();
        $languages   = $this->getLanguages();
        $janolaw     = MainFactory::create('GMJanolaw');
        $versionInfo = $janolaw->versionCheck();

        $contentFiles =
        [
            2       => 'janolaw_datenschutz.php',
            3       => 'janolaw_agb.php',
            4       => 'janolaw_impressum.php',
            3889896 => 'janolaw_widerruf.php',
        ];
        foreach($contentFiles as $content_group => $content_file)
        {
            $db->set('content_file', $content_file)
               ->set('content_type', 'file')
               ->where('content_group', $content_group)
               ->update('content_manager');
        }

        if($versionInfo['version'] == 3)
        {
            $downloadFiles =
            [
                2       => 'datasecurity',
                3       => 'terms',
                4       => 'legaldetails',
                3889896 => 'revocation',
            ];

            foreach($languages as $language_id => $language_code)
            {
                if($versionInfo['multilang'] === false)
                {
                    $language_code = 'de';
                }
                $withdrawalFormPdf = $janolaw->get_pdf_file('model-withdrawal-form', $language_code);
                if($withdrawalFormPdf !== false)
                {
                    gm_set_content('WITHDRAWAL_FORM_FILE', basename($withdrawalFormPdf), $language_id);
                }
                else
                {
                    gm_set_content('WITHDRAWAL_FORM_FILE', '', $language_id);
                }
                foreach($downloadFiles as $content_group => $download_file)
                {
                    $pdfFile = $janolaw->get_pdf_file($download_file, $language_code);
                    if($pdfFile !== false)
                    {
                        $db->set('download_file', basename($pdfFile));
                        $db->where('content_group', $content_group);
                        $db->where('languages_id', $language_id);
                        $db->update('content_manager');
                    }
                }
            }
        }
    }

    protected function getLanguages()
    {
        $supportedLanguages = ['de', 'en', 'fr'];
        $db = StaticGXCoreLoader::getDatabaseQueryBuilder();
        $languages = [];
        foreach($db->get('languages')->result() as $row)
        {
            if(in_array($row->code, $supportedLanguages))
            {
                $languages[$row->languages_id] = $row->code;
            }
        }
        return $languages;
    }

    /**
     * Update janolaw configuration in the database
     *
     * @param KeyValueCollection $userInputCollection
     */
    protected function _store(KeyValueCollection $userInputCollection)
    {
        foreach($userInputCollection->getArray() as $configurationKey => $configurationValue)
        {
            $this->db->set('configuration_value', trim($configurationValue))
                     ->where('configuration_key', $configurationKey)
                     ->update('configuration');
        }
    }


    /**
     * Loads the janolaw configuration from the database
     *
     * @return array $janolawConfiguration
     */
    protected function _getConfiguration()
    {
        $janolawConfiguration       = array();
        $janolawConfigurationResult = $this->db->select('configuration_key, configuration_value')
                                               ->from('configuration')
                                               ->where_in('configuration_key', $this->configurationKeys)
                                               ->get();
        foreach($janolawConfigurationResult->result() as $row)
        {
            $janolawConfiguration[$row->configuration_key] = $row->configuration_value;
        }

        return $janolawConfiguration;
    }
}