* @copyright 2007-2019 Apollotheme * @license http://apollotheme.com - prestashop template provider */ if (!defined('_PS_VERSION_')) { # module validation exit; } class AdminApPageBuilderThemeEditorController extends ModuleAdminControllerCore { public $themeName = ''; public $css_patterns; public function __construct() { $this->bootstrap = true; $this->table = 'appagebuilder_themeeditor'; $this->lang = false; $this->context = Context::getContext(); parent::__construct(); $this->themeName = apPageHelper::getThemeName(); $this->js_patterns = apPageHelper::getConfigDir('_PS_THEME_DIR_').apPageHelper::getJsDir().'patterns/'; $this->css_patterns = apPageHelper::getConfigDir('_PS_THEME_DIR_').apPageHelper::getCssDir().'patterns/'; } public function postProcess() { if (count($this->errors) > 0) { return; } if (Tools::getValue('action') && Tools::getValue('action') == 'savedata' && Tools::getValue('customize')) { $data = LeoFrameworkHelper::getPost(array('action-mode', 'saved_file', 'newfile', 'customize', 'customize_match', 'active'), 0); $selectors = $data['customize']; $matches = $data['customize_match']; $output = ''; $cache = array(); foreach ($selectors as $match => $customizes) { $output .= "\r\n/* customize for $match */ \r\n"; foreach ($customizes as $key => $customize) { if (isset($matches[$match]) && isset($matches[$match][$key])) { $tmp = explode('|', $matches[$match][$key]); $attribute = Tools::strtolower(trim($tmp[1])); if (trim($customize)) { $output .= $tmp[0].' { '; if ($attribute == 'background-image') { $output .= $attribute.':url('.$customize.')'; } elseif ($attribute == 'font-size') { $output .= $attribute.':'.$customize.'px'; } else if (strpos($attribute, 'color') !== false) { $output .= $attribute.':#'.$customize; } else if ($attribute == 'background') { $output .= $attribute.':#'.$customize; } else { $output .= $attribute.':'.$customize; } $output .= "} \r\n"; } $cache[$match][] = array('val' => $customize, 'selector' => $tmp[0], 'attr' => $tmp[1]); } } } # RENAME if (!empty($data['saved_file']) && !empty($data['newfile'])) { # DELETE PATTERN if (isset($data['saved_file']) && $data['saved_file'] && file_exists($this->css_patterns.$data['saved_file'].'.css')) { unlink($this->css_patterns.$data['saved_file'].'.css'); } if (isset($data['saved_file']) && $data['saved_file'] && file_exists($this->css_patterns.$data['saved_file'].'.json')) { unlink($this->css_patterns.$data['saved_file'].'.json'); } } if (empty($data['newfile'])) { # EDIT PATTERN $nameFile = $data['saved_file'] ? $data['saved_file'] : 'profile-'.time(); } else { # CREATE PATTERN $nameFile = preg_replace('#\s+#', '-', trim($data['newfile'])); } if ($data['action-mode'] != 'save-delete') { # CREATE + EDIT if (!is_dir($this->css_patterns)) { mkdir($this->css_patterns, 0755, true); } if (!empty($output)) { LeoFrameworkHelper::writeToCache($this->css_patterns, $nameFile, $output); } if (!empty($cache)) { LeoFrameworkHelper::writeToCache($this->css_patterns, $nameFile, Tools::jsonEncode($cache), 'json'); } if (isset($data['active']) && $data['active']) { # SET ACTIVE - YES apPageHelper::setConfig('C_PROFILE', $nameFile); } elseif (isset($data['active']) && empty($data['active'])) { # SET ACTIVE - NO $pattern_active = apPageHelper::getConfig('C_PROFILE'); if ($nameFile == $pattern_active) { apPageHelper::setConfig('C_PROFILE', ''); } } } else { # SET ACTIVE - NO $pattern_active = apPageHelper::getConfig('C_PROFILE'); if ($data['saved_file'] == $pattern_active) { apPageHelper::setConfig('C_PROFILE', ''); } # DELETE PATTERN if (isset($data['saved_file']) && $data['saved_file'] && file_exists($this->css_patterns.$data['saved_file'].'.css')) { unlink($this->css_patterns.$data['saved_file'].'.css'); } if (isset($data['saved_file']) && $data['saved_file'] && file_exists($this->css_patterns.$data['saved_file'].'.json')) { unlink($this->css_patterns.$data['saved_file'].'.json'); } } Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token); } } /** * get list of files inside folder path. */ private function getFileList($path, $e = null, $nameOnly = false) { $output = array(); $directories = glob($path.'*'.$e); if ($directories) { foreach ($directories as $dir) { $dir = basename($dir); if ($nameOnly) { $dir = str_replace($e, '', $dir); } $output[$dir] = $dir; } } return $output; } /** * render list of modules following positions in the layout editor. */ public function renderList() { $filePath = _PS_ALL_THEMES_DIR_.$this->themeName.''; $xml = simplexml_load_file($filePath.'/config.xml'); if (!isset($xml->theme_key) || empty($xml->theme_key)) { return '