浏览代码

Fix deletion for class build with modulebuilder

Laurent Destailleur 6 年之前
父节点
当前提交
e020597ba7

+ 33 - 4
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

+ 11 - 1
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);
 	}
 
 	/**

+ 0 - 3
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.

+ 1 - 0
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);
 	}
 
 	/**