<?php
/*
*

 ______   _______  __   __  _______  _______  ___   _______ 
|      | |       ||  |_|  ||       ||       ||   | |       |
|  _    ||    ___||       ||   _   ||_     _||   | |       |
| | |   ||   |___ |       ||  | |  |  |   |  |   | |       |
| |_|   ||    ___||       ||  |_|  |  |   |  |   | |      _|
|       ||   |___ | ||_|| ||       |  |   |  |   | |     |_ 
|______| |_______||_|   |_||_______|  |___|  |___| |_______|
 _______  __   __  _______                                  
|       ||  |_|  ||       |                                 
|       ||       ||  _____|                                 
|       ||       || |_____                                  
|      _||       ||_____  |                                 
|     |_ | ||_|| | _____| |                                 
|_______||_|   |_||_______|                                                   
                                              
                                           
*
*
*/

/**
 * Demotic CMS is a fork of ImpressPages.
 * AUTHOR: Demotic
 * @package Demotic
 */

namespace Demotic\Gebtu\Design;


class Event
{
    public static function ipBeforeController()
    {
        $configModel = ConfigModel::instance();
        if ($configModel->isInPreviewState()) {
            ipAddJsVariable('ipRepositoryUrl', ipFileUrl('file/repository/'));
            static::initConfig();
        }

        $lessCompiler = LessCompiler::instance();
        if (ipConfig()->isDevelopmentEnvironment()) {
            if ($lessCompiler->shouldRebuild(ipConfig()->theme())) {
                $lessCompiler->rebuild(ipConfig()->theme());
            }
        }
    }

    /*
     * Loading in Preview mode
     */
    protected static function initConfig()
    {
        ipAddCss('Demotic/Gebtu/Core/assets/admin/admin.css');
        ipAddJs('Demotic/Gebtu/Core/assets/js/jquery-ui/jquery-ui.js');
        ipAddJsVariable('ipTranslationSaving', __('Saving...', 'Demotic-admin', false));
        ipAddJs('Demotic/Gebtu/Design/assets/optionsBox.js');
        ipAddJsVariable('ipModuleDesignConfiguration', Helper::getConfigurationBoxHtml());

        if (file_exists(ipThemeFile(Model::INSTALL_DIR . 'Options.js'))) {
            ipAddJs(ipThemeUrl(Model::INSTALL_DIR . 'Options.js'));
        } elseif (file_exists(ipThemeFile(Model::INSTALL_DIR . 'options.js'))) {
            ipAddJs(ipThemeUrl(Model::INSTALL_DIR . 'options.js'));
        }

        $model = Model::instance();
        $theme = $model->getTheme(ipConfig()->theme());
        if (!$theme) {
            throw new \Demotic\Exception("Theme doesn't exist");
        }

        $options = $theme->getOptionsAsArray();

        $fieldNames = [];
        foreach ($options as $option) {
            if (empty($option['name'])) {
                continue;
            }
            $fieldNames[] = $option['name'];
        }
        ipAddJsVariable('ipModuleDesignOptionNames', $fieldNames);
    }


}
