mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-30 05:56:14 +00:00
agefilter added for persons
This commit is contained in:
parent
c72dc83c06
commit
ebb6ee4a41
@ -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');
|
||||
|
106
src/Bundle/ChillPersonBundle/Export/Filter/AgeFilter.php
Normal file
106
src/Bundle/ChillPersonBundle/Export/Filter/AgeFilter.php
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user