Activity Form : display field according to the parameters

This commit is contained in:
Jean-Francois Monfort
2021-04-29 16:35:39 +02:00
parent 45671bda52
commit 82d8556f24
8 changed files with 354 additions and 209 deletions

View File

@@ -1,19 +0,0 @@
<?php
namespace Chill\ActivityBundle\Form;
use Chill\ActivityBundle\Form\Type\TranslatableActivityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ActivitySelectTypeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('type', TranslatableActivityType::class, array(
'placeholder' => 'Choose a type',
'active_only' => true,
'mapped' => false,
));
}
}

View File

@@ -2,8 +2,16 @@
namespace Chill\ActivityBundle\Form;
use Chill\ActivityBundle\Entity\Activity;
use Chill\ActivityBundle\Entity\ActivityPresence;
use Chill\ActivityBundle\Entity\ActivityReason;
use Chill\MainBundle\Form\Type\CommentType;
use Chill\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
@@ -15,51 +23,33 @@ use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTra
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Chill\ActivityBundle\Form\Type\TranslatableActivityType;
use Chill\ActivityBundle\Form\Type\TranslatableActivityReason;
use Chill\MainBundle\Form\Type\UserPickerType;
use Chill\MainBundle\Form\Type\ScopePickerType;
use Chill\MainBundle\Form\Type\ChillDateType;
class ActivityType extends AbstractType
{
/**
* the user running this form
*
* @var User
*/
protected $user;
protected User $user;
/**
*
* @var AuthorizationHelper
*/
protected AuthorizationHelper $authorizationHelper;
/**
*
* @var ObjectManager
*/
protected $om;
protected ObjectManager $om;
/**
*
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
protected TranslatableStringHelper $translatableStringHelper;
protected $timeChoices;
protected array $timeChoices;
public function __construct(
TokenStorageInterface $tokenStorage,
AuthorizationHelper $authorizationHelper, ObjectManager $om,
TranslatableStringHelper $translatableStringHelper,
array $timeChoices
)
{
public function __construct (
TokenStorageInterface $tokenStorage,
AuthorizationHelper $authorizationHelper,
ObjectManager $om,
TranslatableStringHelper $translatableStringHelper,
array $timeChoices
) {
if (!$tokenStorage->getToken()->getUser() instanceof User) {
throw new \RuntimeException("you should have a valid user");
}
$this->user = $tokenStorage->getToken()->getUser();
$this->authorizationHelper = $authorizationHelper;
$this->om = $om;
@@ -67,121 +57,219 @@ class ActivityType extends AbstractType
$this->timeChoices = $timeChoices;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
// handle times choices
$timeChoices = array();
$timeChoices = [];
foreach ($this->timeChoices as $e) {
$timeChoices[$e['label']] = $e['seconds'];
};
}
$durationTimeTransformer = new DateTimeToTimestampTransformer('GMT', 'GMT');
$durationTimeOptions = array(
'choices' => $timeChoices,
'placeholder' => 'Choose the duration',
);
$durationTimeOptions = [
'choices' => $timeChoices,
'placeholder' => 'Choose the duration',
];
$builder
->add('date', ChillDateType::class, array(
'required' => true
))
->add('durationTime', ChoiceType::class, $durationTimeOptions)
->add('attendee', ChoiceType::class, array(
'expanded' => true,
'required' => false,
'choices' => array(
'present' => true,
'not present' => false
)
))
->add('user', UserPickerType::class, [
/** @var \Chill\ActivityBundle\Entity\ActivityType $activityType */
$activityType = $options['activityType'];
if (!$activityType->isActive()) {
throw new \InvalidArgumentException('Activity type must be active');
}
$builder->add('scope', ScopePickerType::class, [
'center' => $options['center'],
'role' => $options['role']
]);
if ($activityType->isVisible('date')) {
$builder->add('date', ChillDateType::class, [
'label' => $activityType->getLabel('date'),
'required' => $activityType->isRequired('date'),
]);
}
if ($activityType->isVisible('durationTime')) {
$durationTimeOptions['label'] = $activityType->getLabel('durationTime');
$durationTimeOptions['required'] = $activityType->isRequired('durationTime');
$builder->add('durationTime', ChoiceType::class, $durationTimeOptions);
}
if ($activityType->isVisible('travelTime')) {
$durationTimeOptions['label'] = $activityType->getLabel('travelTime');
$durationTimeOptions['required'] = $activityType->isRequired('travelTime');
$builder->add('travelTime', ChoiceType::class, $durationTimeOptions);
}
if ($activityType->isVisible('travelTime')) {
$builder->add('attendee', EntityType::class, [
'label' => $activityType->getLabel('attendee'),
'required' => $activityType->isRequired('attendee'),
'class' => ActivityPresence::class,
'choice_label' => function (ActivityPresence $activityPresence) {
return $this->translatableStringHelper->localize($activityPresence->getName());
},
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('a')
->where('a.active = true');
},
]);
}
if ($activityType->isVisible('user')) {
$builder->add('user', UserPickerType::class, [
'label' => $activityType->getLabel('user'),
'required' => $activityType->isRequired('user'),
'center' => $options['center'],
'role' => $options['role']
])
->add('scope', ScopePickerType::class, [
'center' => $options['center'],
'role' => $options['role']
])
->add('reasons', TranslatableActivityReason::class, array(
]);
}
if ($activityType->isVisible('reasons')) {
$builder->add('reasons', EntityType::class, [
'label' => $activityType->getLabel('reasons'),
'required' => $activityType->isRequired('reasons'),
'class' => ActivityReason::class,
'choice_label' => function (ActivityReason $activityReason) {
return $this->translatableStringHelper->localize($activityReason->getName());
},
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('a')
->where('a.active = true');
},
]);
}
if ($activityType->isVisible('comment')) {
$builder->add('comment', CommentType::class, [
'label' => $activityType->getLabel('comment'),
'required' => $activityType->isRequired('comment'),
]);
}
if ($activityType->isVisible('persons')) {
// TODO Faire évoluer le selecteur et la query
$builder->add('persons', EntityType::class, [
'label' => $activityType->getLabel('persons'),
'required' => $activityType->isRequired('persons'),
'class' => Person::class,
'multiple' => true,
'required' => false,
))
->add('comment', CommentType::class, [
'required' => false,
])
;
'choice_label' => function (Person $person) {
return $person->getFullnameCanonical();
},
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('a');
},
]);
}
if ($activityType->isVisible('thirdParties')) {
$builder->add('thirdParties', EntityType::class, [
'label' => $activityType->getLabel('thirdParties'),
'required' => $activityType->isRequired('thirdParties'),
'class' => ThirdParty::class,
'choice_label' => function (ThirdParty $thirdParty) {
return $thirdParty->getName();
},
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('a');
},
]);
}
// TODO : documents
//$documents
if ($activityType->isVisible('users')) {
$builder->add('users', EntityType::class, [
'label' => $activityType->getLabel('users'),
'required' => $activityType->isRequired('users'),
'class' => User::class,
'choice_label' => function (User $user) {
return $user->getUsername();
},
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u');
},
]);
}
if ($activityType->isVisible('emergency')) {
$builder->add('emergency', CheckboxType::class, [
'label' => $activityType->getLabel('emergency'),
'required' => $activityType->isRequired('emergency'),
]);
}
if ($activityType->isVisible('sentReceived')) {
$builder->add('sentReceived', ChoiceType::class, [
'label' => $activityType->getLabel('sentReceived'),
'required' => $activityType->isRequired('sentReceived'),
'choices' => [
'Sent' => Activity::SENTRECEIVED_SENT,
'Received' => Activity::SENTRECEIVED_RECEIVED,
],
]);
}
$builder->get('durationTime')
->addModelTransformer($durationTimeTransformer);
->addModelTransformer($durationTimeTransformer);
$builder->get('durationTime')
->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $formEvent) use (
$timeChoices,
$builder,
$durationTimeTransformer,
$durationTimeOptions
)
{
// 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'));
->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $formEvent) use (
$timeChoices,
$builder,
$durationTimeTransformer,
$durationTimeOptions
) {
// 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(
'durationTime',
ChoiceType::class,
array_merge(
$durationTimeOptions,
array(
'choices' => $timeChoices,
'auto_initialize' => false
)
));
$form->addModelTransformer($durationTimeTransformer);
$formEvent->getForm()->getParent()->add($form->getForm());
}
});
// 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('durationTime', ChoiceType::class, array_merge(
$durationTimeOptions,
[
'choices' => $timeChoices,
'auto_initialize' => false
]
));
$form->addModelTransformer($durationTimeTransformer);
$formEvent->getForm()->getParent()->add($form->getForm());
}
});
}
/**
* @param OptionsResolverInterface $resolver
*/
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(array(
$resolver->setDefaults([
'data_class' => 'Chill\ActivityBundle\Entity\Activity'
));
]);
$resolver
->setRequired(array('center', 'role'))
->setAllowedTypes('center', 'Chill\MainBundle\Entity\Center')
->setAllowedTypes('role', 'Symfony\Component\Security\Core\Role\Role')
;
->setRequired(['center', 'role', 'activityType'])
->setAllowedTypes('center', 'Chill\MainBundle\Entity\Center')
->setAllowedTypes('role', 'Symfony\Component\Security\Core\Role\Role')
->setAllowedTypes('activityType', \Chill\ActivityBundle\Entity\ActivityType::class)
;
}
/**
* @return string
*/
public function getBlockPrefix()
public function getBlockPrefix(): string
{
return 'chill_activitybundle_activity';
}