src/Form/CarteCadeauType.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Token;
  4. use App\Form\ExtraReservationType;
  5. use App\Service\CarteCadeauService;
  6. use Symfony\Component\Form\FormEvent;
  7. use Symfony\Component\Form\FormEvents;
  8. use Symfony\Component\Form\AbstractType;
  9. use App\Form\Components\PhysicalCardType;
  10. use App\Form\ExtraReservationCollectionType;
  11. use App\Form\Components\TemplateCarteCadeauType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\Validator\Constraints\Callback;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  18. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  19. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  20. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  21. class CarteCadeauType extends AbstractType
  22. {
  23.     public function __construct(
  24.         private TranslatorInterface $translator,
  25.         private CarteCadeauService $carteCadeauService,
  26.     ) {}
  27.     
  28.     public function buildForm(FormBuilderInterface $builder, array $options): void
  29.     {
  30.         $templates $this->carteCadeauService->getAllTemplate();
  31.         foreach($templates as $template) {
  32.             $templateName $template->getTemplateName();
  33.             $choices[$templateName] = $templateName;
  34.             $choiceAttr = [
  35.                 'src' => $template->getPathPreview(),
  36.                 'alt' => $templateName,
  37.                 'label' => $template->getTemplateLabel(),
  38.             ];
  39.             $choicesAttr[$templateName] = $choiceAttr;
  40.         }
  41.         $builder
  42.             ->add('nbParticipants'NumberType::class, [
  43.                 'block_prefix' => 'nb_participants',
  44.                 'mapped' => false,
  45.                 'attr' => [
  46.                     'min' => 1,
  47.                     'max' => 16,
  48.                     'class' => 'nbParticipants',
  49.                     'max_privatisation' => 8,
  50.                     'info_privatisation' => "form.reservation.nbParticipants.privatisation",
  51.                 ],
  52.                 'data' => 2,
  53.             ])
  54.             ->add('nbBath'NumberType::class, [
  55.                 'block_prefix' => 'nb_baths',
  56.                 'mapped' => false,
  57.                 'attr' => [
  58.                     'min' => 1,
  59.                     'max' => 4,
  60.                     'class' => 'nbBath',
  61.                 ],
  62.             ])
  63.             ->add('solo'null, [
  64.                 'attr' => [
  65.                     'class' => 'solo hidden'
  66.                 ]
  67.             ])
  68.             ->add('duo'null, [
  69.                 'attr' => [
  70.                     'class' => 'duo hidden'
  71.                 ]
  72.             ])
  73.             ->add('offer'ChoiceType::class, [
  74.                 'block_prefix' => 'offer',
  75.                 'choices' => [
  76.                     'form.reservation.la_bulle.label' => 60,
  77.                     'form.reservation.la_grande_bulle.label' => 90,
  78.                 ],
  79.                 'choice_attr' => [
  80.                     'form.reservation.la_bulle.label' => [
  81. //                        'image_path' => "/vitrine_old/elements/select-plongeon.svg",
  82.                         'label' => "form.reservation.la_bulle.label",
  83.                         'duration' => "form.reservation.la_bulle.duration",
  84.                         'summary_label' => "tunnel.summary.offer.label_bulle",
  85.                         'bath_duration' => 30,
  86.                     ],
  87.                     'form.reservation.la_grande_bulle.label' => [
  88. //                        'image_path' => "/vitrine_old/elements/select-grand-plongeon.svg",
  89.                         'label' => "form.reservation.la_grande_bulle.label",
  90.                         'duration' => "form.reservation.la_grande_bulle.duration",
  91.                         'summary_label' => "tunnel.summary.offer.label_grande_bulle",
  92.                         'bath_duration' => 60,
  93.                     ],
  94.                 ],
  95.                 'expanded' => true,
  96.                 'multiple' => false,
  97.             ])
  98.             ->add('extraReservations'ExtraReservationCollectionType::class, [
  99.                 'entry_type' => ExtraReservationType::class,
  100.                 'allow_add' => true,
  101.                 'allow_delete' => true,
  102.                 'allow_extra_fields' => true,
  103.                 'by_reference' => false,
  104.                 'required' => false,
  105.                 'prototype' => true,
  106.                 'entry_options' => ['reservation' => $options['data']],
  107.             ])
  108.             ->add('templateCarteCadeau'TemplateCarteCadeauType::class, [
  109.                 'block_prefix' => 'carte_cadeau_tunnel',
  110.                 'choices' => $choices,
  111.                 'expanded' => true,
  112.                 'multiple' => false,
  113.                 'choice_attr' => $choicesAttr,
  114.                 'attr' => ['class' => 'gift-card-options'],
  115.                 'data' => "NORMALE"
  116.             ])
  117.             ->add('attention'TextareaType::class, [
  118.                 'attr' => [
  119.                     'class' => 'resize-none border border-black border-b-0 h-24 w-full p-3 pb-5 -mb-2 focus:outline-none',
  120.                     'id' => 'attention',
  121.                     'placeholder' => 'tunnel.carte_cadeau.attention.placeholder',
  122.                     'maxlength' => 196,
  123.                 ],
  124.                 'required' => false,
  125.             ])
  126.            ->add('physicalCard'CheckboxType::class, [
  127.                'required' => false,
  128.                'label' => 'Impression + envoi postal',
  129.                'block_prefix' => 'physical_card_checkbox',
  130.                'attr' => [
  131.                    'sublabel' => 'Frais de livraison',
  132.                    'class' => 'physical-card-checkbox',
  133.                    'data-postage-price' => 0,
  134.                ],
  135.                'data' => false,
  136.            ])
  137.             ->add('contact'ContactTunnelType::class, [
  138.                 'block_prefix' => 'contact',
  139.                 'block_name' => 'contact',
  140.                 'label' => false,
  141.             ])
  142.             ->add('cgv'CheckboxType::class, [
  143.                 'block_prefix' => 'cgv',
  144.                 'label' => false,
  145.                 'required' => true,
  146.                 'mapped' => false,
  147.             ])
  148.            ->add('deliveryAddress'DeliveryAddressType::class, [
  149.                'useAdresseFacturation_options' => [
  150.                    'data' => false,
  151.                ]
  152.            ])
  153.         ;
  154.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  155.             /** @var Token $token */
  156.             $token $event->getData();
  157.             $form $event->getForm();
  158.             // Clear delivery address if physicalCard is unchecked
  159.             if (!$token->isPhysicalCard()) {
  160.                 $token->setDeliveryAddress(null);
  161.             }
  162.         });
  163.     }
  164.     public function configureOptions(OptionsResolver $resolver): void
  165.     {
  166.         $resolver->setDefaults([
  167.             'data_class' => Token::class,
  168.             'constraints' => [
  169.                 new Callback([$this'validateExtraReservations'])
  170.             ]
  171.         ]);
  172.     }
  173.     public function validateExtraReservations($dataExecutionContextInterface $context)
  174.     {
  175.         /** @var Token $token */
  176.         $token $data;
  177.         foreach ($token->getExtraReservations() as $extraReservation) {
  178.             if ($extraReservation->isEmpty()) {
  179.                 $token->removeExtraReservation($extraReservation);
  180.             }
  181.         }
  182.     }
  183. }