diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 0758c696e29..6519a8b9e13 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; } } diff --git a/htdocs/ticket/class/ticket.class.php b/htdocs/ticket/class/ticket.class.php index d63fa68dbfb..72ec8625e0e 100644 --- a/htdocs/ticket/class/ticket.class.php +++ b/htdocs/ticket/class/ticket.class.php @@ -2013,38 +2013,6 @@ class Ticket extends CommonObject } } - /** - * Link element with a project - * Override core function because of key name 'fk_project' used for this module - * - * @param int $projectid Project id to link element to - * @return int <0 if KO, >0 if OK - */ - public function setProject($projectid) - { - if (!$this->table_element) { - dol_syslog(get_class($this)."::setProject was called on objet with property table_element not defined", LOG_ERR); - return -1; - } - - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - if ($projectid) { - $sql .= ' SET fk_project = '.((int) $projectid); - } else { - $sql .= ' SET fk_project = NULL'; - } - $sql .= ' WHERE rowid = '.((int) $this->id); - - dol_syslog(get_class($this)."::setProject sql=".$sql); - if ($this->db->query($sql)) { - $this->fk_project = ((int) $projectid); - return 1; - } else { - dol_print_error($this->db); - return -1; - } - } - /** * Link element with a contract *