diff --git a/classes/Category.php b/classes/Category.php index 14976922f..35396a13b 100644 --- a/classes/Category.php +++ b/classes/Category.php @@ -1132,8 +1132,10 @@ class CategoryCore extends ObjectModel * @param mixed $id_category_parent * @return boolean true if succeed */ - public static function cleanPositions($id_category_parent) + public static function cleanPositions($id_category_parent = null) { + if ($id_category_parent === null) + return; $return = true; $id = Context::getContext()->shop->id; diff --git a/classes/Product.php b/classes/Product.php index a3192839d..442ce3df3 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -762,24 +762,15 @@ class ProductCore extends ObjectModel if (empty($categories)) return false; - // take the first selected category to get the category tree (nleft and nright) - $category = new Category($categories[0]); - // get max position in each categories - $and = ''; - if (Shop::isFeatureActive()) - $and = ' - AND (c.`nleft` < '.(int)$category->nleft.' - AND c.`nright` > '.(int)$category->nright.' - AND c.`level_depth` = 1)'; $result = Db::getInstance()->executeS(' SELECT c.`id_category` FROM `'._DB_PREFIX_.'category_product` cp LEFT JOIN `'._DB_PREFIX_.'category` c ON (c.`id_category` = cp.`id_category`) WHERE cp.`id_category` NOT IN('.implode(',', array_map('intval', $categories)).') - '.$and.' - AND cp.id_product = '.$this->id + AND cp.id_product = '.$this->id ); + foreach ($result as $categ_to_delete) $this->deleteCategory($categ_to_delete['id_category']); // if none are found, it's an error diff --git a/controllers/admin/AdminCategoriesController.php b/controllers/admin/AdminCategoriesController.php index 0acbb57b0..cfabea6e7 100644 --- a/controllers/admin/AdminCategoriesController.php +++ b/controllers/admin/AdminCategoriesController.php @@ -530,7 +530,8 @@ class AdminCategoriesControllerCore extends AdminController foreach (Tools::getValue($this->table.'Box') as $id_category) { $category = new Category((int)$id_category); - $cats_ids[$category->id] = $category->id_parent; + if (!$category->isRootCategoryForAShop()) + $cats_ids[$category->id] = $category->id_parent; } if (parent::processBulkDelete()) @@ -546,13 +547,13 @@ class AdminCategoriesControllerCore extends AdminController public function processDelete() { + $category = $this->loadObject(); if ($this->tabAccess['delete'] === '1') { - if ($this->isRootCategoryForAShop()) + if ($category->isRootCategoryForAShop()) $this->errors[] = Tools::displayError('You cannot remove this category because a shop uses this category as a root category.'); else if (parent::processDelete()) { - $category = $this->loadObject(); $this->setDeleteMode(); $this->processFatherlessProducts((int)$category->id_parent); return true; @@ -574,7 +575,6 @@ class AdminCategoriesControllerCore extends AdminController /* Delete or link products which were not in others categories */ $fatherless_products = new Collection('Product', Context::getContext()->language->id); $fatherless_products->where('id_product', 'notin', $all_product_asso); - foreach ($fatherless_products as $poor_product) { if (Validate::isLoadedObject($poor_product)) @@ -585,19 +585,12 @@ class AdminCategoriesControllerCore extends AdminController { if ($this->disable_products) $poor_product->active = 0; - $poor_product->addToCategories($id_parent); + $poor_product->id_category_default = (int)$id_parent; + $poor_product->addToCategories((int)$id_parent); $poor_product->save(); } } } - - /* Set category default to Home category where categorie no more exists */ - Db::getInstance()->execute(' - UPDATE `'._DB_PREFIX_.'product_shop` - SET `id_category_default` = '.(int)Configuration::get('PS_HOME_CATEGORY').' - WHERE `id_category_default` - NOT IN (SELECT `id_category` FROM `'._DB_PREFIX_.'category`) - '); } public function processPosition()