Esempio n. 1
0
 public function delete()
 {
     if ((int) $this->id === 0 || (int) $this->id === 1) {
         return false;
     }
     $this->clearCache();
     $all_cat = $this->getAllChildren();
     $all_cat[] = $this;
     foreach ($all_cat as $cat) {
         $cat->deleteLite();
         if (!$this->hasMultishopEntries()) {
             $cat->deleteImage();
             $cat->cleanGroups();
             $cat->cleanAssoProducts();
             // Delete associated restrictions on cart rules
             CartRule::cleanProductRuleIntegrity('categories', array($cat->id));
             Category::cleanPositions($cat->id_parent);
             /* Delete Categories in GroupReduction */
             if (GroupReduction::getGroupReductionByCategoryId((int) $cat->id)) {
                 GroupReduction::deleteCategory($cat->id);
             }
         }
     }
     /* Rebuild the nested tree */
     if (!$this->hasMultishopEntries() && (!isset($this->doNotRegenerateNTree) || !$this->doNotRegenerateNTree)) {
         Category::regenerateEntireNtree();
     }
     Hook::exec('actionCategoryDelete', array('category' => $this));
     return true;
 }
Esempio n. 2
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;
    }