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

View File

@ -6,36 +6,41 @@ use Chill\AsideActivityBundle\Entity\AsideActivity;
use Chill\AsideActivityBundle\Entity\AsideActivityCategory;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\MainBundle\Form\Type\ChillTextareaType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
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\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
final class AsideActivityFormType extends AbstractType
{
// protected array $timeChoices;
protected array $timeChoices;
private TranslatableStringHelper $translatableStringHelper;
public function __construct (TranslatableStringHelper $translatableStringHelper){
// $this->timeChoices = $timeChoices;
public function __construct (TranslatableStringHelper $translatableStringHelper, array $timeChoices){
$this->timeChoices = $timeChoices;
$this->translatableStringHelper = $translatableStringHelper;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// $timeChoices = [];
$timeChoices = [];
// foreach ($this->timeChoices as $e) {
// $timeChoices[$e['label']] = $e['seconds'];
// }
foreach ($this->timeChoices as $e) {
$timeChoices[$e['label']] = $e['seconds'];
}
// $durationTimeOptions = [
// 'choices' => $timeChoices,
// 'placeholder' => 'Choose the duration',
// ];
$durationTimeTransformer = new DateTimeToTimestampTransformer('GMT', 'GMT');
$durationTimeOptions = [
'choices' => $timeChoices,
'placeholder' => 'Choose the duration',
];
$builder
->add('agent', EntityType::class,
@ -50,7 +55,7 @@ final class AsideActivityFormType extends AbstractType
->add('date', ChillDateType::class,
[
'label' => 'date',
'data' => new \DateTime(),
'data' => new \DateTimeImmutable(),
//SETTING RANGE ONLY POSSIBLE WITH WIDGET 'CHOICE' AND NOT 'SINGLE_TEXT'?
// 'widget' => 'choice',
// 'years' => range(2020, date('Y')),
@ -69,11 +74,52 @@ final class AsideActivityFormType extends AbstractType
return $this->translatableStringHelper->localize($asideActivityCategory->getTitle());
},
])
// ->add('durationTime', ChoiceType::class, $durationTimeOptions)
->add('note', TextareaType::class, [
->add('duration', ChoiceType::class, $durationTimeOptions)
->add('note', ChillTextareaType::class, [
'label' => 'Note',
'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

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:
chill.asideactivity.form.type.asideactivity:
class: Chill\AsideActivityBundle\Form\AsideActivityFormType
arguments:
- "@chill.main.helper.translatable_string"
# - "%chill_activity.form.time_duration%"
- "%chill_activity.form.time_duration%"
tags:
- { name: form.type, alias: chill_asideactivitybundle_asideactivity }