<?php
namespace App\Form;
use App\Entity\Appointment;
use App\Entity\AppointmentCategory;
use App\Repository\ParametersRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AppointmentType extends AbstractType
{
public function __construct( ParametersRepository $parametersRepository)
{
$this->params = $parametersRepository->findOneBy([]);
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('lastName', TextType::class, [
'label' => 'Votre nom de famille',
])
->add('firstName', TextType::class, [
'label' => 'Votre prénom',
])
->add('email', EmailType::class, [
'label' => 'Votre email',
])
->add('phone', TextType::class, [
'label' => 'Votre téléphone (10 chiffres)',
'required' => false,
'attr' => [
'pattern'=> '\d{10}$',
'placeholder' => '0000000000',
'maxlength' => 10
]
])
->add('reason', ChoiceType::class, [
'label' => 'Raison de votre rendez-vous',
'required' => true,
'choices' => $this->getReasonsChoices(),
'multiple' => false,
'expanded' => false,
])
->add('inscriptionType', ChoiceType::class, [
'label' => 'S\'agit il',
'choices' => $this->makeCategoryChoices(),
'expanded' => true,
'multiple' => false,
'empty_data' => 'NEW'
])
->add('childCount', NumberType::class, [
'label' => 'Combien d\'enfants souhaitez vous inscrire ?',
'required' => true,
'html5' => true,
'attr' => [
'data-controller' => 'children',
'data-action' => 'change->children#addInputs',
'class' => 'small-number'
],
'empty_data' => 0
])
->add('slot', HiddenType::class, [
'mapped' => false,
])
;
}
private function getReasonsChoices(): array
{
$choices = [];
$reasons = $this->params->getAppointmentReasons();
if(!$reasons) {
return [
'Inscriptions mercredis' => 'Inscriptions mercredis',
'Inscriptions vacances' => 'Inscriptions vacances',
];
}
foreach ($reasons as $reason) {
$choices[$reason] = $reason;
}
return $choices;
}
private function makeCategoryChoices()
{
$choices = [];
foreach (AppointmentCategory::CATEGORIES as $identifier => $label) {
$choices[$label] = $identifier;
}
return $choices;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Appointment::class,
'allow_extra_fields' => true,
]);
}
}