You are here

public function AppointmentCalendarEditForm::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/AppointmentCalendarEditForm.php, line 146

Class

AppointmentCalendarEditForm

Namespace

Drupal\appointment_calendar\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $op = (string) $values['op'];
  if ($op == $this
    ->t('Add More Slots')) {
    $form_state
      ->setRebuild();
  }

  // Pass values to url.
  if ($op == $this
    ->t('Submit')) {
    $date = $values['appointment_slot_date']
      ->getTimestamp();
    $capacity = appointment_calendar_slot_capacity($date);
    $slots = count((array) json_decode($capacity));
    if (!empty($values['appointment_slot'])) {
      $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;
    }

    // Saving date with time slots and capacity.
    $db_conn = \Drupal::database();
    $db_conn
      ->merge('appointment_date')
      ->key([
      'date' => $date,
    ])
      ->fields([
      'no_slots' => $slots,
      'slot_values' => json_encode($slots_save),
      'slot_capacity' => json_encode($slots_capacity),
    ])
      ->execute();
    $this
      ->messenger()
      ->addStatus(t('Changes made successfully'));
    $form_state
      ->setRedirect('appointment_calendar.list_page');
  }
}