diff --git a/htdocs/contact/class/contact.class.php b/htdocs/contact/class/contact.class.php index 71d816c166e..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) @@ -1731,57 +1728,4 @@ class Contact extends CommonObject return $error * -1; } } - - /** - * Delete all contact from a thirdparty - * @param int $socId Thirdparty Id - * @param int $notrigger Disable all trigger - * @return int <0 if KO, >0 if OK - * @throws Exception - */ - public function deleteBySoc($socId = 0, $notrigger = 0) - { - $error = 0; - $deleted = 0; - - if (!empty($socId)) { - $this->db->begin(); - - $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . $this->table_element; - $sql .= " WHERE fk_soc = " . $socId; - dol_syslog(__METHOD__, LOG_DEBUG); - $resql = $this->db->query($sql); - if (!$resql) { - $this->errors[] = $this->db->lasterror() . ' sql=' . $sql; - $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($notrigger); - 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; - } } diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 7fe7fc41303..d8ff3cde3c4 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 @@ -7892,16 +7896,18 @@ abstract class CommonObject foreach ($this->childtablesoncascade as $table) { $deleteFromObject=explode(':', $table); - if (count($deleteFromObject)>1) { + if (count($deleteFromObject)>=2) { $className=str_replace('@', '', $deleteFromObject[0]); $filePath=$deleteFromObject[1]; - $deleteMethod=$deleteFromObject[2]; + $columnName=$deleteFromObject[2]; if (dol_include_once($filePath)) { $childObject = new $className($this->db); - $result= $childObject->{$deleteMethod}($this->id); - if ($result<0) { - $this->errors[] = $childObject->error; - return -1; + if (is_callable($childObject, 'deleteByParentField')) { + $result = $childObject->deleteByParentField($this->id, $columnName); + if ($result < 0) { + $this->errors[] = $childObject->error; + return -1; + } } } else { $this->errors[] = 'Cannot find child class file ' .$filePath; @@ -7963,6 +7969,75 @@ abstract class CommonObject } } + /** + * Delete all child object + * @param int $parentId Parent Id + * @param string $parentField Name of parent FIled + * @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.' = ' . $parentId; + dol_syslog(__METHOD__, LOG_DEBUG); + $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 { + $needUserParam = false; + if (class_exists('ReflectionMethod')) { + $method = new ReflectionMethod($this, 'delete'); + $argsMethod=$method->getParameters(); + if (is_array($argsMethod) && count($argsMethod)>0) { + if ($argsMethod[0]->name == 'user') { + $needUserParam = true; + } + } + } + if ($needUserParam) { + $result = $this->delete($user); + } else { + $result = $this->delete(); + } + 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 f5caa59e1f1..242b3c3efd4 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -84,7 +84,7 @@ class Societe extends CommonObject /** * @var array List of child tables. To know object to delete on cascade. - * if name like with @ClassNAme:FilePathClass:MethodDetele' it will call method to delete object rather tahn SQL delete + * 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", @@ -93,7 +93,7 @@ class Societe extends CommonObject "product_fournisseur_price", "product_customer_price_log", "product_customer_price", - "@Contact:/contact/class/contact.class.php:deleteBySoc", + "@Contact:/contact/class/contact.class.php:fk_soc", "adherent", "societe_account", "societe_rib", @@ -1682,17 +1682,19 @@ class Societe extends CommonObject { if (!$error) { - $delete_from_object=explode(':', $tabletodelete); - if (count($delete_from_object)>1) { - $class_name=str_replace('@', '', $delete_from_object[0]); - $filepath=$delete_from_object[1]; - $delete_method=$delete_from_object[2]; + $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 $class_name($this->db); - $result= $child_object->{$delete_method}($id); - if ($result<0) { - $error++; - $this->errors[] = $child_object->error; + if (class_exists($className)) { + $child_object = new $className($this->db); + $result = $child_object->deleteByParentField($id, $columnName); + if ($result < 0) { + $error++; + $this->errors[] = $child_object->error; + } } } else { $error++; @@ -1706,6 +1708,7 @@ class Societe extends CommonObject $this->errors[] = $this->db->lasterror(); } } + } }