Backend\Modules\Extensions\Engine\Model::deleteTemplate PHP Method

deleteTemplate() public static method

Delete a template.
public static deleteTemplate ( integer $id ) : boolean
$id integer The id of the template to delete.
return boolean
    public static function deleteTemplate($id)
    {
        $id = (int) $id;
        $templates = self::getTemplates();
        // we can't delete a template that doesn't exist
        if (!isset($templates[$id])) {
            return false;
        }
        // we can't delete the last template
        if (count($templates) == 1) {
            return false;
        }
        // we can't delete the default template
        if ($id == BackendModel::get('fork.settings')->get('Pages', 'default_template')) {
            return false;
        }
        if (self::isTemplateInUse($id)) {
            return false;
        }
        $db = BackendModel::getContainer()->get('database');
        $db->delete('themes_templates', 'id = ?', $id);
        $ids = (array) $db->getColumn('SELECT i.revision_id
             FROM pages AS i
             WHERE i.template_id = ? AND i.status != ?', array($id, 'active'));
        if (!empty($ids)) {
            // delete those pages and the linked blocks
            $db->delete('pages', 'revision_id IN(' . implode(',', $ids) . ')');
            $db->delete('pages_blocks', 'revision_id IN(' . implode(',', $ids) . ')');
        }
        return true;
    }

Usage Example

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');
     }
 }