Fixed: [list person] fix list person and add new fields

This commit is contained in:
2022-10-26 12:47:32 +02:00
parent 333c305eef
commit f434cc5c02
13 changed files with 457 additions and 92 deletions

View File

@@ -20,15 +20,20 @@ use Chill\MainBundle\Export\GroupedExportInterface;
use Chill\MainBundle\Export\Helper\ExportAddressHelper;
use Chill\MainBundle\Export\ListInterface;
use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\MainBundle\Repository\CivilityRepositoryInterface;
use Chill\MainBundle\Repository\CountryRepository;
use Chill\MainBundle\Repository\LanguageRepositoryInterface;
use Chill\MainBundle\Repository\UserRepositoryInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Export\Declarations;
use Chill\PersonBundle\Repository\MaritalStatusRepositoryInterface;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use DateTime;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Exception;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
@@ -44,7 +49,6 @@ use function array_merge;
use function count;
use function in_array;
use function strlen;
use function strtolower;
use function uniqid;
/**
@@ -54,42 +58,89 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
{
public const FIELDS = [
'id',
'civility',
'firstName',
'lastName',
'birthdate',
'placeOfBirth', 'gender', 'memo', 'email', 'phonenumber',
'mobilenumber', 'contactInfo', 'countryOfBirth', 'nationality',
'address',
'center',
'deathdate',
'placeOfBirth',
'gender',
'genderComment',
'maritalStatus',
'maritalStatusComment',
'maritalStatusDate',
'memo',
'email',
'phonenumber',
'mobilenumber',
'numberOfChildren',
'contactInfo',
'countryOfBirth',
'nationality',
// add full addresses
'address_fields',
// add a list of spoken languages
'spokenLanguages',
// add household id
'household_id',
// add created at, created by, updated at, and updated by
'lifecycleUpdate',
];
private const HELPER_ATTRIBUTES =
ExportAddressHelper::F_ATTRIBUTES |
ExportAddressHelper::F_BUILDING |
ExportAddressHelper::F_COUNTRY |
ExportAddressHelper::F_GEOM |
ExportAddressHelper::F_POSTAL_CODE |
ExportAddressHelper::F_STREET |
ExportAddressHelper::F_AS_STRING;
private ExportAddressHelper $addressHelper;
private CivilityRepositoryInterface $civilityRepository;
private CountryRepository $countryRepository;
private CustomFieldProvider $customFieldProvider;
private EntityManagerInterface $entityManager;
private LanguageRepositoryInterface $languageRepository;
private MaritalStatusRepositoryInterface $maritalStatusRepository;
private $slugs = [];
private TranslatableStringHelper $translatableStringHelper;
private TranslatorInterface $translator;
private UserRepositoryInterface $userRepository;
public function __construct(
CountryRepository $countryRepository,
ExportAddressHelper $addressHelper,
CivilityRepositoryInterface $civilityRepository,
CountryRepository $countryRepository,
CustomFieldProvider $customFieldProvider,
EntityManagerInterface $em,
TranslatorInterface $translator,
LanguageRepositoryInterface $languageRepository,
MaritalStatusRepositoryInterface $maritalStatusRepository,
TranslatableStringHelper $translatableStringHelper,
CustomFieldProvider $customFieldProvider
TranslatorInterface $translator,
UserRepositoryInterface $userRepository
) {
$this->addressHelper = $addressHelper;
$this->civilityRepository = $civilityRepository;
$this->countryRepository = $countryRepository;
$this->entityManager = $em;
$this->languageRepository = $languageRepository;
$this->maritalStatusRepository = $maritalStatusRepository;
$this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper;
$this->customFieldProvider = $customFieldProvider;
$this->userRepository = $userRepository;
}
public function buildForm(FormBuilderInterface $builder)
@@ -110,7 +161,7 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
'label' => 'Fields to include in export',
'choice_attr' => static function (string $val): array {
// add a 'data-display-target' for address fields
if (substr($val, 0, 7) === 'address') {
if (substr($val, 0, 7) === 'address' || 'center' === $val || 'household' === $val) {
return ['data-display-target' => 'address_date'];
}
@@ -130,7 +181,8 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
// add a date field for addresses
$builder->add('address_date', ChillDateType::class, [
'label' => 'Address valid at this date',
'label' => 'Data valid at this date',
'help' => 'Data regarding center, addresses, and so on will be computed at this date',
'data' => new DateTimeImmutable(),
'input' => 'datetime_immutable',
]);
@@ -153,17 +205,21 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
public function getLabels($key, array $values, $data)
{
if (substr($key, 0, strlen('address')) === 'address') {
return $this->addressHelper->getLabel($key, $values, $data, 'address_');
if (substr($key, 0, strlen('address_fields')) === 'address_fields') {
return $this->addressHelper->getLabel($key, $values, $data, 'address_fields');
}
switch ($key) {
case 'birthdate':
case 'deathdate':
case 'maritalStatusDate':
case 'createdAt':
case 'updatedAt':
// for birthdate, we have to transform the string into a date
// to format the date correctly.
return static function ($value) {
return function ($value) use ($key) {
if ('_header' === $value) {
return 'birthdate';
return $this->translator->trans($key);
}
if (null === $value) {
@@ -172,32 +228,120 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
// warning: won't work with DateTimeImmutable as we reset time a few lines later
$date = DateTime::createFromFormat('Y-m-d', $value);
$hasTime = false;
if (false === $date) {
$date = DateTime::createFromFormat('Y-m-d H:i:s', $value);
$hasTime = true;
}
// check that the creation could occurs.
if (false === $date) {
throw new Exception(sprintf('The value %s could '
. 'not be converted to %s', $value, DateTime::class));
}
$date->setTime(0, 0, 0);
if (!$hasTime) {
$date->setTime(0, 0, 0);
}
return $date;
};
case 'createdBy':
case 'updatedBy':
return function ($value) use ($key) {
if ('_header' === $value) {
return $this->translator->trans($key);
}
if (null === $value) {
return '';
}
return $this->userRepository->find($value)->getLabel();
};
case 'civility':
return function ($value) use ($key) {
if ('_header' === $value) {
return $this->translator->trans($key);
}
if (null === $value) {
return '';
}
$civility = $this->civilityRepository->find($value);
if (null === $civility) {
return '';
}
return $this->translatableStringHelper->localize($civility->getName());
};
case 'gender':
// for gender, we have to translate men/women statement
return function ($value) {
return function ($value) use ($key) {
if ('_header' === $value) {
return 'gender';
return $this->translator->trans($key);
}
return $this->translator->trans($value);
};
case 'maritalStatus':
return function ($value) use ($key) {
if ('_header' === $value) {
return $this->translator->trans($key);
}
if (null === $value) {
return '';
}
$maritalStatus = $this->maritalStatusRepository->find($value);
return $this->translatableStringHelper->localize($maritalStatus->getName());
};
case 'spokenLanguages':
return function ($value) use ($key) {
if ('_header' === $value) {
return $this->translator->trans($key);
}
if (null === $value) {
return '';
}
$ids = json_decode($value);
return
implode(
'|',
array_map(function ($id) {
if (null === $id) {
return '';
}
$lang = $this->languageRepository->find($id);
if (null === $lang) {
return null;
}
return $this->translatableStringHelper->localize($lang->getName());
}, $ids)
);
};
case 'countryOfBirth':
case 'nationality':
return function ($value) use ($key) {
if ('_header' === $value) {
return strtolower($key);
return $this->translator->trans($key);
}
if (null === $value) {
@@ -214,9 +358,9 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
default:
// for fields which are associated with person
if (in_array($key, self::FIELDS, true)) {
return static function ($value) use ($key) {
return function ($value) use ($key) {
if ('_header' === $value) {
return strtolower($key);
return $this->translator->trans($key);
}
return $value;
@@ -231,9 +375,19 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
{
$fields = [];
foreach ($data['fields'] as $key) {
if (substr($key, 0, strlen('address')) === 'address') {
$fields = array_merge($fields, $this->addressHelper->getKeys(0b01111111, 'address_'));
foreach (self::FIELDS as $key) {
if (!in_array($key, $data['fields'], true)) {
continue;
}
if (substr($key, 0, strlen('address_fields')) === 'address_fields') {
$fields = array_merge($fields, $this->addressHelper->getKeys(self::HELPER_ATTRIBUTES, 'address_fields'));
continue;
}
if ('lifecycleUpdate' === $key) {
$fields = array_merge($fields, ['createdAt', 'createdBy', 'updatedAt', 'updatedBy']);
continue;
}
@@ -286,8 +440,10 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
)
->setParameter('authorized_centers', $centers);
$fields = $data['fields'];
foreach (self::FIELDS as $f) {
if (!in_array($f, $data['fields'], true)) {
if (!in_array($f, $fields, true)) {
continue;
}
@@ -298,31 +454,90 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
break;
case 'address':
foreach ($this->addressHelper->getKeys(0b01111111, 'address_') as $key) {
case 'address_fields':
foreach ($this->addressHelper->getKeys(self::HELPER_ATTRIBUTES, 'address_fields') as $key) {
$qb
->addSelect(sprintf('IDENTITY(currentPersonAddress.address) AS %s', $key));
->addSelect(sprintf('IDENTITY(personHouseholdAddress.address) AS %s', $key));
}
if (!(in_array('currentPersonAddress', $qb->getAllAliases(), true))) {
$qb
->leftJoin('person.currentPersonAddress', 'currentPersonAddress')
->andWhere(
$qb->expr()->orX(
// no address at this time
$qb->expr()->isNull('currentPersonAddress'),
// there is one address...
$qb->expr()->andX(
$qb->expr()->lte('currentPersonAddress.validFrom', ':address_date'),
$qb->expr()->orX(
$qb->expr()->isNull('currentPersonAddress.validTo'),
$qb->expr()->gt('currentPersonAddress.validTo', ':address_date')
)
$this->addCurrentAddressAt($qb, $data['address_date']);
break;
case 'spokenLanguages':
$qb
->leftJoin('person.spokenLanguages', 'spokenLanguage')
->addSelect('AGGREGATE(spokenLanguage.id) AS spokenLanguages')
->addGroupBy('person');
if (in_array('center', $fields, true)) {
$qb->addGroupBy('center');
}
if (in_array('address_fields', $fields, true)) {
$qb->addGroupBy('address_fieldsid');
}
if (in_array('household_id', $fields, true)) {
$qb->addGroupBy('household_id');
}
break;
case 'household_id':
$qb
->addSelect('IDENTITY(personHouseholdAddress.household) AS household_id');
$this->addCurrentAddressAt($qb, $data['address_date']);
break;
case 'center':
$qb
->addSelect('IDENTITY(centerHistory.center) AS center')
->leftJoin('person.centerHistory', 'centerHistory')
->andWhere(
$qb->expr()->orX(
$qb->expr()->isNull('centerHistory'),
$qb->expr()->andX(
$qb->expr()->lte('centerHistory.startDate', ':address_date'),
$qb->expr()->orX(
$qb->expr()->isNull('centerHistory.endDate'),
$qb->expr()->gte('centerHistory.endDate', ':address_date')
)
)
)
->setParameter('address_date', $data['address_date']);
}
)
->setParameter('address_date', $data['address_date']);
break;
case 'lifecycleUpdate':
$qb
->addSelect('person.createdAt AS createdAt')
->addSelect('IDENTITY(person.createdBy) AS createdBy')
->addSelect('person.updatedAt AS updatedAt')
->addSelect('IDENTITY(person.updatedBy) AS updatedBy');
break;
case 'genderComment':
$qb->addSelect('person.genderComment.comment AS genderComment');
break;
case 'maritalStatus':
$qb->addSelect('IDENTITY(person.maritalStatus) AS maritalStatus');
break;
case 'maritalStatusComment':
$qb->addSelect('person.maritalStatusComment.comment AS maritalStatusComment');
break;
case 'civility':
$qb->addSelect('IDENTITY(person.civility) AS civility');
break;
@@ -332,6 +547,10 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
}
foreach ($this->getCustomFields() as $cf) {
if (!in_array($cf->getSlug(), $fields, true)) {
continue;
}
$cfType = $this->customFieldProvider->getCustomFieldByType($cf->getType());
if ($cfType instanceof CustomFieldChoice && $cfType->isMultiple($cf)) {
@@ -383,7 +602,7 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
// check if there is one field starting with address in data
if (count(array_intersect($data['fields'], $addressFields)) > 0) {
// if a field address is checked, the date must not be empty
if (empty($data['address_date'])) {
if (!$data['address_date'] instanceof DateTimeImmutable) {
$context
->buildViolation('You must set this date if an address is checked')
->atPath('address_date')
@@ -392,6 +611,29 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
}
}
private function addCurrentAddressAt(QueryBuilder $qb, DateTimeImmutable $date): void
{
if (!(in_array('personHouseholdAddress', $qb->getAllAliases(), true))) {
$qb
->leftJoin('person.householdAddresses', 'personHouseholdAddress')
->andWhere(
$qb->expr()->orX(
// no address at this time
$qb->expr()->isNull('personHouseholdAddress'),
// there is one address...
$qb->expr()->andX(
$qb->expr()->lte('personHouseholdAddress.validFrom', ':address_date'),
$qb->expr()->orX(
$qb->expr()->isNull('personHouseholdAddress.validTo'),
$qb->expr()->gt('personHouseholdAddress.validTo', ':address_date')
)
)
)
)
->setParameter('address_date', $date);
}
}
private function DQLToSlug($cleanedSlug)
{
return $this->slugs[$cleanedSlug]['slug'];
@@ -464,7 +706,7 @@ class ListPerson implements ExportElementValidatedInterface, ListInterface, Grou
. ' | ' . $label;
}
if ('_other' === $slugChoice && $cfType->isChecked($cf, $choiceSlug, $decoded)) {
if ('_other' === $slugChoice && $cfType->isChecked($cf, $slugChoice, $decoded)) {
return $cfType->extractOtherValue($cf, $decoded);
}