diff --git a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php index 61bce30f9..5b919a333 100644 --- a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php +++ b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php @@ -75,7 +75,7 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../config')); $loader->load('services.yaml'); $loader->load('services/widgets.yaml'); - $loader->load('services/exports.yaml'); + $loader->load('services/exports_person.yaml'); $loader->load('services/exports_social_actions.yaml'); $loader->load('services/fixtures.yaml'); $loader->load('services/controller.yaml'); diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AgeFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AgeFilter.php new file mode 100644 index 000000000..ef2bc2fa0 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AgeFilter.php @@ -0,0 +1,106 @@ +getDQLPart('where'); + + $min = null !== $data['min_age'] ? $data['min_age'] : 0; + $max = null !== $data['max_age'] ? $data['max_age'] : 200; + $calc = $data['date_calc']; + + $clause = $qb->expr()->andX( + $qb->expr()->gte('DATE_DIFF(:calc_date, person.birthdate)/365', + ':min_age' + ), + $qb->expr()->lte('DATE_DIFF(:calc_date, person.birthdate)/365', + ':max_age' + ) + ); + + if ($where instanceof Expr\Andx) { + $where->add($clause); + } else { + $where = $qb->expr()->andX($clause); + } + + $qb->add('where', $where); + $qb->setParameter('min_age', $min); + $qb->setParameter('max_age', $max); + $qb->setParameter('calc_date', $calc); + } + + public function applyOn() + { + return Declarations::PERSON_TYPE; + } + + public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder) + { + $builder->add('min_age', IntegerType::class, [ + 'label' => 'Minimum age', + ]); + + $builder->add('max_age', IntegerType::class, [ + 'label' => 'Maximum age', + ]); + + $builder->add('date_calc', DateType::class, [ + 'label' => 'Calculate age in relation to this date', + 'data' => new DateTime('now'), + 'attr' => ['class' => 'datepicker'], + 'widget' => 'single_text', + 'format' => 'dd-MM-yyyy', + ]); + } + + public function describeAction($data, $format = 'string') + { + return ['Filtered by person\'s age']; + } + + public function getTitle() + { + return 'Filter by person\'s age'; + } + + public function validateForm($data, ExecutionContextInterface $context) + { + $min = $data['min_age']; + $max = $data['max_age']; + + if ( + (null !== $min && null !== $max) + && $min >= $max + ) { + $context->buildViolation('The minimum age should be less than the maximum age.') + ->addViolation(); + } + } +} diff --git a/src/Bundle/ChillPersonBundle/config/services/exports.yaml b/src/Bundle/ChillPersonBundle/config/services/exports_person.yaml similarity index 89% rename from src/Bundle/ChillPersonBundle/config/services/exports.yaml rename to src/Bundle/ChillPersonBundle/config/services/exports_person.yaml index b00e6106d..e45930ddb 100644 --- a/src/Bundle/ChillPersonBundle/config/services/exports.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/exports_person.yaml @@ -24,12 +24,20 @@ services: tags: - { name: chill.export, alias: list_person_duplicate } +# FILTERS chill.person.export.filter_gender: class: Chill\PersonBundle\Export\Filter\GenderFilter - arguments: - $translator: '@translator' + autowire: true + autoconfigure: true tags: - { name: chill.export_filter, alias: person_gender_filter } + + chill.person.export.filter_age: + class: Chill\PersonBundle\Export\Filter\AgeFilter + autowire: true + autoconfigure: true + tags: + - { name: chill.export_filter, alias: person_age_filter } chill.person.export.filter_birthdate: class: Chill\PersonBundle\Export\Filter\BirthdateFilter @@ -42,7 +50,8 @@ services: autoconfigure: true tags: - { name: chill.export_filter, alias: person_nationality_filter } - + +# AGGREGATORS chill.person.export.aggregator_nationality: class: Chill\PersonBundle\Export\Aggregator\NationalityAggregator autowire: true