You are here

function appointment_calendar_validate in Appointment Calendar 8

Implements validate function for appointment calendar validate.

1 string reference to 'appointment_calendar_validate'
appointment_calendar_form_alter in ./appointment_calendar.module
implements hook_form_alter().

File

./appointment_calendar.module, line 88

Code

function appointment_calendar_validate(array &$form, FormStateInterface $form_state) {
  $db_conn = \Drupal::database();

  // Form state Values.
  $values = $form_state
    ->getValues();
  $appointment_date = strtotime(date('Y-m-d', $values['field_appointment_date'][0]['value']
    ->getTimestamp()));
  $appointment_slot = $values['field_appointment_slot'];

  //  if ($appointment_slot == NULL || $appointment_slot == '') {
  //    $form_state->setErrorByName('field_appointment_slot', t('Time slot not available for Selected slot'));
  //  }
  $uid = $values['uid'][0]['target_id'];

  // Get Current Path
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $path_args = explode('/', $current_path);
  if ($path_args[1] == 'node' && $path_args[2] == 'add') {
    $check_for_booked = appointment_calendar_check_user($appointment_date, $appointment_slot, $uid);
    if ($check_for_booked >= 1) {
      $form_state
        ->setErrorByName('field_appointment_date', t('Time slot already booked for Selected Date'));
      $form_state
        ->setErrorByName('field_appointment_slot', t('Time slot already booked for Selected slot'));
    }
  }
  $count_values = [];
  $date_query = $db_conn
    ->select('appointment_slots', 'ap');
  $date_query
    ->fields('ap', [
    'slot',
  ]);
  $date_query
    ->condition('date', $appointment_date, '=');
  $date_queryresult = $date_query
    ->execute()
    ->fetchAll(PDO::FETCH_ASSOC);
  foreach ($date_queryresult as $slot_values) {
    if ($slot_values['slot']) {
      @$count_values[$slot_values['slot']]++;
    }
  }
  if ($count_values) {
    ksort($count_values);
    $slot_result = appointment_calendar_slot_capacity($appointment_date);
    foreach ((array) json_decode($slot_result) as $key => $value) {
      $options[$key] = $value;
    }
    $keys = array_keys($count_values);
    for ($a = 0; $a < count($keys); $a++) {
      if ($count_values[$keys[$a]] >= $options[$keys[$a]] && $appointment_slot == $keys) {
        $form_state
          ->setErrorByName('field_appointment_slot', t('Time slot not available for Selected Date'));
      }
    }
  }
}