Backend\Modules\Pages\Engine\Model::updatePagesTemplates PHP Method

updatePagesTemplates() public static method

Switch templates for all existing pages
public static updatePagesTemplates ( integer $oldTemplateId, integer $newTemplateId, boolean $overwrite = false )
$oldTemplateId integer The id of the new template to replace.
$newTemplateId integer The id of the new template to use.
$overwrite boolean Overwrite all pages with default blocks.
    public static function updatePagesTemplates($oldTemplateId, $newTemplateId, $overwrite = false)
    {
        $newTemplateId = (int) $newTemplateId;
        $oldTemplateId = (int) $oldTemplateId;
        $overwrite = (bool) $overwrite;
        // fetch new template data
        $newTemplate = BackendExtensionsModel::getTemplate($newTemplateId);
        $newTemplate['data'] = @unserialize($newTemplate['data']);
        // fetch all pages
        $pages = (array) BackendModel::getContainer()->get('database')->getRecords('SELECT *
             FROM pages
             WHERE template_id = ? AND status IN (?, ?)', array($oldTemplateId, 'active', 'draft'));
        // there is no active/draft page with the old template id
        if (empty($pages)) {
            return;
        }
        // loop pages
        foreach ($pages as $page) {
            // fetch blocks
            $blocksContent = self::getBlocks($page['id'], $page['revision_id'], $page['language']);
            // unset revision id
            unset($page['revision_id']);
            // change template
            $page['template_id'] = $newTemplateId;
            // save new page revision
            $page['revision_id'] = self::update($page);
            // overwrite all blocks with current defaults
            if ($overwrite) {
                // init var
                $blocksContent = array();
                // fetch default blocks for this page
                $defaultBlocks = array();
                if (isset($newTemplate['data']['default_extras_' . $page['language']])) {
                    $defaultBlocks = $newTemplate['data']['default_extras_' . $page['language']];
                } elseif (isset($newTemplate['data']['default_extras'])) {
                    $defaultBlocks = $newTemplate['data']['default_extras'];
                }
                // loop positions
                foreach ($defaultBlocks as $position => $blocks) {
                    // loop blocks
                    foreach ($blocks as $extraId) {
                        // build block
                        $block = array();
                        $block['revision_id'] = $page['revision_id'];
                        $block['position'] = $position;
                        $block['extra_id'] = $extraId;
                        $block['html'] = '';
                        $block['created_on'] = BackendModel::getUTCDate();
                        $block['edited_on'] = $block['created_on'];
                        $block['visible'] = 'Y';
                        $block['sequence'] = count($defaultBlocks[$position]) - 1;
                        // add to the list
                        $blocksContent[] = $block;
                    }
                }
            } else {
                // don't overwrite blocks, just re-use existing
                // set new page revision id
                foreach ($blocksContent as &$block) {
                    $block['revision_id'] = $page['revision_id'];
                    $block['created_on'] = BackendModel::getUTCDate(null, $block['created_on']);
                    $block['edited_on'] = BackendModel::getUTCDate(null, $block['edited_on']);
                }
            }
            // insert the blocks
            self::insertBlocks($blocksContent);
        }
    }

Usage Example

Example #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'));
         // 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 (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')->getChecked() ? 'Y' : 'N';
             // 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;
             // 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=' . urlencode($item['label']) . '&highlight=row-' . $item['id']);
         }
     }
 }
All Usage Examples Of Backend\Modules\Pages\Engine\Model::updatePagesTemplates