You are here

function appointment_calendar_form_alter in Appointment Calendar 7

Same name and namespace in other branches
  1. 8 appointment_calendar.module \appointment_calendar_form_alter()

Implements hook_form_alter().

File

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

Code

function appointment_calendar_form_alter(&$form, &$form_state, $form_id) {

  // In Appointment Calendar node.
  global $base_url;
  if ($form_id == 'appointment_calendar_node_form') {
    $form['appointment_date'][LANGUAGE_NONE][0]['#ajax'] = array(
      'callback' => 'appointment_calendar_appointment_calendar_timeslot_callback_form',
      'wrapper' => 'slot-check',
      'method' => 'replace',
      'effect' => 'fade',
    );
    $form['error_msg'] = array(
      '#markup' => '<div class="error-calendar"></div>',
    );
    $form['appointment_slot']['#prefix'] = '<div id="slot-check">';
    if (!empty($form_state['values'])) {

      // Fetch Timeslots.
      $selected_date = $form_state['values']['appointment_date'][LANGUAGE_NONE][0]['value'];
      $selected_date = explode('T', $form_state['values']['appointment_date'][LANGUAGE_NONE][0]['value']);
      $slot_query = db_select('appointment_date', 'ad');
      $slot_query
        ->fields('ad', array(
        'slot_values',
      ));
      $slot_query
        ->condition('date', strtotime($selected_date[0]));
      $slot_result = $slot_query
        ->execute()
        ->fetchField();
      if (!empty($slot_result)) {
        $form['appointment_slot'][LANGUAGE_NONE]['#options'] = (array) json_decode($slot_result);
      }
    }
    else {
      $selected_date = explode('T', $form['appointment_date'][LANGUAGE_NONE][0]['#default_value']['value']);
      $slot_query = db_select('appointment_date', 'ad');
      $slot_query
        ->fields('ad', array(
        'slot_values',
      ));
      $slot_query
        ->condition('date', strtotime($selected_date[0]));
      $slot_result = $slot_query
        ->execute()
        ->fetchField();
      if (!empty($slot_result)) {
        $form['appointment_slot'][LANGUAGE_NONE]['#options'] = (array) json_decode($slot_result);
      }
    }

    // Set values in edit page.
    if (arg(2) == 'edit') {
      $selected_date = $form['appointment_date'][LANGUAGE_NONE][0]['#default_value']['value'];
      $selected_date = explode('T', $selected_date);
      $slot_query = db_select('appointment_date', 'ad');
      $slot_query
        ->fields('ad', array(
        'slot_values',
      ));
      $slot_query
        ->condition('date', strtotime($selected_date[0]));
      $slot_result = $slot_query
        ->execute()
        ->fetchField();
      $slot_value = appointment_calendar_slot_value(arg(1));

      // Default time slot fetch and values filled for date.
      if (!empty($slot_result)) {
        $form['appointment_slot'][LANGUAGE_NONE]['#options'] = (array) json_decode($slot_result);
        $form['appointment_slot'][LANGUAGE_NONE]['#default_value'] = $slot_value;
      }
    }
    $form['appointment_slot']['#suffix'] = '</div>';
    $form['check'] = array(
      '#type' => 'button',
      '#value' => 'Check Availability',
      '#weight' => 34,
      '#ajax' => array(
        'callback' => 'appointment_calendar_check_availability_form',
        'wrapper' => 'check-availability',
      ),
    );
    $form['calendar'] = array(
      '#weight' => 39,
      '#suffix' => '<div id="check-availability" style="display:none"><iframe width="350px" src= "' . $base_url . '/appointcal"></iframe></div>',
    );

    // Validate function to check availability in submit.
    $form['#validate'][] = 'appointment_calendar_node_form_validate';
    $form['#attached']['js'] = array(
      drupal_get_path('module', 'appointment_calendar') . '/js/appointment_calendar.js',
    );
  }
}