From 2711fcee6d9cde16be08a99f623c90f6272333dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 2 Jan 2016 16:46:11 +0100 Subject: [PATCH] [export] add a first export and first filters - nationality aggregator (work in progress: we have to set the feature 'group by continent') - gender filter - nationality filter - gender filter --- Export/Aggregator/NationalityAggregator.php | 362 ++++++++++++++++++++ Export/Export/CountPerson.php | 79 +++++ Export/Filter/GenderFilter.php | 81 +++++ Export/Filter/NationalityFilter.php | 66 ++++ Resources/config/services.yml | 21 ++ 5 files changed, 609 insertions(+) create mode 100644 Export/Aggregator/NationalityAggregator.php create mode 100644 Export/Export/CountPerson.php create mode 100644 Export/Filter/GenderFilter.php create mode 100644 Export/Filter/NationalityFilter.php diff --git a/Export/Aggregator/NationalityAggregator.php b/Export/Aggregator/NationalityAggregator.php new file mode 100644 index 000000000..1c08d0111 --- /dev/null +++ b/Export/Aggregator/NationalityAggregator.php @@ -0,0 +1,362 @@ + + * + * 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 Symfony\Component\Form\FormBuilderInterface; +use Doctrine\ORM\QueryBuilder; + +/** + * + * + * @author Julien Fastré + */ +class NationalityAggregator implements AggregatorInterface +{ + + const EUROPE_COUNTRY_CODE = array('BE', 'FR'); + + public function applyOn() + { + return 'person'; + } + + + public function buildForm(FormBuilderInterface $builder) + { + $builder->add('group_by_level', 'choice', array( + 'choices' => array( + 'Group by continents' => 'continent', + 'Group by country' => 'country' + ), + 'choices_as_values' => true, + 'expanded' => true, + 'multiple' => false + )); + + } + + public function alterQuery(QueryBuilder $qb, $data) + { + // add a clause in select part + if ($data['group_by_level'] === 'country') { + $qb->addSelect('nationality.countryCode as nationality_aggregator'); + } elseif ($data['group_by_level'] === 'continent') { + $clause = 'CASE ' + . 'WHEN nationality.countryCode IN(:europe_country_codes) THEN \'europe\' ' + . 'ELSE \'other\' ' + . 'END as nationality_aggregator '; + $qb->addSelect($clause); + $qb->setParameter('europe_country_codes', self::EUROPE_COUNTRY_CODE); + } + + + $qb->leftJoin('person.nationality', 'nationality'); + + // add group by + $groupBy = $qb->getDQLPart('groupBy'); + + if (!empty($groupBy)) { + $qb->addGroupBy('nationality_aggregator'); + } else { + $qb->groupBy('nationality_aggregator'); + } + + } + + public function getTitle() + { + return "Group people by nationality"; + } + + public static function getCountryData() + { + // this list is extracted by https://en.wikipedia.org/wiki/List_of_sovereign_states_and_dependent_territories_by_continent_%28data_file%29 + // source : + // Wikipedia contributors, "List of sovereign states and dependent territories by continent (data file)," + // Wikipedia, The Free Encyclopedia, https://en.wikipedia.org/w/index.php?title=List_of_sovereign_states_and_dependent_territories_by_continent_(data_file)&oldid=688980440 + // (accessed January 2, 2016). + return << + * + * 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\Export; + +use Chill\MainBundle\Export\ExportInterface; +use Doctrine\ORM\QueryBuilder; +use Symfony\Component\Form\FormBuilderInterface; + +/** + * + * + * @author Julien Fastré + */ +class CountPerson implements ExportInterface +{ + /** + * + */ + public function getType() + { + return 'person'; + } + + public function getDescription() + { + return "Count persons by various parameters."; + } + + public function getTitle() + { + return "Count persons"; + } + + /** + * Initiate the query + * + * @param QueryBuilder $qb + * @return QueryBuilder + */ + public function initiateQuery(QueryBuilder $qb, array $requiredModifiers) + { + $qb->select('COUNT(person.id) AS export_result') + ->from('ChillPersonBundle:Person', 'person') + ; + + return $qb; + } + + public function buildForm(FormBuilderInterface $builder) { + throw new \LogicException('This export does not require a form'); + } + + public function hasForm(){ + return false; + } + + public function supportsModifiers() + { + return array('person'); + } + +} diff --git a/Export/Filter/GenderFilter.php b/Export/Filter/GenderFilter.php new file mode 100644 index 000000000..3b101e5e7 --- /dev/null +++ b/Export/Filter/GenderFilter.php @@ -0,0 +1,81 @@ + + * + * 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\Filter; + +use Chill\MainBundle\Export\FilterInterface; +use Symfony\Component\Form\FormBuilderInterface; +use Chill\PersonBundle\Entity\Person; +use Doctrine\ORM\QueryBuilder; +use Doctrine\ORM\Query\Expr; + +/** + * + * + * @author Julien Fastré + */ +class GenderFilter implements FilterInterface +{ + + public function applyOn() + { + return 'person'; + } + + /** + * + */ + public function buildForm(FormBuilderInterface $builder) + { + $builder->add('accepted_genders', 'choice', array( + 'choices' => array( + Person::FEMALE_GENDER => 'Woman', + Person::MALE_GENDER => 'Man' + ), + 'multiple' => true, + 'expanded' => false + )); + } + + public function alterQuery(QueryBuilder $qb, $data) + { + $where = $qb->getDQLPart('where'); + $clause = $qb->expr()->in('person.gender', ':person_gender'); + + + if ($where instanceof Expr\Andx) { + $where->add($clause); + } else { + $where = $qb->expr()->andX($clause); + } + + $qb->add('where', $where); + $qb->setParameter('person_gender', $data['accepted_genders']); + } + + /** + * A title which will be used in the label for the form + * + * @return string + */ + public function getTitle() + { + return 'Filter by person gender'; + } +} diff --git a/Export/Filter/NationalityFilter.php b/Export/Filter/NationalityFilter.php new file mode 100644 index 000000000..0f24c5e90 --- /dev/null +++ b/Export/Filter/NationalityFilter.php @@ -0,0 +1,66 @@ + + * + * 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\Filter; + +use Symfony\Component\Form\FormBuilderInterface; +use Doctrine\ORM\QueryBuilder; +use Chill\MainBundle\Export\FilterInterface; +use Doctrine\ORM\Query\Expr; + +/** + * + * + * @author Julien Fastré + */ +class NationalityFilter implements FilterInterface +{ + public function applyOn() + { + return 'person'; + } + + public function buildForm(FormBuilderInterface $builder) + { + $builder->add('nationalities', 'select2_chill_country', array( + 'placeholder' => 'Choose countries' + )); + + } + + public function alterQuery(QueryBuilder $qb, $data) + { + $where = $qb->getDQLPart('where'); + $clause = $qb->expr()->in('person.nationality', ':person_nationality'); + + if ($where instanceof Expr\Andx) { + $where->add($clause); + } else { + $where = $qb->expr()->andX($clause); + } + + $qb->add('where', $where); + $qb->setParameter('person_nationality', array($data['nationalities'])); + } + + public function getTitle() + { + return "Filter by person's nationality"; + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index e934a7c71..575fe4b95 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -62,4 +62,25 @@ services: - "@chill.main.form.data_transformer.center_transformer" tags: - { name: form.type, alias: chill_personbundle_person_creation } + + chill.person.export.export_count_person: + class: Chill\PersonBundle\Export\Export\CountPerson + tags: + - { name: chill.export, alias: count_person } + + chill.person.export.filter_gender: + class: Chill\PersonBundle\Export\Filter\GenderFilter + tags: + - { name: chill.export_filter, alias: person_gender_filter } + + + chill.person.export.filter_nationality: + class: Chill\PersonBundle\Export\Filter\NationalityFilter + tags: + - { name: chill.export_filter, alias: person_nationality_filter } + + chill.person.export.aggregator_nationality: + class: Chill\PersonBundle\Export\Aggregator\NationalityAggregator + tags: + - { name: chill.export_aggregator, alias: person_nationality_aggregator } \ No newline at end of file