Example #1
0
    public function delete()
    {
        if ((int) $this->id === 0 or (int) $this->id === 1) {
            return false;
        }
        $this->clearCache();
        /* Get childs categories */
        $toDelete = array((int) $this->id);
        $this->recursiveDelete($toDelete, (int) $this->id);
        $toDelete = array_unique($toDelete);
        /* Delete category and its child from database */
        $list = sizeof($toDelete) > 1 ? implode(',', array_map('intval', $toDelete)) : (int) $this->id;
        Db::getInstance()->Execute('DELETE FROM `' . _DB_PREFIX_ . 'category` WHERE `id_category` IN (' . $list . ')');
        Db::getInstance()->Execute('DELETE FROM `' . _DB_PREFIX_ . 'category_lang` WHERE `id_category` IN (' . $list . ')');
        Db::getInstance()->Execute('DELETE FROM `' . _DB_PREFIX_ . 'category_product` WHERE `id_category` IN (' . $list . ')');
        Db::getInstance()->Execute('DELETE FROM `' . _DB_PREFIX_ . 'category_group` WHERE `id_category` IN (' . $list . ')');
        self::cleanPositions($this->id_parent);
        /* Delete category images and its children images */
        $tmpCategory = new Category();
        foreach ($toDelete as $id_category) {
            $tmpCategory->id = (int) $id_category;
            $tmpCategory->deleteImage();
        }
        /* Delete products which were not in others categories */
        $result = Db::getInstance()->ExecuteS('
		SELECT `id_product`
		FROM `' . _DB_PREFIX_ . 'product`
		WHERE `id_product` NOT IN (SELECT `id_product` FROM `' . _DB_PREFIX_ . 'category_product`)');
        foreach ($result as $p) {
            $product = new Product((int) $p['id_product']);
            if (Validate::isLoadedObject($product)) {
                $product->delete();
            }
        }
        /* Set category default to 1 where categorie no more exists */
        $result = Db::getInstance()->Execute('
		UPDATE `' . _DB_PREFIX_ . 'product`
		SET `id_category_default` = 1
		WHERE `id_category_default`
		NOT IN (SELECT `id_category` FROM `' . _DB_PREFIX_ . 'category`)');
        /* Rebuild the nested tree */
        if (!isset($this->doNotRegenerateNTree) or !$this->doNotRegenerateNTree) {
            self::regenerateEntireNtree();
        }
        Module::hookExec('categoryDeletion', array('category' => $this));
        /* Delete Categories in GroupReduction */
        foreach ($toDelete as $category) {
            if (GroupReduction::getGroupReductionByCategoryId((int) $category)) {
                GroupReduction::deleteCategory($category);
            }
        }
        return true;
    }