/** * Create a preset form a raw options array. * * @param $name * @param $options * * @return Preset */ public static function createFromArray($name, $options) { $preset = new Preset(); $preset->setName($name); foreach ($options['options'] as $option) { $preset->setOption($option['name'], $option['specific']); } return $preset; }
/** * Remove a preset * * @param array $params List of get and post parameters which were sent to * the json adapter. */ public function removePreset($params) { global $_ARRAYLANG; \Env::get('init')->loadLanguageData('TemplateEditor'); if (!Preset::isValidPresetName($params['post']['preset'])) { return; } $presetName = $params['post']['preset']; /** * Default shouldn't be deletable */ if ($presetName == 'Default') { throw new \LogicException($_ARRAYLANG['TXT_CORE_MODULE_TEMPLATEEDITOR_REMOVE_PRESET_DEFAULT_WARNING']); } $themeID = isset($params['post']['tid']) ? intval($params['post']['tid']) : 1; $themeRepository = new ThemeRepository(); $theme = $themeRepository->findById($themeID); $fileStorage = new OptionSetFileStorage($this->cx->getWebsiteThemesPath()); $themeOptionRepository = new OptionSetRepository($fileStorage); $themeOptions = $themeOptionRepository->get($theme); $preset = $themeOptions->getPresetRepository()->getByName($presetName); if ($themeOptions->getActivePreset()->getName() == $preset->getName()) { $themeOptions->setActivePreset($themeOptions->getPresetRepository()->getByName('Default')); $themeOptionRepository->save($themeOptions); } $themeOptions->getPresetRepository()->remove($preset); }
/** * @param $name * * @return Preset */ public function getByName($name) { return Preset::createFromArray($name, $this->storage->retrieve($name)); }
/** * This renders the backend overview. * * @param \Cx\Core\Html\Sigma $template Template for current CMD * @param array $cmd CMD separated by slashes */ public function parsePage(\Cx\Core\Html\Sigma $template, array $cmd) { \Permission::checkAccess(47, 'static'); $fileStorage = new OptionSetFileStorage($this->cx->getWebsiteThemesPath()); $themeOptionRepository = new OptionSetRepository($fileStorage); $this->themeOptionRepository = $themeOptionRepository; $this->themeRepository = new ThemeRepository(); $themeID = isset($_GET['tid']) ? $_GET['tid'] : 1; $this->theme = $this->themeRepository->findById($themeID); if (!$_SESSION['TemplateEditor']) { $_SESSION['TemplateEditor'] = array(); } if (!$_SESSION['TemplateEditor'][$this->theme->getId()]) { $_SESSION['TemplateEditor'][$this->theme->getId()] = array(); } if (isset($_GET['preset']) && Preset::isValidPresetName($_GET['preset'])) { if ($_SESSION['TemplateEditor'][$this->theme->getId()]['activePreset'] != $_GET['preset']) { // If the preset has changed remove all saved options $_SESSION['TemplateEditor'][$this->theme->getId()] = array(); } $_SESSION['TemplateEditor'][$this->theme->getId()]['activePreset'] = isset($_GET['preset']) ? $_GET['preset'] : 'Default'; } $this->presetRepository = new PresetRepository(new PresetFileStorage($this->cx->getWebsiteThemesPath() . '/' . $this->theme->getFoldername())); try { $this->themeOptions = $this->themeOptionRepository->get($this->theme); // If user opens editor use active preset as active preset. if (!isset($_SESSION['TemplateEditor'][$this->theme->getId()]['activePreset']) || !isset($_GET['preset'])) { $_SESSION['TemplateEditor'][$this->theme->getId()]['activePreset'] = $this->themeOptions->getActivePreset()->getName(); } try { $this->themeOptions->applyPreset($this->presetRepository->getByName($_SESSION['TemplateEditor'][$this->theme->getId()]['activePreset'])); } catch (PresetRepositoryException $e) { // If something fails fallback to the default preset. $_SESSION['TemplateEditor'][$this->theme->getId()]['activePreset'] = 'Default'; $this->themeOptions->applyPreset($this->presetRepository->getByName('Default')); } } catch (\Symfony\Component\Yaml\ParserException $e) { } $this->showOverview($template); }
/** * Apply a preset * * @param Preset $preset */ public function applyPreset(Preset $preset) { $this->appliedPreset = $preset; /* Saving data so later on we can reuse it without worrying about older applied presets */ $data = $this->data; foreach ($data['options'] as &$emptyOption) { if ($presetOption = $preset->getOption($emptyOption['name'])) { if (!is_array($emptyOption['specific'])) { $emptyOption['specific'] = array(); } $emptyOption['specific'] = array_merge($emptyOption['specific'], $presetOption->getValue()); } } // $this->options = array(); foreach ($data['options'] as $option) { $optionReflection = new \ReflectionClass($option['type']); if ($optionReflection->isSubclassOf('Cx\\Core_Modules\\TemplateEditor\\Model\\Entity\\Option')) { if ($this->cx->getMode() == Cx::MODE_BACKEND || $this->cx->getUser()->getFWUserObject()->objUser->login() && isset($_GET['templateEditor'])) { if (isset($_SESSION['TemplateEditor'][$this->theme->getId()][$option['name']])) { $option['specific'] = array_merge($option['specific'], $_SESSION['TemplateEditor'][$this->theme->getId()][$option['name']]->toArray()); } } $this->options[$option['name']] = $optionReflection->newInstance($option['name'], $option['translation'], $option['specific']); } } }