You are here

function check_slot_availability in Appointment Calendar 8

Implements for slot availability check on selected date.

1 call to check_slot_availability()
appointment_calendar_form_alter in ./appointment_calendar.module
implements hook_form_alter().

File

./appointment_calendar.module, line 240

Code

function check_slot_availability($selected_date) {
  $result = [];
  $db_conn = \Drupal::database();
  $date_query = $db_conn
    ->select('appointment_slots', 'ap');
  $date_query
    ->fields('ap', [
    'slot',
  ]);
  $date_query
    ->leftJoin('node_field_data', 'node_data', 'node_data.nid = ap.nid');
  $date_query
    ->condition('date', $selected_date, '=');
  $date_query
    ->condition('node_data.status', 1);
  $date_queryresult = $date_query
    ->execute()
    ->fetchAll(PDO::FETCH_ASSOC);
  $slot_result = appointment_calendar_slot_capacity($selected_date);
  if ($date_queryresult) {
    foreach ($date_queryresult as $slot_values) {
      if ($slot_values['slot']) {
        @$count_values[$slot_values['slot']]++;
      }
    }
    if (!empty($slot_result)) {
      if (!empty($count_values)) {
        ksort($count_values);
      }
      foreach ((array) json_decode($slot_result) as $key => $value) {
        $options[$key] = $value;
      }
      $keys = array_keys($options);
      for ($a = 0; $a < count($keys); $a++) {
        if (isset($count_values[$keys[$a]]) && $count_values[$keys[$a]] >= $options[$keys[$a]]) {
          $result[$keys[$a]][] = t('Booked');
          $result[$keys[$a]]['class'] = 'booked';
          $result[$keys[$a]]['style'] = 'color: red;';
        }
        else {
          $result[$keys[$a]][] = t('Available');
          $result[$keys[$a]]['class'] = 'available';
          $result[$keys[$a]]['style'] = 'color: #00ff47;';
        }
      }
    }
  }
  else {
    foreach ((array) json_decode($slot_result) as $key => $value) {
      $result[$key][] = t('Available');
      $result[$key]['class'] = 'available';
      $result[$key]['style'] = 'color: #00ff47;';
    }
  }
  return $result;
}