[wip] add different steps to exports

This commit is contained in:
Julien Fastré 2016-01-11 21:46:41 +01:00
parent 2711fcee6d
commit d81b2e1037
3 changed files with 108 additions and 2 deletions

View File

@ -22,6 +22,9 @@ 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;
/**
*
@ -30,6 +33,32 @@ use Doctrine\ORM\QueryBuilder;
*/
class NationalityAggregator 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;
}
const EUROPE_COUNTRY_CODE = array('BE', 'FR');
@ -60,11 +89,14 @@ class NationalityAggregator implements AggregatorInterface
$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\' '
. 'WHEN nationality.countryCode IN(:europe_country_codes) THEN \'EU\' '
. 'ELSE \'OC\' ' //this is dummy code !
. 'END as nationality_aggregator ';
$qb->addSelect($clause);
$qb->setParameter('europe_country_codes', self::EUROPE_COUNTRY_CODE);
} else {
throw new \LogicException("The group_by_level '".$data['group_by_level']
." is not known.");
}
@ -86,6 +118,48 @@ class NationalityAggregator implements AggregatorInterface
return "Group people by nationality";
}
public function getQueryKeys($data)
{
return array('nationality_aggregator');
}
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']);
}
return $labels;
} elseif ($data['group_by_level'] === 'continent') {
return 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')
);
}
}
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

View File

@ -22,6 +22,7 @@ namespace Chill\PersonBundle\Export\Export;
use Chill\MainBundle\Export\ExportInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\Query;
/**
*
@ -63,6 +64,33 @@ class CountPerson implements ExportInterface
return $qb;
}
public function getResult($qb, $data)
{
return $qb->getQuery()->getResult(Query::HYDRATE_SCALAR);
}
public function getQueryKeys($data)
{
return array('export_result');
}
public function getLabels($key, array $values, $data)
{
if ($key !== 'export_result') {
throw new \LogicException("the key $key is not used by this export");
}
$labels = array_combine($values, $values);
$labels['_header'] = 'Number of people';
return $labels;
}
public function getAllowedFormattersTypes()
{
return array('tabular');
}
public function buildForm(FormBuilderInterface $builder) {
throw new \LogicException('This export does not require a form');
}

View File

@ -81,6 +81,10 @@ services:
chill.person.export.aggregator_nationality:
class: Chill\PersonBundle\Export\Aggregator\NationalityAggregator
arguments:
- "@chill.main.countries_repository"
- "@chill.main.helper.translatable_string"
- "@translator"
tags:
- { name: chill.export_aggregator, alias: person_nationality_aggregator }