/**
  * 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']);
         }
     }
 }
Exemple #2
0
    /**
     * Delete a template
     *
     * @return	bool
     * @param	int $id		The id of the template to delete.
     */
    public static function deleteTemplate($id)
    {
        // redefine
        $id = (int) $id;
        // get all templates
        $templates = self::getTemplates();
        // we can't delete a template that doesn't exist
        if (!isset($templates[$id])) {
            return false;
        }
        // we can't delete the last template
        if (count($templates) == 1) {
            return false;
        }
        // we can't delete the default template
        if ($id == BackendModel::getModuleSetting('pages', 'default_template')) {
            return false;
        }
        if (BackendPagesModel::isTemplateInUse($id)) {
            return false;
        }
        // get db
        $db = BackendModel::getDB(true);
        // delete
        $db->delete('pages_templates', 'id = ?', $id);
        // get all non-active pages that use this template
        $ids = (array) $db->getColumn('SELECT i.revision_id
										FROM pages AS i
										WHERE i.template_id = ? AND i.status != ?', array($id, 'active'));
        // any items
        if (!empty($ids)) {
            // delete those pages and the linked blocks
            $db->delete('pages', 'revision_id IN(' . implode(',', $ids) . ')');
            $db->delete('pages_blocks', 'revision_id IN(' . implode(',', $ids) . ')');
        }
        // return
        return true;
    }