From e9aaa96aefe3f911ab46bce478c2ff6f6f067d12 Mon Sep 17 00:00:00 2001 From: Eric Espie Date: Wed, 16 Apr 2025 17:41:55 +0200 Subject: [PATCH] SDK Form demonstrator delegate creation of hidden dynamic fields to the iTop Form Builder --- lib/composer/autoload_classmap.php | 1 + lib/composer/autoload_static.php | 1 + sources/FormType/Base/FormBuilder.php | 442 +++++++++++++++++++++++++ sources/FormType/Base/HiddenType.php | 1 - sources/FormType/Base/TextType.php | 1 - sources/FormType/Base/TextareaType.php | 1 - 6 files changed, 444 insertions(+), 3 deletions(-) create mode 100644 sources/FormType/Base/FormBuilder.php diff --git a/lib/composer/autoload_classmap.php b/lib/composer/autoload_classmap.php index beb9b11517..debacc3b86 100644 --- a/lib/composer/autoload_classmap.php +++ b/lib/composer/autoload_classmap.php @@ -431,6 +431,7 @@ return array( 'Combodo\\iTop\\DesignDocument' => $baseDir . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\DesignElement' => $baseDir . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\FormType\\Base\\AbstractType' => $baseDir . '/sources/FormType/Base/AbstractType.php', + 'Combodo\\iTop\\FormType\\Base\\FormBuilder' => $baseDir . '/sources/FormType/Base/FormBuilder.php', 'Combodo\\iTop\\FormType\\Base\\HiddenType' => $baseDir . '/sources/FormType/Base/HiddenType.php', 'Combodo\\iTop\\FormType\\Base\\TextType' => $baseDir . '/sources/FormType/Base/TextType.php', 'Combodo\\iTop\\FormType\\Base\\TextareaType' => $baseDir . '/sources/FormType/Base/TextareaType.php', diff --git a/lib/composer/autoload_static.php b/lib/composer/autoload_static.php index 3b83f838d5..10e9075af9 100644 --- a/lib/composer/autoload_static.php +++ b/lib/composer/autoload_static.php @@ -786,6 +786,7 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f 'Combodo\\iTop\\DesignDocument' => __DIR__ . '/../..' . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\DesignElement' => __DIR__ . '/../..' . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\FormType\\Base\\AbstractType' => __DIR__ . '/../..' . '/sources/FormType/Base/AbstractType.php', + 'Combodo\\iTop\\FormType\\Base\\FormBuilder' => __DIR__ . '/../..' . '/sources/FormType/Base/FormBuilder.php', 'Combodo\\iTop\\FormType\\Base\\HiddenType' => __DIR__ . '/../..' . '/sources/FormType/Base/HiddenType.php', 'Combodo\\iTop\\FormType\\Base\\TextType' => __DIR__ . '/../..' . '/sources/FormType/Base/TextType.php', 'Combodo\\iTop\\FormType\\Base\\TextareaType' => __DIR__ . '/../..' . '/sources/FormType/Base/TextareaType.php', diff --git a/sources/FormType/Base/FormBuilder.php b/sources/FormType/Base/FormBuilder.php new file mode 100644 index 0000000000..66ee6c575b --- /dev/null +++ b/sources/FormType/Base/FormBuilder.php @@ -0,0 +1,442 @@ +builder->count(); + } + + /** + * @param string|FormBuilderInterface $child + */ + public function add($child, ?string $type = null, array $options = []): static + { + if (!is_subclass_of($type, AbstractType::class)) { + throw new \Exception("type must be an instance of AbstractType (found $type)"); + } + $oType = new $type(); + if (is_null($oType->GetPrerequisites($options))) { + $this->builder->add($child, $type, $options); + } else { + $this->builder->add($child, HiddenType::class, ['mapped' => false]); + } + return $this; + } + + public function create(string $name, ?string $type = null, array $options = []): FormBuilderInterface + { + return $this->builder->create($name, $type, $options); + } + + public function get(string $name): FormBuilderInterface + { + return $this->builder->get($name); + } + + public function remove(string $name): static + { + $this->builder->remove($name); + + return $this; + } + + public function has(string $name): bool + { + return $this->builder->has($name); + } + + public function all(): array + { + return $this->builder->all(); + } + + public function getForm(): FormInterface + { + return $this->builder->getForm(); + } + + 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 = null): 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 setInheritData(bool $inheritData): static + { + $this->builder->setInheritData($inheritData); + + return $this; + } + + public function setMapped(bool $mapped): static + { + $this->builder->setMapped($mapped); + + return $this; + } + + public function setMethod(string $method): static + { + $this->builder->setMethod($method); + + return $this; + } + + /** + * @param string|PropertyPathInterface|null $propertyPath + */ + public function setPropertyPath($propertyPath): static + { + $this->builder->setPropertyPath($propertyPath); + + return $this; + } + + public function setRequired(bool $required): static + { + $this->builder->setRequired($required); + + return $this; + } + + public function setAction(?string $action): static + { + $this->builder->setAction($action); + + return $this; + } + + public function setCompound(bool $compound): static + { + $this->builder->setCompound($compound); + + return $this; + } + + public function setDataLocked(bool $locked): static + { + $this->builder->setDataLocked($locked); + + return $this; + } + + public function setFormFactory(FormFactoryInterface $formFactory): static + { + $this->builder->setFormFactory($formFactory); + + return $this; + } + + public function setType(?ResolvedFormTypeInterface $type): static + { + $this->builder->setType($type); + + return $this; + } + + public function setRequestHandler(?RequestHandlerInterface $requestHandler): static + { + $this->builder->setRequestHandler($requestHandler); + + return $this; + } + + public function getAttribute(string $name, mixed $default = null): mixed + { + return $this->builder->getAttribute($name, $default); + } + + public function hasAttribute(string $name): bool + { + return $this->builder->hasAttribute($name); + } + + public function getAttributes(): array + { + return $this->builder->getAttributes(); + } + + public function getDataMapper(): ?DataMapperInterface + { + return $this->builder->getDataMapper(); + } + + public function getEventDispatcher(): EventDispatcherInterface + { + return $this->builder->getEventDispatcher(); + } + + public function getName(): string + { + return $this->builder->getName(); + } + + public function getPropertyPath(): ?PropertyPathInterface + { + return $this->builder->getPropertyPath(); + } + + public function getRequestHandler(): RequestHandlerInterface + { + return $this->builder->getRequestHandler(); + } + + public function getType(): ResolvedFormTypeInterface + { + return $this->builder->getType(); + } + + public function setByReference(bool $byReference): static + { + $this->builder->setByReference($byReference); + + return $this; + } + + public function setData(mixed $data): static + { + $this->builder->setData($data); + + return $this; + } + + public function setAutoInitialize(bool $initialize): static + { + $this->builder->setAutoInitialize($initialize); + + return $this; + } + + public function getFormConfig(): FormConfigInterface + { + return $this->builder->getFormConfig(); + } + + public function setIsEmptyCallback(?callable $isEmptyCallback): static + { + $this->builder->setIsEmptyCallback($isEmptyCallback); + + return $this; + } + + public function getMapped(): bool + { + return $this->builder->getMapped(); + } + + public function getByReference(): bool + { + return $this->builder->getByReference(); + } + + public function getInheritData(): bool + { + return $this->builder->getInheritData(); + } + + public function getCompound(): bool + { + return $this->builder->getCompound(); + } + + public function getViewTransformers(): array + { + return $this->builder->getViewTransformers(); + } + + public function getModelTransformers(): array + { + return $this->builder->getModelTransformers(); + } + + public function getRequired(): bool + { + return $this->builder->getRequired(); + } + + public function getDisabled(): bool + { + return $this->builder->getDisabled(); + } + + public function getErrorBubbling(): bool + { + return $this->builder->getErrorBubbling(); + } + + public function getEmptyData(): mixed + { + return $this->builder->getEmptyData(); + } + + public function getData(): mixed + { + return $this->builder->getData(); + } + + public function getDataClass(): ?string + { + return $this->builder->getDataClass(); + } + + public function getDataLocked(): bool + { + return $this->builder->getDataLocked(); + } + + public function getFormFactory(): FormFactoryInterface + { + return $this->builder->getFormFactory(); + } + + public function getAction(): string + { + return $this->builder->getAction(); + } + + public function getMethod(): string + { + return $this->builder->getMethod(); + } + + public function getAutoInitialize(): bool + { + return $this->builder->getAutoInitialize(); + } + + public function getOptions(): array + { + return $this->builder->getOptions(); + } + + public function hasOption(string $name): bool + { + return $this->builder->hasOption($name); + } + + public function getOption(string $name, mixed $default = null): mixed + { + return $this->builder->getOption($name, $default); + } + + public function getIsEmptyCallback(): ?callable + { + return $this->builder->getIsEmptyCallback(); + } + + public function getIterator(): \Traversable + { + return $this->builder; + } +} \ No newline at end of file diff --git a/sources/FormType/Base/HiddenType.php b/sources/FormType/Base/HiddenType.php index cca49a178f..696520fd0c 100644 --- a/sources/FormType/Base/HiddenType.php +++ b/sources/FormType/Base/HiddenType.php @@ -6,7 +6,6 @@ namespace Combodo\iTop\FormType\Base; -use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\HiddenType as SymfonyHiddenType; class HiddenType extends AbstractType diff --git a/sources/FormType/Base/TextType.php b/sources/FormType/Base/TextType.php index 209a9fcf32..2b63cb6fd3 100644 --- a/sources/FormType/Base/TextType.php +++ b/sources/FormType/Base/TextType.php @@ -6,7 +6,6 @@ namespace Combodo\iTop\FormType\Base; -use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\TextType as SymfonyTextType; class TextType extends AbstractType diff --git a/sources/FormType/Base/TextareaType.php b/sources/FormType/Base/TextareaType.php index c5c7bb9f43..38020cc872 100644 --- a/sources/FormType/Base/TextareaType.php +++ b/sources/FormType/Base/TextareaType.php @@ -6,7 +6,6 @@ namespace Combodo\iTop\FormType\Base; -use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\TextareaType as SymfonyTextareaType; use Symfony\Component\OptionsResolver\OptionsResolver;