コード例 #1
0
 /**
  * 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);
         $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['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 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'] == BackendModel::getModuleSetting('core', 'theme', 'core')) {
                 BackendModel::setModuleSetting($this->getModule(), 'default_template', $item['id']);
             }
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('theme_templates') . '&theme=' . $item['theme'] . '&report=added-template&var=' . urlencode($item['label']) . '&highlight=row-' . $item['id']);
         }
     }
 }