mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-29 10:05:03 +00:00
Add external identifiers for person, editable in edit form, with minimal features associated
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Form\DataMapper;
|
||||
|
||||
use Chill\PersonBundle\Entity\Identifier\PersonIdentifier;
|
||||
use Chill\PersonBundle\PersonIdentifier\Exception\UnexpectedTypeException;
|
||||
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierManagerInterface;
|
||||
use Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Symfony\Component\Form\DataMapperInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
|
||||
final readonly class PersonIdentifiersDataMapper implements DataMapperInterface
|
||||
{
|
||||
public function __construct(
|
||||
private PersonIdentifierManagerInterface $identifierManager,
|
||||
private PersonIdentifierDefinitionRepository $identifierDefinitionRepository,
|
||||
) {}
|
||||
|
||||
public function mapDataToForms($viewData, \Traversable $forms): void
|
||||
{
|
||||
if (!$viewData instanceof Collection) {
|
||||
throw new UnexpectedTypeException($viewData, Collection::class);
|
||||
}
|
||||
/** @var array<string, FormInterface> $formsByKey */
|
||||
$formsByKey = iterator_to_array($forms);
|
||||
|
||||
foreach ($this->identifierManager->getWorkers() as $worker) {
|
||||
if (!$worker->getDefinition()->isEditableByUsers()) {
|
||||
continue;
|
||||
}
|
||||
$form = $formsByKey['identifier_'.$worker->getDefinition()->getId()];
|
||||
$identifier = $viewData->findFirst(fn (int $key, PersonIdentifier $identifier) => $worker->getDefinition()->getId() === $identifier->getId());
|
||||
if (null === $identifier) {
|
||||
$identifier = new PersonIdentifier($worker->getDefinition());
|
||||
}
|
||||
$form->setData($identifier->getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public function mapFormsToData(\Traversable $forms, &$viewData): void
|
||||
{
|
||||
if (!$viewData instanceof Collection) {
|
||||
throw new UnexpectedTypeException($viewData, Collection::class);
|
||||
}
|
||||
|
||||
foreach ($forms as $name => $form) {
|
||||
$identifierId = (int) substr((string) $name, 11);
|
||||
$identifier = $viewData->findFirst(fn (int $key, PersonIdentifier $identifier) => $identifier->getId() === $identifierId);
|
||||
$definition = $this->identifierDefinitionRepository->find($identifierId);
|
||||
if (null === $identifier) {
|
||||
$identifier = new PersonIdentifier($definition);
|
||||
$viewData->add($identifier);
|
||||
}
|
||||
if (!$identifier->getDefinition()->isEditableByUsers()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$worker = $this->identifierManager->buildWorkerByPersonIdentifierDefinition($definition);
|
||||
$identifier->setValue($form->getData());
|
||||
$identifier->setCanonical($worker->canonicalizeValue($identifier->getValue()));
|
||||
}
|
||||
}
|
||||
}
|
48
src/Bundle/ChillPersonBundle/Form/PersonIdentifiersType.php
Normal file
48
src/Bundle/ChillPersonBundle/Form/PersonIdentifiersType.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Form;
|
||||
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Chill\PersonBundle\Form\DataMapper\PersonIdentifiersDataMapper;
|
||||
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierManagerInterface;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
final class PersonIdentifiersType extends AbstractType
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PersonIdentifierManagerInterface $identifierManager,
|
||||
private readonly PersonIdentifiersDataMapper $identifiersDataMapper,
|
||||
private readonly TranslatableStringHelperInterface $translatableStringHelper,
|
||||
) {}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
foreach ($this->identifierManager->getWorkers() as $worker) {
|
||||
if (!$worker->getDefinition()->isEditableByUsers()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$subBuilder = $builder->create(
|
||||
'identifier_'.$worker->getDefinition()->getId(),
|
||||
options: [
|
||||
'compound' => true,
|
||||
'label' => $this->translatableStringHelper->localize($worker->getDefinition()->getLabel()),
|
||||
]
|
||||
);
|
||||
$worker->buildForm($subBuilder);
|
||||
$builder->add($subBuilder);
|
||||
}
|
||||
|
||||
$builder->setDataMapper($this->identifiersDataMapper);
|
||||
}
|
||||
}
|
@@ -72,8 +72,8 @@ class PersonType extends AbstractType
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('firstName')
|
||||
->add('lastName')
|
||||
->add('firstName', TextType::class, ['empty_data' => ''])
|
||||
->add('lastName', TextType::class, ['empty_data' => ''])
|
||||
->add('birthdate', ChillDateType::class, [
|
||||
'required' => false,
|
||||
])
|
||||
@@ -101,7 +101,7 @@ class PersonType extends AbstractType
|
||||
|
||||
if ('visible' === $this->config['memo']) {
|
||||
$builder
|
||||
->add('memo', ChillTextareaType::class, ['required' => false]);
|
||||
->add('memo', ChillTextareaType::class, ['required' => false, 'empty_data' => '']);
|
||||
}
|
||||
|
||||
if ('visible' === $this->config['employment_status']) {
|
||||
@@ -118,6 +118,7 @@ class PersonType extends AbstractType
|
||||
$builder->add('placeOfBirth', TextType::class, [
|
||||
'required' => false,
|
||||
'attr' => ['style' => 'text-transform: uppercase;'],
|
||||
'empty_data' => '',
|
||||
]);
|
||||
|
||||
$builder->get('placeOfBirth')->addModelTransformer(new CallbackTransformer(
|
||||
@@ -127,7 +128,9 @@ class PersonType extends AbstractType
|
||||
}
|
||||
|
||||
if ('visible' === $this->config['contact_info']) {
|
||||
$builder->add('contactInfo', ChillTextareaType::class, ['required' => false]);
|
||||
$builder->add('contactInfo', ChillTextareaType::class, [
|
||||
'required' => false, 'empty_data' => '', 'label' => 'Notes on contact information',
|
||||
]);
|
||||
}
|
||||
|
||||
if ('visible' === $this->config['phonenumber']) {
|
||||
@@ -152,12 +155,12 @@ class PersonType extends AbstractType
|
||||
'required' => false,
|
||||
]
|
||||
)
|
||||
->add('acceptSMS', CheckboxType::class, [
|
||||
->add('acceptSms', CheckboxType::class, [
|
||||
'required' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
$builder->add('otherPhoneNumbers', ChillCollectionType::class, [
|
||||
$builder->add('otherPhonenumbers', ChillCollectionType::class, [
|
||||
'entry_type' => PersonPhoneType::class,
|
||||
'button_add_label' => 'Add new phone',
|
||||
'button_remove_label' => 'Remove phone',
|
||||
@@ -173,12 +176,12 @@ class PersonType extends AbstractType
|
||||
|
||||
if ('visible' === $this->config['email']) {
|
||||
$builder
|
||||
->add('email', EmailType::class, ['required' => false]);
|
||||
->add('email', EmailType::class, ['required' => false, 'empty_data' => '']);
|
||||
}
|
||||
|
||||
if ('visible' === $this->config['acceptEmail']) {
|
||||
$builder
|
||||
->add('acceptEmail', CheckboxType::class, ['required' => false]);
|
||||
->add('acceptEmail', CheckboxType::class, ['required' => false, 'empty_data' => '']);
|
||||
}
|
||||
|
||||
if ('visible' === $this->config['country_of_birth']) {
|
||||
@@ -222,6 +225,10 @@ class PersonType extends AbstractType
|
||||
]);
|
||||
}
|
||||
|
||||
$builder->add('identifiers', PersonIdentifiersType::class, [
|
||||
'by_reference' => false,
|
||||
]);
|
||||
|
||||
if ($options['cFGroup']) {
|
||||
$builder
|
||||
->add(
|
||||
@@ -232,10 +239,7 @@ class PersonType extends AbstractType
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolverInterface $resolver
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Person::class,
|
||||
@@ -251,10 +255,7 @@ class PersonType extends AbstractType
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBlockPrefix()
|
||||
public function getBlockPrefix(): string
|
||||
{
|
||||
return 'chill_personbundle_person';
|
||||
}
|
||||
|
Reference in New Issue
Block a user