Backend\Modules\Blog\Engine\Model::delete PHP Method

delete() public static method

Deletes one or more items
public static delete ( mixed $ids )
$ids mixed The ids to delete.
    public static function delete($ids)
    {
        // make sure $ids is an array
        $ids = (array) $ids;
        // make sure we have elements
        if (empty($ids)) {
            return;
        }
        // loop and cast to integers
        foreach ($ids as &$id) {
            $id = (int) $id;
        }
        // create an string with an equal amount of questionmarks as ids provided
        $idPlaceHolders = implode(', ', array_fill(0, count($ids), '?'));
        // get db
        $db = BackendModel::getContainer()->get('database');
        // get used meta ids
        $metaIds = (array) $db->getColumn('SELECT meta_id
             FROM blog_posts AS p
             WHERE id IN (' . $idPlaceHolders . ') AND language = ?', array_merge($ids, array(BL::getWorkingLanguage())));
        // delete meta
        if (!empty($metaIds)) {
            $db->delete('meta', 'id IN (' . implode(',', $metaIds) . ')');
        }
        // delete image files
        $images = $db->getColumn('SELECT image FROM blog_posts WHERE id IN (' . $idPlaceHolders . ')', $ids);
        foreach ($images as $image) {
            BackendModel::deleteThumbnails(FRONTEND_FILES_PATH . '/blog/images', $image);
        }
        // delete records
        $db->delete('blog_posts', 'id IN (' . $idPlaceHolders . ') AND language = ?', array_merge($ids, array(BL::getWorkingLanguage())));
        $db->delete('blog_comments', 'post_id IN (' . $idPlaceHolders . ') AND language = ?', array_merge($ids, array(BL::getWorkingLanguage())));
        // delete tags
        foreach ($ids as $id) {
            BackendTagsModel::saveTags($id, '', 'Blog');
        }
    }

Usage Example

コード例 #1
0
ファイル: Delete.php プロジェクト: bwgraves/forkcms
 /**
  * Execute the action
  */
 public function execute()
 {
     // get parameters
     $this->id = $this->getParameter('id', 'int');
     // does the item exist
     if ($this->id !== null && BackendBlogModel::exists($this->id)) {
         // call parent, this will probably add some general CSS/JS or other required files
         parent::execute();
         // set category id
         $this->categoryId = \SpoonFilter::getGetValue('category', null, null, 'int');
         if ($this->categoryId == 0) {
             $this->categoryId = null;
         }
         // get data
         $this->record = (array) BackendBlogModel::get($this->id);
         // delete item
         BackendBlogModel::delete($this->id);
         // trigger event
         BackendModel::triggerEvent($this->getModule(), 'after_delete', array('id' => $this->id));
         // delete search indexes
         BackendSearchModel::removeIndex($this->getModule(), $this->id);
         // build redirect URL
         $redirectUrl = BackendModel::createURLForAction('Index') . '&report=deleted&var=' . urlencode($this->record['title']);
         // append to redirect URL
         if ($this->categoryId != null) {
             $redirectUrl .= '&category=' . $this->categoryId;
         }
         // item was deleted, so redirect
         $this->redirect($redirectUrl);
     } else {
         // something went wrong
         $this->redirect(BackendModel::createURLForAction('Index') . '&error=non-existing');
     }
 }