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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 
<?php
/* --------------------------------------------------------------
   AdminLayoutHttpControllerResponse.inc.php 2016-07-26
   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('HttpControllerResponse');

/**
 * Class AdminLayoutHttpControllerResponse
 *
 * This class will be used for rendering the new Admin pages which must be explicitly written in
 * templates. These templates can extend any of the existing admin layouts by themselves.
 *
 * Child controllers can you the "init" method to initialize their dependencies
 *
 * @category System
 * @package  Http
 * @extends  HttpControllerResponse
 */
class AdminLayoutHttpControllerResponse extends HttpControllerResponse
{
    /**
     * Page Title
     *
     * @var string
     */
    protected $title;
    
    /**
     * Template Path
     *
     * @var string
     */
    protected $template;
    
    /**
     * Template data.
     *
     * @var KeyValueCollection
     */
    protected $data;
    
    /**
     * Page Assets
     *
     * Provide paths or filenames to JavaScript, CSS or PHP Translation files.
     *
     * @var AssetCollectionInterface
     */
    protected $assets;
    
    /**
     * Content Sub Navigation
     *
     * The sub navigation will be displayed under the header and can redirect to similar pages.
     *
     * @var ContentNavigationCollectionInterface
     */
    protected $contentNavigation;
    
    /**
     * ContentView instance.
     *
     * Used for parsing the Smarty templates.
     *
     * @var ContentView
     */
    protected $contentView;
    
    
    /**
     * AdminLayoutHttpViewController constructor.
     *
     * @param NonEmptyStringType                        $title             Page title.
     * @param ExistingFile                              $template          Template absolute path.
     * @param KeyValueCollection|null                   $data              A key-value collection containing the data
     *                                                                     to be used by the template.
     * @param AssetCollectionInterface|null             $assets            Page assets (js, css, translations etc).
     * @param ContentNavigationCollectionInterface|null $contentNavigation Sub content navigation (key-value
     *                                                                     collection).
     * @param ContentView|null                          $contentView       Provide a custom content view class if
     *                                                                     needed.
     */
    public function __construct(NonEmptyStringType $title,
                                ExistingFile $template,
                                KeyValueCollection $data = null,
                                AssetCollectionInterface $assets = null,
                                ContentNavigationCollectionInterface $contentNavigation = null,
                                ContentView $contentView = null)
    {
        $this->title             = $title->asString();
        $this->template          = $template->getFilePath();
        $this->data              = $data;
        $this->assets            = $assets;
        $this->contentNavigation = $contentNavigation;
        $this->contentView       = (!empty($contentView)) ? $contentView : MainFactory::create('ContentView');
        $this->_render();
    }
    
    
    /**
     * Render the provided template.
     *
     * Hint: Override this method to change the rendering algorithm.
     *
     * @throws InvalidArgumentException
     * @throws UnexpectedValueException
     */
    protected function _render()
    {
        $developmentEnvironment = file_exists(DIR_FS_CATALOG . '.dev-environment');
        
        $this->contentView->set_flat_assigns(true);
        $this->contentView->set_escape_html(true);
        $this->contentView->set_template_dir(dirname($this->template));
        $this->contentView->set_content_template(basename($this->template));
  
        // Set content data. 
        if($this->data !== null)
        {
            $content = array(); // Content array
            foreach($this->data->getArray() as $key => $value)
            {
                $content[$key] = $value;
            }
            $this->contentView->set_content_data('content', $content); 
        }
        
        // Set $_SESSION
        $this->contentView->set_content_data('_SESSION', $_SESSION);
        
        // Set Base URL
        $this->contentView->set_content_data('base_url', rtrim(HTTP_SERVER . DIR_WS_CATALOG, '/'));
        
        // Set Environment & File Suffix
        $environment = $developmentEnvironment ? 'development' : 'production';
        $this->contentView->set_content_data('environment', $environment);
        $this->contentView->set_content_data('suffix', $environment === 'production' ? '.min' : '');
        
        // Set Template Directory Path 
        $this->contentView->set_content_data('template_dir', DIR_FS_ADMIN . 'html/content');
        
        // Set Page Title
        $this->contentView->set_content_data('page_title', $this->title);
        
        // Set Language Code 
        $this->contentView->set_content_data('language_code', $_SESSION['language_code']);
        
        // Set Shop Version
        $this->contentView->set_content_data('shop_version', gm_get_conf('INSTALLED_VERSION'));

        // Set Shop offline flag
        $this->contentView->set_content_data('shop_offline', gm_get_conf('GM_SHOP_OFFLINE') !== '');
        
        // Set Page Token
        $this->contentView->set_content_data('page_token', $_SESSION['coo_page_token']->generate_token());
        
        // Set Cache Token
        $this->contentView->set_content_data('cache_token', MainFactory::create('CacheTokenHelper')->getCacheToken());
        
        if($developmentEnvironment && $_SESSION['customers_status']['customers_status_id'] === '0' && !isset($_GET['hide_debug_bar']))
        {
            // Enable the debug bar. 
            $this->contentView->set_content_data('debug_bar', true);
            $debugBarAssets = StaticGXCoreLoader::getDebugBarAssets();
            $this->contentView->set_content_data('debug_bar_header_content', $debugBarAssets['head']);
            $this->contentView->set_content_data('debug_bar_body_content', $debugBarAssets['body']);
        }
        else
        {
            // Disable the debug bar.
            $this->contentView->set_content_data('debug_bar', false);
        }
        
        // Set FontAwesome Fallback
        $fontAwesomePath = DIR_FS_ADMIN . 'html/assets/fonts/font-awesome/FontAwesome.otf';
        $this->contentView->set_content_data('fontawesome_fallback', !file_exists($fontAwesomePath));
        
        // Set JavaScript Translations
        $translations = $this->_getTranslations();
        $this->contentView->set_content_data('translations', json_encode($translations));
        
        // Set Main Menu Data 
        $adminMenuContentView = MainFactory::create('AdminMenuContentView');
        $adminMenuContentView->setCustomerId($_SESSION['customer_id']);
        $this->contentView->set_content_data('menu_entries', $adminMenuContentView->prepare_data());

        // Set the initial menu state
        $userConfigurationService = StaticGXCoreLoader::getService('UserConfiguration');
        $menuVisibility = $userConfigurationService->getUserConfiguration(new IdType((int)$_SESSION['customer_id']), 'menuVisibility');
        $this->contentView->set_content_data('menu_visibility', $menuVisibility);
        
        // Get recent search area. 
        $recentSearchArea = $userConfigurationService->getUserConfiguration(new IdType((int)$_SESSION['customer_id']),
                                                                            'recent_search_area');
        $this->contentView->set_content_data('recent_search_area', $recentSearchArea); 

        // Set Shop Key Information
        $this->contentView->set_content_data('shop_key_state', (bool)gm_get_conf('SHOP_KEY_VALID'));
        
        // Set Language Information
        $db               = StaticGXCoreLoader::getDatabaseQueryBuilder();
        $languageProvider = MainFactory::create('LanguageProvider', $db);
        $languages        = array();
        foreach($languageProvider->getCodes()->getArray() as $code)
        {
            $languages[] = array(
                'code' => $code->asString(),
                'name' => $languageProvider->getDirectoryByCode($code)
            );
        }
        asort($languages); // "asort" - german language needs to be first on the array
        $this->contentView->set_content_data('languages', $languages);
        
        // Set Content Navigation
        if($this->contentNavigation !== null)
        {
            $this->contentView->set_content_data('content_navigation', $this->contentNavigation->getArray());
        }
        
        // Set Page Assets
        if($this->assets !== null)
        {
            $scripts = $this->assets->getHtml(new StringType(Asset::JAVASCRIPT));
            $this->contentView->set_content_data('dynamic_script_assets', $scripts); 
            $styles  = $this->assets->getHtml(new StringType(Asset::CSS));
            $this->contentView->set_content_data('dynamic_style_assets', $styles);
        }
        
        // Set Initial Messages
        $this->_setInitialMessages();
        
        // Set message stack data. 
        $this->contentView->set_content_data('message_stack', $GLOBALS['messageStack']->get_messages());
        
        echo $this->contentView->get_html();
    }
    
    
    /**
     * Get the default and assets translations.
     *
     * Hint: Override this method to fetch different default translations.
     *
     * @return array
     */
    protected function _getTranslations()
    {
        $translations                     = ($this->assets !== null) ? $this->assets->getTranslations() : array();
        $languageTextManager              = MainFactory::create('LanguageTextManager', 'general',
                                                                $_SESSION['languages_id']);
        $translations['general']          = $languageTextManager->get_section_array('general');
        $translations['buttons']          = $languageTextManager->get_section_array('buttons');
        $translations['messages']         = $languageTextManager->get_section_array('messages');
        $translations['admin_labels']     = $languageTextManager->get_section_array('admin_labels');
        $translations['admin_general']    = $languageTextManager->get_section_array('admin_general');
        $translations['admin_info_boxes'] = $languageTextManager->get_section_array('admin_info_boxes');
        
        return $translations;
    }
    
    
    /**
     * Set initial messages for new admin layout. 
     */
    protected function _setInitialMessages()
    {
        $languageTextManager = MainFactory::create('LanguageTextManager', 'admin_general', $_SESSION['languages_id']);
        $contentArray        = $this->contentView->get_content_array();

        if($contentArray['environment'] === 'development')
        {
            $GLOBALS['messageStack']->add($languageTextManager->get_text('TEXT_DEV_ENVIRONMENT_WARNING',
                                                                         'admin_general'), 'warning');
        }
        elseif(file_exists(DIR_FS_CATALOG . 'gambio_installer'))
        {
            // Installer directory still exists error message.
            $installerMessage = sprintf($languageTextManager->get_text('WARNING_INSTALL_DIRECTORY_EXISTS',
                'general'), substr(DIR_WS_CATALOG, 0, -1));
    
            $GLOBALS['messageStack']->add($installerMessage, 'error');
        }
    }
}