SDK Form demonstrator delegate ... to the iTop Form Builder

This commit is contained in:
Eric Espie
2025-04-16 17:51:54 +02:00
parent e9aaa96aef
commit f737ad72a0

View File

@@ -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);
}
}
/*