/**
* 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'));
// check if the template file exists
if ($this->frm->getField('theme')->getValue() == 'Core') {
$templateFile = PATH_WWW . '/src/Frontend/Core/Layout/Templates/' . $this->frm->getField('file')->getValue();
} else {
$templateFile = PATH_WWW . '/src/Frontend/Themes/' . $this->frm->getField('theme')->getValue() . '/Core/Layout/Templates/' . $this->frm->getField('file')->getValue();
}
if (!is_file($templateFile)) {
$this->frm->getField('file')->addError(BL::err('TemplateFileNotFound'));
}
// 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 (mb_substr_count($html, '"#position-' . $cell . '"') != 1) {
// can't build proper html -> error
$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['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')->getActualValue();
$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_' . BL::getWorkingLanguage()] = $this->extras;
$item['data']['image'] = $this->frm->getField('image')->isChecked();
// serialize the data
$item['data'] = serialize($item['data']);
// insert the item
$item['id'] = BackendExtensionsModel::insertTemplate($item);
// trigger event
BackendModel::triggerEvent($this->getModule(), 'after_add_template', array('item' => $item));
// set default template
if ($this->frm->getField('default')->getChecked() && $item['theme'] == $this->get('fork.settings')->get('Core', 'theme', 'core')) {
$this->get('fork.settings')->set($this->getModule(), 'default_template', $item['id']);
}
// everything is saved, so redirect to the overview
$this->redirect(BackendModel::createURLForAction('ThemeTemplates') . '&theme=' . $item['theme'] . '&report=added-template&var=' . rawurlencode($item['label']) . '&highlight=row-' . $item['id']);
}
}
}