From 7ada0de6549ff6d110c72626da33179c59742961 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Tue, 26 May 2020 21:27:46 +0200 Subject: [PATCH 01/18] FIX : #13968 --- htdocs/societe/class/societe.class.php | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 9cc36dbb7b4..cffae4011c0 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -105,6 +105,13 @@ class Societe extends CommonObject "actioncomm", ); + /** + * Build as [parentChildTable]=>[childTable]=>array(parentChildTable.PrimaryKey,childTable.ForeignKey,WhereClause) + * Define into Contrcutor because of MAIN_DB_PREFIX cannot be use here + * @var array List of Parent's child's child tables. To know object to delete on cascade. + */ + protected $parentchildchildtablesoncascade = array(); + /** * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png */ @@ -653,6 +660,22 @@ class Societe extends CommonObject $this->forme_juridique_code = 0; $this->tva_assuj = 1; $this->status = 1; + + $this->parentchildchildtablesoncascade = array( + 'socpeople'=> + array( + MAIN_DB_PREFIX.'socpeople_extrafields' => + array(MAIN_DB_PREFIX.'socpeople.rowid',MAIN_DB_PREFIX.'socpeople_extrafields.fk_object',''), + MAIN_DB_PREFIX.'element_contact' => + array(MAIN_DB_PREFIX.'socpeople.rowid', + MAIN_DB_PREFIX.'element_contact.fk_socpeople', + MAIN_DB_PREFIX.'element_contact.fk_c_type_contact IN (SELECT ct.rowid FROM '.MAIN_DB_PREFIX.'c_type_contact as ct WHERE ct.source=\'external\')'), + MAIN_DB_PREFIX.'societe_contacts' => + array(MAIN_DB_PREFIX.'socpeople.rowid', + MAIN_DB_PREFIX.'societe_contacts.fk_socpeople', + MAIN_DB_PREFIX.'societe_contacts.fk_c_type_contact IN (SELECT ct.rowid FROM '.MAIN_DB_PREFIX.'c_type_contact as ct WHERE ct.source=\'external\')'), + ) + ); } @@ -1673,6 +1696,22 @@ class Societe extends CommonObject foreach ($this->childtablesoncascade as $tabletodelete) { + if (!$error && array_key_exists($tabletodelete, $this->parentchildchildtablesoncascade)) + { + if (count($this->parentchildchildtablesoncascade[$tabletodelete])>0){ + foreach($this->parentchildchildtablesoncascade[$tabletodelete] as $childtabletodelete=>$dataToDelette) { + $sql = "DELETE FROM ". $childtabletodelete; + $sql .= " WHERE ".$dataToDelette[1]." IN (SELECT ".$dataToDelette[0]." FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE fk_soc = " . $id.")"; + if (!empty($dataToDelette[3])) { + $sql .= " AND ".$dataToDelette[3]; + } + if (!$this->db->query($sql)) { + $error++; + $this->errors[] = $this->db->lasterror(); + } + } + } + } if (!$error) { $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete; @@ -1683,6 +1722,7 @@ class Societe extends CommonObject $this->errors[] = $this->db->lasterror(); } } + } // Removed extrafields From e5edc06f3d3379c90947ff2664e6d90fa5a4fc5d Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Tue, 26 May 2020 21:29:47 +0200 Subject: [PATCH 02/18] spelling --- htdocs/societe/class/societe.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index cffae4011c0..f1a32f1ff2f 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -107,7 +107,7 @@ class Societe extends CommonObject /** * Build as [parentChildTable]=>[childTable]=>array(parentChildTable.PrimaryKey,childTable.ForeignKey,WhereClause) - * Define into Contrcutor because of MAIN_DB_PREFIX cannot be use here + * Define into Contructor because of MAIN_DB_PREFIX cannot be use here * @var array List of Parent's child's child tables. To know object to delete on cascade. */ protected $parentchildchildtablesoncascade = array(); From ceb0a5b0b5910ded71384bc34663a6cc4f256980 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Tue, 26 May 2020 21:32:18 +0200 Subject: [PATCH 03/18] space --- htdocs/societe/class/societe.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index f1a32f1ff2f..abf818f2fe0 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -1722,7 +1722,6 @@ class Societe extends CommonObject $this->errors[] = $this->db->lasterror(); } } - } // Removed extrafields From 2337d75d92e60ca74f05e096bffce7295fbd9f4f Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Mon, 1 Jun 2020 10:43:57 +0200 Subject: [PATCH 04/18] better fix for #13968 --- htdocs/contact/class/contact.class.php | 63 ++++++++++++++++++-- htdocs/core/class/commonobject.class.php | 35 +++++++++--- htdocs/societe/class/societe.class.php | 73 +++++++++--------------- 3 files changed, 110 insertions(+), 61 deletions(-) diff --git a/htdocs/contact/class/contact.class.php b/htdocs/contact/class/contact.class.php index 8a1b26e9d33..e200a309378 100644 --- a/htdocs/contact/class/contact.class.php +++ b/htdocs/contact/class/contact.class.php @@ -1081,7 +1081,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(get_class($this)."::".__METHOD__, LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { @@ -1094,7 +1094,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(get_class($this)."::".__METHOD__, LOG_DEBUG); $result = $this->db->query($sqldel); if (!$result) { @@ -1116,7 +1116,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(get_class($this)."::".__METHOD__, LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { @@ -1130,7 +1130,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(get_class($this)."::".__METHOD__, LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { @@ -1144,7 +1144,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(get_class($this)."::".__METHOD__, LOG_DEBUG); $result = $this->db->query($sql); if (!$result) { @@ -1731,4 +1731,57 @@ 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 10e04896314..7fe7fc41303 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7891,15 +7891,32 @@ abstract class CommonObject if (!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)>1) { + $className=str_replace('@', '', $deleteFromObject[0]); + $filePath=$deleteFromObject[1]; + $deleteMethod=$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; + } + } else { + $this->errors[] = 'Cannot find child class file ' .$filePath; + return -1; + } + } else { + $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; + } + } } } diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index e51f8913e48..f5caa59e1f1 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:MethodDetele' it will call method to delete object rather tahn SQL delete */ 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:deleteBySoc", "adherent", "societe_account", "societe_rib", @@ -105,13 +106,6 @@ class Societe extends CommonObject "actioncomm", ); - /** - * Build as [parentChildTable]=>[childTable]=>array(parentChildTable.PrimaryKey,childTable.ForeignKey,WhereClause) - * Define into Contructor because of MAIN_DB_PREFIX cannot be use here - * @var array List of Parent's child's child tables. To know object to delete on cascade. - */ - protected $parentchildchildtablesoncascade = array(); - /** * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png */ @@ -666,22 +660,6 @@ class Societe extends CommonObject $this->forme_juridique_code = 0; $this->tva_assuj = 1; $this->status = 1; - - $this->parentchildchildtablesoncascade = array( - 'socpeople'=> - array( - MAIN_DB_PREFIX.'socpeople_extrafields' => - array(MAIN_DB_PREFIX.'socpeople.rowid',MAIN_DB_PREFIX.'socpeople_extrafields.fk_object',''), - MAIN_DB_PREFIX.'element_contact' => - array(MAIN_DB_PREFIX.'socpeople.rowid', - MAIN_DB_PREFIX.'element_contact.fk_socpeople', - MAIN_DB_PREFIX.'element_contact.fk_c_type_contact IN (SELECT ct.rowid FROM '.MAIN_DB_PREFIX.'c_type_contact as ct WHERE ct.source=\'external\')'), - MAIN_DB_PREFIX.'societe_contacts' => - array(MAIN_DB_PREFIX.'socpeople.rowid', - MAIN_DB_PREFIX.'societe_contacts.fk_socpeople', - MAIN_DB_PREFIX.'societe_contacts.fk_c_type_contact IN (SELECT ct.rowid FROM '.MAIN_DB_PREFIX.'c_type_contact as ct WHERE ct.source=\'external\')'), - ) - ); } @@ -1702,30 +1680,31 @@ class Societe extends CommonObject foreach ($this->childtablesoncascade as $tabletodelete) { - if (!$error && array_key_exists($tabletodelete, $this->parentchildchildtablesoncascade)) - { - if (count($this->parentchildchildtablesoncascade[$tabletodelete])>0){ - foreach($this->parentchildchildtablesoncascade[$tabletodelete] as $childtabletodelete=>$dataToDelette) { - $sql = "DELETE FROM ". $childtabletodelete; - $sql .= " WHERE ".$dataToDelette[1]." IN (SELECT ".$dataToDelette[0]." FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE fk_soc = " . $id.")"; - if (!empty($dataToDelette[3])) { - $sql .= " AND ".$dataToDelette[3]; - } - if (!$this->db->query($sql)) { - $error++; - $this->errors[] = $this->db->lasterror(); - } - } - } - } if (!$error) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete; - $sql .= " WHERE fk_soc = ".$id; - if (!$this->db->query($sql)) - { - $error++; - $this->errors[] = $this->db->lasterror(); + $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]; + 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; + } + } else { + $error++; + $this->errors[] = 'Cannot find child class file ' .$filepath; + } + } else { + $sql = "DELETE FROM " . MAIN_DB_PREFIX . $tabletodelete; + $sql .= " WHERE fk_soc = " . $id; + if (!$this->db->query($sql)) { + $error++; + $this->errors[] = $this->db->lasterror(); + } } } } From c655720b78d9eaa5b5f4aa1c63b1f03db4215052 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Mon, 1 Jun 2020 10:51:50 +0200 Subject: [PATCH 05/18] fix travis --- htdocs/contact/class/contact.class.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/htdocs/contact/class/contact.class.php b/htdocs/contact/class/contact.class.php index e200a309378..71d816c166e 100644 --- a/htdocs/contact/class/contact.class.php +++ b/htdocs/contact/class/contact.class.php @@ -1081,7 +1081,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)."::".__METHOD__, LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { @@ -1094,7 +1094,7 @@ class Contact extends CommonObject $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact"; $sqldel .= " WHERE rowid = ".$obj->rowid; - dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $result = $this->db->query($sqldel); if (!$result) { @@ -1116,7 +1116,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)."::".__METHOD__, LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { @@ -1130,7 +1130,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)."::".__METHOD__, LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { @@ -1144,7 +1144,7 @@ class Contact extends CommonObject { $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople"; $sql .= " WHERE rowid=".$this->id; - dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $result = $this->db->query($sql); if (!$result) { From da481049c1b93a05ff73928817206d0e3012db3c Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 2 Jun 2020 15:45:40 +0200 Subject: [PATCH 06/18] Update commonobject.class.php --- htdocs/core/class/commonobject.class.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 7fe7fc41303..819b4b20839 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7898,11 +7898,13 @@ abstract class CommonObject $deleteMethod=$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 (method_exists($childObject, $deleteMethod)) { + $result= $childObject->{$deleteMethod}($this->id); + if ($result<0) { + $this->errors[] = $childObject->error; + return -1; + } + } } else { $this->errors[] = 'Cannot find child class file ' .$filePath; return -1; From 7115bd7033139aff8bdce5567d8c01ff4b07f933 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Thu, 4 Jun 2020 20:14:19 +0200 Subject: [PATCH 07/18] better fix --- htdocs/contact/class/contact.class.php | 56 --------------- htdocs/core/class/commonobject.class.php | 91 +++++++++++++++++++++--- htdocs/societe/class/societe.class.php | 27 +++---- 3 files changed, 98 insertions(+), 76 deletions(-) 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(); } } + } } From b47761e05cd71d5c1ba945f2470fb5c7adbff450 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Thu, 4 Jun 2020 20:20:43 +0200 Subject: [PATCH 08/18] fix comment --- htdocs/core/class/commonobject.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index d8ff3cde3c4..798783c951c 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7972,7 +7972,7 @@ abstract class CommonObject /** * Delete all child object * @param int $parentId Parent Id - * @param string $parentField Name of parent FIled + * @param string $parentField Name of Foreign key parent column * @return int <0 if KO, >0 if OK * @throws Exception */ From e9b17aac8db567d39b9bde6921f4b781f031fff7 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 4 Jun 2020 18:20:48 +0000 Subject: [PATCH 09/18] Fixing style errors. --- htdocs/core/class/commonobject.class.php | 2 +- htdocs/societe/class/societe.class.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index d8ff3cde3c4..190720a3f73 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7976,7 +7976,7 @@ abstract class CommonObject * @return int <0 if KO, >0 if OK * @throws Exception */ - public function deleteByParentField($parentId = 0, $parentField='') + public function deleteByParentField($parentId = 0, $parentField = '') { global $user; diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 242b3c3efd4..12280843b2a 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -1708,7 +1708,6 @@ class Societe extends CommonObject $this->errors[] = $this->db->lasterror(); } } - } } From afafe1a7377f4e6da3ec3a5efee83716a79c9afd Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Thu, 4 Jun 2020 21:38:44 +0200 Subject: [PATCH 10/18] forget[] --- htdocs/core/class/commonobject.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 1645ca2f982..922d7d3f3af 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -8017,7 +8017,7 @@ abstract class CommonObject } if ($result < 0) { $error++; - $this->errors = $this->error; + $this->errors[] = $this->error; } else { $deleted++; } From 699557adbe40e9aa86d7904d4c841876d1a2ac2c Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Thu, 4 Jun 2020 21:39:23 +0200 Subject: [PATCH 11/18] forget[] --- htdocs/core/class/commonobject.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 922d7d3f3af..0747f1553f3 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7998,7 +7998,7 @@ abstract class CommonObject $result = $this->fetch($obj->rowid); if ($result < 0) { $error++; - $this->errors = $this->error; + $this->errors[] = $this->error; } else { $needUserParam = false; if (class_exists('ReflectionMethod')) { From a66926cf08491f2aa7fa668477ac150cedcff376 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 6 Jun 2020 21:30:08 +0200 Subject: [PATCH 12/18] Update commonobject.class.php --- htdocs/core/class/commonobject.class.php | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 0747f1553f3..fec5ea35485 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7970,7 +7970,8 @@ abstract class CommonObject } /** - * Delete all child object + * 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 @@ -7988,7 +7989,7 @@ abstract class CommonObject $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(); @@ -8000,21 +8001,7 @@ abstract class CommonObject $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(); - } + $result = $this->delete($user); if ($result < 0) { $error++; $this->errors[] = $this->error; @@ -8029,7 +8016,7 @@ abstract class CommonObject $this->db->commit(); return $deleted; } else { - $this->error = implode(' ', $this->errors); + $this->error = implode(', ', $this->errors); $this->db->rollback(); return $error * -1; } From af440b7c49cdf3a098a7dc3a2239a4dc6520e4a2 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 6 Jun 2020 21:32:25 +0200 Subject: [PATCH 13/18] Update societe.class.php --- htdocs/societe/class/societe.class.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 12280843b2a..0ad6ab22f23 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -1688,13 +1688,11 @@ class Societe extends CommonObject $filepath=$deleteFromObject[1]; $columnName=$deleteFromObject[2]; if (dol_include_once($filepath)) { - 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; - } + $child_object = new $className($this->db); + $result = $child_object->deleteByParentField($id, $columnName); + if ($result < 0) { + $error++; + $this->errors[] = $child_object->error; } } else { $error++; From d9ac7ab9da8ab3d2632ceb3aa3c1a46a465e3836 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 6 Jun 2020 21:37:53 +0200 Subject: [PATCH 14/18] Update commonobject.class.php --- htdocs/core/class/commonobject.class.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index fec5ea35485..ffd5e53dccf 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7892,35 +7892,37 @@ abstract class CommonObject } // Delete cascade first - if (!empty($this->childtablesoncascade)) { + if (is_array($this->childtablesoncascade)) { foreach ($this->childtablesoncascade as $table) { - $deleteFromObject=explode(':', $table); + $deleteFromObject = explode(':', $table); if (count($deleteFromObject)>=2) { - $className=str_replace('@', '', $deleteFromObject[0]); - $filePath=$deleteFromObject[1]; - $columnName=$deleteFromObject[2]; + $className = str_replace('@', '', $deleteFromObject[0]); + $filePath = $deleteFromObject[1]; + $columnName = $deleteFromObject[2]; if (dol_include_once($filePath)) { $childObject = new $className($this->db); - if (is_callable($childObject, 'deleteByParentField')) { + if (method_exists($childObject, 'deleteByParentField')) { $result = $childObject->deleteByParentField($this->id, $columnName); if ($result < 0) { + $error++; $this->errors[] = $childObject->error; - return -1; + break; } } } else { - $this->errors[] = 'Cannot find child class file ' .$filePath; - return -1; + $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; - $this->db->rollback(); - return -1; + break; } } } From 87806346c7079bb703a2da1f0fb5c62876bfc7f0 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 6 Jun 2020 21:40:20 +0200 Subject: [PATCH 15/18] Update commonobject.class.php --- htdocs/core/class/commonobject.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index ffd5e53dccf..6634ccd7fcd 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7990,7 +7990,7 @@ abstract class CommonObject $this->db->begin(); $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . $this->table_element; - $sql .= ' WHERE '.$parentField.' = ' . $parentId; + $sql .= ' WHERE '.$parentField.' = ' . (int) $parentId; $resql = $this->db->query($sql); if (!$resql) { From bb192195f19cd4d20d5cd3146f5f3f45f780ba2d Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 6 Jun 2020 21:41:33 +0200 Subject: [PATCH 16/18] Update commonobject.class.php --- htdocs/core/class/commonobject.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 6634ccd7fcd..9f252879fda 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7892,7 +7892,7 @@ abstract class CommonObject } // Delete cascade first - if (is_array($this->childtablesoncascade)) { + if (is_array($this->childtablesoncascade) && !empty($this->childtablesoncascade)) { foreach ($this->childtablesoncascade as $table) { $deleteFromObject = explode(':', $table); From 38d95aa5cdecf66b4757b2fe9fd66df0e57a6932 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 6 Jun 2020 21:45:53 +0200 Subject: [PATCH 17/18] Update societe.class.php --- htdocs/societe/class/societe.class.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 0ad6ab22f23..a37830b0ed9 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -1678,9 +1678,9 @@ class Societe extends CommonObject } } - foreach ($this->childtablesoncascade as $tabletodelete) + if (!$error) { - if (!$error) + foreach ($this->childtablesoncascade as $tabletodelete) { $deleteFromObject=explode(':', $tabletodelete); if (count($deleteFromObject)>=2) { @@ -1693,10 +1693,12 @@ class Societe extends CommonObject if ($result < 0) { $error++; $this->errors[] = $child_object->error; + break; } } else { $error++; - $this->errors[] = 'Cannot find child class file ' .$filepath; + $this->errors[] = 'Cannot include child class file ' .$filepath; + break; } } else { $sql = "DELETE FROM " . MAIN_DB_PREFIX . $tabletodelete; @@ -1704,6 +1706,7 @@ class Societe extends CommonObject if (!$this->db->query($sql)) { $error++; $this->errors[] = $this->db->lasterror(); + break; } } } From 261d0772f8812a3c7883e7ebf3dd880c36b4f26e Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 6 Jun 2020 21:47:27 +0200 Subject: [PATCH 18/18] Update commonobject.class.php --- htdocs/core/class/commonobject.class.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 9f252879fda..9dba821c012 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7909,7 +7909,11 @@ abstract class CommonObject $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;