/** * Load the record * * @return void */ private function loadData() { // get record $this->id = $this->getParameter('id', 'int'); // check if something went wrong if ($this->id === null || !BackendPagesModel::exists($this->id)) { $this->redirect(BackendModel::createURLForAction('index') . '&error=non-existing'); } // get the record $this->record = BackendPagesModel::get($this->id); // load blocks $this->blocksContent = BackendPagesModel::getBlocks($this->id); // is there a revision specified? $revisionToLoad = $this->getParameter('revision', 'int'); // if this is a valid revision if ($revisionToLoad !== null) { // save current template $templateId = $this->record['template_id']; // overwrite the current record $this->record = (array) BackendPagesModel::getRevision($this->id, $revisionToLoad); // template is not part of revision; only data $this->record['template_id'] = $templateId; // load blocks $this->blocksContent = BackendPagesModel::getBlocksRevision($this->id, $revisionToLoad); // show warning $this->tpl->assign('appendRevision', true); } // is there a revision specified? $draftToLoad = $this->getParameter('draft', 'int'); // if this is a valid revision if ($draftToLoad !== null) { // save current template $templateId = $this->record['template_id']; // overwrite the current record $this->record = (array) BackendPagesModel::getRevision($this->id, $draftToLoad); // template is not part of draft; only data $this->record['template_id'] = $templateId; // load blocks $this->blocksContent = BackendPagesModel::getBlocksRevision($this->id, $draftToLoad); // show warning $this->tpl->assign('appendRevision', true); } // reset some vars $this->record['full_url'] = BackendPagesModel::getFullURL($this->record['id']); $this->record['is_hidden'] = $this->record['hidden'] == 'Y'; }
/** * 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'); } }