src/Form/TokenReservationType.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Token;
  4. use App\Entity\Reservation;
  5. use App\Form\ContactTunnelType;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  8. use Symfony\Component\Form\FormBuilderInterface;
  9. use Symfony\Component\Validator\Constraints\Callback;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  14. use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
  15. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  16. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  17. class TokenReservationType extends AbstractType
  18. {
  19.     public function __construct(
  20.         private TranslatorInterface $translator
  21.     ) {}
  22.     
  23.     public function buildForm(FormBuilderInterface $builder, array $options): void
  24.     {
  25.         /** @var Token $token */
  26.         $token $options['data'];
  27.         $builder
  28.             ->add('nbParticipants'NumberType::class, [
  29.                 'block_prefix' => 'nb_participants',
  30.                 'mapped' => false,
  31.                 'attr' => [
  32.                     'min' => 1,
  33.                     'max' => 16,
  34.                     'class' => 'nbParticipants',
  35.                     'max_privatisation' => 8,
  36.                     'info_privatisation' => "form.reservation.nbParticipants.privatisation",
  37.                 ],
  38.                 'data' => ($token->getSolo() + $token->getDuo() * 2) ?: 2
  39.             ])
  40.             ->add('nbBath'NumberType::class, [
  41.                 'block_prefix' => 'nb_baths',
  42.                 'mapped' => false,
  43.                 'attr' => [
  44.                     'min' => 1,
  45.                     'max' => 4,
  46.                     'class' => 'nbBath',
  47.                 ],
  48.                 'data' => ($token->getSolo() + $token->getDuo()) ?: 1
  49.             ])
  50.             ->add('solo'null, [
  51.                 'attr' => [
  52.                     'class' => 'solo hidden',
  53.                     'data-initial' => $token->getSolo(),
  54.                 ]
  55.             ])
  56.             ->add('duo'null, [
  57.                 'attr' => [
  58.                     'class' => 'duo hidden',
  59.                     'data-initial' => $token->getDuo(),
  60.                 ]
  61.             ])
  62.             ->add('offer'ChoiceType::class, [
  63.                 'block_prefix' => 'offer',
  64.                 'choices' => [
  65.                     'form.reservation.la_bulle.label' => 60,
  66.                     'form.reservation.la_grande_bulle.label' => 90,
  67.                 ],
  68.                 'choice_attr' => [
  69.                     'form.reservation.la_bulle.label' => [
  70. //                        'image_path' => "/vitrine_old/elements/select-plongeon.svg",
  71.                         'label' => "form.reservation.la_bulle.label",
  72.                         'duration' => "form.reservation.la_bulle.duration",
  73.                         'summary_label' => "tunnel.summary.offer.label_bulle",
  74.                         'price_solo' => 60,
  75.                         'price_duo' => 50,
  76.                         'bath_duration' => 30,
  77.                         'data-initial' => $token->getOffer(),
  78.                     ],
  79.                     'form.reservation.la_grande_bulle.label' => [
  80. //                        'image_path' => "/vitrine_old/elements/select-grand-plongeon.svg",
  81.                         'label' => "form.reservation.la_grande_bulle.label",
  82.                         'duration' => "form.reservation.la_grande_bulle.duration",
  83.                         'summary_label' => "tunnel.summary.offer.label_grande_bulle",
  84.                         'price_solo' => 90,
  85.                         'price_duo' => 75,
  86.                         'bath_duration' => 60,
  87.                         'data-initial' => $token->getOffer(),
  88.                     ],
  89.                 ],
  90.                 'expanded' => true,
  91.                 'multiple' => false,
  92.             ])
  93.             ->add('extraReservations'ExtraReservationCollectionType::class, [
  94.                 'entry_type' => ExtraReservationType::class,
  95.                 'allow_add' => true,
  96.                 'allow_delete' => true,
  97.                 'allow_extra_fields' => true,
  98.                 'by_reference' => false,
  99.                 'required' => false,
  100.                 'prototype' => true,
  101.                 'entry_options' => ['reservation' => $options['data']],
  102.             ])
  103.             ->add('date'DateTimeType::class, [
  104.                 'date_widget' => 'single_text',
  105.                 'time_widget' => 'single_text',
  106.                 'block_prefix' => 'reservation_date',
  107.                 'input' => 'datetime',
  108.                 'required' => true,
  109.             ])
  110.             ->add('contact'ContactTunnelType::class, [
  111.                 'block_prefix' => 'contact',
  112.                 'block_name' => 'contact',
  113.                 'label' => false,
  114.             ])
  115.             ->add('participants'CollectionType::class, [
  116.                 'block_prefix' => 'participants',
  117.                 'entry_type' => ParticipantType::class,
  118.                 'allow_add' => true,
  119.                 'allow_delete' => true,
  120.                 'entry_options' => ['label' => false],
  121.                 'by_reference' => false,
  122.                 'attr' => [
  123.                     'class' => 'mx-auto flex flex-col gap-3 participantsDetails',
  124.                 ],
  125.             ])
  126.             ->add('cgv'CheckboxType::class, [
  127.                 'block_prefix' => 'cgv',
  128.                 'label' => false,
  129.                 'required' => true,
  130.                 'mapped' => false,
  131.             ])
  132.         ;
  133.     }
  134.     public function configureOptions(OptionsResolver $resolver): void
  135.     {
  136.         $resolver->setDefaults([
  137.             'data_class' => Token::class,
  138.             'constraints' => [
  139.                 new Callback([$this'validateExtraReservations'])
  140.             ]
  141.         ]);
  142.     }
  143.     public function validateExtraReservations($dataExecutionContextInterface $context)
  144.     {
  145.         /** @var Token $token */
  146.         $token $data;
  147.         foreach ($token->getExtraReservations() as $extraReservation) {
  148.             if ($extraReservation->isEmpty()) {
  149.                 $token->removeExtraReservation($extraReservation);
  150.             }
  151.         }
  152.     }
  153. }