From fab03d7c3386ddb4fd8f466b7c601653906e6f60 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 19 Jun 2025 13:35:17 +0200 Subject: [PATCH] WIP change animator field --- .../Controller/EventController.php | 5 +- .../ChillEventBundle/Entity/Animator.php | 127 ++++++++++++++++++ src/Bundle/ChillEventBundle/Entity/Event.php | 14 +- .../ChillEventBundle/Form/EventType.php | 17 ++- .../Form/Type/PickAnimatorType.php | 56 ++++++++ .../config/services/forms.yaml | 4 + .../migrations/Version20250619102259.php | 63 +++++++++ 7 files changed, 272 insertions(+), 14 deletions(-) create mode 100644 src/Bundle/ChillEventBundle/Entity/Animator.php create mode 100644 src/Bundle/ChillEventBundle/Form/Type/PickAnimatorType.php create mode 100644 src/Bundle/ChillEventBundle/migrations/Version20250619102259.php diff --git a/src/Bundle/ChillEventBundle/Controller/EventController.php b/src/Bundle/ChillEventBundle/Controller/EventController.php index d4370db90..38fcc357f 100644 --- a/src/Bundle/ChillEventBundle/Controller/EventController.php +++ b/src/Bundle/ChillEventBundle/Controller/EventController.php @@ -221,9 +221,6 @@ final class EventController extends AbstractController { $role = 'CHILL_EVENT_CREATE'; - /** - * @var Center $centers - */ $centers = $this->authorizationHelper->getReachableCenters($this->getUser(), $role); if (1 === \count($centers)) { @@ -243,7 +240,7 @@ final class EventController extends AbstractController ->add('center_id', EntityType::class, [ 'class' => Center::class, 'choices' => $centers, - 'placeholder' => '', + 'placeholder' => $this->translator->trans('Pick a center'), 'label' => 'To which centre should the event be associated ?', ]) ->add('submit', SubmitType::class, [ diff --git a/src/Bundle/ChillEventBundle/Entity/Animator.php b/src/Bundle/ChillEventBundle/Entity/Animator.php new file mode 100644 index 000000000..1f9557fe6 --- /dev/null +++ b/src/Bundle/ChillEventBundle/Entity/Animator.php @@ -0,0 +1,127 @@ +id; + } + + public function getThirdparty(): ?ThirdParty + { + return $this->thirdparty; + } + + public function setThirdparty(?ThirdParty $thirdparty): self + { + $this->thirdparty = $thirdparty; + + if (null !== $thirdparty) { + $this->user = null; + } + + return $this; + } + + public function getUser(): ?User + { + return $this->user; + } + + public function setUser(?User $user): self + { + $this->user = $user; + + if (null !== $user) { + $this->thirdparty = null; + } + + return $this; + } + + public function getEvent(): ?Event + { + return $this->event; + } + + public function setEvent(?Event $event): self + { + $this->event = $event; + return $this; + } + + public function getAnimator(): User|ThirdParty|null + { + return $this->user ?? $this->thirdparty; + } + + public function setAnimator(User|ThirdParty|null $animator): self + { + if ($animator instanceof User) { + $this->setUser($animator); + } elseif ($animator instanceof ThirdParty) { + $this->setThirdparty($animator); + } else { + $this->user = null; + $this->thirdparty = null; + } + + return $this; + } + + public function isUser(): bool + { + return null !== $this->user; + } + + public function isThirdParty(): bool + { + return null !== $this->thirdparty; + } +} diff --git a/src/Bundle/ChillEventBundle/Entity/Event.php b/src/Bundle/ChillEventBundle/Entity/Event.php index a7e366eac..772310927 100644 --- a/src/Bundle/ChillEventBundle/Entity/Event.php +++ b/src/Bundle/ChillEventBundle/Entity/Event.php @@ -61,11 +61,9 @@ class Event implements HasCenterInterface, HasScopeInterface, TrackCreationInter private ?User $moderator = null; /** - * @var Collection + * @var Collection */ - #[ORM\ManyToMany(targetEntity: ThirdParty::class)] - #[Serializer\Groups(['read'])] - #[ORM\JoinTable('chill_event_thirdparty')] + #[ORM\OneToMany(mappedBy: 'event', targetEntity: Animator::class)] private Collection $animators; #[Assert\NotBlank] @@ -194,16 +192,16 @@ class Event implements HasCenterInterface, HasScopeInterface, TrackCreationInter return $this->animators; } - public function addAnimator(ThirdParty $tp): self + public function addAnimator(ThirdParty|User $a): self { - $this->animators->add($tp); + $this->animators->add($a); return $this; } - public function removeAnimator(ThirdParty $tp): void + public function removeAnimator(ThirdParty|User $a): void { - $this->animators->removeElement($tp); + $this->animators->removeElement($a); } public function getCenter(): Center diff --git a/src/Bundle/ChillEventBundle/Form/EventType.php b/src/Bundle/ChillEventBundle/Form/EventType.php index a6c421d04..2e1e95946 100644 --- a/src/Bundle/ChillEventBundle/Form/EventType.php +++ b/src/Bundle/ChillEventBundle/Form/EventType.php @@ -15,6 +15,7 @@ use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Form\StoredObjectType; use Chill\EventBundle\Entity\BudgetTypeEnum; use Chill\EventBundle\Entity\Event; +use Chill\EventBundle\Form\Type\PickAnimatorType; use Chill\EventBundle\Form\Type\PickEventThemeType; use Chill\EventBundle\Form\Type\PickEventTypeType; use Chill\EventBundle\Repository\EventBudgetKindRepository; @@ -67,8 +68,20 @@ class EventType extends AbstractType ->add('moderator', PickUserDynamicType::class, [ 'label' => 'Pick a moderator', ]) - ->add('animators', PickThirdpartyDynamicType::class, [ - 'multiple' => true, + ->add('animators', ChillCollectionType::class, [ + 'entry_type' => PickAnimatorType::class, + 'entry_options' => [ + 'label' => false, + ], + 'allow_add' => true, + 'allow_delete' => true, + 'by_reference' => false, + 'prototype' => true, + 'label' => 'Animators', + 'help' => 'Add users or third parties who will animate this event', + 'attr' => [ + 'data-collection' => 'true', // For JS handling + ], ]) ->add('budgetElements', ChillCollectionType::class, [ 'entry_type' => AddEventBudgetElementType::class, diff --git a/src/Bundle/ChillEventBundle/Form/Type/PickAnimatorType.php b/src/Bundle/ChillEventBundle/Form/Type/PickAnimatorType.php new file mode 100644 index 000000000..5b3d4ce20 --- /dev/null +++ b/src/Bundle/ChillEventBundle/Form/Type/PickAnimatorType.php @@ -0,0 +1,56 @@ +add('thirdparty', PickThirdpartyDynamicType::class, [ + 'required' => false, + ]) + ->add('user', PickUserDynamicType::class, [ + 'required' => false, + ]); + + // Add the data transformer +// $builder->addModelTransformer(new AnimatorCollectionTransformer($this->entityManager)); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => null, + 'multiple' => false, + 'placeholder' => 'Select an animator...', + 'required' => false, + 'by_reference' => false, + ]); + + $resolver->setAllowedTypes('multiple', 'bool'); + $resolver->setAllowedTypes('placeholder', ['string', 'null']); + } +} diff --git a/src/Bundle/ChillEventBundle/config/services/forms.yaml b/src/Bundle/ChillEventBundle/config/services/forms.yaml index 6b8e68dc3..ab5d788f8 100644 --- a/src/Bundle/ChillEventBundle/config/services/forms.yaml +++ b/src/Bundle/ChillEventBundle/config/services/forms.yaml @@ -52,4 +52,8 @@ services: tags: - { name: form.type } + Chill\EventBundle\Form\Type\PickAnimatorType: + tags: + - { name: form.type } + diff --git a/src/Bundle/ChillEventBundle/migrations/Version20250619102259.php b/src/Bundle/ChillEventBundle/migrations/Version20250619102259.php new file mode 100644 index 000000000..a9eb88caf --- /dev/null +++ b/src/Bundle/ChillEventBundle/migrations/Version20250619102259.php @@ -0,0 +1,63 @@ +addSql(<<<'SQL' + CREATE SEQUENCE chill_event_animator_id_seq INCREMENT BY 1 MINVALUE 1 START 1 + SQL); + $this->addSql(<<<'SQL' + CREATE TABLE chill_event_animator (id INT NOT NULL, thirdparty_id INT DEFAULT NULL, user_id INT DEFAULT NULL, event_id INT NOT NULL, PRIMARY KEY(id)) + SQL); + $this->addSql(<<<'SQL' + CREATE INDEX IDX_A7FA35FEC7D3A8E6 ON chill_event_animator (thirdparty_id) + SQL); + $this->addSql(<<<'SQL' + CREATE INDEX IDX_A7FA35FEA76ED395 ON chill_event_animator (user_id) + SQL); + $this->addSql(<<<'SQL' + CREATE INDEX IDX_A7FA35FE71F7E88B ON chill_event_animator (event_id) + SQL); + $this->addSql(<<<'SQL' + ALTER TABLE chill_event_animator ADD CONSTRAINT FK_A7FA35FEC7D3A8E6 FOREIGN KEY (thirdparty_id) REFERENCES chill_3party.third_party (id) NOT DEFERRABLE INITIALLY IMMEDIATE + SQL); + $this->addSql(<<<'SQL' + ALTER TABLE chill_event_animator ADD CONSTRAINT FK_A7FA35FEA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE + SQL); + $this->addSql(<<<'SQL' + ALTER TABLE chill_event_animator ADD CONSTRAINT FK_A7FA35FE71F7E88B FOREIGN KEY (event_id) REFERENCES chill_event_event (id) NOT DEFERRABLE INITIALLY IMMEDIATE + SQL); + } + + public function down(Schema $schema): void + { + $this->addSql(<<<'SQL' + DROP SEQUENCE chill_event_animator_id_seq CASCADE + SQL); + $this->addSql(<<<'SQL' + ALTER TABLE chill_event_animator DROP CONSTRAINT FK_A7FA35FEC7D3A8E6 + SQL); + $this->addSql(<<<'SQL' + ALTER TABLE chill_event_animator DROP CONSTRAINT FK_A7FA35FEA76ED395 + SQL); + $this->addSql(<<<'SQL' + ALTER TABLE chill_event_animator DROP CONSTRAINT FK_A7FA35FE71F7E88B + SQL); + $this->addSql(<<<'SQL' + DROP TABLE chill_event_animator + SQL); + } +}