mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 10:38:45 +02:00
N°8772 - cleanup code
This commit is contained in:
@@ -296,6 +296,7 @@ abstract class AbstractFormBlock
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -333,6 +334,7 @@ abstract class AbstractFormBlock
|
||||
$aBindings[$oFormOutput->GetName()] = $oFormOutput->GetBinding();
|
||||
}
|
||||
}
|
||||
|
||||
return $aBindings;
|
||||
}
|
||||
|
||||
@@ -389,11 +391,11 @@ abstract class AbstractFormBlock
|
||||
/** Iterate throw output @var FormOutput $oFormOutput */
|
||||
foreach ($this->aFormOutputs as $oFormOutput) {
|
||||
|
||||
try{
|
||||
try {
|
||||
// Compute the output value
|
||||
$oFormOutput->ComputeValue($sEventType, $oData);
|
||||
}
|
||||
catch(IOException $oException){
|
||||
catch (IOException $oException) {
|
||||
IssueLog::Exception(sprintf('Unable to compute values for output %s of block %s', $oFormOutput->GetName(), $this->GetName()), $oException);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,13 +15,13 @@ use Combodo\iTop\Forms\Block\FormType\ChoiceFormType;
|
||||
*/
|
||||
class ChoiceFormBlock extends AbstractFormBlock
|
||||
{
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function GetFormType(): string
|
||||
{
|
||||
return ChoiceFormType::class;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitOptions(): array
|
||||
{
|
||||
return [
|
||||
|
||||
@@ -22,13 +22,13 @@ class CollectionBlock extends AbstractFormBlock
|
||||
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function GetFormType(): string
|
||||
{
|
||||
return CollectionType::class;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitOptions(): array
|
||||
{
|
||||
$sBlockEntryType = $this->GetOptions()['block_entry_type'];
|
||||
@@ -39,7 +39,7 @@ class CollectionBlock extends AbstractFormBlock
|
||||
$oBlock = new ($sBlockEntryType)('prototype', $sBlockEntryOptions);
|
||||
|
||||
return [
|
||||
'entry_type' => $oBlock->GetFormType()
|
||||
'entry_type' => $oBlock->GetFormType(),
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Combodo\iTop\Forms\Block\Base;
|
||||
|
||||
use Combodo\iTop\Forms\Block\AbstractFormBlock;
|
||||
use Combodo\iTop\Forms\FormsException;
|
||||
use Exception;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
|
||||
/**
|
||||
@@ -40,17 +41,17 @@ class FormBlock extends AbstractFormBlock
|
||||
}
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function GetFormType(): string
|
||||
{
|
||||
return FormType::class;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitOptions(): array
|
||||
{
|
||||
return [
|
||||
'compound' => true
|
||||
'compound' => true,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -68,6 +69,7 @@ class FormBlock extends AbstractFormBlock
|
||||
$oSubFormBlock = new ($sType)($sName, $aOptions);
|
||||
$this->aChildrenBlocks[$sName] = $oSubFormBlock;
|
||||
$oSubFormBlock->SetParent($this);
|
||||
|
||||
return $oSubFormBlock;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,13 +15,13 @@ use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
*/
|
||||
class TextAreaFormBlock extends AbstractFormBlock
|
||||
{
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function GetFormType(): string
|
||||
{
|
||||
return TextareaType::class;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitOptions(): array
|
||||
{
|
||||
return [];
|
||||
|
||||
@@ -8,10 +8,9 @@ namespace Combodo\iTop\Forms\Block\DataModel;
|
||||
|
||||
use Combodo\iTop\Forms\Block\Base\ChoiceFormBlock;
|
||||
use Combodo\iTop\Forms\Block\FormBlockException;
|
||||
use Combodo\iTop\Forms\Block\IO\Converter\StringToAttributeConverter;
|
||||
use Combodo\iTop\Forms\Block\IO\Format\AttributeIOFormat;
|
||||
use Combodo\iTop\Forms\Block\IO\Format\ClassIOFormat;
|
||||
use Combodo\iTop\Forms\Block\IO\Converter\StringToAttributeConverter;
|
||||
use Combodo\iTop\Forms\FormType\AttributeFormType;
|
||||
use CoreException;
|
||||
use MetaModel;
|
||||
|
||||
@@ -28,22 +27,23 @@ class AttributeChoiceFormBlock extends ChoiceFormBlock
|
||||
// outputs
|
||||
public const OUTPUT_ATTRIBUTE = 'attribute';
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitOptions(): array
|
||||
{
|
||||
$aOptions = parent::InitOptions();
|
||||
|
||||
// $aOptions['placeholder'] = 'Select an attribute...';
|
||||
return $aOptions;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitInputs(): void
|
||||
{
|
||||
parent::InitInputs();
|
||||
$this->AddInput(self::INPUT_CLASS_NAME, ClassIOFormat::class);
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitOutputs(): void
|
||||
{
|
||||
parent::InitOutputs();
|
||||
@@ -67,11 +67,12 @@ class AttributeChoiceFormBlock extends ChoiceFormBlock
|
||||
$aOptions = parent::GetOptions();
|
||||
|
||||
$oValue = $this->GetInput(self::INPUT_CLASS_NAME)->Value();
|
||||
if($oValue == '')
|
||||
if ($oValue == '') {
|
||||
return $aOptions;
|
||||
}
|
||||
|
||||
$aAttributeCodes = MetaModel::GetAttributesList($oValue);
|
||||
$aAttributeCodes = array_combine($aAttributeCodes, $aAttributeCodes) ;
|
||||
$aAttributeCodes = array_combine($aAttributeCodes, $aAttributeCodes);
|
||||
$aOptions['choices'] = $aAttributeCodes;
|
||||
|
||||
return $aOptions;
|
||||
|
||||
@@ -11,7 +11,6 @@ use Combodo\iTop\Forms\Block\FormBlockException;
|
||||
use Combodo\iTop\Forms\Block\IO\Format\AttributeIOFormat;
|
||||
use Combodo\iTop\Forms\Block\IO\Format\ClassIOFormat;
|
||||
use Combodo\iTop\Forms\Block\IO\Format\RawFormat;
|
||||
use Combodo\iTop\Forms\FormType\AttributeValueFormType;
|
||||
use Exception;
|
||||
use MetaModel;
|
||||
|
||||
@@ -27,21 +26,21 @@ class AttributeValueChoiceFormBlock extends ChoiceFormBlock
|
||||
public const INPUT_ATTRIBUTE = 'attribute';
|
||||
|
||||
// Outputs
|
||||
public const OUTPUT_VALUE = 'value';
|
||||
public const OUTPUT_VALUE = 'value';
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitOptions(array &$aOptions = []): array
|
||||
{
|
||||
$aOptions['multiple'] = true;
|
||||
$aOptions['attr'] = [
|
||||
'size' => 5,
|
||||
'style' => 'height: auto;'
|
||||
'size' => 5,
|
||||
'style' => 'height: auto;',
|
||||
];
|
||||
|
||||
return $aOptions;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitInputs(): void
|
||||
{
|
||||
parent::InitInputs();
|
||||
@@ -49,7 +48,7 @@ class AttributeValueChoiceFormBlock extends ChoiceFormBlock
|
||||
$this->AddInput(self::INPUT_ATTRIBUTE, AttributeIOFormat::class);
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitOutputs(): void
|
||||
{
|
||||
parent::InitOutputs();
|
||||
@@ -64,12 +63,12 @@ class AttributeValueChoiceFormBlock extends ChoiceFormBlock
|
||||
{
|
||||
$bDependentOk = $this->GetInput(self::INPUT_CLASS_NAME)->Value() != '' && $this->GetInput(self::INPUT_ATTRIBUTE)->Value() != '';
|
||||
|
||||
if($bDependentOk) {
|
||||
if ($bDependentOk) {
|
||||
$oAttDef = MetaModel::GetAttributeDef($this->GetInput(self::INPUT_CLASS_NAME)->Value()->__toString(), $this->GetInput(self::INPUT_ATTRIBUTE)->Value()->__toString());
|
||||
$aValues = $oAttDef->GetAllowedValues();
|
||||
|
||||
return $aValues != null;
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -82,18 +81,21 @@ class AttributeValueChoiceFormBlock extends ChoiceFormBlock
|
||||
$aOptions = parent::GetOptions();
|
||||
|
||||
$oClassName = $this->GetInput(self::INPUT_CLASS_NAME)->Value();
|
||||
if($oClassName == '')
|
||||
if ($oClassName == '') {
|
||||
return $aOptions;
|
||||
}
|
||||
|
||||
$oAttribute = $this->GetInput(self::INPUT_ATTRIBUTE)->Value();
|
||||
if($oAttribute == '')
|
||||
if ($oAttribute == '') {
|
||||
return $aOptions;
|
||||
}
|
||||
|
||||
$oAttDef = MetaModel::GetAttributeDef(strval($oClassName), strval($oAttribute));
|
||||
$aValues = $oAttDef->GetAllowedValues();
|
||||
|
||||
if($aValues === null)
|
||||
if ($aValues === null) {
|
||||
return $aOptions;
|
||||
}
|
||||
|
||||
$aOptions['choices'] = array_flip($aValues ?? []);
|
||||
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
namespace Combodo\iTop\Forms\Block\DataModel;
|
||||
|
||||
use Combodo\iTop\Forms\Block\Base\TextAreaFormBlock;
|
||||
use Combodo\iTop\Forms\Block\IO\Format\ClassIOFormat;
|
||||
use Combodo\iTop\Forms\Block\IO\Converter\OqlToClassConverter;
|
||||
use Combodo\iTop\Forms\Block\FormType\OqlFormType;
|
||||
use Combodo\iTop\Forms\Block\IO\Converter\OqlToClassConverter;
|
||||
use Combodo\iTop\Forms\Block\IO\Format\ClassIOFormat;
|
||||
|
||||
/**
|
||||
* Form block for oql expression.
|
||||
@@ -21,27 +21,27 @@ class OqlFormBlock extends TextAreaFormBlock
|
||||
// outputs
|
||||
public const OUTPUT_SELECTED_CLASS = 'selected_class';
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function GetFormType(): string
|
||||
{
|
||||
return OqlFormType::class;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitOutputs(): void
|
||||
{
|
||||
parent::InitOutputs();
|
||||
$this->AddOutput(self::OUTPUT_SELECTED_CLASS, ClassIOFormat::class, new OqlToClassConverter());
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function InitOptions(): array
|
||||
{
|
||||
$aOptions = parent::InitOptions();
|
||||
$aOptions['with_ai_button'] = true;
|
||||
|
||||
return $aOptions;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
namespace Combodo\iTop\Forms\Block\FormType;
|
||||
|
||||
use MetaModel;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
|
||||
class AttributeValueFormType extends AbstractType
|
||||
@@ -19,14 +20,14 @@ class AttributeValueFormType extends AbstractType
|
||||
{
|
||||
$aValues = [];
|
||||
|
||||
if(!empty($inputs['attribute'])){
|
||||
$oAttDef = \MetaModel::GetAttributeDef($inputs['object_class'], $inputs['attribute']);
|
||||
if (!empty($inputs['attribute'])) {
|
||||
$oAttDef = MetaModel::GetAttributeDef($inputs['object_class'], $inputs['attribute']);
|
||||
$aValues = $oAttDef->GetAllowedValues();
|
||||
$aValues = $aValues !== null ? array_combine($aValues, $aValues) : [];
|
||||
}
|
||||
|
||||
return [
|
||||
'choices' => $aValues
|
||||
'choices' => $aValues,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -19,22 +19,22 @@ use Symfony\Component\Form\FormEvents;
|
||||
*/
|
||||
class ChoiceFormType extends AbstractType
|
||||
{
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function getParent(): string
|
||||
{
|
||||
return ChoiceType::class;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
// on preset data
|
||||
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (PreSetDataEvent $event) use ($options){
|
||||
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (PreSetDataEvent $event) use ($options) {
|
||||
|
||||
if($options['multiple'] === false && $options['required'] === true) {
|
||||
if ($options['multiple'] === false && $options['required'] === true) {
|
||||
if ($event->getData() === null) {
|
||||
$FirstElement = array_shift($options['choices']);
|
||||
if($FirstElement !== null){
|
||||
if ($FirstElement !== null) {
|
||||
$event->setData($FirstElement);
|
||||
}
|
||||
}
|
||||
@@ -43,10 +43,10 @@ class ChoiceFormType extends AbstractType
|
||||
});
|
||||
|
||||
// on pre submit
|
||||
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (PreSubmitEvent $event) use ($options){
|
||||
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (PreSubmitEvent $event) use ($options) {
|
||||
|
||||
// reset value if not in available choices
|
||||
if(!empty($event->getData()) && !$this->CheckValue($event->getData(), $options)){
|
||||
if (!empty($event->getData()) && !$this->CheckValue($event->getData(), $options)) {
|
||||
$event->getForm()->addError(new FormError("The value has been reset because it is not part of the available choices anymore."));
|
||||
$event->setData(null);
|
||||
}
|
||||
@@ -63,16 +63,15 @@ class ChoiceFormType extends AbstractType
|
||||
private function CheckValue($oValue, $options): bool
|
||||
{
|
||||
// Check multi selection values
|
||||
if($options['multiple'] === true){
|
||||
foreach ($oValue as $v){
|
||||
if(!in_array($v, $options['choices'])){
|
||||
if ($options['multiple'] === true) {
|
||||
foreach ($oValue as $v) {
|
||||
if (!in_array($v, $options['choices'])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Check single selection values
|
||||
else{
|
||||
if(!in_array($oValue, $options['choices'])){
|
||||
} // Check single selection values
|
||||
else {
|
||||
if (!in_array($oValue, $options['choices'])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -81,5 +80,4 @@ class ChoiceFormType extends AbstractType
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -7,13 +7,7 @@
|
||||
namespace Combodo\iTop\Forms\Block\FormType;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Event\PreSetDataEvent;
|
||||
use Symfony\Component\Form\Event\PreSubmitEvent;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
@@ -21,7 +15,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
*/
|
||||
class CollectionFormType extends AbstractType
|
||||
{
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function getParent(): string
|
||||
{
|
||||
return CollectionType::class;
|
||||
|
||||
@@ -34,38 +34,40 @@ class OqlFormType extends AbstractType
|
||||
]);
|
||||
|
||||
$resolver->setDefault('outputs', array(
|
||||
'selected_class' => function($oData) {
|
||||
if($oData === null)
|
||||
'selected_class' => function ($oData) {
|
||||
if ($oData === null) {
|
||||
return null;
|
||||
}
|
||||
// extract selected class
|
||||
preg_match('/SELECT\s+(\w+)/', $oData, $aMatches);
|
||||
|
||||
return $aMatches[1] ?? null;
|
||||
}
|
||||
},
|
||||
));
|
||||
|
||||
$resolver->setDefined('with_ai_button');
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
parent::buildForm($builder, $options);
|
||||
|
||||
// on pre submit
|
||||
$builder->addEventListener(FormEvents::POST_SUBMIT, function (PostSubmitEvent $event) use ($options){
|
||||
$builder->addEventListener(FormEvents::POST_SUBMIT, function (PostSubmitEvent $event) use ($options) {
|
||||
|
||||
try{
|
||||
try {
|
||||
$oClassConverter = new OqlToClassConverter();
|
||||
$oClassConverter->Convert($event->getData());
|
||||
}
|
||||
catch(Exception $e){
|
||||
catch (Exception $e) {
|
||||
$event->getForm()->addError(new FormError($e->getMessage()));
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function buildView(FormView $view, FormInterface $form, array $options): void
|
||||
{
|
||||
parent::buildView($view, $form, $options);
|
||||
|
||||
@@ -64,6 +64,7 @@ class AbstractFormIO
|
||||
public function SetOwnerBlock(AbstractFormBlock $oOwnerBlock): self
|
||||
{
|
||||
$this->oOwnerBlock = $oOwnerBlock;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -87,6 +88,7 @@ class AbstractFormIO
|
||||
public function SetName(string $sName): self
|
||||
{
|
||||
$this->sName = $sName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -136,6 +138,7 @@ class AbstractFormIO
|
||||
{
|
||||
$PostSetDataExist = array_key_exists(FormEvents::POST_SET_DATA, $this->aValues) && $this->aValues[FormEvents::POST_SET_DATA] !== null;
|
||||
$PostSubmitExist = array_key_exists(FormEvents::POST_SUBMIT, $this->aValues) && $this->aValues[FormEvents::POST_SUBMIT] !== null;
|
||||
|
||||
return $PostSetDataExist || $PostSubmitExist;
|
||||
}
|
||||
|
||||
@@ -170,12 +173,13 @@ class AbstractFormIO
|
||||
*/
|
||||
public function Value(): mixed
|
||||
{
|
||||
if(array_key_exists(FormEvents::POST_SUBMIT, $this->aValues) ){
|
||||
if (array_key_exists(FormEvents::POST_SUBMIT, $this->aValues)) {
|
||||
return $this->aValues[FormEvents::POST_SUBMIT];
|
||||
}
|
||||
if(array_key_exists(FormEvents::POST_SET_DATA, $this->aValues) ){
|
||||
if (array_key_exists(FormEvents::POST_SET_DATA, $this->aValues)) {
|
||||
return $this->aValues[FormEvents::POST_SET_DATA];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,24 +15,25 @@ use Symfony\Component\Filesystem\Exception\IOException;
|
||||
*/
|
||||
class OqlToClassConverter extends AbstractConverter
|
||||
{
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function Convert(mixed $oData): ?ClassIOFormat
|
||||
{
|
||||
if($oData === null)
|
||||
if ($oData === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Extract OQL information
|
||||
preg_match('/SELECT\s+(\w+)/', $oData, $aMatches);
|
||||
|
||||
// Selected class
|
||||
if(isset($aMatches[1])){
|
||||
if (isset($aMatches[1])) {
|
||||
$sSelectedClass = $aMatches[1];
|
||||
if(!MetaModel::IsValidClass($sSelectedClass)){
|
||||
throw new IOException('Incorrect OQL select class name ' . $sSelectedClass);
|
||||
if (!MetaModel::IsValidClass($sSelectedClass)) {
|
||||
throw new IOException('Incorrect OQL select class name '.$sSelectedClass);
|
||||
}
|
||||
|
||||
return new ClassIOFormat($aMatches[1]);
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
throw new IOException('Incorrect OQL sentence');
|
||||
}
|
||||
|
||||
|
||||
@@ -13,11 +13,13 @@ use Combodo\iTop\Forms\Block\IO\Format\AttributeIOFormat;
|
||||
*/
|
||||
class StringToAttributeConverter extends AbstractConverter
|
||||
{
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function Convert(mixed $oData): ?AttributeIOFormat
|
||||
{
|
||||
if($oData === null)
|
||||
if ($oData === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new AttributeIOFormat($oData);
|
||||
}
|
||||
}
|
||||
@@ -13,11 +13,11 @@ use Combodo\iTop\Forms\Block\IO\Converter\AbstractConverter;
|
||||
*/
|
||||
class FormOutput extends AbstractFormIO
|
||||
{
|
||||
/** @var AbstractConverter|null */
|
||||
/** @var AbstractConverter|null */
|
||||
private null|AbstractConverter $oConverter;
|
||||
|
||||
|
||||
/** @var array */
|
||||
/** @var array */
|
||||
private array $aBindingsToOutputs = [];
|
||||
|
||||
/**
|
||||
@@ -45,6 +45,7 @@ class FormOutput extends AbstractFormIO
|
||||
if (is_null($this->oConverter)) {
|
||||
return $oData;
|
||||
}
|
||||
|
||||
return $this->oConverter->Convert($oData);
|
||||
}
|
||||
|
||||
@@ -83,7 +84,6 @@ class FormOutput extends AbstractFormIO
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Bind to output.
|
||||
*
|
||||
@@ -103,7 +103,6 @@ class FormOutput extends AbstractFormIO
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the bindings.
|
||||
*
|
||||
|
||||
@@ -89,15 +89,15 @@ class DependencyHandler
|
||||
foreach ($this->oDependenciesMap->GetListenedOutputBlockNames() as $sOutputBlockName) {
|
||||
|
||||
// inner binding
|
||||
if($sOutputBlockName === $this->oFormBlock->getName()) {
|
||||
if ($sOutputBlockName === $this->oFormBlock->getName()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->aDebugData[] = [
|
||||
'builder' => $this->oFormBuilder->getName(),
|
||||
'event' => 'form.listen',
|
||||
'form' => $sOutputBlockName,
|
||||
'value' => 'NA'
|
||||
'event' => 'form.listen',
|
||||
'form' => $sOutputBlockName,
|
||||
'value' => 'NA',
|
||||
];
|
||||
|
||||
// Listen the output block POST_SET_DATA & POST_SUBMIT
|
||||
@@ -121,9 +121,9 @@ class DependencyHandler
|
||||
|
||||
$this->aDebugData[] = [
|
||||
'builder' => $this->oFormBuilder->getName(),
|
||||
'event' => $sEventType,
|
||||
'form' => $oEvent->getForm()->getName(),
|
||||
'value' => $oEvent->getData()
|
||||
'event' => $sEventType,
|
||||
'form' => $oEvent->getForm()->getName(),
|
||||
'value' => $oEvent->getData(),
|
||||
];
|
||||
|
||||
// Get the form
|
||||
@@ -133,7 +133,7 @@ class DependencyHandler
|
||||
$oFormBlock = $this->aSubBlocks[$oForm->getName()];
|
||||
|
||||
// Compute the block outputs with the data
|
||||
if(!$oFormBlock instanceof FormBlock) {
|
||||
if (!$oFormBlock instanceof FormBlock) {
|
||||
$oFormBlock->ComputeOutputs($sEventType, $oEvent->getData());
|
||||
}
|
||||
|
||||
@@ -152,16 +152,15 @@ class DependencyHandler
|
||||
private function CheckDependencies(FormInterface|FormBuilderInterface $oForm): void
|
||||
{
|
||||
/** Iterate throw dependencies... @var AbstractFormBlock $oDependentBlock */
|
||||
foreach ($this->aDependentBlocks as $qBlockName => $oDependentBlock)
|
||||
{
|
||||
foreach ($this->aDependentBlocks as $qBlockName => $oDependentBlock) {
|
||||
// When dependencies met, add the dependent field if not already done
|
||||
if(!$oDependentBlock->IsAdded() && $oDependentBlock->IsInputsDataReady()) {
|
||||
if (!$oDependentBlock->IsAdded() && $oDependentBlock->IsInputsDataReady()) {
|
||||
|
||||
// Get the dependent field options
|
||||
$aOptions = $oDependentBlock->UpdateOptions();
|
||||
|
||||
// Add the listener callback to the dependent field if it is also a dependency for another field
|
||||
if($this->oDependenciesMap->IsTheBlockInDependencies($oDependentBlock->getName())) {
|
||||
if ($this->oDependenciesMap->IsTheBlockInDependencies($oDependentBlock->getName())) {
|
||||
|
||||
// Pass the listener call back to be registered by the dependency form builder
|
||||
$aOptions = array_merge($aOptions, [
|
||||
@@ -169,22 +168,23 @@ class DependencyHandler
|
||||
]);
|
||||
}
|
||||
|
||||
if($oDependentBlock->AllowAdd()) {
|
||||
if ($oDependentBlock->AllowAdd()) {
|
||||
|
||||
$this->aDebugData[] = [
|
||||
'builder' => $this->oFormBuilder->getName(),
|
||||
'event' => 'form.add',
|
||||
'form' => $oDependentBlock->getName(),
|
||||
'value' => 'NA'
|
||||
'event' => 'form.add',
|
||||
'form' => $oDependentBlock->getName(),
|
||||
'value' => 'NA',
|
||||
];
|
||||
|
||||
if(array_key_exists('builder_listener', $aOptions))
|
||||
$this->aDebugData[] = [
|
||||
'builder' => $this->oFormBuilder->getName(),
|
||||
'event' => 'form.listen',
|
||||
'form' => $oDependentBlock->getName(),
|
||||
'value' => 'NA'
|
||||
];
|
||||
if (array_key_exists('builder_listener', $aOptions)) {
|
||||
$this->aDebugData[] = [
|
||||
'builder' => $this->oFormBuilder->getName(),
|
||||
'event' => 'form.listen',
|
||||
'form' => $oDependentBlock->getName(),
|
||||
'value' => 'NA',
|
||||
];
|
||||
}
|
||||
|
||||
// Mark the dependency as added
|
||||
$oDependentBlock->SetAdded(true);
|
||||
@@ -196,11 +196,11 @@ class DependencyHandler
|
||||
|
||||
}
|
||||
|
||||
if($oDependentBlock->IsAdded() && !$oDependentBlock->IsInputsDataReady()) {
|
||||
if ($oDependentBlock->IsAdded() && !$oDependentBlock->IsInputsDataReady()) {
|
||||
$oForm->add($oDependentBlock->GetName(), HiddenType::class, [
|
||||
'form_block' => $oDependentBlock,
|
||||
'form_block' => $oDependentBlock,
|
||||
'prevent_form_build' => true,
|
||||
]);
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
namespace Combodo\iTop\Forms\FormBuilder;
|
||||
|
||||
use Combodo\iTop\Forms\Block\FormBlock;
|
||||
use Combodo\iTop\Forms\Block\IO\FormBinding;
|
||||
use Combodo\iTop\Forms\Block\IO\FormInput;
|
||||
use Combodo\iTop\Forms\Block\IO\FormOutput;
|
||||
@@ -46,31 +45,31 @@ class DependencyMap
|
||||
*/
|
||||
private function Init(): void
|
||||
{
|
||||
/** Iterate throw blocks with dependencies... @var FormBlock $oDependentBlock */
|
||||
/** Iterate throw blocks with dependencies... @var \Combodo\iTop\Forms\Block\Base\FormBlock $oDependentBlock */
|
||||
foreach ($this->aDependentBlocks as $sBlockName => $oDependentBlock) {
|
||||
|
||||
/** Iterate throw the block inputs bindings... @var FormBinding $oBinding**/
|
||||
/** Iterate throw the block inputs bindings... @var FormBinding $oBinding * */
|
||||
foreach ($oDependentBlock->GetInputsBindings() as $oBinding) {
|
||||
|
||||
// Output to inputs map
|
||||
if($oBinding->oSourceIO instanceof FormOutput
|
||||
&& $oBinding->oDestinationIO instanceof FormInput){
|
||||
if ($oBinding->oSourceIO instanceof FormOutput
|
||||
&& $oBinding->oDestinationIO instanceof FormInput) {
|
||||
$this->AddBindingToMap($this->aOutputToInputsMap, $oBinding);
|
||||
}
|
||||
// Input to inputs map
|
||||
if($oBinding->oSourceIO instanceof FormInput
|
||||
&& $oBinding->oDestinationIO instanceof FormInput){
|
||||
if ($oBinding->oSourceIO instanceof FormInput
|
||||
&& $oBinding->oDestinationIO instanceof FormInput) {
|
||||
$this->AddBindingToMap($this->aInputToInputsMap, $oBinding);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Iterate throw the block inputs connections... @var FormBinding $oBinding**/
|
||||
/** Iterate throw the block inputs connections... @var FormBinding $oBinding * */
|
||||
foreach ($oDependentBlock->GetOutputBindings() as $oBinding) {
|
||||
|
||||
// Output to outputs map
|
||||
if($oBinding->oSourceIO instanceof FormOutput
|
||||
&& $oBinding->oDestinationIO instanceof FormOutput){
|
||||
if ($oBinding->oSourceIO instanceof FormOutput
|
||||
&& $oBinding->oDestinationIO instanceof FormOutput) {
|
||||
$this->AddBindingToMap($this->aOutputToOutputsMap, $oBinding);
|
||||
}
|
||||
|
||||
@@ -113,8 +112,8 @@ class DependencyMap
|
||||
{
|
||||
$aResult = [];
|
||||
|
||||
foreach(array_keys($this->aOutputToInputsMap) as $sOutputBlockName) {
|
||||
if(!array_key_exists($sOutputBlockName, $this->aDependentBlocks)){
|
||||
foreach (array_keys($this->aOutputToInputsMap) as $sOutputBlockName) {
|
||||
if (!array_key_exists($sOutputBlockName, $this->aDependentBlocks)) {
|
||||
$aResult[] = $sOutputBlockName;
|
||||
}
|
||||
}
|
||||
@@ -149,9 +148,8 @@ class DependencyMap
|
||||
|
||||
public function IsTheBlockInDependencies(string $sBlockName): bool
|
||||
{
|
||||
foreach ($this->aDependentBlocks as $oDependentBlock)
|
||||
{
|
||||
if($oDependentBlock->getName() === $sBlockName) {
|
||||
foreach ($this->aDependentBlocks as $oDependentBlock) {
|
||||
if ($oDependentBlock->getName() === $sBlockName) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Combodo\iTop\Forms\FormBuilder;
|
||||
|
||||
use Combodo\iTop\Forms\Block\AbstractFormBlock;
|
||||
use Combodo\iTop\Forms\Block\Base\FormBlock;
|
||||
use IteratorAggregate;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\Form\DataMapperInterface;
|
||||
@@ -22,9 +23,9 @@ use Symfony\Component\Form\ResolvedFormTypeInterface;
|
||||
use Symfony\Component\PropertyAccess\PropertyPathInterface;
|
||||
use Traversable;
|
||||
|
||||
class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
class FormBuilder implements FormBuilderInterface, IteratorAggregate
|
||||
{
|
||||
/** @var DependencyHandler|null */
|
||||
/** @var DependencyHandler|null */
|
||||
private ?DependencyHandler $oDependencyHandler = null;
|
||||
|
||||
/** @var AbstractFormBlock */
|
||||
@@ -47,7 +48,7 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
$oFormBlock = $this->builder->getOption('form_block');
|
||||
|
||||
// Build the form
|
||||
if($oFormBlock instanceof FormBlock) {
|
||||
if ($oFormBlock instanceof FormBlock) {
|
||||
$this->BuildForm($oFormBlock);
|
||||
}
|
||||
}
|
||||
@@ -63,7 +64,7 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
{
|
||||
// Hidden (ignore)
|
||||
$aOptions = $this->builder->getOptions();
|
||||
if(array_key_exists('prevent_form_build', $aOptions) && $aOptions['prevent_form_build']) {
|
||||
if (array_key_exists('prevent_form_build', $aOptions) && $aOptions['prevent_form_build']) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -78,7 +79,7 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
$bHasDependency = $this->HandleSubBlock($oSubFormBlock);
|
||||
|
||||
// Add to the dependencies array
|
||||
if($bHasDependency){
|
||||
if ($bHasDependency) {
|
||||
$aDependentBlocks[$oSubFormBlock->GetName()] = $oSubFormBlock;
|
||||
}
|
||||
|
||||
@@ -105,17 +106,15 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
|
||||
// Insert a hidden type to save the place
|
||||
$this->builder->add($oSubFormBlock->GetName(), HiddenType::class, [
|
||||
'form_block' => $oSubFormBlock,
|
||||
'form_block' => $oSubFormBlock,
|
||||
'prevent_form_build' => true,
|
||||
// 'mapped' => false,
|
||||
// 'disabled' => true,
|
||||
// 'mapped' => false,
|
||||
// 'disabled' => true,
|
||||
]);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
} else {
|
||||
// Directly insert the block corresponding form type
|
||||
$this->add($oSubFormBlock->GetName(), $oSubFormBlock->GetFormType(), $oSubFormBlock->UpdateOptions());
|
||||
$oSubFormBlock->SetAdded(true);
|
||||
@@ -152,6 +151,7 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
public function add(string|FormBuilderInterface $child, ?string $type = null, array $options = []): static
|
||||
{
|
||||
$this->builder->add($child, $type, $options);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -164,6 +164,7 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
{
|
||||
return $this->builder->count();
|
||||
}
|
||||
|
||||
public function create(string $name, ?string $type = null, array $options = []): FormBuilderInterface
|
||||
{
|
||||
return $this->builder->create($name, $type, $options);
|
||||
@@ -177,6 +178,7 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
public function remove(string $name): static
|
||||
{
|
||||
$this->builder->remove($name);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -198,126 +200,147 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
public function addEventListener(string $eventName, callable $listener, int $priority = 0): static
|
||||
{
|
||||
$this->builder->addEventListener($eventName, $listener, $priority);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addEventSubscriber(EventSubscriberInterface $subscriber): static
|
||||
{
|
||||
$this->builder->addEventSubscriber($subscriber);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addViewTransformer(DataTransformerInterface $viewTransformer, bool $forcePrepend = false): static
|
||||
{
|
||||
$this->builder->addViewTransformer($viewTransformer, $forcePrepend);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function resetViewTransformers(): static
|
||||
{
|
||||
$this->builder->resetViewTransformers();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addModelTransformer(DataTransformerInterface $modelTransformer, bool $forceAppend = false): static
|
||||
{
|
||||
$this->builder->addModelTransformer($modelTransformer, $forceAppend);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function resetModelTransformers(): static
|
||||
{
|
||||
$this->builder->resetModelTransformers();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAttribute(string $name, mixed $value): static
|
||||
{
|
||||
$this->builder->setAttribute($name, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAttributes(array $attributes): static
|
||||
{
|
||||
$this->builder->setAttributes($attributes);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDataMapper(?DataMapperInterface $dataMapper): static
|
||||
{
|
||||
$this->builder->setDataMapper($dataMapper);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDisabled(bool $disabled): static
|
||||
{
|
||||
$this->builder->setDisabled($disabled);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setEmptyData(mixed $emptyData): static
|
||||
{
|
||||
$this->builder->setEmptyData($emptyData);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setErrorBubbling(bool $errorBubbling): static
|
||||
{
|
||||
$this->builder->setErrorBubbling($errorBubbling);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setRequired(bool $required): static
|
||||
{
|
||||
$this->builder->setRequired($required);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPropertyPath(PropertyPathInterface|string|null $propertyPath): static
|
||||
{
|
||||
$this->builder->setPropertyPath($propertyPath);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMapped(bool $mapped): static
|
||||
{
|
||||
$this->builder->setMapped($mapped);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setByReference(bool $byReference): static
|
||||
{
|
||||
$this->builder->setByReference($byReference);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setInheritData(bool $inheritData): static
|
||||
{
|
||||
$this->builder->setInheritData($inheritData);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setCompound(bool $compound): static
|
||||
{
|
||||
$this->builder->setCompound($compound);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setType(ResolvedFormTypeInterface $type): static
|
||||
{
|
||||
$this->builder->setType($type);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setData(mixed $data): static
|
||||
{
|
||||
$this->builder->setData($data);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDataLocked(bool $locked): static
|
||||
{
|
||||
$this->builder->setDataLocked($locked);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -329,24 +352,28 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
public function setAction(string $action): static
|
||||
{
|
||||
$this->builder->setAction($action);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMethod(string $method): static
|
||||
{
|
||||
$this->builder->setMethod($method);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setRequestHandler(RequestHandlerInterface $requestHandler): static
|
||||
{
|
||||
$this->builder->setRequestHandler($requestHandler);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAutoInitialize(bool $initialize): static
|
||||
{
|
||||
$this->builder->setAutoInitialize($initialize);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -358,6 +385,7 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
public function setIsEmptyCallback(?callable $isEmptyCallback): static
|
||||
{
|
||||
$this->builder->setIsEmptyCallback($isEmptyCallback);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,8 +24,7 @@ class FormHelper
|
||||
return FormEvents::POST_SET_DATA;
|
||||
} else if ($event instanceof PostSubmitEvent) {
|
||||
return FormEvents::POST_SUBMIT;
|
||||
}
|
||||
else if ($event instanceof PreSubmitEvent) {
|
||||
} else if ($event instanceof PreSubmitEvent) {
|
||||
return FormEvents::PRE_SUBMIT;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
namespace Combodo\iTop\Forms\FormBuilder;
|
||||
|
||||
use Combodo\iTop\Forms\Block\FormBlock;
|
||||
use Symfony\Component\Form\AbstractTypeExtension;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
@@ -22,38 +21,38 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
class FormTypeExtension extends AbstractTypeExtension
|
||||
{
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public static function getExtendedTypes(): iterable
|
||||
{
|
||||
return [
|
||||
FormType::class
|
||||
FormType::class,
|
||||
];
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefined([
|
||||
'form_block',
|
||||
'builder_listener',
|
||||
'prevent_form_build'
|
||||
'prevent_form_build',
|
||||
]);
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
if(array_key_exists('builder_listener', $options)) {
|
||||
if (array_key_exists('builder_listener', $options)) {
|
||||
$builder->addEventListener(FormEvents::POST_SET_DATA, $options['builder_listener']);
|
||||
$builder->addEventListener(FormEvents::POST_SUBMIT, $options['builder_listener']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc */
|
||||
public function buildView(FormView $view, FormInterface $form, array $options): void
|
||||
{
|
||||
if(array_key_exists('form_block', $options)) {
|
||||
if (array_key_exists('form_block', $options)) {
|
||||
$view->vars['form_block'] = $options['form_block'];
|
||||
|
||||
$oFormBlock = $options['form_block'];
|
||||
|
||||
@@ -16,6 +16,7 @@ class ResolvedFormType extends SymfonyResolvedFormType implements ResolvedFormTy
|
||||
protected function newBuilder(string $name, ?string $dataClass, FormFactoryInterface $factory, array $options): FormBuilderInterface
|
||||
{
|
||||
$builder = parent::newBuilder($name, $dataClass, $factory, $options);
|
||||
|
||||
return new FormBuilder($builder);
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ class FormCompatibilityExtension extends AbstractExtension
|
||||
// Alias of dict_s, to be compatible with Symfony/Twig standard
|
||||
new TwigFilter('trans', function ($sStringCode, $aData = null, $sTransDomain = false) {
|
||||
return Dict::S($sStringCode);
|
||||
})
|
||||
}),
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user