*
* 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 <<