improve exports, filters, aggregators

This commit is contained in:
Julien Fastré 2017-01-17 00:33:44 +01:00
parent d75783632f
commit 045119d61f
7 changed files with 249 additions and 10 deletions

View File

@ -66,6 +66,10 @@ class AgeAggregator implements AggregatorInterface
return "Age"; return "Age";
} }
if ($value === NULL) {
return "no data";
}
return $value; return $value;
}; };
} }

View File

@ -0,0 +1,190 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
*
* 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 <http://www.gnu.org/licenses/>.
*/
namespace Chill\PersonBundle\Export\Aggregator;
use Chill\MainBundle\Export\AggregatorInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\EntityRepository;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Component\Translation\TranslatorInterface;
use Chill\MainBundle\Util\CountriesInfo;
use Symfony\Component\Security\Core\Role\Role;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CountryOfBirthAggregator implements AggregatorInterface
{
/**
*
* @var EntityRepository
*/
protected $countriesRepository;
/**
*
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
/**
*
* @var TranslatorInterface
*/
protected $translator;
public function __construct(EntityRepository $countriesRepository,
TranslatableStringHelper $translatableStringHelper,
TranslatorInterface $translator)
{
$this->countriesRepository = $countriesRepository;
$this->translatableStringHelper = $translatableStringHelper;
$this->translator = $translator;
}
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('countryOfBirth.countryCode as country_of_birth_aggregator');
} elseif ($data['group_by_level'] === 'continent') {
$clause = 'CASE '
. 'WHEN countryOfBirth.countryCode IN(:cob_africa_codes) THEN \'AF\' '
. 'WHEN countryOfBirth.countryCode IN(:cob_asia_codes) THEN \'AS\' '
. 'WHEN countryOfBirth.countryCode IN(:cob_europe_codes) THEN \'EU\' '
. 'WHEN countryOfBirth.countryCode IN(:cob_north_america_codes) THEN \'NA\' '
. 'WHEN countryOfBirth.countryCode IN(:cob_south_america_codes) THEN \'SA\' '
. 'WHEN countryOfBirth.countryCode IN(:cob_oceania_codes) THEN \'OC\' '
. 'WHEN countryOfBirth.countryCode IN(:cob_antartica_codes) THEN \'AN\' '
. 'ELSE \'\' '
. 'END as country_of_birth_aggregator ';
$qb->addSelect($clause);
$params =
array(
'cob_africa_codes' => CountriesInfo::getCountriesCodeByContinent('AF'),
'cob_asia_codes' => CountriesInfo::getCountriesCodeByContinent('AS'),
'cob_europe_codes' => CountriesInfo::getCountriesCodeByContinent('EU'),
'cob_north_america_codes' => CountriesInfo::getCountriesCodeByContinent('NA'),
'cob_south_america_codes' => CountriesInfo::getCountriesCodeByContinent('SA'),
'cob_oceania_codes' => CountriesInfo::getCountriesCodeByContinent('OC'),
'cob_antartica_codes' => CountriesInfo::getCountriesCodeByContinent('AN')
);
foreach ($params as $k => $v) {
$qb->setParameter($k, $v);
}
} else {
throw new \LogicException("The group_by_level '".$data['group_by_level']
." is not known.");
}
$qb->leftJoin('person.countryOfBirth', 'countryOfBirth');
// add group by
$groupBy = $qb->getDQLPart('groupBy');
if (!empty($groupBy)) {
$qb->addGroupBy('country_of_birth_aggregator');
} else {
$qb->groupBy('country_of_birth_aggregator');
}
}
public function getTitle()
{
return "Group people by country of birth";
}
public function getQueryKeys($data)
{
return array('country_of_birth_aggregator');
}
public function addRole()
{
return NULL;
}
public function getLabels($key, array $values, $data)
{
if ($data['group_by_level'] === 'country') {
$qb = $this->countriesRepository->createQueryBuilder('c');
$countries = $qb
->andWhere($qb->expr()->in('c.countryCode', ':countries'))
->setParameter('countries', $values)
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_SCALAR);
// initialize array and add blank key for null values
$labels[''] = $this->translator->trans('without data');
$labels['_header'] = $this->translator->trans('Nationality');
foreach($countries as $row) {
$labels[$row['c_countryCode']] = $this->translatableStringHelper->localize($row['c_name']);
}
} elseif ($data['group_by_level'] === 'continent') {
$labels = array(
'EU' => $this->translator->trans('Europe'),
'AS' => $this->translator->trans('Asia'),
'AN' => $this->translator->trans('Antartica'),
'AF' => $this->translator->trans('Africa'),
'SA' => $this->translator->trans('South America'),
'NA' => $this->translator->trans('North America'),
'OC' => $this->translator->trans('Oceania'),
'' => $this->translator->trans('without data'),
'_header' => $this->translator->trans('Continent')
);
}
return function($value) use ($labels) {
return $labels[$value];
};
}
}

View File

@ -28,4 +28,5 @@ namespace Chill\PersonBundle\Export;
abstract class Declarations abstract class Declarations
{ {
CONST PERSON_TYPE = 'person'; CONST PERSON_TYPE = 'person';
CONST PERSON_IMPLIED_IN = 'person_implied_in';
} }

View File

@ -130,7 +130,7 @@ class CountPerson implements ExportInterface
public function supportsModifiers() public function supportsModifiers()
{ {
return array(Declarations::PERSON_TYPE); return array(Declarations::PERSON_TYPE, Declarations::PERSON_IMPLIED_IN);
} }
} }

View File

@ -15,6 +15,7 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\Callback; use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface; use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
/** /**
* Render a list of peoples * Render a list of peoples
@ -35,18 +36,27 @@ class ListPerson implements ListInterface
*/ */
protected $translator; protected $translator;
/**
*
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
protected $fields = array( protected $fields = array(
'id', 'firstName', 'lastName', 'birthdate', 'id', 'firstName', 'lastName', 'birthdate',
'placeOfBirth', 'gender', 'memo', 'email', 'phonenumber' 'placeOfBirth', 'gender', 'memo', 'email', 'phonenumber',
'countryOfBirth', 'nationality'
); );
public function __construct( public function __construct(
EntityManagerInterface $em, EntityManagerInterface $em,
TranslatorInterface $translator TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper
) )
{ {
$this->entityManager = $em; $this->entityManager = $em;
$this->translator = $translator; $this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper;
} }
/** /**
@ -133,7 +143,26 @@ class ListPerson implements ListInterface
return $this->translator->trans($value); return $this->translator->trans($value);
}; };
case 'countryOfBirth':
case 'nationality':
$countryRepository = $this->entityManager
->getRepository('ChillMainBundle:Country');
// load all countries in a single query
$countryRepository->findBy(array('countryCode' => $values));
return function($value) use ($key, $countryRepository) {
if ($value === '_header') { return \strtolower($key); }
if ($value === NULL) {
return $this->translator->trans('no data');
}
$country = $countryRepository->find($value);
return $this->translatableStringHelper->localize(
$country->getName());
};
default: default:
return function($value) use ($key) { return function($value) use ($key) {
if ($value === '_header') { return \strtolower($key); } if ($value === '_header') { return \strtolower($key); }
@ -202,7 +231,14 @@ class ListPerson implements ListInterface
foreach ($this->fields as $f) { foreach ($this->fields as $f) {
if (in_array($f, $data['fields'])) { if (in_array($f, $data['fields'])) {
$qb->addSelect(sprintf('person.%s as %s', $f, $f)); switch ($f) {
case 'countryOfBirth':
case 'nationality':
$qb->addSelect(sprintf('IDENTITY(person.%s) as %s', $f, $f));
break;
default:
$qb->addSelect(sprintf('person.%s as %s', $f, $f));
}
} }
} }
@ -232,6 +268,6 @@ class ListPerson implements ListInterface
*/ */
public function supportsModifiers() public function supportsModifiers()
{ {
return array(Declarations::PERSON_TYPE); return array(Declarations::PERSON_TYPE, Declarations::PERSON_IMPLIED_IN);
} }
} }

View File

@ -11,6 +11,7 @@ services:
arguments: arguments:
- "@doctrine.orm.entity_manager" - "@doctrine.orm.entity_manager"
- "@translator" - "@translator"
- "@chill.main.helper.translatable_string"
tags: tags:
- { name: chill.export, alias: list_person } - { name: chill.export, alias: list_person }
@ -40,6 +41,15 @@ services:
tags: tags:
- { name: chill.export_aggregator, alias: person_nationality_aggregator } - { name: chill.export_aggregator, alias: person_nationality_aggregator }
chill.person.export.aggregator_country_of_birth:
class: Chill\PersonBundle\Export\Aggregator\CountryOfBirthAggregator
arguments:
- "@chill.main.countries_repository"
- "@chill.main.helper.translatable_string"
- "@translator"
tags:
- { name: chill.export_aggregator, alias: person_country_of_birth_aggregator }
chill.person.export.aggregator_gender: chill.person.export.aggregator_gender:
class: Chill\PersonBundle\Export\Aggregator\GenderAggregator class: Chill\PersonBundle\Export\Aggregator\GenderAggregator
arguments: arguments:

View File

@ -35,10 +35,6 @@ class ListPersonTest extends AbstractExportTest
*/ */
private $export; private $export;
protected $fields = array(
'id', 'firstName', 'lastName', 'birthdate',
'placeOfBirth', 'gender', 'memo', 'email', 'phonenumber'
);
public function setUp() public function setUp()
{ {
@ -64,7 +60,9 @@ class ListPersonTest extends AbstractExportTest
return array( return array(
array('fields' => ['id', 'firstName', 'lastName']), array('fields' => ['id', 'firstName', 'lastName']),
array('fields' => ['id', 'birthdate', 'gender', 'memo', 'email', 'phonenumber']), array('fields' => ['id', 'birthdate', 'gender', 'memo', 'email', 'phonenumber']),
array('fields' => ['firstName', 'lastName', 'phonenumber']) array('fields' => ['firstName', 'lastName', 'phonenumber']),
array('fields' => ['id', 'nationality']),
array('fields' => ['id', 'countryOfBirth'])
); );
} }