From 7f10ef5215fc4c784a61f819ac3702c85b118992 Mon Sep 17 00:00:00 2001 From: kkhelifa Date: Tue, 7 Mar 2023 17:33:59 +0100 Subject: [PATCH 1/2] NEW - Provide the oldcopy value when calling setValueFrom() function with a trigger key --- htdocs/core/class/commonobject.class.php | 35 +++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index e3c3dff2b7c..097530c6aa5 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -2089,10 +2089,6 @@ abstract class CommonObject $id_field = 'rowid'; } - $error = 0; - - $this->db->begin(); - // Special case if ($table == 'product' && $field == 'note_private') { $field = 'note'; @@ -2101,6 +2097,32 @@ abstract class CommonObject $fk_user_field = 'fk_user_mod'; } + if ($trigkey) { + $oldvalue = null; + + $sql = "SELECT " . $field; + $sql .= " FROM " . MAIN_DB_PREFIX . $table; + $sql .= " WHERE " . $id_field . " = " . ((int)$id); + + $resql = $this->db->query($sql); + if ($resql) { + if ($obj = $this->db->fetch_object($resql)) { + if ($format == 'date') { + $oldvalue = $this->db->jdate($obj->$field); + } else { + $oldvalue = $obj->$field; + } + } + } else { + $this->error = $this->db->lasterror(); + return -1; + } + } + + $error = 0; + + $this->db->begin(); + $sql = "UPDATE ".$this->db->prefix().$table." SET "; if ($format == 'text') { @@ -2133,6 +2155,11 @@ abstract class CommonObject } else { $result = $this->fetchCommon($id); } + $this->oldcopy = clone $this; + if (property_exists($this->oldcopy, $field)) { + $this->oldcopy->$field = $oldvalue; + } + if ($result >= 0) { $result = $this->call_trigger($trigkey, (!empty($fuser) && is_object($fuser)) ? $fuser : $user); // This may set this->errors } From e47c395fe73688de5f4bf6491c6a33265630f95f Mon Sep 17 00:00:00 2001 From: kkhelifa Date: Fri, 10 Mar 2023 09:14:33 +0100 Subject: [PATCH 2/2] Fix stickler-ci --- htdocs/core/class/commonobject.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 097530c6aa5..bf05df918ce 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -2102,7 +2102,7 @@ abstract class CommonObject $sql = "SELECT " . $field; $sql .= " FROM " . MAIN_DB_PREFIX . $table; - $sql .= " WHERE " . $id_field . " = " . ((int)$id); + $sql .= " WHERE " . $id_field . " = " . ((int) $id); $resql = $this->db->query($sql); if ($resql) {