N°8772 - compilation of choices values

This commit is contained in:
Eric Espie
2025-12-15 17:15:07 +01:00
parent a77e54c8cb
commit 2530d59e08
7 changed files with 59 additions and 12 deletions

View File

@@ -1900,6 +1900,12 @@ SQL;
return $response; return $response;
} }
public static function QuoteForPHP(string $sValue): string
{
$sEscaped = str_replace(['\\', "'"], ['\\\\', "\\'"], $sValue);
return "'$sEscaped'";
}
/** /**
* Get a standard list of character sets * Get a standard list of character sets
* *

View File

@@ -78,10 +78,4 @@ abstract class AbstractProperty
return null; return null;
} }
public function QuoteForPHP(string $sValue): string
{
$sEscaped = str_replace(['\\', "'"], ['\\\\', "\\'"], $sValue);
return "'$sEscaped'";
}
} }

View File

@@ -63,11 +63,19 @@ class Property extends AbstractProperty
foreach ($this->oValueType->GetInputValues() as $sInputName => $sValue) { foreach ($this->oValueType->GetInputValues() as $sInputName => $sValue) {
$this->GenerateInputs($sInputName, $sValue, $sPrerequisiteExpressions, $sInputs); $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 <<<PHP return <<<PHP
{$sPrerequisiteExpressions}\$this->Add('$this->sId', '$sFormBlockClass', [ {$sPrerequisiteExpressions}\$this->Add('$this->sId', '$sFormBlockClass', [
'label' => $sLabel, $sOptions\t\t]){$sInputs};
]){$sInputs};
PHP; PHP;
} }
@@ -109,7 +117,7 @@ PHP;
default => throw new PropertyTreeException("Node: {$this->sId}, unsupported expression for input type: $sInputName"), default => throw new PropertyTreeException("Node: {$this->sId}, unsupported expression for input type: $sInputName"),
}; };
$sExpression = $this->QuoteForPHP($sExpression); $sExpression = utils::QuoteForPHP($sExpression);
$sPrerequisiteExpressions = <<<PHP $sPrerequisiteExpressions = <<<PHP
\$this->Add('{$this->sId}_{$sInputName}_expression', '$sExpressionClass', [ \$this->Add('{$this->sId}_{$sInputName}_expression', '$sExpressionClass', [
'expression' => $sExpression, 'expression' => $sExpression,
@@ -120,7 +128,7 @@ PHP;
$sInputs .= "\n ->InputDependsOn('$sInputName', '{$this->sId}_{$sInputName}_expression', 'result')"; $sInputs .= "\n ->InputDependsOn('$sInputName', '{$this->sId}_{$sInputName}_expression', 'result')";
} else { } else {
$sInputs .= "\n ->SetInputValue('$sInputName', ".$this->QuoteForPHP($sValue).")"; $sInputs .= "\n ->SetInputValue('$sInputName', ".utils::QuoteForPHP($sValue).")";
} }
} }
} }

View File

@@ -22,6 +22,7 @@ abstract class AbstractValueType
protected array $aInputs = []; protected array $aInputs = [];
protected array $aOutputs = []; protected array $aOutputs = [];
protected array $aInputValues = []; protected array $aInputValues = [];
protected array $aFormBlockOptionsForPHP = [];
public function InitFromDomNode(DesignElement $oDomNode): void public function InitFromDomNode(DesignElement $oDomNode): void
{ {
@@ -40,6 +41,11 @@ abstract class AbstractValueType
} }
} }
public function GetFormBlockOptions(): array
{
return $this->aFormBlockOptionsForPHP;
}
public function GetInputValues(): array public function GetInputValues(): array
{ {
return $this->aInputValues; return $this->aInputValues;

View File

@@ -7,8 +7,9 @@
namespace Combodo\iTop\PropertyTree\ValueType; namespace Combodo\iTop\PropertyTree\ValueType;
use Combodo\iTop\DesignElement;
use Combodo\iTop\Forms\Block\Base\ChoiceFormBlock; use Combodo\iTop\Forms\Block\Base\ChoiceFormBlock;
use Combodo\iTop\Forms\Block\Base\FormBlock; use utils;
/** /**
* @since 3.3.0 * @since 3.3.0
@@ -19,4 +20,23 @@ class ValueTypeChoice extends AbstractValueType
{ {
return ChoiceFormBlock::class; 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 .= <<<PHP
\Dict::S($sLabel) => $sValue,
PHP;
}
$sChoices .= "\t\t\t]";
$this->aFormBlockOptionsForPHP['choices'] = $sChoices;
}
} }

View File

@@ -15,6 +15,7 @@ use ParseError;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use ReflectionMethod; use ReflectionMethod;
use SetupUtils; use SetupUtils;
use Symfony\Component\ErrorHandler\Error\FatalError;
use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\HttpKernel\KernelInterface;
use const DEBUG_BACKTRACE_IGNORE_ARGS; use const DEBUG_BACKTRACE_IGNORE_ARGS;

View File

@@ -116,6 +116,14 @@ PHP,
<node id="choice_property" xsi:type="Combodo-Property"> <node id="choice_property" xsi:type="Combodo-Property">
<label>UI:Choice</label> <label>UI:Choice</label>
<value-type xsi:type="Combodo-ValueTypeChoice"> <value-type xsi:type="Combodo-ValueTypeChoice">
<values>
<value id="value_a">
<label>Label A</label>
</value>
<value id="value_b">
<label>Label B</label>
</value>
</values>
</value-type> </value-type>
</node> </node>
<node id="class_property" xsi:type="Combodo-Property"> <node id="class_property" xsi:type="Combodo-Property">
@@ -172,6 +180,10 @@ class FormFor__AllValueTypesTest extends Combodo\iTop\Forms\Block\Base\FormBlock
\$this->Add('choice_property', 'Combodo\iTop\Forms\Block\Base\ChoiceFormBlock', [ \$this->Add('choice_property', 'Combodo\iTop\Forms\Block\Base\ChoiceFormBlock', [
'label' => 'UI:Choice', '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', [ \$this->Add('class_property', 'Combodo\iTop\Forms\Block\Base\TextFormBlock', [