From 096bc90802c87bfdb81e845089c483b127541c4a Mon Sep 17 00:00:00 2001 From: dMetzger Date: Tue, 24 Jul 2012 13:11:56 +0000 Subject: [PATCH] [-] BO : cart rules are now cleaned when a component of a product rule (like a product, a category, a supplier...) is deleted git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@16548 b9a71923-0436-4b27-9f14-aed3839534dd --- classes/Attribute.php | 3 +++ classes/CartRule.php | 40 ++++++++++++++++++++++++++++++++++++++++ classes/Category.php | 3 +++ classes/Manufacturer.php | 3 +++ classes/Product.php | 9 ++++++++- classes/Supplier.php | 3 +++ 6 files changed, 60 insertions(+), 1 deletion(-) diff --git a/classes/Attribute.php b/classes/Attribute.php index 85c1afe94..5950d43a2 100644 --- a/classes/Attribute.php +++ b/classes/Attribute.php @@ -93,6 +93,9 @@ class AttributeCore extends ObjectModel foreach ($combinations as $combination) $combination->delete(); } + + // Delete associated restrictions on cart rules + CartRule::cleanProductRuleIntegrity('attributes', $this->id); /* Reinitializing position */ $this->cleanPositions((int)$this->id_attribute_group); diff --git a/classes/CartRule.php b/classes/CartRule.php index d753fed71..c53b886ee 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -1096,6 +1096,46 @@ class CartRuleCore extends ObjectModel { return (bool)Configuration::get('PS_CART_RULE_FEATURE_ACTIVE'); } + + /* When an entity associated to a product rule (product, category, attribute, supplier, manufacturer...) is deleted, the product rules must be updated */ + public static function cleanProductRuleIntegrity($type, $list) + { + // Type must be available in the 'type' enum of the table cart_rule_product_rule + if (!in_array($type, array('products', 'categories', 'attributes', 'manufacturers', 'suppliers'))) + return false; + + // This check must not be removed because this var is used a few lines below + $list = (is_array($list) ? implode(',', array_map('intval', $list)) : (int)$list); + if (!preg_match('/^[0-9,]+$/', $list)) + return false; + + // Delete associated restrictions on cart rules + Db::getInstance()->execute(' + DELETE crprv + FROM `'._DB_PREFIX_.'cart_rule_product_rule` crpr + LEFT JOIN `'._DB_PREFIX_.'cart_rule_product_rule_value` crprv ON crpr.`id_product_rule` = crprv.`id_product_rule` + WHERE crpr.`type` = "'.pSQL($type).'" + AND crprv.`id_item` IN ('.$list.')'); // $list is checked a few lines above + + // Delete the product rules that does not have any values + if (Db::getInstance()->Affected_Rows() > 0) + Db::getInstance()->execute(' + DELETE FROM `'._DB_PREFIX_.'cart_rule_product_rule` + WHERE `id_product_rule` NOT IN (SELECT id_product_rule FROM `'._DB_PREFIX_.'cart_rule_product_rule_value`)'); + // If the product rules were the only conditions of a product rule group, delete the product rule group + if (Db::getInstance()->Affected_Rows() > 0) + Db::getInstance()->execute(' + DELETE FROM `'._DB_PREFIX_.'cart_rule_product_rule_group` + WHERE `id_product_rule_group` NOT IN (SELECT id_product_rule_group FROM `'._DB_PREFIX_.'cart_rule_product_rule`)'); + // If the product rule group were the only restrictions of a cart rule, update de cart rule restriction cache + if (Db::getInstance()->Affected_Rows() > 0) + Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'cart_rule` cr + LEFT JOIN `'._DB_PREFIX_.'cart_rule_product_rule_group` crprg ON cr.id_cart_rule = crprg.id_cart_rule + SET product_restriction = IF(crprg.id_product_rule_group IS NULL, 0, 1)'); + + return true; + } /** * @static diff --git a/classes/Category.php b/classes/Category.php index be7f0fada..6123ae2e9 100644 --- a/classes/Category.php +++ b/classes/Category.php @@ -323,6 +323,9 @@ class CategoryCore extends ObjectModel 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); diff --git a/classes/Manufacturer.php b/classes/Manufacturer.php index a384520b7..1c927ed33 100644 --- a/classes/Manufacturer.php +++ b/classes/Manufacturer.php @@ -117,7 +117,10 @@ class ManufacturerCore extends ObjectModel return false; if (parent::delete()) + { + CartRule::cleanProductRuleIntegrity('manufacturers', $this->id); return $this->deleteImage(); + } } /** diff --git a/classes/Product.php b/classes/Product.php index 181f7620d..765546ec9 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -672,7 +672,8 @@ class ProductCore extends ObjectModel !$this->deleteAccessories() || !$this->deleteFromAccessories() || !$this->deleteFromSupplier() || - !$this->deleteDownload()) + !$this->deleteDownload() || + !$this->deleteFromCartRules()) return false; return true; @@ -690,6 +691,12 @@ class ProductCore extends ObjectModel return $return; } + public function deleteFromCartRules() + { + CartRule::cleanProductRuleIntegrity('products', $this->id); + return true; + } + public function deleteFromSupplier() { return Db::getInstance()->delete('product_supplier', 'id_product = '.(int)$this->id); diff --git a/classes/Supplier.php b/classes/Supplier.php index 41bc06e52..204a40e11 100644 --- a/classes/Supplier.php +++ b/classes/Supplier.php @@ -355,7 +355,10 @@ class SupplierCore extends ObjectModel public function delete() { if (parent::delete()) + { + CartRule::cleanProductRuleIntegrity('suppliers', $this->id); return $this->deleteImage(); + } } /**