From 2530d59e08ebed44c19c07cee2b0af3afe347628 Mon Sep 17 00:00:00 2001 From: Eric Espie Date: Mon, 15 Dec 2025 17:15:07 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B08772=20-=20compilation=20of=20choices=20?= =?UTF-8?q?values?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/utils.inc.php | 6 +++++ sources/PropertyTree/AbstractProperty.php | 6 ----- sources/PropertyTree/Property.php | 18 ++++++++++----- .../ValueType/AbstractValueType.php | 6 +++++ .../ValueType/ValueTypeChoice.php | 22 ++++++++++++++++++- .../src/BaseTestCase/ItopTestCase.php | 1 + .../Forms/Compiler/FormsCompilerTest.php | 12 ++++++++++ 7 files changed, 59 insertions(+), 12 deletions(-) diff --git a/application/utils.inc.php b/application/utils.inc.php index ea226e995..7eea30491 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -1900,6 +1900,12 @@ SQL; return $response; } + public static function QuoteForPHP(string $sValue): string + { + $sEscaped = str_replace(['\\', "'"], ['\\\\', "\\'"], $sValue); + return "'$sEscaped'"; + } + /** * Get a standard list of character sets * diff --git a/sources/PropertyTree/AbstractProperty.php b/sources/PropertyTree/AbstractProperty.php index a0af621a7..a03cfbddf 100644 --- a/sources/PropertyTree/AbstractProperty.php +++ b/sources/PropertyTree/AbstractProperty.php @@ -78,10 +78,4 @@ abstract class AbstractProperty return null; } - - public function QuoteForPHP(string $sValue): string - { - $sEscaped = str_replace(['\\', "'"], ['\\\\', "\\'"], $sValue); - return "'$sEscaped'"; - } } diff --git a/sources/PropertyTree/Property.php b/sources/PropertyTree/Property.php index 2ea67dc92..12b6d039c 100644 --- a/sources/PropertyTree/Property.php +++ b/sources/PropertyTree/Property.php @@ -63,11 +63,19 @@ class Property extends AbstractProperty foreach ($this->oValueType->GetInputValues() as $sInputName => $sValue) { $this->GenerateInputs($sInputName, $sValue, $sPrerequisiteExpressions, $sInputs); } - $sLabel = $this->QuoteForPHP($this->sLabel); + + $sLabel = utils::QuoteForPHP($this->sLabel); + $aOptions = [ + 'label' => $sLabel, + ]; + $aOptions += $this->oValueType->GetFormBlockOptions(); + $sOptions = ''; + foreach ($aOptions as $sOption => $sValue) { + $sOptions .= "\t\t\t".utils::QuoteForPHP($sOption)." => $sValue,\n"; + } return <<Add('$this->sId', '$sFormBlockClass', [ - 'label' => $sLabel, - ]){$sInputs}; +$sOptions\t\t]){$sInputs}; PHP; } @@ -109,7 +117,7 @@ PHP; default => throw new PropertyTreeException("Node: {$this->sId}, unsupported expression for input type: $sInputName"), }; - $sExpression = $this->QuoteForPHP($sExpression); + $sExpression = utils::QuoteForPHP($sExpression); $sPrerequisiteExpressions = <<Add('{$this->sId}_{$sInputName}_expression', '$sExpressionClass', [ 'expression' => $sExpression, @@ -120,7 +128,7 @@ PHP; $sInputs .= "\n ->InputDependsOn('$sInputName', '{$this->sId}_{$sInputName}_expression', 'result')"; } else { - $sInputs .= "\n ->SetInputValue('$sInputName', ".$this->QuoteForPHP($sValue).")"; + $sInputs .= "\n ->SetInputValue('$sInputName', ".utils::QuoteForPHP($sValue).")"; } } } diff --git a/sources/PropertyTree/ValueType/AbstractValueType.php b/sources/PropertyTree/ValueType/AbstractValueType.php index 246204edb..5154b9722 100644 --- a/sources/PropertyTree/ValueType/AbstractValueType.php +++ b/sources/PropertyTree/ValueType/AbstractValueType.php @@ -22,6 +22,7 @@ abstract class AbstractValueType protected array $aInputs = []; protected array $aOutputs = []; protected array $aInputValues = []; + protected array $aFormBlockOptionsForPHP = []; public function InitFromDomNode(DesignElement $oDomNode): void { @@ -40,6 +41,11 @@ abstract class AbstractValueType } } + public function GetFormBlockOptions(): array + { + return $this->aFormBlockOptionsForPHP; + } + public function GetInputValues(): array { return $this->aInputValues; diff --git a/sources/PropertyTree/ValueType/ValueTypeChoice.php b/sources/PropertyTree/ValueType/ValueTypeChoice.php index c32b7df35..70ca05689 100644 --- a/sources/PropertyTree/ValueType/ValueTypeChoice.php +++ b/sources/PropertyTree/ValueType/ValueTypeChoice.php @@ -7,8 +7,9 @@ namespace Combodo\iTop\PropertyTree\ValueType; +use Combodo\iTop\DesignElement; use Combodo\iTop\Forms\Block\Base\ChoiceFormBlock; -use Combodo\iTop\Forms\Block\Base\FormBlock; +use utils; /** * @since 3.3.0 @@ -19,4 +20,23 @@ class ValueTypeChoice extends AbstractValueType { return ChoiceFormBlock::class; } + + public function InitFromDomNode(DesignElement $oDomNode): void + { + parent::InitFromDomNode($oDomNode); + + $sChoices = "[\n"; + foreach ($oDomNode->GetNodes('values/value') as $oValueNode) { + /** @var DesignElement $oValueNode */ + $sValue = utils::QuoteForPHP($oValueNode->GetAttribute('id')); + $sLabel = utils::QuoteForPHP($oValueNode->GetChildText('label')); + $sChoices .= << $sValue, + +PHP; + } + $sChoices .= "\t\t\t]"; + + $this->aFormBlockOptionsForPHP['choices'] = $sChoices; + } } diff --git a/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php b/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php index 18b073610..ad9abdb67 100644 --- a/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php @@ -15,6 +15,7 @@ use ParseError; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use ReflectionMethod; use SetupUtils; +use Symfony\Component\ErrorHandler\Error\FatalError; use Symfony\Component\HttpKernel\KernelInterface; use const DEBUG_BACKTRACE_IGNORE_ARGS; diff --git a/tests/php-unit-tests/unitary-tests/sources/Forms/Compiler/FormsCompilerTest.php b/tests/php-unit-tests/unitary-tests/sources/Forms/Compiler/FormsCompilerTest.php index eadb3a739..5b40f5f1b 100644 --- a/tests/php-unit-tests/unitary-tests/sources/Forms/Compiler/FormsCompilerTest.php +++ b/tests/php-unit-tests/unitary-tests/sources/Forms/Compiler/FormsCompilerTest.php @@ -116,6 +116,14 @@ PHP, + + + + + + + + @@ -172,6 +180,10 @@ class FormFor__AllValueTypesTest extends Combodo\iTop\Forms\Block\Base\FormBlock \$this->Add('choice_property', 'Combodo\iTop\Forms\Block\Base\ChoiceFormBlock', [ 'label' => 'UI:Choice', + 'choices' => [ + \Dict::S('Label A') => 'value_a', + \Dict::S('Label B') => 'value_b', + ], ]); \$this->Add('class_property', 'Combodo\iTop\Forms\Block\Base\TextFormBlock', [