You are here

public function AppointmentCalendarForm::submitForm in Appointment Calendar 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/AppointmentCalendarForm.php, line 168

Class

AppointmentCalendarForm

Namespace

Drupal\appointment_calendar\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $db_conn = \Drupal::database();
  $op = (string) $values['op'];
  if ($op == $this
    ->t('Submit')) {
    $start_date = $values['appointment_from_date']
      ->getTimestamp();
    $end_date = $values['appointment_to_date']
      ->getTimestamp();
    $slots = $values['appointment_slot'];

    // Fetching Timeslot and capacity values.
    for ($i = 1; $i <= $slots; $i++) {
      $time_slot = $values['time_slot_' . $i];
      $time_capacity = $values['time_slot_' . $i . '_capacity'];
      $slots_save[$time_slot] = $time_slot;
      $slots_capacity[$time_slot] = $time_capacity;
    }
    ksort($slots_save);
    ksort($slots_capacity);

    // Getting all dates in between Start and End Dates.
    $dates = appointment_calendar_daysbetween($start_date, $end_date);

    // Saving date with time slots and capacity.
    foreach ($dates as $each_date) {
      $db_conn
        ->merge('appointment_date')
        ->key([
        'date' => $each_date,
      ])
        ->fields([
        'no_slots' => $slots,
        'slot_values' => json_encode($slots_save),
        'slot_capacity' => json_encode($slots_capacity),
      ])
        ->execute();
    }

    // Redirect to list page.
    $this
      ->messenger()
      ->addStatus(t('Slot(s) created successfully'));
  }
}