mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-27 00:55:01 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,37 +1,25 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
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;
|
||||
use Chill\MainBundle\Export\ExportElementValidatedInterface;
|
||||
use Chill\MainBundle\Repository\CountryRepository;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\MainBundle\Util\CountriesInfo;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use LogicException;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
final class NationalityAggregator implements AggregatorInterface, ExportElementValidatedInterface
|
||||
{
|
||||
@@ -51,39 +39,17 @@ final class NationalityAggregator implements AggregatorInterface, ExportElementV
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function applyOn()
|
||||
public function addRole()
|
||||
{
|
||||
return 'person';
|
||||
}
|
||||
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('group_by_level', ChoiceType::class, array(
|
||||
'choices' => array(
|
||||
'Group by continents' => 'continent',
|
||||
'Group by country' => 'country'
|
||||
),
|
||||
'expanded' => true,
|
||||
'multiple' => false
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
public function validateForm($data, ExecutionContextInterface $context)
|
||||
{
|
||||
if ($data['group_by_level'] === null) {
|
||||
$context->buildViolation("You should select an option")
|
||||
->addViolation();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function alterQuery(QueryBuilder $qb, $data)
|
||||
{
|
||||
// add a clause in select part
|
||||
if ($data['group_by_level'] === 'country') {
|
||||
if ('country' === $data['group_by_level']) {
|
||||
$qb->addSelect('nationality.countryCode as nationality_aggregator');
|
||||
} elseif ($data['group_by_level'] === 'continent') {
|
||||
} elseif ('continent' === $data['group_by_level']) {
|
||||
$clause = 'CASE '
|
||||
. 'WHEN nationality.countryCode IN(:africa_codes) THEN \'AF\' '
|
||||
. 'WHEN nationality.countryCode IN(:asia_codes) THEN \'AS\' '
|
||||
@@ -96,24 +62,24 @@ final class NationalityAggregator implements AggregatorInterface, ExportElementV
|
||||
. 'END as nationality_aggregator ';
|
||||
$qb->addSelect($clause);
|
||||
$params =
|
||||
array(
|
||||
[
|
||||
'africa_codes' => CountriesInfo::getCountriesCodeByContinent('AF'),
|
||||
'asia_codes' => CountriesInfo::getCountriesCodeByContinent('AS'),
|
||||
'europe_codes' => CountriesInfo::getCountriesCodeByContinent('EU'),
|
||||
'north_america_codes' => CountriesInfo::getCountriesCodeByContinent('NA'),
|
||||
'south_america_codes' => CountriesInfo::getCountriesCodeByContinent('SA'),
|
||||
'oceania_codes' => CountriesInfo::getCountriesCodeByContinent('OC'),
|
||||
'antartica_codes' => CountriesInfo::getCountriesCodeByContinent('AN')
|
||||
);
|
||||
'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.");
|
||||
throw new LogicException("The group_by_level '" . $data['group_by_level']
|
||||
. ' is not known.');
|
||||
}
|
||||
|
||||
|
||||
$qb->leftJoin('person.nationality', 'nationality');
|
||||
|
||||
// add group by
|
||||
@@ -124,36 +90,37 @@ final class NationalityAggregator implements AggregatorInterface, ExportElementV
|
||||
} else {
|
||||
$qb->groupBy('nationality_aggregator');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
public function applyOn()
|
||||
{
|
||||
return "Group people by nationality";
|
||||
return 'person';
|
||||
}
|
||||
|
||||
public function getQueryKeys($data)
|
||||
public function buildForm(FormBuilderInterface $builder)
|
||||
{
|
||||
return array('nationality_aggregator');
|
||||
}
|
||||
|
||||
public function addRole()
|
||||
{
|
||||
return NULL;
|
||||
$builder->add('group_by_level', ChoiceType::class, [
|
||||
'choices' => [
|
||||
'Group by continents' => 'continent',
|
||||
'Group by country' => 'country',
|
||||
],
|
||||
'expanded' => true,
|
||||
'multiple' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getLabels($key, array $values, $data)
|
||||
{
|
||||
$labels = [];
|
||||
|
||||
if ($data['group_by_level'] === 'country') {
|
||||
if ('country' === $data['group_by_level']) {
|
||||
$qb = $this->countriesRepository->createQueryBuilder('c');
|
||||
|
||||
$countries = $qb
|
||||
->andWhere($qb->expr()->in('c.countryCode', ':countries'))
|
||||
->setParameter('countries', $values)
|
||||
->getQuery()
|
||||
->getResult(\Doctrine\ORM\Query::HYDRATE_SCALAR);
|
||||
->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 = [
|
||||
@@ -161,13 +128,13 @@ final class NationalityAggregator implements AggregatorInterface, ExportElementV
|
||||
'_header' => $this->translator->trans('Nationality'),
|
||||
];
|
||||
|
||||
foreach($countries as $row) {
|
||||
foreach ($countries as $row) {
|
||||
$labels[$row['c_countryCode']] = $this->translatableStringHelper->localize($row['c_name']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($data['group_by_level'] === 'continent') {
|
||||
$labels = array(
|
||||
if ('continent' === $data['group_by_level']) {
|
||||
$labels = [
|
||||
'EU' => $this->translator->trans('Europe'),
|
||||
'AS' => $this->translator->trans('Asia'),
|
||||
'AN' => $this->translator->trans('Antartica'),
|
||||
@@ -175,14 +142,31 @@ final class NationalityAggregator implements AggregatorInterface, ExportElementV
|
||||
'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')
|
||||
);
|
||||
'' => $this->translator->trans('without data'),
|
||||
'_header' => $this->translator->trans('Continent'),
|
||||
];
|
||||
}
|
||||
|
||||
return function(string $value) use ($labels): string {
|
||||
return function (string $value) use ($labels): string {
|
||||
return $labels[$value];
|
||||
};
|
||||
}
|
||||
|
||||
public function getQueryKeys($data)
|
||||
{
|
||||
return ['nationality_aggregator'];
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return 'Group people by nationality';
|
||||
}
|
||||
|
||||
public function validateForm($data, ExecutionContextInterface $context)
|
||||
{
|
||||
if (null === $data['group_by_level']) {
|
||||
$context->buildViolation('You should select an option')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user