diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index 59cb16b76fcc9d6ed4205727e35cb405f04c5745..9484e379a0cc02c75485b741dc523579a4143e5e 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -265,52 +265,110 @@ class Categorie } /** - * Delete category - * Les produits et sous-categories deviennent orphelins - * si $all = false, et sont (seront :) supprimes sinon + * Delete a category from database + * @param user Object user that ask to delete */ - function remove ($all = false) + function delete($user) { + global $conf,$langs; - $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_product"; - $sql .= " WHERE fk_categorie = ".$this->id; + $error=0; + + dol_syslog("Categorie::remove"); + + $this->db->begin(); - if (!$this->db->query($sql)) + if (! $error) { - dol_print_error($this->db); - return -1; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_societe"; + $sql .= " WHERE fk_categorie = ".$this->id; + if (!$this->db->query($sql)) + { + $this->error=$this->db->lasterror(); + dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR); + $error++; + } } - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association"; - $sql .= " WHERE fk_categorie_mere = ".$this->id; - $sql .= " OR fk_categorie_fille = ".$this->id; - - if (!$this->db->query($sql)) + if (! $error) { - dol_print_error($this->db); - return -1; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_fournisseur"; + $sql .= " WHERE fk_categorie = ".$this->id; + if (!$this->db->query($sql)) + { + $this->error=$this->db->lasterror(); + dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR); + $error++; + } } - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie"; - $sql .= " WHERE rowid = ".$this->id; - - if (!$this->db->query($sql)) + if (! $error) { - dol_print_error($this->db); - return -1; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_product"; + $sql .= " WHERE fk_categorie = ".$this->id; + if (!$this->db->query($sql)) + { + $this->error=$this->db->lasterror(); + dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR); + $error++; + } } - else + if (! $error) { - // Appel des triggers - include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"); - $interface=new Interfaces($this->db); - $result=$interface->run_triggers('CATEGORY_DELETE',$this,$user,$langs,$conf); - if ($result < 0) { $error++; $this->errors=$interface->errors; } - // Fin appel triggers + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_member"; + $sql .= " WHERE fk_categorie = ".$this->id; + if (!$this->db->query($sql)) + { + $this->error=$this->db->lasterror(); + dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR); + $error++; + } + } + + // Link childs to parent + if (! $error) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association"; + $sql .= " WHERE fk_categorie_mere = ".$this->id; + $sql .= " OR fk_categorie_fille = ".$this->id; + if (!$this->db->query($sql)) + { + $this->error=$this->db->lasterror(); + dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR); + $error++; + } + } + // Delete category + if (! $error) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie"; + $sql .= " WHERE rowid = ".$this->id; + if (!$this->db->query($sql)) + { + $this->error=$this->db->lasterror(); + dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR); + $error++; + } + else + { + // Appel des triggers + include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"); + $interface=new Interfaces($this->db); + $result=$interface->run_triggers('CATEGORY_DELETE',$this,$user,$langs,$conf); + if ($result < 0) { $error++; $this->errors=$interface->errors; $this->error=join(',',$this->errors); } + // Fin appel triggers + } + } + + if (! $error) + { + $this->db->commit(); return 1; } - + else + { + $this->db->rollback(); + return -1; + } } diff --git a/htdocs/categories/viewcat.php b/htdocs/categories/viewcat.php index ce6f6b8a72bf025ba8bc8c65a68315d369fa029b..329827cfa6a41029d4c643ced3c127992eb2e9b2 100644 --- a/htdocs/categories/viewcat.php +++ b/htdocs/categories/viewcat.php @@ -60,7 +60,7 @@ $type=$c->type; if ($user->rights->categorie->supprimer && $_POST["action"] == 'confirm_delete' && $_POST['confirm'] == 'yes') { - if ($c->remove() >= 0) + if ($c->delete($user) >= 0) { header("Location: ".DOL_URL_ROOT.'/categories/index.php?type='.$type); exit;