getTemplate() public static method

Get a given template
public static getTemplate ( integer $id ) : array
$id integer The id of the requested template.
return array
Example #1
0
 /**
  * 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('ThemeTemplates') . '&theme=' . $item['theme'] . '&report=deleted-template&var=' . urlencode($item['label']));
         } else {
             $this->redirect(BackendModel::createURLForAction('ThemeTemplates') . '&error=non-existing');
         }
     } else {
         // something went wrong
         $this->redirect(BackendModel::createURLForAction('ThemeTemplates') . '&error=non-existing');
     }
 }
Example #2
0
 /**
  * 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('ThemeTemplates') . '&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'];
     }
     if (!array_key_exists('image', (array) $this->record['data'])) {
         $this->record['data']['image'] = false;
     }
     // 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'] == $this->get('fork.settings')->get('Pages', 'default_template')) {
         $deleteAllowed = false;
     } elseif (count(BackendExtensionsModel::getTemplates()) == 1) {
         $deleteAllowed = false;
     } elseif ($inUse) {
         $deleteAllowed = false;
     }
     // assign
     $this->tpl->assign('inUse', $inUse);
     $this->tpl->assign('allowExtensionsDeleteThemeTemplate', $deleteAllowed);
 }
Example #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 $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::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);
     }
 }