Backend\Modules\Blog\Engine\Model::deleteCategory PHP Метод

deleteCategory() публичный статический Метод

Deletes a category
public static deleteCategory ( integer $id )
$id integer The id of the category to delete.
    public static function deleteCategory($id)
    {
        $id = (int) $id;
        $db = BackendModel::getContainer()->get('database');
        // get item
        $item = self::getCategory($id);
        if (!empty($item)) {
            // delete meta
            $db->delete('meta', 'id = ?', array($item['meta_id']));
            // delete category
            $db->delete('blog_categories', 'id = ?', array($id));
            // update category for the posts that might be in this category
            $db->update('blog_posts', array('category_id' => null), 'category_id = ?', array($id));
        }
    }

Usage Example

Пример #1
0
 /**
  * Execute the action
  */
 public function execute()
 {
     $this->id = $this->getParameter('id', 'int');
     // does the item exist
     if ($this->id !== null && BackendBlogModel::existsCategory($this->id)) {
         // get data
         $this->record = (array) BackendBlogModel::getCategory($this->id);
         // allowed to delete the category?
         if (BackendBlogModel::deleteCategoryAllowed($this->id)) {
             // call parent, this will probably add some general CSS/JS or other required files
             parent::execute();
             // delete item
             BackendBlogModel::deleteCategory($this->id);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_delete_category', array('id' => $this->id));
             // category was deleted, so redirect
             $this->redirect(BackendModel::createURLForAction('Categories') . '&report=deleted-category&var=' . rawurlencode($this->record['title']));
         } else {
             $this->redirect(BackendModel::createURLForAction('Categories') . '&error=delete-category-not-allowed&var=' . rawurlencode($this->record['title']));
         }
     } else {
         // something went wrong
         $this->redirect(BackendModel::createURLForAction('Categories') . '&error=non-existing');
     }
 }