From bc555ce3f50a75c1a24942d1a08a4e8458f9d60d Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 29 Apr 2025 10:03:42 +0200 Subject: [PATCH] Add field in event for themes --- .../ChillEventBundle/Entity/EventTheme.php | 5 +-- .../ChillEventBundle/Form/EventType.php | 11 +++++ .../Form/Type/PickEventThemeType.php | 45 +++++++++++++++++++ .../Form/Type/PickEventTypeType.php | 5 +-- .../Repository/EventThemeRepository.php | 38 ++++++++++++++++ .../Templating/Entity/EventThemeRender.php | 8 ++-- .../config/services/forms.yaml | 8 +++- 7 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 src/Bundle/ChillEventBundle/Form/Type/PickEventThemeType.php create mode 100644 src/Bundle/ChillEventBundle/Repository/EventThemeRepository.php diff --git a/src/Bundle/ChillEventBundle/Entity/EventTheme.php b/src/Bundle/ChillEventBundle/Entity/EventTheme.php index 902fc32fb..28bc87e04 100644 --- a/src/Bundle/ChillEventBundle/Entity/EventTheme.php +++ b/src/Bundle/ChillEventBundle/Entity/EventTheme.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Chill\EventBundle\Entity; +use Chill\EventBundle\Repository\EventThemeRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; @@ -19,8 +20,8 @@ use Doctrine\ORM\Mapping as ORM; /** * Class EventTheme. */ -#[ORM\Entity] #[ORM\HasLifecycleCallbacks] +#[ORM\Entity(repositoryClass: EventThemeRepository::class)] #[ORM\Table(name: 'chill_event_event_theme')] class EventTheme { @@ -82,7 +83,6 @@ class EventTheme /** * Set active. * - * @param bool $active * @return EventTheme */ public function setIsActive(bool $active): static @@ -95,7 +95,6 @@ class EventTheme /** * Set label. * - * @param array $label * @return EventTheme */ public function setName(array $label): static diff --git a/src/Bundle/ChillEventBundle/Form/EventType.php b/src/Bundle/ChillEventBundle/Form/EventType.php index ebdf66010..77844aaba 100644 --- a/src/Bundle/ChillEventBundle/Form/EventType.php +++ b/src/Bundle/ChillEventBundle/Form/EventType.php @@ -14,7 +14,10 @@ namespace Chill\EventBundle\Form; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Form\StoredObjectType; use Chill\EventBundle\Entity\Event; +use Chill\EventBundle\Entity\EventTheme; +use Chill\EventBundle\Form\Type\PickEventThemeType; use Chill\EventBundle\Form\Type\PickEventTypeType; +use Chill\EventBundle\Repository\EventThemeRepository; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Form\Type\ChillCollectionType; use Chill\MainBundle\Form\Type\ChillDateTimeType; @@ -22,14 +25,19 @@ use Chill\MainBundle\Form\Type\CommentType; use Chill\MainBundle\Form\Type\PickUserDynamicType; use Chill\MainBundle\Form\Type\PickUserLocationType; use Chill\MainBundle\Form\Type\ScopePickerType; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; +use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\MoneyType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Contracts\Translation\TranslatorInterface; class EventType extends AbstractType { + public function __construct(private readonly EventThemeRepository $eventThemeRepository, private readonly TranslatorInterface $translator, private readonly TranslatableStringHelperInterface $translatableStringHelper) {} + public function buildForm(FormBuilderInterface $builder, array $options) { $builder @@ -49,6 +57,9 @@ class EventType extends AbstractType 'class' => '', ], ]) + ->add('themes', PickEventThemeType::class, [ + 'multiple' => true, + ]) ->add('moderator', PickUserDynamicType::class, [ 'label' => 'Pick a moderator', ]) diff --git a/src/Bundle/ChillEventBundle/Form/Type/PickEventThemeType.php b/src/Bundle/ChillEventBundle/Form/Type/PickEventThemeType.php new file mode 100644 index 000000000..3010df271 --- /dev/null +++ b/src/Bundle/ChillEventBundle/Form/Type/PickEventThemeType.php @@ -0,0 +1,45 @@ +setDefaults([ + 'class' => EventTheme::class, + 'choices' => $this->eventThemeRepository->findByActiveOrdered(), + 'choice_label' => fn (EventTheme $et) => $this->eventThemeRender->renderString($et, []), + 'placeholder' => 'event.form.Select one or more themes', + 'required' => true, + 'attr' => ['class' => 'select2'], + 'label' => 'event.theme.label', + 'multiple' => false, + ]) + ->setAllowedTypes('multiple', ['bool']); + } + + public function getParent(): string + { + return EntityType::class; + } +} diff --git a/src/Bundle/ChillEventBundle/Form/Type/PickEventTypeType.php b/src/Bundle/ChillEventBundle/Form/Type/PickEventTypeType.php index d027c05cf..472fdd463 100644 --- a/src/Bundle/ChillEventBundle/Form/Type/PickEventTypeType.php +++ b/src/Bundle/ChillEventBundle/Form/Type/PickEventTypeType.php @@ -23,10 +23,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; */ class PickEventTypeType extends AbstractType { - /** - * @var TranslatableStringHelper - */ - protected $translatableStringHelper; + protected TranslatableStringHelper $translatableStringHelper; public function __construct(TranslatableStringHelper $helper) { diff --git a/src/Bundle/ChillEventBundle/Repository/EventThemeRepository.php b/src/Bundle/ChillEventBundle/Repository/EventThemeRepository.php new file mode 100644 index 000000000..c8956356b --- /dev/null +++ b/src/Bundle/ChillEventBundle/Repository/EventThemeRepository.php @@ -0,0 +1,38 @@ + + */ +class EventThemeRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, EventTheme::class); + } + + public function findByActiveOrdered(): array + { + return $this->createQueryBuilder('t') + ->select('t') + ->where('t.isActive = True') + ->orderBy('t.ordering', 'ASC') + ->getQuery() + ->getResult(); + } + +} diff --git a/src/Bundle/ChillEventBundle/Templating/Entity/EventThemeRender.php b/src/Bundle/ChillEventBundle/Templating/Entity/EventThemeRender.php index ad765d714..6a10b61f7 100644 --- a/src/Bundle/ChillEventBundle/Templating/Entity/EventThemeRender.php +++ b/src/Bundle/ChillEventBundle/Templating/Entity/EventThemeRender.php @@ -47,18 +47,18 @@ class EventThemeRender implements ChillEntityRenderInterface * @throws SyntaxError * @throws LoaderError */ - public function renderBox($eventTheme, array $options): string + public function renderBox($entity, array $options): string { $options = array_merge(self::DEFAULT_ARGS, $options); // give some help to twig: an array of parents - $parents = $this->buildParents($eventTheme); + $parents = $this->buildParents($entity); return $this ->engine ->render( '@ChillEvent/Entity/event_theme.html.twig', [ - 'eventTheme' => $eventTheme, + 'eventTheme' => $entity, 'parents' => $parents, 'options' => $options, ] @@ -76,7 +76,7 @@ class EventThemeRender implements ChillEntityRenderInterface while ($entity->hasParent()) { $entity = $entity->getParent(); $titles[] = $this->translatableStringHelper->localize( - $entity->getTitle() + $entity->getName() ); } diff --git a/src/Bundle/ChillEventBundle/config/services/forms.yaml b/src/Bundle/ChillEventBundle/config/services/forms.yaml index 205307234..9264a09ee 100644 --- a/src/Bundle/ChillEventBundle/config/services/forms.yaml +++ b/src/Bundle/ChillEventBundle/config/services/forms.yaml @@ -32,6 +32,12 @@ services: tags: - { name: form.type } - Chill\EventBundle\Form\EventThemeType: + Chill\EventBundle\Form\Type\PickEventThemeType: tags: - { name: form.type } + + Chill\EventBundle\Form\EventType: + tags: + - { name: form.type } + +