From e020597ba7d638df246876ed66dab27e774d7710 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 19 Nov 2018 21:11:26 +0100 Subject: [PATCH] Fix deletion for class build with modulebuilder --- htdocs/core/class/commonobject.class.php | 37 +++++++++++++++++-- .../class/emailcollector.class.php | 12 +++++- .../class/emailcollectoraction.class.php | 3 -- .../template/class/myobject.class.php | 1 + 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 7ea5f483803..307923e9bef 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7274,16 +7274,45 @@ abstract class CommonObject /** * Delete object in database * - * @param User $user User that deletes - * @param bool $notrigger false=launch triggers after, true=disable triggers - * @return int <0 if KO, >0 if OK + * @param User $user User that deletes + * @param bool $notrigger false=launch triggers after, true=disable triggers + * @param int $forcechilddeletion 0=no, 1=Force deletion of children + * @return int <=0 if KO, >0 if OK */ - public function deleteCommon(User $user, $notrigger = false) + public function deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0) { $error=0; $this->db->begin(); + if ($forcechilddeletion) + { + foreach($this->childtables 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; + } + } + } + elseif (! empty($this->fk_element) && ! empty($this->childtables)) // If object has childs linked with a foreign key field, we check all child tables. + { + $objectisused = $this->isObjectUsed($this->id); + if (! empty($objectisused)) + { + dol_syslog(get_class($this)."::deleteCommon Can't delete record as it has some child", LOG_WARNING); + $this->error='ErrorRecordHasChildren'; + $this->errors[]=$this->error; + $this->db->rollback(); + return 0; + } + } + if (! $error) { if (! $notrigger) { // Call triggers diff --git a/htdocs/emailcollector/class/emailcollector.class.php b/htdocs/emailcollector/class/emailcollector.class.php index 23522308c7e..e317d02a504 100644 --- a/htdocs/emailcollector/class/emailcollector.class.php +++ b/htdocs/emailcollector/class/emailcollector.class.php @@ -55,6 +55,16 @@ class EmailCollector extends CommonObject */ public $picto = 'generic'; + /** + * @var int Field with ID of parent key if this field has a parent + */ + public $fk_element = 'fk_emailcollector'; + + /** + * @var array Array of child tables (child tables to delete before deleting a record) + */ + protected $childtables=array('emailcollector_emailcollectorfilter', 'emailcollector_emailcollectoraction'); + /** * 'type' if the field format. @@ -382,7 +392,7 @@ class EmailCollector extends CommonObject */ public function delete(User $user, $notrigger = false) { - return $this->deleteCommon($user, $notrigger); + return $this->deleteCommon($user, $notrigger, 1); } /** diff --git a/htdocs/emailcollector/class/emailcollectoraction.class.php b/htdocs/emailcollector/class/emailcollectoraction.class.php index a1d8940d3f6..1d42a15134c 100644 --- a/htdocs/emailcollector/class/emailcollectoraction.class.php +++ b/htdocs/emailcollector/class/emailcollectoraction.class.php @@ -58,9 +58,6 @@ class EmailCollectorAction extends CommonObject public $picto = 'emailcollectoraction@emailcollector'; - public $fk_element = 'fk_emailcollector'; - - /** * 'type' if the field format. * 'label' the translation key. diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index 6e3fc1bd63f..571c8614b17 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -336,6 +336,7 @@ class MyObject extends CommonObject public function delete(User $user, $notrigger = false) { return $this->deleteCommon($user, $notrigger); + //return $this->deleteCommon($user, $notrigger, 1); } /**