<?php
namespace App\Form;
use App\Entity\Token;
use App\Entity\Reservation;
use App\Form\ContactTunnelType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class TokenReservationType extends AbstractType
{
public function __construct(
private TranslatorInterface $translator
) {}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
/** @var Token $token */
$token = $options['data'];
$builder
->add('nbParticipants', NumberType::class, [
'block_prefix' => 'nb_participants',
'mapped' => false,
'attr' => [
'min' => 1,
'max' => 16,
'class' => 'nbParticipants',
'max_privatisation' => 8,
'info_privatisation' => "form.reservation.nbParticipants.privatisation",
],
'data' => ($token->getSolo() + $token->getDuo() * 2) ?: 2
])
->add('nbBath', NumberType::class, [
'block_prefix' => 'nb_baths',
'mapped' => false,
'attr' => [
'min' => 1,
'max' => 4,
'class' => 'nbBath',
],
'data' => ($token->getSolo() + $token->getDuo()) ?: 1
])
->add('solo', null, [
'attr' => [
'class' => 'solo hidden',
'data-initial' => $token->getSolo(),
]
])
->add('duo', null, [
'attr' => [
'class' => 'duo hidden',
'data-initial' => $token->getDuo(),
]
])
->add('offer', ChoiceType::class, [
'block_prefix' => 'offer',
'choices' => [
'form.reservation.la_bulle.label' => 60,
'form.reservation.la_grande_bulle.label' => 90,
],
'choice_attr' => [
'form.reservation.la_bulle.label' => [
// 'image_path' => "/vitrine_old/elements/select-plongeon.svg",
'label' => "form.reservation.la_bulle.label",
'duration' => "form.reservation.la_bulle.duration",
'summary_label' => "tunnel.summary.offer.label_bulle",
'price_solo' => 60,
'price_duo' => 50,
'bath_duration' => 30,
'data-initial' => $token->getOffer(),
],
'form.reservation.la_grande_bulle.label' => [
// 'image_path' => "/vitrine_old/elements/select-grand-plongeon.svg",
'label' => "form.reservation.la_grande_bulle.label",
'duration' => "form.reservation.la_grande_bulle.duration",
'summary_label' => "tunnel.summary.offer.label_grande_bulle",
'price_solo' => 90,
'price_duo' => 75,
'bath_duration' => 60,
'data-initial' => $token->getOffer(),
],
],
'expanded' => true,
'multiple' => false,
])
->add('extraReservations', ExtraReservationCollectionType::class, [
'entry_type' => ExtraReservationType::class,
'allow_add' => true,
'allow_delete' => true,
'allow_extra_fields' => true,
'by_reference' => false,
'required' => false,
'prototype' => true,
'entry_options' => ['reservation' => $options['data']],
])
->add('date', DateTimeType::class, [
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'block_prefix' => 'reservation_date',
'input' => 'datetime',
'required' => true,
])
->add('contact', ContactTunnelType::class, [
'block_prefix' => 'contact',
'block_name' => 'contact',
'label' => false,
])
->add('participants', CollectionType::class, [
'block_prefix' => 'participants',
'entry_type' => ParticipantType::class,
'allow_add' => true,
'allow_delete' => true,
'entry_options' => ['label' => false],
'by_reference' => false,
'attr' => [
'class' => 'mx-auto flex flex-col gap-3 participantsDetails',
],
])
->add('cgv', CheckboxType::class, [
'block_prefix' => 'cgv',
'label' => false,
'required' => true,
'mapped' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Token::class,
'constraints' => [
new Callback([$this, 'validateExtraReservations'])
]
]);
}
public function validateExtraReservations($data, ExecutionContextInterface $context)
{
/** @var Token $token */
$token = $data;
foreach ($token->getExtraReservations() as $extraReservation) {
if ($extraReservation->isEmpty()) {
$token->removeExtraReservation($extraReservation);
}
}
}
}