mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-10 00:34:58 +00:00
create generic data transformer with get an object by id, and add test
for CalendarType
This commit is contained in:
@@ -12,16 +12,15 @@ declare(strict_types=1);
|
||||
namespace Chill\CalendarBundle\Form;
|
||||
|
||||
use Chill\CalendarBundle\Entity\Calendar;
|
||||
use Chill\CalendarBundle\Entity\CalendarRange;
|
||||
use Chill\CalendarBundle\Entity\CancelReason;
|
||||
use Chill\MainBundle\Entity\Location;
|
||||
use Chill\CalendarBundle\Form\DataTransformer\IdToCalendarRangeDataTransformer;
|
||||
use Chill\MainBundle\Form\DataTransformer\IdToLocationDataTransformer;
|
||||
use Chill\MainBundle\Form\DataTransformer\IdToUserDataTransformer;
|
||||
use Chill\MainBundle\Form\DataTransformer\IdToUsersDataTransformer;
|
||||
use Chill\MainBundle\Form\Type\CommentType;
|
||||
use Chill\MainBundle\Form\Type\PickUserDynamicType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Chill\PersonBundle\Form\DataTransformer\PersonsToIdDataTransformer;
|
||||
use Chill\ThirdPartyBundle\Form\DataTransformer\ThirdPartiesToIdDataTransformer;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\CallbackTransformer;
|
||||
@@ -32,16 +31,32 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class CalendarType extends AbstractType
|
||||
{
|
||||
protected ObjectManager $om;
|
||||
private IdToCalendarRangeDataTransformer $calendarRangeDataTransformer;
|
||||
|
||||
protected TranslatableStringHelper $translatableStringHelper;
|
||||
private IdToLocationDataTransformer $idToLocationDataTransformer;
|
||||
|
||||
private IdToUserDataTransformer $idToUserDataTransformer;
|
||||
|
||||
private IdToUsersDataTransformer $idToUsersDataTransformer;
|
||||
|
||||
private ThirdPartiesToIdDataTransformer $partiesToIdDataTransformer;
|
||||
|
||||
private PersonsToIdDataTransformer $personsToIdDataTransformer;
|
||||
|
||||
public function __construct(
|
||||
TranslatableStringHelper $translatableStringHelper,
|
||||
ObjectManager $om
|
||||
PersonsToIdDataTransformer $personsToIdDataTransformer,
|
||||
IdToUserDataTransformer $idToUserDataTransformer,
|
||||
IdToUsersDataTransformer $idToUsersDataTransformer,
|
||||
IdToLocationDataTransformer $idToLocationDataTransformer,
|
||||
ThirdPartiesToIdDataTransformer $partiesToIdDataTransformer,
|
||||
IdToCalendarRangeDataTransformer $idToCalendarRangeDataTransformer
|
||||
) {
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
$this->om = $om;
|
||||
$this->personsToIdDataTransformer = $personsToIdDataTransformer;
|
||||
$this->idToUserDataTransformer = $idToUserDataTransformer;
|
||||
$this->idToUsersDataTransformer = $idToUsersDataTransformer;
|
||||
$this->idToLocationDataTransformer = $idToLocationDataTransformer;
|
||||
$this->partiesToIdDataTransformer = $partiesToIdDataTransformer;
|
||||
$this->calendarRangeDataTransformer = $idToCalendarRangeDataTransformer;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
@@ -67,11 +82,8 @@ class CalendarType extends AbstractType
|
||||
]);
|
||||
|
||||
if ($options['data'] instanceof Calendar && $options['data']->getId() === null) {
|
||||
$builder->add('mainUser', PickUserDynamicType::class, [
|
||||
'multiple' => false,
|
||||
'required' => true,
|
||||
'help' => 'chill_calendar.form.The main user is mandatory. He will organize the appointment.',
|
||||
]);
|
||||
$builder->add('mainUser', HiddenType::class);
|
||||
$builder->get('mainUser')->addModelTransformer($this->idToUserDataTransformer);
|
||||
}
|
||||
|
||||
$builder->add('startDate', HiddenType::class);
|
||||
@@ -110,89 +122,20 @@ class CalendarType extends AbstractType
|
||||
|
||||
$builder->add('persons', HiddenType::class);
|
||||
$builder->get('persons')
|
||||
->addModelTransformer(new CallbackTransformer(
|
||||
static function (iterable $personsAsIterable): string {
|
||||
$personIds = [];
|
||||
|
||||
foreach ($personsAsIterable as $value) {
|
||||
$personIds[] = $value->getId();
|
||||
}
|
||||
|
||||
return implode(',', $personIds);
|
||||
},
|
||||
function (?string $personsAsString): array {
|
||||
if (null === $personsAsString) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(
|
||||
fn (string $id): ?Person => $this->om->getRepository(Person::class)->findOneBy(['id' => (int) $id]),
|
||||
explode(',', $personsAsString)
|
||||
);
|
||||
}
|
||||
));
|
||||
|
||||
$builder->add('professionals', HiddenType::class);
|
||||
$builder->get('professionals')
|
||||
->addModelTransformer(new CallbackTransformer(
|
||||
static function (iterable $thirdpartyAsIterable): string {
|
||||
$thirdpartyIds = [];
|
||||
|
||||
foreach ($thirdpartyAsIterable as $value) {
|
||||
$thirdpartyIds[] = $value->getId();
|
||||
}
|
||||
|
||||
return implode(',', $thirdpartyIds);
|
||||
},
|
||||
function (?string $thirdpartyAsString): array {
|
||||
if (null === $thirdpartyAsString) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(
|
||||
fn (string $id): ?ThirdParty => $this->om->getRepository(ThirdParty::class)->findOneBy(['id' => (int) $id]),
|
||||
explode(',', $thirdpartyAsString)
|
||||
);
|
||||
}
|
||||
));
|
||||
->addModelTransformer($this->personsToIdDataTransformer);
|
||||
/*
|
||||
$builder->add('professionals', HiddenType::class);
|
||||
$builder->get('professionals')
|
||||
->addModelTransformer($this->partiesToIdDataTransformer);
|
||||
*/
|
||||
|
||||
$builder->add('calendarRange', HiddenType::class);
|
||||
$builder->get('calendarRange')
|
||||
->addModelTransformer(new CallbackTransformer(
|
||||
static function (?CalendarRange $calendarRange): ?int {
|
||||
if (null !== $calendarRange) {
|
||||
$res = $calendarRange->getId();
|
||||
} else {
|
||||
$res = -1;
|
||||
}
|
||||
|
||||
return $res;
|
||||
},
|
||||
function (?string $calendarRangeId): ?CalendarRange {
|
||||
if (null !== $calendarRangeId) {
|
||||
$res = $this->om->getRepository(CalendarRange::class)->findOneBy(['id' => (int) $calendarRangeId]);
|
||||
} else {
|
||||
$res = null;
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
));
|
||||
->addModelTransformer($this->calendarRangeDataTransformer);
|
||||
|
||||
$builder->add('location', HiddenType::class)
|
||||
->get('location')
|
||||
->addModelTransformer(new CallbackTransformer(
|
||||
static function (?Location $location): string {
|
||||
if (null === $location) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $location->getId();
|
||||
},
|
||||
function (?string $id): ?Location {
|
||||
return $this->om->getRepository(Location::class)->findOneBy(['id' => (int) $id]);
|
||||
}
|
||||
));
|
||||
->addModelTransformer($this->idToLocationDataTransformer);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
@@ -200,10 +143,6 @@ class CalendarType extends AbstractType
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Calendar::class,
|
||||
]);
|
||||
|
||||
$resolver
|
||||
->setRequired(['accompanyingPeriod'])
|
||||
->setAllowedTypes('accompanyingPeriod', [\Chill\PersonBundle\Entity\AccompanyingPeriod::class, 'null']);
|
||||
}
|
||||
|
||||
public function getBlockPrefix()
|
||||
|
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\CalendarBundle\Form\DataTransformer;
|
||||
|
||||
use Chill\CalendarBundle\Repository\CalendarRangeRepository;
|
||||
use Chill\MainBundle\Form\DataTransformer\IdToEntityDataTransformer;
|
||||
|
||||
class IdToCalendarRangeDataTransformer extends IdToEntityDataTransformer
|
||||
{
|
||||
public function __construct(CalendarRangeRepository $repository)
|
||||
{
|
||||
parent::__construct($repository, false);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user