From 96c432566d034024c4d431b505f5d15ee7ef4044 Mon Sep 17 00:00:00 2001 From: atm-florian Date: Fri, 1 Apr 2022 19:20:47 +0200 Subject: [PATCH 1/3] NEW 16.0 - proposal for a prefix-based consistency mechanism for trigger names --- htdocs/core/class/commonobject.class.php | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index f9bbb9ed879..ef9856a2035 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -42,6 +42,7 @@ */ abstract class CommonObject { + const TRIGGER_PREFIX = ''; // to be overriden in child class implementations, i.e. 'BILL', 'TASK', 'PROPAL', etc. /** * @var DoliDb Database handler (result of a new DoliDB) */ @@ -1987,6 +1988,10 @@ abstract class CommonObject $result = $this->fetchCommon($id); } if ($result >= 0) { + + if (!empty(self::TRIGGER_PREFIX) && strpos($trigkey, self::TRIGGER_PREFIX) !== 0) { + $trigkey = self::TRIGGER_PREFIX . '_' . $trigkey; + } $result = $this->call_trigger($trigkey, (!empty($fuser) && is_object($fuser)) ? $fuser : $user); // This may set this->errors } if ($result < 0) { @@ -4269,6 +4274,9 @@ abstract class CommonObject if ($trigkey) { // Call trigger + if (!empty(self::TRIGGER_PREFIX) && strpos($trigkey, self::TRIGGER_PREFIX) !== 0) { + $trigkey = self::TRIGGER_PREFIX . '_' . $trigkey; + } $result = $this->call_trigger($trigkey, $user); if ($result < 0) { $error++; @@ -5603,6 +5611,9 @@ abstract class CommonObject // phpcs:enable global $langs, $conf; + if (!empty(self::TRIGGER_PREFIX) && strpos($triggerName, self::TRIGGER_PREFIX) !== 0) { + $triggerName = self::TRIGGER_PREFIX . '_' . $triggerName; + } if (!is_object($langs)) { // If lang was not defined, we set it. It is required by run_triggers. include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php'; $langs = new Translate('', $conf); @@ -6190,6 +6201,9 @@ abstract class CommonObject if (!$error && $trigger) { // Call trigger $this->context = array('extrafieldaddupdate'=>1); + if (!empty(self::TRIGGER_PREFIX) && strpos($trigger, self::TRIGGER_PREFIX) !== 0) { + $trigger = self::TRIGGER_PREFIX . '_' . $trigger; + } $result = $this->call_trigger($trigger, $userused); if ($result < 0) { $error++; @@ -6308,6 +6322,9 @@ abstract class CommonObject if (!$error && $trigger) { // Call trigger $this->context = array('extralanguagesaddupdate'=>1); + if (!empty(self::TRIGGER_PREFIX) && strpos($trigger, self::TRIGGER_PREFIX) !== 0) { + $trigger = self::TRIGGER_PREFIX . '_' . $trigger; + } $result = $this->call_trigger($trigger, $userused); if ($result < 0) { $error++; @@ -6506,6 +6523,10 @@ abstract class CommonObject if (!$error && $trigger) { // Call trigger $this->context = array('extrafieldupdate'=>1); + + if (!empty(self::TRIGGER_PREFIX) && strpos($trigger, self::TRIGGER_PREFIX) !== 0) { + $trigger = self::TRIGGER_PREFIX . '_' . $trigger; + } $result = $this->call_trigger($trigger, $userused); if ($result < 0) { $error++; @@ -9426,6 +9447,10 @@ abstract class CommonObject if (!$error && !$notrigger) { // Call trigger + + if (!empty(self::TRIGGER_PREFIX) && strpos($triggercode, self::TRIGGER_PREFIX) !== 0) { + $triggercode = self::TRIGGER_PREFIX . '_' . $triggercode; + } $result = $this->call_trigger($triggercode, $user); if ($result < 0) { $error++; From 563716b633badb6b8db073f2abacd55063bea098 Mon Sep 17 00:00:00 2001 From: atm-florian Date: Mon, 4 Apr 2022 10:00:10 +0200 Subject: [PATCH 2/3] 16.0 - Proposal for trigger prefix consistency: instead of adding missing prefix, check that trigger starts with declared prefix --- htdocs/core/class/commonobject.class.php | 26 ++---------------------- 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index ef9856a2035..c4d0635dde7 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1988,10 +1988,6 @@ abstract class CommonObject $result = $this->fetchCommon($id); } if ($result >= 0) { - - if (!empty(self::TRIGGER_PREFIX) && strpos($trigkey, self::TRIGGER_PREFIX) !== 0) { - $trigkey = self::TRIGGER_PREFIX . '_' . $trigkey; - } $result = $this->call_trigger($trigkey, (!empty($fuser) && is_object($fuser)) ? $fuser : $user); // This may set this->errors } if ($result < 0) { @@ -4274,9 +4270,6 @@ abstract class CommonObject if ($trigkey) { // Call trigger - if (!empty(self::TRIGGER_PREFIX) && strpos($trigkey, self::TRIGGER_PREFIX) !== 0) { - $trigkey = self::TRIGGER_PREFIX . '_' . $trigkey; - } $result = $this->call_trigger($trigkey, $user); if ($result < 0) { $error++; @@ -5610,9 +5603,8 @@ abstract class CommonObject { // phpcs:enable global $langs, $conf; - - if (!empty(self::TRIGGER_PREFIX) && strpos($triggerName, self::TRIGGER_PREFIX) !== 0) { - $triggerName = self::TRIGGER_PREFIX . '_' . $triggerName; + if (!empty(self::TRIGGER_PREFIX) && strpos($triggerName, self::TRIGGER_PREFIX . '_') !== 0) { + throw new Exception('The trigger "' . $triggerName . '" does not start with "' . self::TRIGGER_PREFIX . '_" as required.'); } if (!is_object($langs)) { // If lang was not defined, we set it. It is required by run_triggers. include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php'; @@ -6201,9 +6193,6 @@ abstract class CommonObject if (!$error && $trigger) { // Call trigger $this->context = array('extrafieldaddupdate'=>1); - if (!empty(self::TRIGGER_PREFIX) && strpos($trigger, self::TRIGGER_PREFIX) !== 0) { - $trigger = self::TRIGGER_PREFIX . '_' . $trigger; - } $result = $this->call_trigger($trigger, $userused); if ($result < 0) { $error++; @@ -6322,9 +6311,6 @@ abstract class CommonObject if (!$error && $trigger) { // Call trigger $this->context = array('extralanguagesaddupdate'=>1); - if (!empty(self::TRIGGER_PREFIX) && strpos($trigger, self::TRIGGER_PREFIX) !== 0) { - $trigger = self::TRIGGER_PREFIX . '_' . $trigger; - } $result = $this->call_trigger($trigger, $userused); if ($result < 0) { $error++; @@ -6523,10 +6509,6 @@ abstract class CommonObject if (!$error && $trigger) { // Call trigger $this->context = array('extrafieldupdate'=>1); - - if (!empty(self::TRIGGER_PREFIX) && strpos($trigger, self::TRIGGER_PREFIX) !== 0) { - $trigger = self::TRIGGER_PREFIX . '_' . $trigger; - } $result = $this->call_trigger($trigger, $userused); if ($result < 0) { $error++; @@ -9447,10 +9429,6 @@ abstract class CommonObject if (!$error && !$notrigger) { // Call trigger - - if (!empty(self::TRIGGER_PREFIX) && strpos($triggercode, self::TRIGGER_PREFIX) !== 0) { - $triggercode = self::TRIGGER_PREFIX . '_' . $triggercode; - } $result = $this->call_trigger($triggercode, $user); if ($result < 0) { $error++; From 4ced0bac8965f4ca1cf54a4cb291d025aaa6808f Mon Sep 17 00:00:00 2001 From: atm-florian Date: Thu, 7 Apr 2022 14:39:14 +0200 Subject: [PATCH 3/3] =?UTF-8?q?replace=20throw=20new=20Exception(=E2=80=A6?= =?UTF-8?q?)=20with=20dol=5Fprint=5Ferror=20+=20exit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htdocs/core/class/commonobject.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index b1f7997b4f7..6713157c78f 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -5620,7 +5620,8 @@ abstract class CommonObject // phpcs:enable global $langs, $conf; if (!empty(self::TRIGGER_PREFIX) && strpos($triggerName, self::TRIGGER_PREFIX . '_') !== 0) { - throw new Exception('The trigger "' . $triggerName . '" does not start with "' . self::TRIGGER_PREFIX . '_" as required.'); + dol_print_error('', 'The trigger "' . $triggerName . '" does not start with "' . self::TRIGGER_PREFIX . '_" as required.'); + exit; } if (!is_object($langs)) { // If lang was not defined, we set it. It is required by run_triggers. include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';