Commit cf2408be authored by Julien Fastré's avatar Julien Fastré

improvement on import - person may have a null gender

parent b5a9105a
Pipeline #78115 failed with stages
in 55 seconds
Master branch
=============
- Improve import of person to allow multiple centers by file ;
- Launch an event on person import ;
- Allow person to have a `null` gender ;
- Allow filters and aggregator to handle null gender ;
This diff is collapsed.
......@@ -62,6 +62,7 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
$loader->load('services/controller.yml');
$loader->load('services/search.yml');
$loader->load('services/menu.yml');
$loader->load('services/command.yml');
}
private function handlePersonFieldsParameters(ContainerBuilder $container, $config)
......
......@@ -83,6 +83,10 @@ class GenderAggregator implements AggregatorInterface
return $this->translator->trans('woman');
case Person::MALE_GENDER :
return $this->translator->trans('man');
case Person::BOTH_GENDER:
return $this->translator->trans('both');
case null:
return $this->translator->trans('Not given');
case '_header' :
return $this->translator->trans('Gender');
default:
......
......@@ -51,17 +51,19 @@ class GenderFilter implements FilterInterface,
$builder->add('accepted_genders', ChoiceType::class, array(
'choices' => array(
'Woman' => Person::FEMALE_GENDER,
'Man' => Person::MALE_GENDER
'Man' => Person::MALE_GENDER,
'Both' => Person::BOTH_GENDER,
'Not given' => 'null'
),
'choices_as_values' => true,
'multiple' => false,
'expanded' => false
'multiple' => true,
'expanded' => true
));
}
public function validateForm($data, ExecutionContextInterface $context)
{
if ($data['accepted_genders'] === null) {
if (!is_array($data['accepted_genders']) || count($data['accepted_genders']) === 0 ) {
$context->buildViolation("You should select an option")
->addViolation();
}
......@@ -70,8 +72,13 @@ class GenderFilter implements FilterInterface,
public function alterQuery(QueryBuilder $qb, $data)
{
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('person.gender', ':person_gender');
$isIn = $qb->expr()->in('person.gender', ':person_gender');
if (!\in_array('null', $data['accepted_genders'])) {
$clause = $isIn;
} else {
$clause = $qb->expr()->orX($isIn, $qb->expr()->isNull('person.gender'));
}
if ($where instanceof Expr\Andx) {
$where->add($clause);
......@@ -80,7 +87,11 @@ class GenderFilter implements FilterInterface,
}
$qb->add('where', $where);
$qb->setParameter('person_gender', $data['accepted_genders']);
$qb->setParameter('person_gender', \array_filter(
$data['accepted_genders'],
function($el) {
return $el !== 'null';
}));
}
/**
......
......@@ -28,6 +28,7 @@ Chill\PersonBundle\Entity\Person:
gender:
type: string
length: 9
nullable: true
memo:
type: text
default: ''
......
services:
Chill\PersonBundle\Command\ImportPeopleFromCSVCommand:
arguments:
$logger: '@logger'
$helper: '@Chill\MainBundle\Templating\TranslatableStringHelper'
$em: '@Doctrine\ORM\EntityManagerInterface'
$customFieldProvider: '@Chill\CustomFieldsBundle\Service\CustomFieldProvider'
$eventDispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface'
tags:
- { name: console.command }
<?php declare(strict_types=1);
namespace Application\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Set person gender not null
*/
final class Version20181005140249 extends AbstractMigration
{
public function up(Schema $schema) : void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('ALTER TABLE chill_person_person ALTER gender DROP NOT NULL');
}
public function down(Schema $schema) : void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('ALTER TABLE chill_person_person ALTER gender SET NOT NULL');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment