You are here

function appointment_calendar_views_pre_render in Appointment Calendar 7

Implements hook_views_pre_render().

File

./appointment_calendar.module, line 303
Adds Appointment calendar filtering and displays Availability.

Code

function appointment_calendar_views_pre_render(&$view) {

  // If view is appointment calendar block.
  if ($view->name == 'appointment_calendar') {

    // Get Selected Date.
    $query = drupal_get_query_parameters();
    if (!empty($query['date'])) {
      $view->date_info->date_arg = $query['date'];
    }
    $selected_date = $view->date_info->date_arg;

    // Fetch timeslots based on date.
    $slot_query = db_select('appointment_date', 'ad');
    $slot_query
      ->fields('ad', array(
      'slot_values',
    ));
    $slot_query
      ->condition('date', strtotime($selected_date));
    $slot_result = $slot_query
      ->execute()
      ->fetchField();
    $date_values = '';
    $slot_values = '';
    $key = array_keys((array) json_decode($slot_result));
    if (!empty($slot_result)) {
      foreach ($key as $value) {
        $explode = explode('-', $value);
        $date_values .= $explode[0] . ':00,';
        $slot_values .= $value . ',';
      }
    }
    $view->header['area']->options['content'] = rtrim($slot_values, ',');

    // Setting time slot values.
    $view->style_plugin->options['groupby_times_custom'] = rtrim($date_values, ',');

    // Get filled appointment time slots.
    $date_query = db_select('field_data_appointment_date', 'ap');
    $date_query
      ->join('field_data_appointment_slot', 'aps', 'aps.entity_id = ap.entity_id');
    $date_query
      ->fields('aps', array(
      'appointment_slot_value ',
    ));
    $date_query
      ->condition('ap.appointment_date_value', '%' . db_like($selected_date) . '%', 'LIKE');
    $date_queryresult = $date_query
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);
    $count_values = '';
    foreach ($date_queryresult as $slot_values) {
      @$count_values[$slot_values['appointment_slot_value']]++;
    }
    if (!empty($count_values)) {
      ksort($count_values);
    }
    $unset_values = '';
    $slot_capacity = appointment_calendar_slot_capacity(strtotime($selected_date));
    $slot_capacity_array = (array) json_decode($slot_capacity);
    if (!empty($count_values)) {
      $keys = array_keys($count_values);
      $booked = 0;
      for ($count = 0; $count < count($view->result); $count++) {
        $unset_values[] = $count;
      }

      // Checking Booked node count with filled slot capacity.
      for ($a = 0; $a < count($keys); $a++) {

        // Unset View Result if not booked.
        $booked += $count_values[$keys[$a]];
        if ($count_values[$keys[$a]] >= $slot_capacity_array[$keys[$a]]) {
          $view->result[$booked - 1]->field_appointment_date[0]['rendered']['#markup'] = '<span style="color:red">Booked</span>';
          unset($unset_values[$booked - 1]);
        }
      }

      // Unset Remaining View results.
      foreach ($unset_values as $reset) {
        unset($view->result[$reset]);
      }
    }
    drupal_add_js(drupal_get_path('module', 'appointment_calendar') . '/js/calendar.js');
  }
}