<?php
namespace App\Form;
use App\Entity\Token;
use App\Form\ExtraReservationType;
use App\Service\CarteCadeauService;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\AbstractType;
use App\Form\Components\PhysicalCardType;
use App\Form\ExtraReservationCollectionType;
use App\Form\Components\TemplateCarteCadeauType;
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\NumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class CarteCadeauType extends AbstractType
{
public function __construct(
private TranslatorInterface $translator,
private CarteCadeauService $carteCadeauService,
) {}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$templates = $this->carteCadeauService->getAllTemplate();
foreach($templates as $template) {
$templateName = $template->getTemplateName();
$choices[$templateName] = $templateName;
$choiceAttr = [
'src' => $template->getPathPreview(),
'alt' => $templateName,
'label' => $template->getTemplateLabel(),
];
$choicesAttr[$templateName] = $choiceAttr;
}
$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' => 2,
])
->add('nbBath', NumberType::class, [
'block_prefix' => 'nb_baths',
'mapped' => false,
'attr' => [
'min' => 1,
'max' => 4,
'class' => 'nbBath',
],
])
->add('solo', null, [
'attr' => [
'class' => 'solo hidden'
]
])
->add('duo', null, [
'attr' => [
'class' => 'duo hidden'
]
])
->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",
'bath_duration' => 30,
],
'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",
'bath_duration' => 60,
],
],
'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('templateCarteCadeau', TemplateCarteCadeauType::class, [
'block_prefix' => 'carte_cadeau_tunnel',
'choices' => $choices,
'expanded' => true,
'multiple' => false,
'choice_attr' => $choicesAttr,
'attr' => ['class' => 'gift-card-options'],
'data' => "NORMALE"
])
->add('attention', TextareaType::class, [
'attr' => [
'class' => 'resize-none border border-black border-b-0 h-24 w-full p-3 pb-5 -mb-2 focus:outline-none',
'id' => 'attention',
'placeholder' => 'tunnel.carte_cadeau.attention.placeholder',
'maxlength' => 196,
],
'required' => false,
])
->add('physicalCard', CheckboxType::class, [
'required' => false,
'label' => 'Impression + envoi postal',
'block_prefix' => 'physical_card_checkbox',
'attr' => [
'sublabel' => 'Frais de livraison',
'class' => 'physical-card-checkbox',
'data-postage-price' => 0,
],
'data' => false,
])
->add('contact', ContactTunnelType::class, [
'block_prefix' => 'contact',
'block_name' => 'contact',
'label' => false,
])
->add('cgv', CheckboxType::class, [
'block_prefix' => 'cgv',
'label' => false,
'required' => true,
'mapped' => false,
])
->add('deliveryAddress', DeliveryAddressType::class, [
'useAdresseFacturation_options' => [
'data' => false,
]
])
;
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
/** @var Token $token */
$token = $event->getData();
$form = $event->getForm();
// Clear delivery address if physicalCard is unchecked
if (!$token->isPhysicalCard()) {
$token->setDeliveryAddress(null);
}
});
}
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);
}
}
}
}