some advancements on dynamic socialaction filter

This commit is contained in:
Julie Lenaerts 2022-07-27 15:50:56 +02:00
parent cb42b68c33
commit ab2b2bc235
2 changed files with 235 additions and 1 deletions

View File

@ -0,0 +1,227 @@
<?php
namespace Chill\PersonBundle\Export\Filter;
use Chill\MainBundle\Export\FilterInterface;
use Chill\PersonBundle\Entity\SocialWork\Goal;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
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\PreSetDataEvent;
use Symfony\Component\Form\FormEvents;
class SocialWorkTypeFilter implements FilterInterface
{
private SocialActionRender $socialActionRender;
public function __construct(SocialActionRender $socialActionRender)
{
$this->socialActionRender = $socialActionRender;
}
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,
'choice_label' => function (SocialAction $sa) {
return $this->socialActionRender->renderString($sa, []);
},
'multiple' => true,
'expanded' => true
]);
$refreshGoals = function ($form, $actionType) {
$form->add('goal', EntityType::class, [
'class' => Goal::class,
'choice_label' => function (Goal $g) {
return $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) {
$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());
}
});
}
public function getTitle(): string
{
return 'Filter by type of action, objectives and results';
}
public function describeAction($data, $format = 'string'): array
{
$actionTypes = [];
$objectives = [];
$results = [];
foreach ($data['actionType'] as $at) {
$actionTypes[] = $at->getTitle();
}
foreach ($data['objectives'] as $o) {
$objectives[] = $o->getTitle();
}
foreach ($data['results'] as $r) {
$results[] = $r->getTitle();
}
return ['Filtered by referrers: only %actionTypes%', [
'%actionTypes%' => implode(', ou ', $actionTypes)
]];
}
public function addRole()
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('r', ':referrers');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('referrers', $data['referrers']);
}
public function applyOn(): string
{
return Declarations::SOCIAL_WORK_ACTION_TYPE;
}
}

View File

@ -12,4 +12,11 @@ services:
autowire: true
autoconfigure: true
tags:
- { name: chill.export_filter, alias: social_work_actions_referrer_filter }
- { name: chill.export_filter, alias: social_work_actions_referrer_filter }
chill.person.export.filter_social_work_type:
class: Chill\PersonBundle\Export\Filter\SocialWorkTypeFilter
autowire: true
autoconfigure: true
tags:
- { name: chill.export_filter, alias: social_work_type_filter }