src/Form/ExtraReservationType.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Token;
  4. use App\Entity\Reservation;
  5. use App\Entity\Privatisation;
  6. use App\Service\ExtraService;
  7. use App\Entity\ExtraReservation;
  8. use App\Entity\InterfaceReservation;
  9. use Symfony\Component\Form\FormView;
  10. use Symfony\Component\Form\FormEvent;
  11. use Symfony\Component\Form\FormEvents;
  12. use Symfony\Component\Form\AbstractType;
  13. use App\Form\OptionSelectReservationType;
  14. use Symfony\Component\Form\FormInterface;
  15. use App\Service\OptionReservationService;
  16. use App\Form\OptionQuantityReservationType;
  17. use App\Form\OptionReservationCollectionType;
  18. use Symfony\Component\Form\FormBuilderInterface;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  21. class ExtraReservationType extends AbstractType
  22. {
  23.     public function __construct(
  24.         private ExtraService $extraService,
  25.         private OptionReservationService $optionReservationService
  26.     )
  27.     {}
  28.     public function buildForm(FormBuilderInterface $builder, array $options): void
  29.     {
  30.         // Sert à ajouter les types des extra
  31.         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
  32.             /** @var ExtraReservation $extraReservation */
  33.             $extraReservation $event->getData();
  34.             if($extraReservation) {
  35.                 $form $event->getForm();
  36.                 $this->addExtraReservationFields($form$extraReservation);
  37.             }
  38.         });
  39.         $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
  40.             /** @var ExtraReservation $extraReservation */
  41.             $extraReservation $event->getData();
  42.             $form $event->getForm();
  43.             $reservation $form->getConfig()->getOption('reservation');
  44.             if ($extraReservation) {
  45.                 switch (get_class($reservation)) {
  46.                     case Privatisation::class:
  47.                         $extraReservation->setPrivatisation($reservation);
  48.                         break;
  49.                     case Token::class:
  50.                         $extraReservation->setToken($reservation);
  51.                         break;
  52.                     case Reservation::class:
  53.                         $extraReservation->setReservation($reservation);
  54.                         break;
  55.                     default:
  56.                         throw new \InvalidArgumentException('Type de réservation inconnu');
  57.                 }
  58.             }
  59.             
  60.             if(!$extraReservation) {
  61.                 // Principalement utilisé pour ajouter les types des extraReservations single object 
  62.                 $extraData $form->getExtraData();
  63.                 if(isset($extraData['idExtra'])) {
  64.                     $extra $this->extraService->getExtraById($extraData['idExtra']);
  65.                     $extraReservation = new ExtraReservation;
  66.                     $extraReservation->setExtra($extra);
  67.                     $this->optionReservationService->generateOptionReservation($extraReservation);
  68.                     foreach($extraReservation->getOptionQuantityReservations() as $index => $optionQuantityReservation) {
  69.                         if(isset($extraData['optionQuantityReservations'][$index])) {
  70.                             $optionQuantityReservation->setQuantity(intval($extraData['optionQuantityReservations'][$index]['quantity']));
  71.                         }
  72.                         
  73.                     }
  74.                     foreach($extraReservation->getOptionSelectReservations() as $index => $optionSelectReservation) {
  75.                         if(isset($extraData['optionSelectReservations'][$index])) {
  76.                             $idOptionSelectItem $extraData['optionSelectReservations'][$index]['optionSelectItem'];
  77.                             $optionSelectItem $this->optionReservationService->getOptionSelectItemById($idOptionSelectItem);
  78.                             $optionSelectReservation->setOptionSelectItem($optionSelectItem);
  79.                         }
  80.                     }
  81.                     
  82.                     if(isset($extraData['commentary'])) {
  83.                         $extraReservation->setCommentary($extraData['commentary']);
  84.                     }
  85.                     $event->setData($extraReservation);
  86.                 }
  87.             } 
  88.         });
  89.     }
  90.     public function buildView(FormView $viewFormInterface $form, array $options)
  91.     {
  92.         /** @var ExtraReservation $extraReservation */
  93.         $extraReservation $form->getData();
  94.         $parentFullName $view->parent->vars['full_name'];
  95.         if ($extraReservation instanceof ExtraReservation && $extraReservation->getExtra()->isSingleObject()) {
  96.             if(is_null($extraReservation->getId())) {
  97.                 $namePlaceholder '__name__';
  98.                 $parentFullName $view->parent->vars['full_name'];
  99.                 $id sprintf('%s_%s'$view->parent->vars['id'], $namePlaceholder);
  100.                 $fullName sprintf('%s[%s]'$parentFullName$namePlaceholder);
  101.                 $view->vars['name'] = $view->vars['name'] * 1000;
  102.                 $view->vars['full_name'] = $fullName;
  103.                 $view->vars['id'] = $id;
  104.             }
  105.         }
  106.         if($extraReservation) {
  107.             $view->vars['price'] = $this->extraService->getPriceExtra($extraReservation);
  108.         }
  109.     }
  110.     public function configureOptions(OptionsResolver $resolver): void
  111.     {
  112.         $resolver->setRequired(
  113.             ['reservation']
  114.         );
  115.         $resolver->setAllowedTypes('reservation'InterfaceReservation::class);
  116.         $resolver->setDefaults([
  117.             'data_class' => ExtraReservation::class,
  118.             'allow_extra_fields' => true,
  119.         ]);
  120.     }
  121.     private function addExtraReservationFields(FormInterface $formExtraReservation $extraReservation): void
  122.     {
  123.         if(!$extraReservation->getOptionQuantityReservations()->isEmpty()) {
  124.             $form->add('optionQuantityReservations'OptionReservationCollectionType::class, [
  125.                 'entry_type' => OptionQuantityReservationType::class,
  126.                 'allow_add' => true,
  127.                 'allow_extra_fields' => true,
  128.                 'entry_options' => [
  129.                     'with_total_quantities' => $extraReservation->getExtra()->isSingleObject() ? false true
  130.                 ],
  131.             ]);
  132.         }
  133.         if(!$extraReservation->getOptionSelectReservations()->isEmpty()) {
  134.             $form->add('optionSelectReservations'OptionReservationCollectionType::class, [
  135.                 'entry_type' => OptionSelectReservationType::class,
  136.                 'allow_add' => true,
  137.                 'allow_extra_fields' => true,
  138.             ]);
  139.         }
  140.         if($extraReservation->getExtra()->getPlaceholderCommentary()) {
  141.             $form->add('commentary'TextareaType::class, [
  142.                 'attr' => [
  143.                     'placeholder' => $extraReservation->getExtra()->getPlaceholderCommentary(),
  144.                 ],
  145.             ]);
  146.         }
  147.     }
  148. }