diff --git a/classes/Category.php b/classes/Category.php index 6123ae2e9..cc6356e5c 100644 --- a/classes/Category.php +++ b/classes/Category.php @@ -311,52 +311,47 @@ class CategoryCore extends ObjectModel $this->clearCache(); - /* Get childs categories */ - $to_delete = array((int)$this->id); - $this->recursiveDelete($to_delete, (int)$this->id); - $to_delete = array_unique($to_delete); - - /* Delete category and its child from database */ - $list = count($to_delete) > 1 ? implode(',', array_map('intval', $to_delete)) : (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.')'); - Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'category_shop` WHERE `id_category` IN ('.$list.')'); - - // Delete associated restrictions on cart rules - CartRule::cleanProductRuleIntegrity('categories', $to_delete); - - Category::cleanPositions($this->id_parent); - - /* Delete category images and its children images */ - $tmp_category = new Category(); - foreach ($to_delete as $id_category) + $all_cat = $this->getAllChildren(); + $all_cat[] = $this; + foreach ($all_cat as $cat) { - $tmp_category->id = (int)$id_category; - $tmp_category->deleteImage(); + parent::delete(); + 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); + } } - + + $all_product_asso = array(); + $tmp = Db::getInstance()->executeS('SELECT `id_product` FROM `'._DB_PREFIX_.'category_product`'); + foreach ($tmp as $val) + $all_product_asso[] = $val; + /* Delete or link 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) + $fatherless_products = new Collection('Product', Context::getContext()->language->id); + $fatherless_products->where('id_product', 'notin', $all_product_asso); + + foreach ($fatherless_products as $poor_product) { - $product = new Product((int)$p['id_product']); - if (Validate::isLoadedObject($product)) + if (Validate::isLoadedObject($poor_product)) { if ($this->remove_products || $this->id_parent == 0) - $product->delete(); + $poor_product->delete(); else { if ($this->disable_products) - $product->active = 0; + $poor_product->active = 0; - $product->addToCategories($this->id_parent); - $product->save(); + $poor_product->addToCategories($this->id_parent); + $poor_product->save(); } } } @@ -368,18 +363,13 @@ class CategoryCore extends ObjectModel WHERE `id_category_default` NOT IN (SELECT `id_category` FROM `'._DB_PREFIX_.'category`) '); - + /* Rebuild the nested tree */ - if (!isset($this->doNotRegenerateNTree) || !$this->doNotRegenerateNTree) + if (!$this->hasMultishopEntries() && (!isset($this->doNotRegenerateNTree) || !$this->doNotRegenerateNTree)) Category::regenerateEntireNtree(); Hook::exec('actionCategoryDelete', array('category' => $this)); - - /* Delete Categories in GroupReduction */ - foreach ($to_delete as $category) - if (GroupReduction::getGroupReductionByCategoryId((int)$category)) - GroupReduction::deleteCategory($category); - + return true; } @@ -1041,6 +1031,11 @@ class CategoryCore extends ObjectModel { Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'category_group` WHERE `id_category` = '.(int)$this->id); } + + public function cleanAssoProducts() + { + Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'category_product` WHERE `id_category` = '.(int)$this->id); + } public function addGroups($groups) { diff --git a/classes/ObjectModel.php b/classes/ObjectModel.php index 8fd9b26c6..58136b3cf 100644 --- a/classes/ObjectModel.php +++ b/classes/ObjectModel.php @@ -310,9 +310,9 @@ abstract class ObjectModelCore else { $fields = array($this->id_lang => $this->formatFields(self::FORMAT_LANG, $this->id_lang)); - $fields['id_lang'] = $this->id_lang; + $fields[$this->id_lang]['id_lang'] = $this->id_lang; if ($this->id_shop && $is_lang_multishop) - $fields['id_shop'] = (int)$this->id_shop; + $fields[$this->id_lang]['id_shop'] = (int)$this->id_shop; } return $fields; @@ -577,13 +577,12 @@ abstract class ObjectModelCore $id_shop_list = Shop::getContextListShopID(); if (count($this->id_shop_list) > 0) $id_shop_list = $this->id_shop_list; - foreach ($id_shop_list as $id_shop) { $field['id_shop'] = (int)$id_shop; $where = pSQL($this->def['primary']).' = '.(int)$this->id .' AND id_lang = '.(int)$field['id_lang'] - .' AND id_shop = '.$field['id_shop']; + .' AND id_shop = '.(int)$id_shop; if (Db::getInstance()->getValue('SELECT COUNT(*) FROM '.pSQL(_DB_PREFIX_.$this->def['table']).'_lang WHERE '.$where)) $result &= Db::getInstance()->update($this->def['table'].'_lang', $field, $where);