agefilter added for persons

This commit is contained in:
Julie Lenaerts 2022-08-02 14:39:09 +02:00
parent c72dc83c06
commit ebb6ee4a41
3 changed files with 119 additions and 4 deletions

View File

@ -75,7 +75,7 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../config')); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../config'));
$loader->load('services.yaml'); $loader->load('services.yaml');
$loader->load('services/widgets.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/exports_social_actions.yaml');
$loader->load('services/fixtures.yaml'); $loader->load('services/fixtures.yaml');
$loader->load('services/controller.yaml'); $loader->load('services/controller.yaml');

View File

@ -0,0 +1,106 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Export\Filter;
use Chill\MainBundle\Export\ExportElementValidatedInterface;
use Chill\MainBundle\Export\FilterInterface;
use Chill\PersonBundle\Export\Declarations;
use DateTime;
use Doctrine\ORM\Query\Expr;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class AgeFilter implements ExportElementValidatedInterface, FilterInterface
{
public function addRole()
{
return null;
}
public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data)
{
$where = $qb->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();
}
}
}

View File

@ -24,12 +24,20 @@ services:
tags: tags:
- { name: chill.export, alias: list_person_duplicate } - { name: chill.export, alias: list_person_duplicate }
# FILTERS
chill.person.export.filter_gender: chill.person.export.filter_gender:
class: Chill\PersonBundle\Export\Filter\GenderFilter class: Chill\PersonBundle\Export\Filter\GenderFilter
arguments: autowire: true
$translator: '@translator' autoconfigure: true
tags: tags:
- { name: chill.export_filter, alias: person_gender_filter } - { 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: chill.person.export.filter_birthdate:
class: Chill\PersonBundle\Export\Filter\BirthdateFilter class: Chill\PersonBundle\Export\Filter\BirthdateFilter
@ -42,7 +50,8 @@ services:
autoconfigure: true autoconfigure: true
tags: tags:
- { name: chill.export_filter, alias: person_nationality_filter } - { name: chill.export_filter, alias: person_nationality_filter }
# AGGREGATORS
chill.person.export.aggregator_nationality: chill.person.export.aggregator_nationality:
class: Chill\PersonBundle\Export\Aggregator\NationalityAggregator class: Chill\PersonBundle\Export\Aggregator\NationalityAggregator
autowire: true autowire: true