You are here

function appointment_calendar_node_form_validate in Appointment Calendar 7

Implements hook_form_validate().

1 string reference to 'appointment_calendar_node_form_validate'
appointment_calendar_form_alter in ./appointment_calendar.module
Implements hook_form_alter().

File

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

Code

function appointment_calendar_node_form_validate($form, &$form_state) {

  // In Appointment calendar node.
  if (arg(1) == 'add') {
    if ($form_state['values']['op'] == t('Save')) {
      $form['#attached']['js'] = array(
        drupal_get_path('module', 'appointment_calendar') . '/js/appointment_calendar.js',
      );
      $time_slot = $form_state['values']['appointment_slot'][LANGUAGE_NONE][0]['value'];

      // Check for empty timeslot.
      if (strlen($time_slot) == 0) {
        form_set_error('appointment_slot', t('Time slot not available for Selected Date'));
        return FALSE;
      }

      // Checking for completed slots before submit.
      $selected_date = $form_state['values']['appointment_date'][LANGUAGE_NONE][0]['value'];
      $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', $selected_date, '=');
      $date_queryresult = $date_query
        ->execute()
        ->fetchAll(PDO::FETCH_ASSOC);
      $date = explode('T', $selected_date);
      $slot_capacity = appointment_calendar_slot_capacity(strtotime($date[0]));

      // Booked slots is greater than or equal to allowed capacity.
      if (!empty($slot_capacity)) {
        $capacity = json_decode($slot_capacity);
        if (count($date_queryresult) >= $capacity->{$time_slot}) {
          $form_state['values']['appointment_date'][LANGUAGE_NONE][0]['value'] = '';
          form_set_error('appointment_date', t('Time slot not available/booked for Selected Date'));
          form_set_error('appointment_slot', t('Time slot not available/booked for Selected Date'));
        }
      }
    }
  }
  if (arg(2) == 'edit') {
    if ($form_state['values']['op'] == t('Save')) {
      $node_load = node_load(arg(1));
      $previous_slot = $node_load->appointment_slot[LANGUAGE_NONE][0]['value'];
      drupal_add_js(drupal_get_path('module', 'appointment_calendar') . '/js/appointment_calendar.js');
      $time_slot = $form_state['values']['appointment_slot'][LANGUAGE_NONE][0]['value'];
      $selected_date = $form_state['values']['appointment_date'][LANGUAGE_NONE][0]['value'];

      // Check for empty timeslot.
      if (strlen($time_slot) == 0) {
        form_set_error('appointment_slot', t('Time slot not available for Selected Date'));
        return FALSE;
      }
      $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', $selected_date, '=');
      $date_queryresult = $date_query
        ->execute()
        ->fetchAll(PDO::FETCH_ASSOC);
      $date = explode('T', $selected_date);
      $slot_capacity = appointment_calendar_slot_capacity(strtotime($date[0]));

      // Booked slots is greater than or equal to allowed capacity.
      // If pervious saved slot is not equal to new selected slot anda date.
      if ($previous_slot != $time_slot) {
        if (!empty($slot_capacity)) {
          $capacity = json_decode($slot_capacity);
          if (count($date_queryresult) >= $capacity->{$time_slot}) {
            $form_state['values']['appointment_date'][LANGUAGE_NONE][0]['value'] = '';
            form_set_error('appointment_date', t('Time slot not available/booked for Selected Date'));
            form_set_error('appointment_slot', t('Time slot not available/booked for Selected Date'));
          }
        }
      }
    }
  }
}