mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-02 21:13:57 +00:00
add model + form to handle alt names
This commit is contained in:
73
Form/DataMapper/PersonAltNameDataMapper.php
Normal file
73
Form/DataMapper/PersonAltNameDataMapper.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Form\DataMapper;
|
||||
|
||||
use Symfony\Component\Form\DataMapperInterface;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||
use Chill\PersonBundle\Entity\PersonAltName;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
class PersonAltNameDataMapper implements DataMapperInterface
|
||||
{
|
||||
public function mapDataToForms($viewData, $forms)
|
||||
{
|
||||
if (null === $viewData) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$viewData instanceof Collection) {
|
||||
throw new UnexpectedTypeException($viewData, Collection::class);
|
||||
}
|
||||
|
||||
$mapIndexToKey = [];
|
||||
foreach ($viewData->getIterator() as $key => $altName) {
|
||||
/** @var PersonAltName $altName */
|
||||
$mapIndexToKey[$altName->getKey()] = $key;
|
||||
}
|
||||
|
||||
foreach ($forms as $key => $form) {
|
||||
if (\array_key_exists($key, $mapIndexToKey)) {
|
||||
$form->setData($viewData->get($mapIndexToKey[$key])->getLabel());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param FormInterface[] $forms
|
||||
* @param Collection $viewData
|
||||
*/
|
||||
public function mapFormsToData($forms, &$viewData)
|
||||
{
|
||||
$mapIndexToKey = [];
|
||||
foreach ($viewData->getIterator() as $key => $altName) {
|
||||
/** @var PersonAltName $altName */
|
||||
$mapIndexToKey[$altName->getKey()] = $key;
|
||||
}
|
||||
|
||||
foreach ($forms as $key => $form) {
|
||||
$isEmpty = empty($form->getData());
|
||||
|
||||
if (\array_key_exists($key, $mapIndexToKey)) {
|
||||
if ($isEmpty) {
|
||||
$viewData->remove($mapIndexToKey[$key]);
|
||||
} else {
|
||||
$viewData->get($mapIndexToKey[$key])->setLabel($form->getData());
|
||||
}
|
||||
} else {
|
||||
if (!$isEmpty) {
|
||||
$altName = (new PersonAltName())
|
||||
->setKey($key)
|
||||
->setLabel($form->getData())
|
||||
;
|
||||
$viewData->add($altName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -34,6 +34,8 @@ use Chill\MainBundle\Form\Type\Select2CountryType;
|
||||
use Chill\MainBundle\Form\Type\Select2LanguageType;
|
||||
use Chill\CustomFieldsBundle\Form\Type\CustomFieldType;
|
||||
use Chill\PersonBundle\Form\Type\Select2MaritalStatusType;
|
||||
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
|
||||
use Chill\PersonBundle\Form\Type\PersonAltNameType;
|
||||
|
||||
class PersonType extends AbstractType
|
||||
{
|
||||
@@ -46,14 +48,23 @@ class PersonType extends AbstractType
|
||||
* @var string[]
|
||||
*/
|
||||
protected $config = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ConfigPersonAltNamesHelper
|
||||
*/
|
||||
protected $configAltNamesHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string[] $personFieldsConfiguration configuration of visibility of some fields
|
||||
*/
|
||||
public function __construct(array $personFieldsConfiguration)
|
||||
{
|
||||
public function __construct(
|
||||
array $personFieldsConfiguration,
|
||||
ConfigPersonAltNamesHelper $configAltNamesHelper
|
||||
) {
|
||||
$this->config = $personFieldsConfiguration;
|
||||
$this->configAltNamesHelper = $configAltNamesHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,6 +81,12 @@ class PersonType extends AbstractType
|
||||
'required' => true
|
||||
));
|
||||
|
||||
if ($this->configAltNamesHelper->hasAltNames()) {
|
||||
$builder->add('altNames', PersonAltNameType::class, [
|
||||
'by_reference' => false
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->config['memo'] === 'visible') {
|
||||
$builder
|
||||
->add('memo', TextareaType::class, array('required' => false))
|
||||
|
70
Form/Type/PersonAltNameType.php
Normal file
70
Form/Type/PersonAltNameType.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
class PersonAltNameType extends AbstractType
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var ConfigPersonAltNamesHelper
|
||||
*/
|
||||
private $configHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
private $translatableStringHelper;
|
||||
|
||||
public function __construct(
|
||||
ConfigPersonAltNamesHelper $configHelper,
|
||||
TranslatableStringHelper $translatableStringHelper
|
||||
) {
|
||||
$this->configHelper = $configHelper;
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
foreach ($this->getKeyChoices() as $label => $key) {
|
||||
$builder->add($key, TextType::class, [
|
||||
'label' => $label,
|
||||
'required' => false
|
||||
]);
|
||||
}
|
||||
|
||||
$builder->setDataMapper(new \Chill\PersonBundle\Form\DataMapper\PersonAltNameDataMapper());
|
||||
}
|
||||
|
||||
protected function getKeyChoices()
|
||||
{
|
||||
$choices = $this->configHelper->getChoices();
|
||||
$translatedChoices = [];
|
||||
|
||||
foreach ($choices as $key => $labels) {
|
||||
$label = $this->translatableStringHelper->localize($labels);
|
||||
$translatedChoices[$label] = $key;
|
||||
}
|
||||
|
||||
return $translatedChoices;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver
|
||||
->setDefault('class', \Chill\PersonBundle\Entity\PersonAltName::class)
|
||||
;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user