diff --git a/Export/Aggregator/AgeAggregator.php b/Export/Aggregator/AgeAggregator.php new file mode 100644 index 000000000..ba84bfa13 --- /dev/null +++ b/Export/Aggregator/AgeAggregator.php @@ -0,0 +1,85 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\PersonBundle\Export\Aggregator; + +use Chill\MainBundle\Export\AggregatorInterface; +use Doctrine\ORM\QueryBuilder; +use Symfony\Component\Form\Extension\Core\Type\DateType; + +/** + * + * + * @author Julien Fastré + */ +class AgeAggregator implements AggregatorInterface +{ + + public function addRole() + { + return null; + } + + public function alterQuery(QueryBuilder $qb, $data) + { + $qb->addSelect('DATE_DIFF(:date_age_calculation, person.birthdate)/365 as person_age'); + $qb->setParameter('date_age_calculation', $data['date_age_calculation']); + $qb->addGroupBy('person_age'); + } + + public function applyOn() + { + return 'person'; + } + + public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder) + { + $builder->add('date_age_calculation', DateType::class, array( + 'label' => "Calculate age in relation to this date", + 'data' => new \DateTime(), + 'attr' => array('class' => 'datepicker'), + 'widget'=> 'single_text', + 'format' => 'dd-MM-yyyy' + )); + } + + public function getLabels($key, array $values, $data) + { + return function($value) { + if ($value === '_header') { + return "Age"; + } + + return $value; + }; + } + + public function getQueryKeys($data) + { + return array( + 'person_age' + ); + } + + public function getTitle() + { + return "Aggregate by age"; + } + +} diff --git a/Resources/config/services/exports.yml b/Resources/config/services/exports.yml index c306755db..7416d5cbf 100644 --- a/Resources/config/services/exports.yml +++ b/Resources/config/services/exports.yml @@ -42,3 +42,8 @@ services: - "@translator" tags: - { name: chill.export_aggregator, alias: person_gender_aggregator } + + chill.person.export.aggregator_age: + class: Chill\PersonBundle\Export\Aggregator\AgeAggregator + tags: + - { name: chill.export_aggregator, alias: person_age_aggregator } diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index 643107d51..8be23ca04 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -161,3 +161,6 @@ Group by continents: Grouper par continent Group by country: Grouper par pays Group people by gender: Aggréger les personnes par genre + +Aggregate by age: Aggréger par âge +Calculate age in relation to this date: Calculer l'âge par rapport à cette date diff --git a/Tests/Export/Aggregator/AgeAggregatorTest.php b/Tests/Export/Aggregator/AgeAggregatorTest.php new file mode 100644 index 000000000..690f873e7 --- /dev/null +++ b/Tests/Export/Aggregator/AgeAggregatorTest.php @@ -0,0 +1,76 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\PersonBundle\Tests\Export\Aggregator; + +use Chill\MainBundle\Test\Export\AbstractAggregatorTest; + +/** + * + * + * @author Julien Fastré + */ +class AgeAggregatorTest extends AbstractAggregatorTest +{ + /** + * + * @var \Chill\PersonBundle\Export\Aggregator\AgeAggregator + */ + private $aggregator; + + public function setUp() + { + static::bootKernel(); + + $container = static::$kernel->getContainer(); + + $this->aggregator = $container->get('chill.person.export.aggregator_age'); + } + + public function getAggregator() + { + return $this->aggregator; + } + + public function getFormData() + { + return array( + array( + 'date_age_calculation' => \DateTime::createFromFormat('Y-m-d','2016-06-16') + ) + ); + } + + public function getQueryBuilders() + { + if (static::$kernel === null) { + static::bootKernel(); + } + + $em = static::$kernel->getContainer() + ->get('doctrine.orm.entity_manager'); + + return array( + $em->createQueryBuilder() + ->select('count(person.id)') + ->from('ChillPersonBundle:Person', 'person') + ); + } + +}