getting all the asideActivity form to work with all fields. Still bug with DateTimeImmutable value somehwere

This commit is contained in:
Julie Lenaerts 2021-08-06 17:35:46 +02:00
parent 9ee140a7d8
commit b74f9cf5dc
5 changed files with 74 additions and 81 deletions

View File

@ -32,39 +32,39 @@ final class AsideActivity implements TrackUpdateInterface, TrackCreationInterfac
* @ORM\ManyToOne(targetEntity=User::class) * @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=false) * @ORM\JoinColumn(nullable=false)
*/ */
private $createdBy; private \Chill\MainBundle\Entity\User $createdBy;
/** /**
* @ORM\Column(type="datetime_immutable") * @ORM\Column(type="datetime_immutable")
*/ */
private $createdAt; private \DateTimeInterface $createdAt;
/** /**
* @ORM\ManyToOne(targetEntity=User::class) * @ORM\ManyToOne(targetEntity=User::class)
*/ */
private $updatedBy; private \Chill\MainBundle\Entity\User $updatedBy;
/** /**
* @ORM\Column(type="datetime_immutable", nullable=true) * @ORM\Column(type="datetime_immutable", nullable=true)
*/ */
private $updatedAt; private \DateTimeInterface $updatedAt;
/** /**
* @ORM\ManyToOne(targetEntity=User::class) * @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=false) * @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank() * @Assert\NotBlank()
*/ */
private $agent; private \Chill\MainBundle\Entity\User $agent;
/** /**
* @ORM\Column(type="datetime_immutable") * @ORM\Column(type="datetime_immutable")
*/ */
private $date; private \DateTimeInterface $date;
/** /**
* @ORM\Column(type="integer", nullable=true) * @ORM\Column(type="time", nullable=true)
*/ */
private $duration; private ?\DateTime $duration = null;
/** /**
* @ORM\Column(type="string", length=100, nullable=true) * @ORM\Column(type="string", length=100, nullable=true)
@ -129,7 +129,7 @@ final class AsideActivity implements TrackUpdateInterface, TrackCreationInterfac
return $this; return $this;
} }
public function getUpdatedAt(): ?\DateTimeImmutable public function getUpdatedAt(): ?\DateTimeInterface
{ {
return $this->updatedAt; return $this->updatedAt;
} }
@ -165,12 +165,12 @@ final class AsideActivity implements TrackUpdateInterface, TrackCreationInterfac
return $this; return $this;
} }
public function getDuration(): ?\DateTimeInterface public function getDuration(): ?\DateTime
{ {
return $this->duration; return $this->duration;
} }
public function setDuration(?\DateTimeInterface $duration): self public function setDuration(?\DateTime $duration): self
{ {
$this->duration = $duration; $this->duration = $duration;

View File

@ -6,36 +6,41 @@ use Chill\AsideActivityBundle\Entity\AsideActivity;
use Chill\AsideActivityBundle\Entity\AsideActivityCategory; use Chill\AsideActivityBundle\Entity\AsideActivityCategory;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\ChillDateType; use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\MainBundle\Form\Type\ChillTextareaType;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
final class AsideActivityFormType extends AbstractType final class AsideActivityFormType extends AbstractType
{ {
// protected array $timeChoices; protected array $timeChoices;
private TranslatableStringHelper $translatableStringHelper; private TranslatableStringHelper $translatableStringHelper;
public function __construct (TranslatableStringHelper $translatableStringHelper){ public function __construct (TranslatableStringHelper $translatableStringHelper, array $timeChoices){
// $this->timeChoices = $timeChoices; $this->timeChoices = $timeChoices;
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
} }
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
// $timeChoices = []; $timeChoices = [];
// foreach ($this->timeChoices as $e) { foreach ($this->timeChoices as $e) {
// $timeChoices[$e['label']] = $e['seconds']; $timeChoices[$e['label']] = $e['seconds'];
// } }
// $durationTimeOptions = [ $durationTimeTransformer = new DateTimeToTimestampTransformer('GMT', 'GMT');
// 'choices' => $timeChoices, $durationTimeOptions = [
// 'placeholder' => 'Choose the duration', 'choices' => $timeChoices,
// ]; 'placeholder' => 'Choose the duration',
];
$builder $builder
->add('agent', EntityType::class, ->add('agent', EntityType::class,
@ -50,7 +55,7 @@ final class AsideActivityFormType extends AbstractType
->add('date', ChillDateType::class, ->add('date', ChillDateType::class,
[ [
'label' => 'date', 'label' => 'date',
'data' => new \DateTime(), 'data' => new \DateTimeImmutable(),
//SETTING RANGE ONLY POSSIBLE WITH WIDGET 'CHOICE' AND NOT 'SINGLE_TEXT'? //SETTING RANGE ONLY POSSIBLE WITH WIDGET 'CHOICE' AND NOT 'SINGLE_TEXT'?
// 'widget' => 'choice', // 'widget' => 'choice',
// 'years' => range(2020, date('Y')), // 'years' => range(2020, date('Y')),
@ -69,11 +74,52 @@ final class AsideActivityFormType extends AbstractType
return $this->translatableStringHelper->localize($asideActivityCategory->getTitle()); return $this->translatableStringHelper->localize($asideActivityCategory->getTitle());
}, },
]) ])
// ->add('durationTime', ChoiceType::class, $durationTimeOptions) ->add('duration', ChoiceType::class, $durationTimeOptions)
->add('note', TextareaType::class, [ ->add('note', ChillTextareaType::class, [
'label' => 'Note', 'label' => 'Note',
'required' => false, 'required' => false,
]); ]);
foreach (['duration'] as $fieldName)
{
$builder->get($fieldName)
->addModelTransformer($durationTimeTransformer);
$builder->get($fieldName)
->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $formEvent) use (
$timeChoices,
$builder,
$durationTimeTransformer,
$durationTimeOptions,
$fieldName
) {
// set the timezone to GMT, and fix the difference between current and GMT
// the datetimetransformer will then handle timezone as GMT
$timezoneUTC = new \DateTimeZone('GMT');
/* @var $data \DateTime */
$data = $formEvent->getData() === NULL ?
\DateTime::createFromFormat('U', 300) :
$formEvent->getData();
$seconds = $data->getTimezone()->getOffset($data);
$data->setTimeZone($timezoneUTC);
$data->add(new \DateInterval('PT'.$seconds.'S'));
// test if the timestamp is in the choices.
// If not, recreate the field with the new timestamp
if (!in_array($data->getTimestamp(), $timeChoices)) {
// the data are not in the possible values. add them
$timeChoices[$data->format('H:i')] = $data->getTimestamp();
$form = $builder->create($fieldName, ChoiceType::class, array_merge(
$durationTimeOptions, [
'choices' => $timeChoices,
'auto_initialize' => false
]
));
$form->addModelTransformer($durationTimeTransformer);
$formEvent->getForm()->getParent()->add($form->getForm());
}
});
}
} }
public function configureOptions(OptionsResolver $resolver): void public function configureOptions(OptionsResolver $resolver): void

View File

@ -1,54 +0,0 @@
<?php
namespace Chill\AsideActivityBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
/**
* Description of TranslatableAsideActivityCategory
*
* @author Champs-Libres Coop
*/
final class TranslatableAsideActivityCategory extends AbstractType
{
/**
* @var RequestStack
*/
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function getBlockPrefix()
{
return 'translatable_aside_activity_category';
}
public function getParent()
{
return EntityType::class;
}
public function configureOptions(OptionsResolver $resolver)
{
$locale = $this->requestStack->getCurrentRequest()->getLocale();
$resolver->setDefaults(
array(
'class' => 'ChillAsideActivityBundle:AsideActivityCategory',
'choice_label' => 'name['.$locale.']',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->where('c.active = true');
}
)
);
}
}

View File

@ -1,8 +1,9 @@
---
services: services:
chill.asideactivity.form.type.asideactivity: chill.asideactivity.form.type.asideactivity:
class: Chill\AsideActivityBundle\Form\AsideActivityFormType class: Chill\AsideActivityBundle\Form\AsideActivityFormType
arguments: arguments:
- "@chill.main.helper.translatable_string" - "@chill.main.helper.translatable_string"
# - "%chill_activity.form.time_duration%" - "%chill_activity.form.time_duration%"
tags: tags:
- { name: form.type, alias: chill_asideactivitybundle_asideactivity } - { name: form.type, alias: chill_asideactivitybundle_asideactivity }