/** * Validate the form */ private function validateForm() { // is the form submitted? if ($this->frm->isSubmitted()) { // cleanup the submitted fields, ignore fields that were added by hackers $this->frm->cleanupFields(); // required fields $this->frm->getField('file')->isFilled(BL::err('FieldIsRequired')); $this->frm->getField('label')->isFilled(BL::err('FieldIsRequired')); $this->frm->getField('format')->isFilled(BL::err('FieldIsRequired')); // validate syntax $syntax = trim(str_replace(array("\n", "\r", ' '), '', $this->frm->getField('format')->getValue())); // init var $table = BackendExtensionsModel::templateSyntaxToArray($syntax); // validate the syntax if ($table === false) { $this->frm->getField('format')->addError(BL::err('InvalidTemplateSyntax')); } else { $html = BackendExtensionsModel::buildTemplateHTML($syntax); $cellCount = 0; $first = true; $errors = array(); // loop rows foreach ($table as $row) { // first row defines the cellcount if ($first) { $cellCount = count($row); } // not same number of cells if (count($row) != $cellCount) { // add error $errors[] = BL::err('InvalidTemplateSyntax'); // stop break; } // doublecheck position names foreach ($row as $cell) { // ignore unavailable space if ($cell != '/') { // not alphanumeric -> error if (!in_array($cell, $this->names)) { $errors[] = sprintf(BL::getError('NonExistingPositionName'), $cell); } elseif (substr_count($html, '"#position-' . $cell . '"') != 1) { $errors[] = BL::err('InvalidTemplateSyntax'); } } } // reset $first = false; } // add errors if ($errors) { $this->frm->getField('format')->addError(implode('<br />', array_unique($errors))); } } // no errors? if ($this->frm->isCorrect()) { // build array $item['id'] = $this->id; $item['theme'] = $this->frm->getField('theme')->getValue(); $item['label'] = $this->frm->getField('label')->getValue(); $item['path'] = 'core/layout/templates/' . $this->frm->getField('file')->getValue(); $item['active'] = $this->frm->getField('active')->getChecked() ? 'Y' : 'N'; $item['data']['format'] = trim(str_replace(array("\n", "\r", ' '), '', $this->frm->getField('format')->getValue())); $item['data']['names'] = $this->names; $item['data']['default_extras'] = $this->extras; $item['data']['default_extras_' . BackendLanguage::getWorkingLanguage()] = $this->extras; // serialize $item['data'] = serialize($item['data']); // if this is the default template make the template active if (BackendModel::getModuleSetting('pages', 'default_template') == $this->record['id']) { $item['active'] = 'Y'; } // if the template is in use we can't de-activate it if (BackendExtensionsModel::isTemplateInUse($item['id'])) { $item['active'] = 'Y'; } // insert the item BackendExtensionsModel::updateTemplate($item); // trigger event BackendModel::triggerEvent($this->getModule(), 'after_edit_template', array('item' => $item)); // set default template if ($this->frm->getField('default')->getChecked() && $item['theme'] == BackendModel::getModuleSetting('core', 'theme', 'core')) { BackendModel::setModuleSetting('pages', 'default_template', $item['id']); } // update all existing pages using this template to add the newly inserted block(s) if (BackendExtensionsModel::isTemplateInUse($item['id'])) { BackendPagesModel::updatePagesTemplates($item['id'], $item['id'], $this->frm->getField('overwrite')->getChecked()); } // everything is saved, so redirect to the overview $this->redirect(BackendModel::createURLForAction('theme_templates') . '&theme=' . $item['theme'] . '&report=edited-template&var=' . urlencode($item['label']) . '&highlight=row-' . $item['id']); } } }
/** * Validates the form * * @return void */ private function validateForm() { // is the form submitted? if ($this->frm->isSubmitted()) { // no errors? if ($this->frm->isCorrect()) { // determine themes $newTheme = $this->frm->getField('theme')->getValue(); $oldTheme = BackendModel::getModuleSetting('core', 'theme', 'core'); // check if we actually switched themes if ($newTheme != $oldTheme) { // fetch templates $oldTemplates = BackendPagesModel::getTemplates($oldTheme); $newTemplates = BackendPagesModel::getTemplates($newTheme); // check if templates already exist if (empty($newTemplates)) { // templates do not yet exist; don't switch $this->redirect(BackendModel::createURLForAction('themes') . '&error=no-templates-available'); exit; } // fetch current default template $oldDefaultTemplatePath = $oldTemplates[BackendModel::getModuleSetting('pages', 'default_template')]['path']; // loop new templates foreach ($newTemplates as $newTemplateId => $newTemplate) { // check if a a similar default template exists if ($newTemplate['path'] == $oldDefaultTemplatePath) { // set new default id $newDefaultTemplateId = (int) $newTemplateId; break; } } // no default template was found, set first template as default if (!isset($newDefaultTemplateId)) { $newDefaultTemplateId = array_keys($newTemplates); $newDefaultTemplateId = $newDefaultTemplateId[0]; } // update theme BackendModel::setModuleSetting('core', 'theme', $newTheme); // set amount of blocks BackendPagesModel::setMaximumBlocks(); // save new default template BackendModel::setModuleSetting('pages', 'default_template', $newDefaultTemplateId); // loop old templates foreach ($oldTemplates as $oldTemplateId => $oldTemplate) { // loop new templates foreach ($newTemplates as $newTemplateId => $newTemplate) { // check if we have a matching template if ($oldTemplate['path'] == $newTemplate['path']) { // switch template BackendPagesModel::updatePagesTemplates($oldTemplateId, $newTemplateId); // break loop continue 2; } } // getting here meant we found no matching template for the new theme; pick first theme's template as default BackendPagesModel::updatePagesTemplates($oldTemplateId, $newDefaultTemplateId); } // trigger event BackendModel::triggerEvent($this->getModule(), 'after_changed_theme'); } // assign report $this->tpl->assign('report', true); $this->tpl->assign('reportMessage', BL::msg('Saved')); } } }
/** * Validate the form * * @return void */ private function validateForm() { // is the form submitted? if ($this->frm->isSubmitted()) { // cleanup the submitted fields, ignore fields that were added by hackers $this->frm->cleanupFields(); // num blocks cant be altered when the template is in use $numBlocks = (int) $this->frm->getField('num_blocks')->getValue(); // required fields $this->frm->getField('file')->isFilled(BL::err('FieldIsRequired')); $this->frm->getField('label')->isFilled(BL::err('FieldIsRequired')); $this->frm->getField('format')->isFilled(BL::err('FieldIsRequired')); // loop the know fields and validate them for ($i = 1; $i <= $numBlocks; $i++) { $this->frm->getField('name_' . $i)->isFilled(BL::err('FieldIsRequired')); } // validate syntax $syntax = trim(str_replace(array("\n", "\r"), '', $this->frm->getField('format')->getValue())); // init var $table = BackendPagesModel::templateSyntaxToArray($syntax); $cellCount = 0; $first = true; // loop rows foreach ($table as $row) { // first row defines the cellcount if ($first) { $cellCount = count($row); } // not same number of cells if (count($row) != $cellCount) { // add error $this->frm->getField('format')->addError(BL::err('InvalidTemplateSyntax')); // stop break; } // reset $first = false; } // no errors? if ($this->frm->isCorrect()) { // build array $item['id'] = $this->id; $item['theme'] = $this->frm->getField('theme')->getValue(); $item['label'] = $this->frm->getField('label')->getValue(); $item['path'] = 'core/layout/templates/' . $this->frm->getField('file')->getValue(); $item['num_blocks'] = $numBlocks; $item['active'] = $this->frm->getField('active')->getChecked() ? 'Y' : 'N'; // if this is the default template make the template active if (BackendModel::getModuleSetting($this->getModule(), 'default_template') == $this->record['id']) { $item['active'] = 'Y'; } // if the template is in use we can't de-activate it if (BackendPagesModel::isTemplateInUse($item['id'])) { $item['active'] = 'Y'; } // init template data $item['data']['format'] = $this->record['data']['format']; // loop fields for ($i = 1; $i <= $item['num_blocks']; $i++) { $item['data']['names'][$i - 1] = $this->frm->getField('name_' . $i)->getValue(); $item['data']['default_extras'][$i - 1] = $this->frm->getField('type_' . $i)->getValue(); $item['data']['default_extras_' . BackendLanguage::getWorkingLanguage()][$i - 1] = $this->frm->getField('type_' . $i)->getValue(); } // blocks layout $item['data']['format'] = trim(str_replace(array("\n", "\r"), '', $this->frm->getField('format')->getValue())); // serialize $item['data'] = serialize($item['data']); // insert the item BackendPagesModel::updateTemplate($item); // trigger event BackendModel::triggerEvent($this->getModule(), 'after_edit_template', array('item' => $item)); // set default template if ($this->frm->getField('default')->getChecked() && $item['theme'] == BackendModel::getModuleSetting('core', 'theme', 'core')) { BackendModel::setModuleSetting($this->getModule(), 'default_template', $item['id']); } // update all existing pages using this template to add the newly inserted block(s) if (BackendPagesModel::isTemplateInUse($item['id'])) { BackendPagesModel::updatePagesTemplates($item['id'], $item['id']); } // everything is saved, so redirect to the overview $this->redirect(BackendModel::createURLForAction('templates') . '&theme=' . $item['theme'] . '&report=edited-template&var=' . urlencode($item['label']) . '&highlight=row-' . $item['id']); } } }