/** * Load the record * * @return void */ private function loadData() { // get record $this->id = $this->getParameter('id', 'int'); // validate id if ($this->id === null || !BackendPagesModel::existsTemplate($this->id)) { $this->redirect(BackendModel::createURLForAction('templates') . '&error=non-existing'); } // get the record $this->record = BackendPagesModel::getTemplate($this->id); // unserialize $this->record['data'] = unserialize($this->record['data']); // assign $this->tpl->assign('template', $this->record); // is the template being used $inUse = BackendPagesModel::isTemplateInUse($this->id); // determine if deleting is allowed $deleteAllowed = true; if ($this->record['id'] == BackendModel::getModuleSetting($this->getModule(), 'default_template')) { $deleteAllowed = false; } elseif (count(BackendPagesModel::getTemplates()) == 1) { $deleteAllowed = false; } elseif ($inUse) { $deleteAllowed = false; } // assign $this->tpl->assign('inUse', $inUse); $this->tpl->assign('deleteAllowed', $deleteAllowed); }
/** * Execute the action * * @return void */ public function execute() { // get parameters $this->id = $this->getParameter('id', 'int'); // does the item exist if ($this->id !== null && BackendPagesModel::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 = BackendPagesModel::getTemplate($this->id); // valid template? if (!empty($item)) { // delete the page $success = BackendPagesModel::deleteTemplate($this->id); // trigger event BackendModel::triggerEvent($this->getModule(), 'after_delete_template', array('id' => $this->id)); // build cache BackendPagesModel::buildCache(BL::getWorkingLanguage()); } // page is deleted, so redirect to the overview if ($success) { $this->redirect(BackendModel::createURLForAction('templates') . '&theme=' . $item['theme'] . '&report=deleted-template&var=' . urlencode($item['label'])); } else { $this->redirect(BackendModel::createURLForAction('templates') . '&error=non-existing'); } } else { $this->redirect(BackendModel::createURLForAction('templates') . '&error=non-existing'); } }
/** * Switch templates for all existing pages * * @return void * @param int $oldTemplateId The id of the new template to replace. * @param int $newTemplateId The id of the new template to use. */ public static function updatePagesTemplates($oldTemplateId, $newTemplateId) { // fetch new template data $newTemplate = BackendPagesModel::getTemplate($newTemplateId); $newTemplate['data'] = @unserialize($newTemplate['data']); // fetch all pages $pages = 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::getBlocksRevision($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); // init blocks array $blocks = array(); // loop missing blocks for ($i = 0; $i < max(count($blocksContent), (int) $newTemplate['num_blocks']); $i++) { $block = array(); // block already exists if (isset($blocksContent[$i])) { $block = $blocksContent[$i]; $block['created_on'] = BackendModel::getUTCDate(null, $block['created_on']); $block['edited_on'] = BackendModel::getUTCDate(null, $block['edited_on']); $block['revision_id'] = $page['revision_id']; } else { $block['id'] = BackendPagesModel::getMaximumBlockId() + $i + 1; $block['revision_id'] = $page['revision_id']; $block['html'] = ''; $block['status'] = 'active'; $block['created_on'] = BackendModel::getUTCDate(); $block['edited_on'] = $block['created_on']; $block['html'] = ''; $block['extra_id'] = null; $block['has_extra'] = 'N'; } // verify that there is no existing content and that we actually have new default content if (!isset($blocksContent[$i]) || !$blocksContent[$i]['html'] && !$blocksContent[$i]['extra_id'] && $i < $newTemplate['num_blocks']) { // get default extras in this language if (isset($newTemplate['data']['default_extras_' . $page['language']])) { // check if a default extra has been defined if ($newTemplate['data']['default_extras_' . $page['language']][$i] != 'editor') { $page['has_extra'] = 'Y'; $block['extra_id'] = $newTemplate['data']['default_extras_' . $page['language']][$i]; } else { $block['extra_id'] = null; } } else { // check if a default extra has been defined if ($newTemplate['data']['default_extras'][$i] != 'editor') { $page['has_extra'] = 'Y'; $block['extra_id'] = $newTemplate['data']['default_extras'][$i]; } else { $block['extra_id'] = null; } } } // add block $blocks[] = $block; } // insert the blocks BackendPagesModel::updateBlocks($blocks, $page['has_extra'] == 'Y'); } }