diff --git a/htdocs/contact/class/contact.class.php b/htdocs/contact/class/contact.class.php index 8a1b26e9d33..dadb41179ca 100644 --- a/htdocs/contact/class/contact.class.php +++ b/htdocs/contact/class/contact.class.php @@ -1067,9 +1067,6 @@ class Contact extends CommonObject $error = 0; - //$this->old_lastname = $obj->lastname; - //$this->old_firstname = $obj->firstname; - $this->db->begin(); if (!$error) @@ -1081,7 +1078,7 @@ class Contact extends CommonObject $sql .= " WHERE ec.fk_socpeople=".$this->id; $sql .= " AND ec.fk_c_type_contact=tc.rowid"; $sql .= " AND tc.source='external'"; - dol_syslog(get_class($this)."::delete", LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { @@ -1094,7 +1091,7 @@ class Contact extends CommonObject $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact"; $sqldel .= " WHERE rowid = ".$obj->rowid; - dol_syslog(get_class($this)."::delete", LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $result = $this->db->query($sqldel); if (!$result) { @@ -1116,7 +1113,7 @@ class Contact extends CommonObject { // Remove Roles $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".$this->id; - dol_syslog(get_class($this)."::delete", LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { @@ -1130,7 +1127,7 @@ class Contact extends CommonObject { // Remove category $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".$this->id; - dol_syslog(get_class($this)."::delete", LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { @@ -1144,7 +1141,7 @@ class Contact extends CommonObject { $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople"; $sql .= " WHERE rowid=".$this->id; - dol_syslog(get_class($this)."::delete", LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $result = $this->db->query($sql); if (!$result) { diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 10e04896314..9dba821c012 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -438,10 +438,14 @@ abstract class CommonObject public $next_prev_filter; - + /** + * @var array List of child tables. To know object to delete on cascade. + * if name like with @ClassNAme:FilePathClass;ParentFkFieldName' it will + * call method deleteByParentField(parentId,ParentFkFieldName) to fetch and delete child object + */ + protected $childtablesoncascade = array(); // No constructor as it is an abstract class - /** * Check an object id/ref exists * If you don't need/want to instantiate object and just need to know if object exists, use this method instead of fetch @@ -7888,18 +7892,43 @@ abstract class CommonObject } // Delete cascade first - if (!empty($this->childtablesoncascade)) { + if (is_array($this->childtablesoncascade) && !empty($this->childtablesoncascade)) { foreach ($this->childtablesoncascade as $table) { - $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$table.' WHERE '.$this->fk_element.' = '.$this->id; - $resql = $this->db->query($sql); - if (!$resql) - { - $this->error = $this->db->lasterror(); - $this->errors[] = $this->error; - $this->db->rollback(); - return -1; - } + $deleteFromObject = explode(':', $table); + if (count($deleteFromObject)>=2) { + $className = str_replace('@', '', $deleteFromObject[0]); + $filePath = $deleteFromObject[1]; + $columnName = $deleteFromObject[2]; + if (dol_include_once($filePath)) { + $childObject = new $className($this->db); + if (method_exists($childObject, 'deleteByParentField')) { + $result = $childObject->deleteByParentField($this->id, $columnName); + if ($result < 0) { + $error++; + $this->errors[] = $childObject->error; + break; + } + } else { + $error++; + $this->errors[] = "You defined a cascade delete on an object $childObject but there is no method deleteByParentField for it"; + break; + } + } else { + $error++; + $this->errors[] = 'Cannot include child class file ' .$filePath; + break; + } + } else { + $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $table . ' WHERE ' . $this->fk_element . ' = ' . $this->id; + $resql = $this->db->query($sql); + if (!$resql) { + $error++; + $this->error = $this->db->lasterror(); + $this->errors[] = $this->error; + break; + } + } } } @@ -7946,6 +7975,62 @@ abstract class CommonObject } } + /** + * Delete all child object from a parent ID + * + * @param int $parentId Parent Id + * @param string $parentField Name of Foreign key parent column + * @return int <0 if KO, >0 if OK + * @throws Exception + */ + public function deleteByParentField($parentId = 0, $parentField = '') + { + global $user; + + $error = 0; + $deleted = 0; + + if (!empty($parentId) && !empty($parentField)) { + $this->db->begin(); + + $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . $this->table_element; + $sql .= ' WHERE '.$parentField.' = ' . (int) $parentId; + + $resql = $this->db->query($sql); + if (!$resql) { + $this->errors[] = $this->db->lasterror(); + $error++; + } else { + while ($obj = $this->db->fetch_object($resql)) { + $result = $this->fetch($obj->rowid); + if ($result < 0) { + $error++; + $this->errors[] = $this->error; + } else { + $result = $this->delete($user); + if ($result < 0) { + $error++; + $this->errors[] = $this->error; + } else { + $deleted++; + } + } + } + } + + if (empty($error)) { + $this->db->commit(); + return $deleted; + } else { + $this->error = implode(', ', $this->errors); + $this->db->rollback(); + return $error * -1; + } + } + + return $deleted; + } + /** * Delete a line of object in database * diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 67bdbf5142c..a37830b0ed9 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -83,7 +83,8 @@ class Societe extends CommonObject ); /** - * @var array List of child tables. To know object to delete on cascade. + * @var array List of child tables. To know object to delete on cascade. + * if name like with @ClassNAme:FilePathClass;ParentFkFieldName' it will call method deleteByParentField (with parentId as parameters) and FieldName to fetch and delete child object */ protected $childtablesoncascade = array( "societe_prices", @@ -92,7 +93,7 @@ class Societe extends CommonObject "product_fournisseur_price", "product_customer_price_log", "product_customer_price", - "socpeople", + "@Contact:/contact/class/contact.class.php:fk_soc", "adherent", "societe_account", "societe_rib", @@ -1677,16 +1678,36 @@ class Societe extends CommonObject } } - foreach ($this->childtablesoncascade as $tabletodelete) + if (!$error) { - if (!$error) + foreach ($this->childtablesoncascade as $tabletodelete) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete; - $sql .= " WHERE fk_soc = ".$id; - if (!$this->db->query($sql)) - { - $error++; - $this->errors[] = $this->db->lasterror(); + $deleteFromObject=explode(':', $tabletodelete); + if (count($deleteFromObject)>=2) { + $className=str_replace('@', '', $deleteFromObject[0]); + $filepath=$deleteFromObject[1]; + $columnName=$deleteFromObject[2]; + if (dol_include_once($filepath)) { + $child_object = new $className($this->db); + $result = $child_object->deleteByParentField($id, $columnName); + if ($result < 0) { + $error++; + $this->errors[] = $child_object->error; + break; + } + } else { + $error++; + $this->errors[] = 'Cannot include child class file ' .$filepath; + break; + } + } else { + $sql = "DELETE FROM " . MAIN_DB_PREFIX . $tabletodelete; + $sql .= " WHERE fk_soc = " . $id; + if (!$this->db->query($sql)) { + $error++; + $this->errors[] = $this->db->lasterror(); + break; + } } } }