diff --git a/admin-dev/themes/default/template/controllers/categories/list_header.tpl b/admin-dev/themes/default/template/controllers/categories/list_header.tpl index c37044f5a..1ceeefcd8 100644 --- a/admin-dev/themes/default/template/controllers/categories/list_header.tpl +++ b/admin-dev/themes/default/template/controllers/categories/list_header.tpl @@ -50,4 +50,38 @@ {/foreach} {/if} + {if isset($delete_category) && $delete_category} +
+
+

{l s='Do you want to delete products too?'}

+ + {foreach $POST as $key => $value} + {if $key != 'deleteMode'} + {if is_array($value)} + {foreach $value as $val} + + {/foreach} + {else} + + {/if} + {/if} + {/foreach} +
+
+
+
 
+ {/if} {/block} diff --git a/classes/Category.php b/classes/Category.php index 5f7eb405e..12e2d4e0f 100644 --- a/classes/Category.php +++ b/classes/Category.php @@ -78,6 +78,12 @@ class CategoryCore extends ObjectModel public $is_root_category; public $groupBox; + + /** @var boolean does the product have to be removed during the delete process */ + public $remove_products = true; + + /** @var boolean does the product have to be disable during the delete process */ + public $disable_products = false; protected static $_links = array(); @@ -195,15 +201,15 @@ class CategoryCore extends ObjectModel } /** - * Recursive scan of subcategories - * - * @param integer $max_depth Maximum depth of the tree (i.e. 2 => 3 levels depth) - * @param integer $current_depth specify the current depth in the tree (don't use it, only for rucursivity!) - * @param integer $id_lang Specify the id of the language used - * @param array $excluded_ids_array specify a list of ids to exclude of results - * - * @return array Subcategories lite tree - */ + * Recursive scan of subcategories + * + * @param integer $max_depth Maximum depth of the tree (i.e. 2 => 3 levels depth) + * @param integer $current_depth specify the current depth in the tree (don't use it, only for rucursivity!) + * @param integer $id_lang Specify the id of the language used + * @param array $excluded_ids_array specify a list of ids to exclude of results + * + * @return array Subcategories lite tree + */ public function recurseLiteCategTree($max_depth = 3, $current_depth = 0, $id_lang = null, $excluded_ids_array = null) { $id_lang = is_null($id_lang) ? Context::getContext()->language->id : (int)$id_lang; @@ -245,15 +251,15 @@ class CategoryCore extends ObjectModel /** - * Recursively add specified category childs to $to_delete array - * - * @param array &$to_delete Array reference where categories ID will be saved - * @param array $id_category Parent category ID - */ + * Recursively add specified category childs to $to_delete array + * + * @param array &$to_delete Array reference where categories ID will be saved + * @param array $id_category Parent category ID + */ protected function recursiveDelete(&$to_delete, $id_category) { - if (!is_array($to_delete) || !$id_category) - die(Tools::displayError()); + if (!is_array($to_delete) || !$id_category) + die(Tools::displayError()); $result = Db::getInstance()->executeS(' SELECT `id_category` @@ -295,7 +301,7 @@ class CategoryCore extends ObjectModel $tmp_category->deleteImage(); } - /* Delete products which were not in others categories */ + /* Delete or link products which were not in others categories */ $result = Db::getInstance()->executeS(' SELECT `id_product` FROM `'._DB_PREFIX_.'product` @@ -305,7 +311,20 @@ class CategoryCore extends ObjectModel { $product = new Product((int)$p['id_product']); if (Validate::isLoadedObject($product)) - $product->delete(); + { + if ($this->remove_products) + $product->delete(); + else + { + if ($this->disable_products) + { + $product->active = 0; + } + + $product->addToCategories($this->id_parent); + $product->save(); + } + } } /* Set category default to 1 where categorie no more exists */ @@ -892,11 +911,11 @@ class CategoryCore extends ObjectModel } /** - * Get Each parent category of this category until the root category - * - * @param integer $id_lang Language ID - * @return array Corresponding categories - */ + * Get Each parent category of this category until the root category + * + * @param integer $id_lang Language ID + * @return array Corresponding categories + */ public function getParentsCategories($id_lang = null) { $context = Context::getContext(); diff --git a/controllers/admin/AdminCategoriesController.php b/controllers/admin/AdminCategoriesController.php index c9ca9a245..99031d870 100644 --- a/controllers/admin/AdminCategoriesController.php +++ b/controllers/admin/AdminCategoriesController.php @@ -90,11 +90,23 @@ class AdminCategoriesControllerCore extends AdminController parent::init(); // context->shop is set in the init() function, so we move the _category instanciation after that - if ($id_category = Tools::getvalue('id_category')) + if (($id_category = Tools::getvalue('id_category')) && $this->action != 'select_delete') $this->_category = new Category($id_category); else $this->_category = new Category($this->context->shop->id_category); } + + public function initContent() + { + if ($this->action == 'select_delete') + $this->context->smarty->assign(array( + 'delete_form' => true, + 'url_delete' => htmlentities($_SERVER['REQUEST_URI']), + 'boxes' => $this->boxes, + )); + + parent::initContent(); + } public function setMedia() { @@ -159,6 +171,11 @@ class AdminCategoriesControllerCore extends AdminController $this->tpl_list_vars['categories_name'] = $categories_name; $this->tpl_list_vars['category_root'] = $root; + if (Tools::isSubmit('submitBulkdelete'.$this->table) OR Tools::isSubmit('delete'.$this->table)) + $this->tpl_list_vars['delete_category'] = true; + $this->tpl_list_vars['REQUEST_URI'] = $_SERVER['REQUEST_URI']; + $this->tpl_list_vars['POST'] = $_POST; + return parent::renderList(); } @@ -225,7 +242,14 @@ class AdminCategoriesControllerCore extends AdminController $this->action = 'add'.$this->table.'root'; $this->display = 'edit'; } - return parent::initProcess(); + + parent::initProcess(); + + if ($this->action == 'delete' || $this->action == 'bulkdelete') + if (Tools::getValue('deleteMode') == 'link' || Tools::getValue('deleteMode') == 'linkanddisable' || Tools::getValue('deleteMode') == 'delete') + $this->delete_mode = Tools::getValue('deleteMode'); + else + $this->action = 'select_delete'; } public function renderForm() @@ -443,6 +467,21 @@ class AdminCategoriesControllerCore extends AdminController { if ($this->tabAccess['delete'] === '1') { + if ($this->delete_mode == 'link' || $this->delete_mode == 'linkanddisable') + { + if (Validate::isLoadedObject($object = $this->loadObject())) + { + $object->remove_products = false; + if ($this->delete_mode == 'linkanddisable') + $object->disable_products = true; + } + } + else if ($this->delete_mode != 'delete') + { + $this->_errors[] = Tools::displayError('Unknown delete mode:'.' '.$this->deleted); + return; + } + if (Tools::isSubmit($this->table.'Box')) { if (isset($_POST[$this->table.'Box']))