diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/SocialWorkTypeFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/SocialWorkTypeFilter.php new file mode 100644 index 000000000..8f8d70349 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Export/Filter/SocialWorkTypeFilter.php @@ -0,0 +1,227 @@ +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; + } +} \ No newline at end of file diff --git a/src/Bundle/ChillPersonBundle/config/services/exports_social_actions.yaml b/src/Bundle/ChillPersonBundle/config/services/exports_social_actions.yaml index 8936af9b9..cdf4f50d4 100644 --- a/src/Bundle/ChillPersonBundle/config/services/exports_social_actions.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/exports_social_actions.yaml @@ -12,4 +12,11 @@ services: autowire: true autoconfigure: true tags: - - { name: chill.export_filter, alias: social_work_actions_referrer_filter } \ No newline at end of file + - { 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 } \ No newline at end of file