From 96c432566d034024c4d431b505f5d15ee7ef4044 Mon Sep 17 00:00:00 2001 From: atm-florian Date: Fri, 1 Apr 2022 19:20:47 +0200 Subject: [PATCH 1/7] 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/7] 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 b929c2f671908619ec69a4900eb6a196e023fc3e Mon Sep 17 00:00:00 2001 From: atm-florian Date: Tue, 5 Apr 2022 11:46:06 +0200 Subject: [PATCH 3/7] FIX 16.0 - supplier invoice card creates two invoices instead of one when creating from a template --- htdocs/fourn/facture/card.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php index f4d628d5882..95bd9cbd136 100644 --- a/htdocs/fourn/facture/card.php +++ b/htdocs/fourn/facture/card.php @@ -929,7 +929,7 @@ if (empty($reshook)) { } // Standard invoice or Deposit invoice, not from a Predefined template invoice - if (GETPOST('type') == FactureFournisseur::TYPE_STANDARD || GETPOST('type') == FactureFournisseur::TYPE_DEPOSIT && GETPOST('fac_rec') <= 0) { + elseif (GETPOST('type') == FactureFournisseur::TYPE_STANDARD || GETPOST('type') == FactureFournisseur::TYPE_DEPOSIT && GETPOST('fac_rec') <= 0) { if (GETPOST('socid', 'int') < 1) { setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('Supplier')), null, 'errors'); $action = 'create'; From 392ba6deedb42a31f7388bff636feecb1411058e Mon Sep 17 00:00:00 2001 From: atm-florian Date: Tue, 5 Apr 2022 12:16:06 +0200 Subject: [PATCH 4/7] FIX 16.0 - my previous fix was incomplete (missing parentheses around OR-separated conditions) + replace GETPOST('fac_rec', 'int') with variable + make sure it works even if, one day, the "nothing selected" value for the template supplier invoice selector changes to -1 instead of '' --- htdocs/fourn/facture/card.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php index 95bd9cbd136..02e77d13f58 100644 --- a/htdocs/fourn/facture/card.php +++ b/htdocs/fourn/facture/card.php @@ -76,7 +76,7 @@ $lineid = GETPOST('lineid', 'int'); $projectid = GETPOST('projectid', 'int'); $origin = GETPOST('origin', 'alpha'); $originid = GETPOST('originid', 'int'); -$fac_rec = GETPOST('fac_rec', 'int'); +$fac_recid = GETPOST('fac_rec', 'int'); // PDF $hidedetails = (GETPOST('hidedetails', 'int') ? GETPOST('hidedetails', 'int') : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0)); @@ -885,7 +885,7 @@ if (empty($reshook)) { } // Standard invoice or Deposit invoice, created from a Predefined template invoice - if ((GETPOST('type') == FactureFournisseur::TYPE_STANDARD || GETPOST('type') == FactureFournisseur::TYPE_DEPOSIT) && GETPOST('fac_rec', 'int') > 0) { + elseif ($fac_recid > 0 && (GETPOST('type') == FactureFournisseur::TYPE_STANDARD || GETPOST('type') == FactureFournisseur::TYPE_DEPOSIT)) { if (empty($dateinvoice)) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors'); @@ -918,7 +918,7 @@ if (empty($reshook)) { $object->multicurrency_tx = GETPOST('originmulticurrency_tx', 'int'); // Source facture - $object->fac_rec = GETPOST('fac_rec', 'int'); + $object->fac_rec = $fac_recid; $fac_rec = new FactureFournisseurRec($db); $fac_rec->fetch($object->fac_rec); $fac_rec->fetch_lines(); @@ -929,7 +929,7 @@ if (empty($reshook)) { } // Standard invoice or Deposit invoice, not from a Predefined template invoice - elseif (GETPOST('type') == FactureFournisseur::TYPE_STANDARD || GETPOST('type') == FactureFournisseur::TYPE_DEPOSIT && GETPOST('fac_rec') <= 0) { + elseif ($fac_recid <= 0 && (GETPOST('type') == FactureFournisseur::TYPE_STANDARD || GETPOST('type') == FactureFournisseur::TYPE_DEPOSIT)) { if (GETPOST('socid', 'int') < 1) { setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('Supplier')), null, 'errors'); $action = 'create'; @@ -2024,15 +2024,15 @@ if ($action == 'create') { $exampletemplateinvoice = new FactureFournisseurRec($db); $invoice_predefined = new FactureFournisseurRec($db); - if (empty($origin) && empty($originid) && GETPOST('fac_rec', 'int') > 0) { - $invoice_predefined->fetch(GETPOST('fac_rec', 'int')); + if (empty($origin) && empty($originid) && $fac_recid > 0) { + $invoice_predefined->fetch($fac_recid); } // Third party print ''.$langs->trans('Supplier').''; print ''; - if ($societe->id > 0 && (!GETPOST('fac_rec', 'int') || !empty($invoice_predefined->frequency))) { + if ($societe->id > 0 && ($fac_recid <= 0 || !empty($invoice_predefined->frequency))) { $absolute_discount = $societe->getAvailableDiscounts('', '', 0, 1); print $societe->getNomUrl(1, 'supplier'); print ''; @@ -2050,15 +2050,15 @@ if ($action == 'create') { }); '; } - if (!GETPOST('fac_rec', 'int')) { + if ($fac_recid <= 0) { print ' '; } } print ''; // Overwrite some values if creation of invoice is from a predefined invoice - if (empty($origin) && empty($originid) && GETPOST('fac_rec', 'int') > 0) { - $invoice_predefined->fetch(GETPOST('fac_rec', 'int')); + if (empty($origin) && empty($originid) && $fac_recid > 0) { + $invoice_predefined->fetch($fac_recid); $dateinvoice = $invoice_predefined->date_when; // To use next gen date by default later if (empty($projectid)) { @@ -2088,15 +2088,15 @@ if ($action == 'create') { if ($num > 0) { print ''.$langs->trans('CreateFromRepeatableInvoice').''; - //print ''; + //print ''; print '