From 6e38c8433ca13ecfd4ec2780e9d9c672fb19102d Mon Sep 17 00:00:00 2001 From: Braito Date: Thu, 28 May 2026 10:56:32 +0200 Subject: [PATCH 1/8] Fix mail object-line substitutions to respect selected language (#38201) * Fix mail object-line substitutions to respect selected language * FIX pre-commit missing parameter comments in FormMail helpers * Clean up docblock comments in html.formmail.class.php Removed unnecessary docblock comments for several methods. * FIX phan nullable multilangs in FormMail translation helper --------- Co-authored-by: caminotravelcenter Co-authored-by: Laurent Destailleur Co-authored-by: braito4 --- htdocs/core/class/html.formmail.class.php | 105 +++++++++++++++++++++- htdocs/core/tpl/card_presend.tpl.php | 2 +- 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/htdocs/core/class/html.formmail.class.php b/htdocs/core/class/html.formmail.class.php index 17a2f2d980e..3a0c13cb3b6 100644 --- a/htdocs/core/class/html.formmail.class.php +++ b/htdocs/core/class/html.formmail.class.php @@ -2004,11 +2004,87 @@ class FormMail extends Form } } + /** + * Normalize a text for equality checks. + * @param string $value Raw text to normalize. + * @return string + */ + private static function normalizeTextForComparison($value) + { + $value = dol_string_nohtmltag((string) $value); + $value = preg_replace('/\s+/', ' ', $value); + return trim((string) $value); + } + /** + * Return language prefix from a language code. + * @param string $langcode Full language code (for example fr_FR). + * @return string + */ + private static function getLangPrefix($langcode) + { + $langcode = trim((string) $langcode); + if ($langcode === '') { + return ''; + } + + $prefix = preg_replace('/[_-].*$/', '', $langcode); + $prefix = strtolower((string) $prefix); + return preg_replace('/[^a-z]/', '', $prefix); + } + + /** + * Pick best translated label/description from product multilangs. + * + * @param array|null $multilangs Product multilang rows indexed by language code. + * @param string $langcode Preferred language code. + * @return array{label:string,description:string} + */ + private static function getBestProductTranslation($multilangs, $langcode) + { + $langcode = trim((string) $langcode); + if ($langcode === '' || !is_array($multilangs) || empty($multilangs)) { + return array('label' => '', 'description' => ''); + } + + $prefix = self::getLangPrefix($langcode); + $candidates = array($langcode); + if ($prefix !== '' && $prefix !== $langcode) { + $candidates[] = $prefix; + } + + foreach ($candidates as $candidate) { + if (empty($multilangs[$candidate]) || !is_array($multilangs[$candidate])) { + continue; + } + $label = trim((string) (isset($multilangs[$candidate]['label']) ? $multilangs[$candidate]['label'] : '')); + $description = trim((string) (isset($multilangs[$candidate]['description']) ? $multilangs[$candidate]['description'] : '')); + if ($label !== '' || $description !== '') { + return array('label' => $label, 'description' => $description); + } + } + + if ($prefix !== '') { + foreach ($multilangs as $code => $row) { + if (!is_array($row)) { + continue; + } + if (!(strpos($code, $prefix.'_') === 0 || strpos($code, $prefix.'-') === 0)) { + continue; + } + $label = trim((string) (isset($row['label']) ? $row['label'] : '')); + $description = trim((string) (isset($row['description']) ? $row['description'] : '')); + if ($label !== '' || $description !== '') { + return array('label' => $label, 'description' => $description); + } + } + } + + return array('label' => '', 'description' => ''); + } /** * Set ->substit (and ->substit_line) array from object. This is call when suggesting the email template into forms before sending email. - * * @param CommonObject $object Object to use * @param Translate $outputlangs Object lang * @return void @@ -2023,6 +2099,10 @@ class FormMail extends Form complete_substitutions_array($tmparray, $outputlangs, null, $parameters); $this->substit = $tmparray; + $targetLang = ''; + if (is_object($outputlangs) && !empty($outputlangs->defaultlang)) { + $targetLang = trim((string) $outputlangs->defaultlang); + } // Fill substit_lines with each object lines content if (is_array($object->lines)) { @@ -2050,6 +2130,28 @@ class FormMail extends Form $product->fetch($line->fk_product, '', '', '1'); $product->fetch_optionals(); + if (getDolGlobalInt('MAIN_MULTILANGS') && $targetLang !== '' && !empty($product->multilangs) && is_array($product->multilangs)) { + $translated = self::getBestProductTranslation($product->multilangs, $targetLang); + $translatedLabel = trim((string) (isset($translated['label']) ? $translated['label'] : '')); + $translatedDescription = trim((string) (isset($translated['description']) ? $translated['description'] : '')); + + $currentLabelNorm = self::normalizeTextForComparison($substit_line['__PRODUCT_LABEL__']); + $currentProductDescriptionNorm = self::normalizeTextForComparison($substit_line['__PRODUCT_DESCRIPTION__']); + $currentLineDescriptionNorm = self::normalizeTextForComparison($substit_line['__DESCRIPTION__']); + $productLabelNorm = self::normalizeTextForComparison($product->label); + $productDescriptionNorm = self::normalizeTextForComparison($product->description); + + if ($translatedLabel !== '' && ($currentLabelNorm === '' || $currentLabelNorm === $productLabelNorm)) { + $substit_line['__PRODUCT_LABEL__'] = $translatedLabel; + } + if ($translatedDescription !== '' && ($currentProductDescriptionNorm === '' || $currentProductDescriptionNorm === $productDescriptionNorm)) { + $substit_line['__PRODUCT_DESCRIPTION__'] = $translatedDescription; + } + if ($translatedDescription !== '' && ($currentLineDescriptionNorm === '' || $currentLineDescriptionNorm === $productDescriptionNorm || $currentLineDescriptionNorm === $currentProductDescriptionNorm)) { + $substit_line['__DESCRIPTION__'] = $translatedDescription; + } + } + $extrafields->fetch_name_optionals_label($product->table_element, true); if (!empty($extrafields->attributes[$product->table_element]['label']) && is_array($extrafields->attributes[$product->table_element]['label']) && count($extrafields->attributes[$product->table_element]['label']) > 0) { @@ -2067,7 +2169,6 @@ class FormMail extends Form /** * Get list of substitution keys available for emails. This is used for tooltips help. * This include the complete_substitutions_array. - * * @param string $mode 'formemail', 'formemailwithlines', 'formemailforlines', 'emailing', ... * @param ?Object $object Object if applicable * @return array Array of substitution values for emails. diff --git a/htdocs/core/tpl/card_presend.tpl.php b/htdocs/core/tpl/card_presend.tpl.php index 6722c544526..46a28f4b37a 100644 --- a/htdocs/core/tpl/card_presend.tpl.php +++ b/htdocs/core/tpl/card_presend.tpl.php @@ -308,7 +308,7 @@ if ($action == 'presend') { // Make substitution in email content if (!empty($object)) { // First we set ->substit (useless, it will be erased later) and ->substit_lines - $formmail->setSubstitFromObject($object, $langs); + $formmail->setSubstitFromObject($object, $outputlangs); } $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object); From 25eff32faee65eb3c14ab7c6d3a6513810bf089a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 28 May 2026 14:09:32 +0200 Subject: [PATCH 2/8] fix CI in interface_50_modBlockedlog_ActionsBlockedLog.class.php --- .../interface_50_modBlockedlog_ActionsBlockedLog.class.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php index 3105788ba10..5872a075dad 100644 --- a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php +++ b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php @@ -105,6 +105,7 @@ class InterfaceActionsBlockedLog extends DolibarrTriggers } + /** @var Facture|Don|Paiement|PaymentDonation|Subscription|PaymentVarious|CashControl $object */ if ($action === 'BILL_UNVALIDATE') { if ($object->isEditable() <= 0) { $this->errors[] = 'Modifying this invoice is not allowed'; @@ -112,8 +113,6 @@ class InterfaceActionsBlockedLog extends DolibarrTriggers } } - - /** @var Facture|Don|Paiement|PaymentDonation|Subscription|PaymentVarious|CashControl $object */ dol_syslog("Trigger '".$this->name."' for action '".$action."' launched by ".__FILE__.". id=".$object->id); require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php'; From 1d34f258e2482c235b6670bd2718377f8b03a188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 28 May 2026 14:31:24 +0200 Subject: [PATCH 3/8] Update interface_50_modBlockedlog_ActionsBlockedLog.class.php --- .../interface_50_modBlockedlog_ActionsBlockedLog.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php index 5872a075dad..ed09afc3cd2 100644 --- a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php +++ b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php @@ -105,14 +105,15 @@ class InterfaceActionsBlockedLog extends DolibarrTriggers } - /** @var Facture|Don|Paiement|PaymentDonation|Subscription|PaymentVarious|CashControl $object */ if ($action === 'BILL_UNVALIDATE') { + /** @var Facture $object */ if ($object->isEditable() <= 0) { $this->errors[] = 'Modifying this invoice is not allowed'; return -2; } } + /** @var Facture|Don|Paiement|PaymentDonation|Subscription|PaymentVarious|CashControl $object */ dol_syslog("Trigger '".$this->name."' for action '".$action."' launched by ".__FILE__.". id=".$object->id); require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php'; From 6b86c1663525d4725021f60bdb976022d1f8540a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 28 May 2026 14:45:42 +0200 Subject: [PATCH 4/8] Update interface_50_modBlockedlog_ActionsBlockedLog.class.php --- .../interface_50_modBlockedlog_ActionsBlockedLog.class.php | 1 + 1 file changed, 1 insertion(+) diff --git a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php index ed09afc3cd2..da616f9b117 100644 --- a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php +++ b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php @@ -107,6 +107,7 @@ class InterfaceActionsBlockedLog extends DolibarrTriggers if ($action === 'BILL_UNVALIDATE') { /** @var Facture $object */ + '@phan-var-force Facture $object'; if ($object->isEditable() <= 0) { $this->errors[] = 'Modifying this invoice is not allowed'; return -2; From b0d1d0efea9638b7a9fd3ac0e58f387a3da3a01e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 28 May 2026 15:00:46 +0200 Subject: [PATCH 5/8] fix --- .../interface_50_modBlockedlog_ActionsBlockedLog.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php index da616f9b117..17150394515 100644 --- a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php +++ b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php @@ -1,7 +1,7 @@ * Copyright (C) 2017-2018 Laurent Destailleur - * Copyright (C) 2025 Frédéric France + * Copyright (C) 2025-2026 Frédéric France * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -129,6 +129,8 @@ class InterfaceActionsBlockedLog extends DolibarrTriggers // If we are here, we are on an action code that will have a control or will generate a record in blockedlog database. if ($action === 'PAYMENT_CUSTOMER_CREATE' && $object->element == 'payment') { + /** @var Paiement $object */ + '@phan-var-force Paiement $object'; include_once DOL_DOCUMENT_ROOT.'/blockedlog/lib/blockedlog.lib.php'; if (isALNERunningVersion() && $mysoc->country_code == 'FR') { if (empty($object->paiementcode) && !empty($object->paiementid)) { From d492818edf8785eb38dc8606f16e65900d8639ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 28 May 2026 15:03:42 +0200 Subject: [PATCH 6/8] fix --- .../interface_50_modBlockedlog_ActionsBlockedLog.class.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php index 17150394515..f2a566b51a3 100644 --- a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php +++ b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php @@ -199,9 +199,11 @@ class InterfaceActionsBlockedLog extends DolibarrTriggers 'MEMBER_SUBSCRIPTION_CREATE', 'MEMBER_SUBSCRIPTION_MODIFY', 'MEMBER_SUBSCRIPTION_DELETE', 'DON_VALIDATE', 'DON_MODIFY', 'DON_DELETE'))) { /** @var Don|Subscription $object */ + '@phan-var-force Don|Subscription $object'; $amounts = (float) $object->amount; } elseif ($action == 'CASHCONTROL_CLOSE') { /** @var CashControl $object */ + '@phan-var-force CashControl $object'; $amounts = (float) $object->cash + (float) $object->cheque + (float) $object->card; } else { if (property_exists($object, 'total_ht')) { @@ -214,6 +216,8 @@ class InterfaceActionsBlockedLog extends DolibarrTriggers } if ($action === 'PAYMENT_CUSTOMER_CREATE' || $action === 'PAYMENT_SUPPLIER_CREATE' || $action === 'DONATION_PAYMENT_CREATE' || $action === 'PAYMENT_CUSTOMER_DELETE' || $action === 'PAYMENT_SUPPLIER_DELETE' || $action === 'DONATION_PAYMENT_DELETE') { + /** @var Paiement $object */ + '@phan-var-force Paiement $object'; $qualified++; if (!empty($object->amounts)) { foreach ($object->amounts as $amount) { @@ -223,6 +227,8 @@ class InterfaceActionsBlockedLog extends DolibarrTriggers $amounts = $object->amount; } } elseif (strpos($action, 'PAYMENT') !== false && !in_array($action, array('PAYMENT_ADD_TO_BANK'))) { + /** @var Paiement $object */ + '@phan-var-force Paiement $object'; $qualified++; $amounts = (float) $object->amount; } From 55c1b74622006f6e68493db2646192bc104cd2c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 28 May 2026 15:04:53 +0200 Subject: [PATCH 7/8] fix --- htdocs/core/class/commonobject.class.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 6f3273b8584..96f345d155a 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -12,7 +12,7 @@ * Copyright (C) 2017 ATM Consulting * Copyright (C) 2017-2026 Nicolas ZABOURI * Copyright (C) 2017 Rui Strecht - * Copyright (C) 2018-2025 Frédéric France + * Copyright (C) 2018-2026 Frédéric France * Copyright (C) 2018 Josep Lluís Amador * Copyright (C) 2023 Gauthier VERDOL * Copyright (C) 2021 Grégory Blémand @@ -93,7 +93,13 @@ abstract class CommonObject public $errors = array(); /** - * @var string[] Array of warning strings + * @var string Warning string + * @see $warnings + */ + public $warning; + + /** + * @var string[] Array of warning strings */ public $warnings = array(); From 01b8731a8f21980706f81bc4c5bdf8cb6052cc3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 28 May 2026 15:59:59 +0200 Subject: [PATCH 8/8] fix CI --- .../interface_50_modBlockedlog_ActionsBlockedLog.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php index f2a566b51a3..049082e722e 100644 --- a/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php +++ b/htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php @@ -161,6 +161,8 @@ class InterfaceActionsBlockedLog extends DolibarrTriggers // Protect against modification of data that should be immutable on a validated invoice (memory test only, no database access) if ($action === 'BILL_MODIFY' && !empty($object->oldcopy) && in_array($object->element, array('invoice', 'facture')) && $object->status != 0) { + /** @var Facture $object */ + '@phan-var-force Facture $object'; if ($object->oldcopy->ref != $object->ref) { $this->errors[] = 'Modifying the property Ref of a non draft invoice is not allowed'; return -2;