public function delete() { if ((int) $this->id === 0 || (int) $this->id === (int) Configuration::get('PS_ROOT_CATEGORY')) { return false; } $this->clearCache(); $deleted_children = $all_cat = $this->getAllChildren(); $all_cat[] = $this; foreach ($all_cat as $cat) { /** @var Category $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::getGroupsReductionByCategoryId((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, 'deleted_children' => $deleted_children)); return true; }
public function cleanPositions($table, $shopList = null) { if ($table == 'category') { //clean category position $cat = Category::getCategories(1, false, false); foreach ($cat as $i => $categ) { Category::cleanPositions((int) $categ['id_category']); } } if ($table == 'product') { //clean products position $cat = Category::getCategories(1, false, false); foreach ($cat as $i => $categ) { Product::cleanPositions((int) $categ['id_category']); } } }
public function postProcess($token = NULL) { global $cookie, $currentIndex; $this->tabAccess = Profile::getProfileAccess($cookie->profile, $this->id); if (Tools::isSubmit('submitAdd' . $this->table)) { if ($id_category = (int) Tools::getValue('id_category')) { if (!Category::checkBeforeMove($id_category, (int) Tools::getValue('id_parent'))) { $this->_errors[] = Tools::displayError('Category cannot be moved here'); return false; } } } elseif (isset($_GET['status']) and Tools::getValue($this->identifier)) { if ($this->tabAccess['edit'] === '1') { if (Validate::isLoadedObject($object = $this->loadObject())) { if ($object->toggleStatus()) { $target = ''; if ($id_category = (int) Tools::getValue('id_category') and Tools::getValue('id_product')) { $target = '&id_category=' . (int) $id_category; } else { $referrer = Tools::secureReferrer($_SERVER['HTTP_REFERER']); if (preg_match('/id_category=(\\d+)/', $referrer, $matches)) { $target = '&id_category=' . (int) $matches[1]; } } Module::hookExec('categoryUpdate'); Tools::redirectAdmin($currentIndex . '&conf=5' . $target . '&token=' . Tools::getValue('token')); } else { $this->_errors[] = Tools::displayError('An error occurred while updating status.'); } } else { $this->_errors[] = Tools::displayError('An error occurred while updating status for object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)'); } } else { $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); } } elseif (isset($_GET['delete' . $this->table])) { if ($this->tabAccess['delete'] === '1') { if (Validate::isLoadedObject($object = $this->loadObject()) and isset($this->fieldImageSettings)) { // check if request at least one object with noZeroObject if (isset($object->noZeroObject) and sizeof($taxes = call_user_func(array($this->className, $object->noZeroObject))) <= 1) { $this->_errors[] = Tools::displayError('You need at least one object.') . ' <b>' . $this->table . '</b><br />' . Tools::displayError('You cannot delete all of the items.'); } else { if ($this->deleted) { $object->deleteImage(); $object->deleted = 1; if ($object->update()) { Tools::redirectAdmin($currentIndex . '&conf=1&token=' . Tools::getValue('token') . '&id_category=' . (int) $object->id_parent); } } elseif ($object->delete()) { Tools::redirectAdmin($currentIndex . '&conf=1&token=' . Tools::getValue('token') . '&id_category=' . (int) $object->id_parent); } $this->_errors[] = Tools::displayError('An error occurred during deletion.'); } } else { $this->_errors[] = Tools::displayError('An error occurred while deleting object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)'); } } else { $this->_errors[] = Tools::displayError('You do not have permission to delete here.'); } } elseif (isset($_GET['position'])) { if ($this->tabAccess['edit'] !== '1') { $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); } elseif (!Validate::isLoadedObject($object = new Category((int) Tools::getValue($this->identifier, Tools::getValue('id_category_to_move', 1))))) { $this->_errors[] = Tools::displayError('An error occurred while updating status for object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)'); } if (!$object->updatePosition((int) Tools::getValue('way'), (int) Tools::getValue('position'))) { $this->_errors[] = Tools::displayError('Failed to update the position.'); } else { Tools::redirectAdmin($currentIndex . '&' . $this->table . 'Orderby=position&' . $this->table . 'Orderway=asc&conf=5' . (($id_category = (int) Tools::getValue($this->identifier, Tools::getValue('id_category_parent', 1))) ? '&' . $this->identifier . '=' . $id_category : '') . '&token=' . Tools::getAdminTokenLite('AdminCatalog')); } } elseif (Tools::getValue('submitDel' . $this->table)) { if ($this->tabAccess['delete'] === '1') { if (isset($_POST[$this->table . 'Box'])) { $category = new Category(); $result = true; $result = $category->deleteSelection(Tools::getValue($this->table . 'Box')); if ($result) { $category->cleanPositions((int) Tools::getValue('id_category')); Tools::redirectAdmin($currentIndex . '&conf=2&token=' . Tools::getAdminTokenLite('AdminCatalog') . '&id_category=' . (int) Tools::getValue('id_category')); } $this->_errors[] = Tools::displayError('An error occurred while deleting selection.'); } else { $this->_errors[] = Tools::displayError('You must select at least one element to delete.'); } } else { $this->_errors[] = Tools::displayError('You do not have permission to delete here.'); } return; } parent::postProcess(); }