From f737ad72a058b76e6e88807b298980ac6c4e7af6 Mon Sep 17 00:00:00 2001 From: Eric Espie Date: Wed, 16 Apr 2025 17:51:54 +0200 Subject: [PATCH] SDK Form demonstrator delegate ... to the iTop Form Builder --- sources/FormType/Base/FormBuilder.php | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/sources/FormType/Base/FormBuilder.php b/sources/FormType/Base/FormBuilder.php index 66ee6c575b..62741c9ea3 100644 --- a/sources/FormType/Base/FormBuilder.php +++ b/sources/FormType/Base/FormBuilder.php @@ -6,12 +6,17 @@ namespace Combodo\iTop\FormType\Base; +use Combodo\iTop\FormType\Orm\AttCodeGroupByType; +use Combodo\iTop\FormType\Orm\ValuesFromAttcodeType; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Form\DataMapperInterface; use Symfony\Component\Form\DataTransformerInterface; +use Symfony\Component\Form\Event\PostSubmitEvent; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormConfigInterface; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\RequestHandlerInterface; @@ -24,6 +29,61 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate public function __construct(private FormBuilderInterface $builder) { + $this->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { + $this->aModelData = []; + }); + } + + public function Finalize(): void + { + $aCallbacks['query'] = function (FormEvent $event) { + if ($event instanceof PostSubmitEvent) { + $this->aModelData['query'] = $event->getForm()->getData(); + $options['hook_type'] = FormEvents::POST_SUBMIT; + } else { + $this->aModelData['query'] = $event->getData()['query']; + $options['hook_type'] = FormEvents::POST_SET_DATA; + } + $options['callback'] = function (FormEvent $event) { + $this->aModelData['group_by'] = $event->getForm()->getData(); + $oParentForm = $event->getForm()->getParent(); + $aUserOptions = [ + 'source_attcode' => 'group_by', + 'source_class' => 'query', + ]; + // TODO Filtrer seulement les données requises dans aModelData + $aFieldOptions = (new ValuesFromAttcodeType())->BuildOptions($aUserOptions, $this->aModelData); + if (!is_null($aFieldOptions)) { + $oParentForm->add('values', ValuesFromAttcodeType::class, $aFieldOptions); + } else { + // Remove field and dependencies + $oParentForm->add('values', HiddenType::class, ['mapped' => false]); + } + }; + if ($event instanceof PostSubmitEvent) { + $oForm = $event->getForm()->getParent(); + } else { + $oForm = $event->getForm(); + } + + $aUserOptions = [ + 'source_class' => 'query', + ]; + // TODO Filtrer seulement les données requises dans aModelData + $aFieldOptions = (new AttCodeGroupByType())->BuildOptions($aUserOptions, $this->aModelData); + if (!is_null($aFieldOptions)) { + $oForm->add('group_by', AttCodeGroupByType::class, array_merge($options, $aFieldOptions)); + } else { + // Remove field and dependencies + $oForm->add('group_by', HiddenType::class, ['mapped' => false]); + $oForm->add('values', HiddenType::class, ['mapped' => false]); + } + }; + + foreach($aCallbacks as $sField => $cCallback) { + $this->addEventListener(FormEvents::POST_SET_DATA, $cCallback); + $this->get($sField)->addEventListener(FormEvents::POST_SUBMIT, $cCallback); + } } /*