src/Form/AppointmentType.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Appointment;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  6. use Symfony\Component\Form\Extension\Core\Type\TimeType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. class AppointmentType extends AbstractType
  12. {
  13.     public function buildForm(FormBuilderInterface $builder, array $options): void
  14.     {
  15.         $availableDates $this->getAvailableDates($options['days_of_week']);
  16.         $availableTimes $this->getAvailableTimes();
  17.         $builder
  18.             ->add('firstName')
  19.             ->add('surname')
  20.             ->add('phoneNumber')
  21.             ->add('email')
  22.             ->add('message')
  23.             ->add('date'ChoiceType::class, [
  24.                 'mapped' => false,
  25.                 'label' => 'Date of appointment',
  26.                 'choices' => $availableDates,
  27.                 'attr' => [
  28.                     'class' => 'form-control',
  29.                 ],
  30.             ])
  31.             ->add('time'ChoiceType::class, [
  32.                 'label' => 'Time of appointment',
  33.                 'choices' => $availableTimes,
  34.                 'attr' => [
  35.                     'class' => 'form-control',
  36.                 ],
  37.             ])
  38.         ;
  39.         
  40.         // Event listener to set the date from the choice
  41.         $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
  42.             $data $event->getData();
  43.             $form $event->getForm();
  44.             $date $data['date']; // Get the selected date from the form data
  45.             $appointment $form->getData();
  46.             $appointment->setDate(new \DateTime($date)); // Set the selected date on the Appointment entity
  47.             $event->setData($data);
  48.         });
  49.     }
  50.     public function configureOptions(OptionsResolver $resolver): void
  51.     {
  52.         $resolver->setDefaults([
  53.             'data_class' => Appointment::class,
  54.             'days_of_week' => [24], // Tuesday (2), Thursday (4)
  55.         ]);
  56.     }
  57.     /**
  58.      * Get available dates (Tuesdays and Thursdays) for the next two weeks.
  59.      */
  60.     private function getAvailableDates(array $daysOfWeek): array
  61.     {
  62.         $availableDates = [];
  63.         $currentDate = new \DateTime();
  64.         $interval = new \DateInterval('P1D');
  65.         $endDate = (clone $currentDate)->modify('+2 weeks');
  66.         while ($currentDate <= $endDate) {
  67.             if (in_array($currentDate->format('N'), $daysOfWeek)) { // Filter by Tuesday and Thursday
  68.                 $label $currentDate->format('l d-m-Y'); // e.g., "Tuesday 12-07-2024"
  69.                 $value $currentDate->format('Y-m-d'); // e.g., "2024-07-12"
  70.                 $availableDates[$label] = $value;
  71.             }
  72.             $currentDate->add($interval);
  73.         }
  74.         return $availableDates;
  75.     }
  76.     /**
  77.      * Get available time slots for the appointment.
  78.      */
  79.     private function getAvailableTimes(): array
  80.     {
  81.         return [
  82.             '9:30 AM - 10:30 AM' => '09:30',
  83.             '10:30 AM - 11:30 AM' => '10:30',
  84.             '11:30 AM - 12:30 PM' => '11:30',
  85.             '12:30 PM - 1:30 PM' => '12:30',
  86.             '1:30 PM - 2:30 PM' => '13:30',
  87.             '2:30 PM - 3:30 PM' => '14:30',
  88.             '3:30 PM - 4:30 PM' => '15:30',
  89.         ];
  90.     }
  91. }