From 19cedae1c148992b8afed629847e910fc1c3be72 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 6 May 2022 16:14:38 +0200 Subject: [PATCH] Add trigger on commonObject setProject --- htdocs/core/class/commonobject.class.php | 27 ++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 25982ceb03c..5bbb65652c1 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -2238,8 +2238,11 @@ abstract class CommonObject * @param int $projectid Project id to link element to * @return int <0 if KO, >0 if OK */ - public function setProject($projectid) + public function setProject($projectid, $notrigger = 0) { + global $user; + $error = 0; + if (!$this->table_element) { dol_syslog(get_class($this)."::setProject was called on objet with property table_element not defined", LOG_ERR); return -1; @@ -2270,13 +2273,33 @@ abstract class CommonObject $sql .= " WHERE rowid = ".((int) $this->id); } + $this->db->begin(); + dol_syslog(get_class($this)."::setProject", LOG_DEBUG); if ($this->db->query($sql)) { $this->fk_project = ((int) $projectid); - return 1; } else { dol_print_error($this->db); + $error++; + } + + // Triggers + if (!$error && !$notrigger) { + // Call triggers + $result = $this->call_trigger(strtoupper($this->element) . '_MODIFY', $user); + if ($result < 0) { + $error++; + } //Do also here what you must do to rollback action if trigger fail + // End call triggers + } + + // Commit or rollback + if ($error) { + $this->db->rollback(); return -1; + } else { + $this->db->commit(); + return 1; } }