mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-01 14:36:13 +00:00
trying to get dynamic form to work
This commit is contained in:
parent
208d625258
commit
0c2c364eb6
@ -3,6 +3,7 @@
|
|||||||
namespace Chill\PersonBundle\Export\Filter\SocialWorkFilters;
|
namespace Chill\PersonBundle\Export\Filter\SocialWorkFilters;
|
||||||
|
|
||||||
use Chill\MainBundle\Export\FilterInterface;
|
use Chill\MainBundle\Export\FilterInterface;
|
||||||
|
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||||
use Chill\PersonBundle\Entity\SocialWork\Goal;
|
use Chill\PersonBundle\Entity\SocialWork\Goal;
|
||||||
use Chill\PersonBundle\Export\Declarations;
|
use Chill\PersonBundle\Export\Declarations;
|
||||||
use Doctrine\ORM\Query\Expr\Andx;
|
use Doctrine\ORM\Query\Expr\Andx;
|
||||||
@ -12,117 +13,27 @@ use Symfony\Component\Form\FormBuilderInterface;
|
|||||||
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
|
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
|
||||||
use Chill\PersonBundle\Templating\Entity\SocialActionRender;
|
use Chill\PersonBundle\Templating\Entity\SocialActionRender;
|
||||||
use Doctrine\ORM\EntityRepository;
|
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\Event\PreSetDataEvent;
|
||||||
|
use Symfony\Component\Form\FormEvent;
|
||||||
use Symfony\Component\Form\FormEvents;
|
use Symfony\Component\Form\FormEvents;
|
||||||
|
use Symfony\Component\Form\FormInterface;
|
||||||
|
|
||||||
class SocialWorkTypeFilter implements FilterInterface
|
class SocialWorkTypeFilter implements FilterInterface
|
||||||
{
|
{
|
||||||
private SocialActionRender $socialActionRender;
|
private SocialActionRender $socialActionRender;
|
||||||
|
|
||||||
public function __construct(SocialActionRender $socialActionRender)
|
private TranslatableStringHelper $translatableStringHelper;
|
||||||
|
|
||||||
|
public function __construct(SocialActionRender $socialActionRender, TranslatableStringHelper $translatableStringHelper)
|
||||||
{
|
{
|
||||||
$this->socialActionRender = $socialActionRender;
|
$this->socialActionRender = $socialActionRender;
|
||||||
|
$this->translatableStringHelper = $translatableStringHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildForm(FormBuilderInterface $builder)
|
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, [
|
$builder->add('actionType', EntityType::class, [
|
||||||
'class' => SocialAction::class,
|
'class' => SocialAction::class,
|
||||||
@ -133,45 +44,39 @@ class SocialWorkTypeFilter implements FilterInterface
|
|||||||
'expanded' => true
|
'expanded' => true
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$refreshGoals = function ($form, $actionType) {
|
$refreshGoals = function (FormInterface $form, SocialAction $actionType = null) {
|
||||||
|
|
||||||
|
$goals = null === $actionType ? [] : $actionType->getGoals();
|
||||||
|
|
||||||
$form->add('goal', EntityType::class, [
|
$form->add('goal', EntityType::class, [
|
||||||
'class' => Goal::class,
|
'class' => Goal::class,
|
||||||
|
'placeholder' => '',
|
||||||
|
'choices' => $goals,
|
||||||
'choice_label' => function (Goal $g) {
|
'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();
|
$form = $event->getForm();
|
||||||
$data = $event->getData();
|
$data = $event->getData();
|
||||||
|
|
||||||
if ($data == null){
|
$refreshGoals($form, $data);
|
||||||
$refreshGoals($form, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($data instanceof SocialAction) {
|
|
||||||
$refreshGoals($form, $data->getGoals());
|
|
||||||
// $refreshCity($form, $data->getRegion()->getCities());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$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
|
public function getTitle(): string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user