Backend\Modules\Extensions\Actions\EditThemeTemplate::validateForm PHP Метод

validateForm() приватный Метод

Validate the form
private validateForm ( )
    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;
                    }
                    // double check 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['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')->getActualValue();
                // copy data from previous version, otherwise default_extras from other languages are overwritten
                $item['data'] = $this->record['data'];
                $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
                $item['data'] = serialize($item['data']);
                // if this is the default template make the template active
                if ($this->get('fork.settings')->get('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'] == $this->get('fork.settings')->get('Core', 'theme', 'core')) {
                    $this->get('fork.settings')->set('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('ThemeTemplates') . '&theme=' . $item['theme'] . '&report=edited-template&var=' . rawurlencode($item['label']) . '&highlight=row-' . $item['id']);
            }
        }
    }