/**
  * Execute the action
  */
 public function execute()
 {
     // get parameters
     $this->id = $this->getParameter('id', 'int');
     // does the item exist
     if ($this->id !== null && BackendExtensionsModel::existsTemplate($this->id)) {
         // call parent, this will probably add some general CSS/JS or other required files
         parent::execute();
         // init var
         $success = false;
         // get template (we need the title)
         $item = BackendExtensionsModel::getTemplate($this->id);
         // valid template?
         if (!empty($item)) {
             // delete the page
             $success = BackendExtensionsModel::deleteTemplate($this->id);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_delete_template', array('id' => $this->id));
         }
         // page is deleted, so redirect to the overview
         if ($success) {
             $this->redirect(BackendModel::createURLForAction('theme_templates') . '&theme=' . $item['theme'] . '&report=deleted-template&var=' . urlencode($item['label']));
         } else {
             $this->redirect(BackendModel::createURLForAction('theme_templates') . '&error=non-existing');
         }
     } else {
         $this->redirect(BackendModel::createURLForAction('theme_templates') . '&error=non-existing');
     }
 }
 /**
  * Load the record
  */
 private function loadData()
 {
     // get record
     $this->id = $this->getParameter('id', 'int');
     // validate id
     if ($this->id === null || !BackendExtensionsModel::existsTemplate($this->id)) {
         $this->redirect(BackendModel::createURLForAction('theme_templates') . '&error=non-existing');
     }
     // get the record
     $this->record = BackendExtensionsModel::getTemplate($this->id);
     // unserialize
     $this->record['data'] = unserialize($this->record['data']);
     $this->names = $this->record['data']['names'];
     if (isset($this->record['data']['default_extras_' . BL::getWorkingLanguage()])) {
         $this->extras = $this->record['data']['default_extras_' . BL::getWorkingLanguage()];
     } elseif (isset($this->record['data']['default_extras'])) {
         $this->extras = $this->record['data']['default_extras'];
     }
     // assign
     $this->tpl->assign('template', $this->record);
     // is the template being used
     $inUse = BackendExtensionsModel::isTemplateInUse($this->id);
     // determine if deleting is allowed
     $deleteAllowed = true;
     if ($this->record['id'] == BackendModel::getModuleSetting('pages', 'default_template')) {
         $deleteAllowed = false;
     } elseif (count(BackendExtensionsModel::getTemplates()) == 1) {
         $deleteAllowed = false;
     } elseif ($inUse) {
         $deleteAllowed = false;
     } elseif (!BackendAuthentication::isAllowedAction('delete_theme_template')) {
         $deleteAllowed = false;
     }
     // assign
     $this->tpl->assign('inUse', $inUse);
     $this->tpl->assign('showExtensionsDeleteThemeTemplate', $deleteAllowed);
 }
Beispiel #3
0
    /**
     * Switch templates for all existing pages
     *
     * @param int $oldTemplateId The id of the new template to replace.
     * @param int $newTemplateId The id of the new template to use.
     * @param bool[optional] $overwrite 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::getDB()->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 = BackendPagesModel::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'] = BackendPagesModel::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 {
                // set new page revision id
                foreach ($blocksContent as &$block) {
                    $block['revision_id'] = $page['revision_id'];
                }
            }
            // insert the blocks
            BackendPagesModel::insertBlocks($blocksContent);
        }
    }