|
@@ -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
|