better fix for #13968
This commit is contained in:
parent
face2d644e
commit
2337d75d92
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user