From 0c2c364eb668936b25eb9477201e713208ce3e07 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 10 Aug 2022 13:58:51 +0200 Subject: [PATCH] trying to get dynamic form to work --- .../SocialWorkTypeFilter.php | 159 ++++-------------- 1 file changed, 32 insertions(+), 127 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/SocialWorkFilters/SocialWorkTypeFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/SocialWorkFilters/SocialWorkTypeFilter.php index bf28fb695..b5b8fbfd1 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/SocialWorkFilters/SocialWorkTypeFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/SocialWorkFilters/SocialWorkTypeFilter.php @@ -3,6 +3,7 @@ namespace Chill\PersonBundle\Export\Filter\SocialWorkFilters; use Chill\MainBundle\Export\FilterInterface; +use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Entity\SocialWork\Goal; use Chill\PersonBundle\Export\Declarations; use Doctrine\ORM\Query\Expr\Andx; @@ -12,117 +13,27 @@ use Symfony\Component\Form\FormBuilderInterface; use Chill\PersonBundle\Entity\SocialWork\SocialAction; use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Doctrine\ORM\EntityRepository; +use Symfony\Component\Form\Event\PostSetDataEvent; +use Symfony\Component\Form\Event\PostSubmitEvent; use Symfony\Component\Form\Event\PreSetDataEvent; +use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormInterface; class SocialWorkTypeFilter implements FilterInterface { private SocialActionRender $socialActionRender; - public function __construct(SocialActionRender $socialActionRender) + private TranslatableStringHelper $translatableStringHelper; + + public function __construct(SocialActionRender $socialActionRender, TranslatableStringHelper $translatableStringHelper) { $this->socialActionRender = $socialActionRender; + $this->translatableStringHelper = $translatableStringHelper; } public function buildForm(FormBuilderInterface $builder) { - /** - * EXAMPLE CODE TO INSPIRE - */ - // parent::buildForm($builder, $options); - // //other fields - - // $builder->add('country', 'entity', array( - // 'class' => 'Orfos\CoreBundle\Entity\Country', - // 'property' => $locale . 'name', - // 'label' => 'register.country.label', - // 'query_builder' => function(EntityRepository $er) { - // return $er->createQueryBuilder('c') - // ->select('c', 't') - // ->join('c.translations', 't'); - // }, - // )); - - // $factory = $builder->getFormFactory(); - // $refreshRegion = function ($form, $country) use ($factory, $locale) { - // $form->add($factory->createNamed('entity', 'region', null, array( - // 'class' => 'Orfos\CoreBundle\Entity\Region', - // 'property' => $locale . 'name', - // 'label' => 'register.region.label', - // 'query_builder' => function (EntityRepository $repository) use ($country) { - // $qb = $repository->createQueryBuilder('region') - // ->select('region', 'translation') - // ->innerJoin('region.country', 'country') - // ->join('region.translations', 'translation'); - - // if ($country instanceof Country) { - // $qb = $qb->where('region.country = :country') - // ->setParameter('country', $country); - // } elseif (is_numeric($country)) { - // $qb = $qb->where('country.id = :country_id') - // ->setParameter('country_id', $country); - // } else { - // $qb = $qb->where('country.id = 1'); - // } - - // return $qb; - // } - // ))); - // }; - // $factory = $builder->getFormFactory(); - // $refreshCity = function($form, $region) use ($factory, $locale) { - // $form->add($factory->createNamed('entity', 'city', null, array( - // 'class' => 'Orfos\CoreBundle\Entity\City', - // 'property' => $locale . 'name', - // 'label' => 'register.city.label', - // 'query_builder' => function (EntityRepository $repository) use ($region) { - // $qb = $repository->createQueryBuilder('city') - // ->select('city', 'translation') - // ->innerJoin('city.region', 'region') - // ->innerJoin('city.translations', 'translation'); - - // if ($region instanceof Region) { - // $qb = $qb->where('city.region = :region') - // ->setParameter('region', $region); - // } elseif (is_numeric($region)) { - // $qb = $qb->where('region.id = :region_id') - // ->setParameter('region_id', $region); - // } else { - // $qb = $qb->where('region.id = 1'); - // } - - // return $qb; - // } - // ))); - // }; - - // $builder->addEventListener(FormEvents::PRE_SET_DATA, function (DataEvent $event) use ($refreshRegion, $refreshCity) { - // $form = $event->getForm(); - // $data = $event->getData(); - - // if ($data == null){ - // $refreshRegion($form, null); - // $refreshCity($form, null); - // } - - // if ($data instanceof Country) { - // $refreshRegion($form, $data->getCountry()->getRegions()); - // $refreshCity($form, $data->getRegion()->getCities()); - // } - // }); - - // $builder->addEventListener(FormEvents::PRE_BIND, function (DataEvent $event) use ($refreshRegion, $refreshCity) { - // $form = $event->getForm(); - // $data = $event->getData(); - - // if (array_key_exists('country', $data)) { - // $refreshRegion($form, $data['country']); - // } - // if (array_key_exists('region', $data)) { - // $refreshCity($form, $data['region']); - // } - // }); - $builder->add('actionType', EntityType::class, [ 'class' => SocialAction::class, @@ -133,45 +44,39 @@ class SocialWorkTypeFilter implements FilterInterface 'expanded' => true ]); - $refreshGoals = function ($form, $actionType) { + $refreshGoals = function (FormInterface $form, SocialAction $actionType = null) { + + $goals = null === $actionType ? [] : $actionType->getGoals(); + $form->add('goal', EntityType::class, [ 'class' => Goal::class, + 'placeholder' => '', + 'choices' => $goals, 'choice_label' => function (Goal $g) { - return $g->getTitle(); + return $this->translatableStringHelper->localize($g->getTitle()); }, - 'query_builder' => function (EntityRepository $repository) use ($actionType) { - $qb = $repository->createQueryBuilder('g') - ->select('g') - ->join('g.socialActions', 'socialActions'); - - if ($actionType instanceof SocialAction) { - $qb = $qb->where( - $qb->expr()->andX( - $qb->expr()->in('g', ':socialActions') - )) - ->setParameter('country', $actionType); - } - - dump($qb->getQuery()->getResult()); - - return $qb; - } ]); - }; + }; - $builder->addEventListener(FormEvents::PRE_SET_DATA, function (PreSetDataEvent $event) use ($refreshGoals) { + $builder->addEventListener(FormEvents::POST_SUBMIT, function (PostSubmitEvent $event) use ($refreshGoals) { + dump($event); $form = $event->getForm(); $data = $event->getData(); - if ($data == null){ - $refreshGoals($form, null); - } - - if ($data instanceof SocialAction) { - $refreshGoals($form, $data->getGoals()); - // $refreshCity($form, $data->getRegion()->getCities()); - } + $refreshGoals($form, $data); + }); + + $builder->get('actionType')->addEventListener( + FormEvents::POST_SUBMIT, + function (FormEvent $event) use ($refreshGoals) { + $actionType = $event->getForm()->getData(); + dump('after submit listener'); + dump($actionType); + + $refreshGoals($event->getForm()->getParent(), $actionType); + } + ); } public function getTitle(): string