You are here

function appointment_calendar_settings_form_validate in Appointment Calendar 7

Implements hook_form_validate().

File

./appointment_calendar_admin_settings.inc, line 99
Provide Configuration form for Appointment calendar.

Code

function appointment_calendar_settings_form_validate($form, &$form_state) {

  // For settings page.
  if ($form_state['values']['op'] == t('Reset')) {
    drupal_goto(current_path());
  }
  if ($form_state['values']['op'] == t('Submit')) {
    $start_date = strtotime($form_state['values']['appointment_from_date']);
    $end_date = strtotime($form_state['values']['appointment_to_date']);

    // Getting all dates in between Start and End Dates.
    $dates = appointment_calendar_daysbetween($start_date, $end_date);
    $check_count = 0;

    // Checking for already filled slots.
    foreach ($dates as $each_date) {
      $date_check = db_select('appointment_date', 'ad');
      $date_check
        ->fields('ad', array(
        'date',
      ));
      $date_check
        ->condition('date', $each_date, '=');
      $date_result = $date_check
        ->execute()
        ->fetchField();
      if (!empty($date_result)) {
        $check_count++;
        $date = date('Y-m-d', $each_date);
        drupal_set_message($date . ' Already filled', 'error');
      }
    }
    if ($check_count > 0) {
      form_set_error('appointment_from_date', t('Verify and try adding again'));
    }

    // Date Validation.
    if ($start_date > $end_date) {
      form_set_error('appointment_to_date', t('End Date Should be greater than Start Date'));
    }
    if ($start_date < strtotime(date('Y-m-d', time()))) {
      form_set_error('appointment_start_date', t('Start Date Should be greater than Today(s) Date'));
    }
    $slots = $form_state['values']['appointment_slot'];

    // Time slot and Capacity Validation.
    $time_slots_check = array();
    for ($i = 1; $i <= $slots; $i++) {
      $time_slot = $form_state['values']['time_slot_' . $i];
      $time_capacity = $form_state['values']['time_slot_' . $i . '_capacity'];
      $regex = '/^(?:[01][0-9]|2[0-3]):[0-5][0-9]-(?:[01][0-9]|2[0-3]):[0-5][0-9]$/';

      // Timeslot.
      if (!preg_match($regex, $time_slot)) {
        form_set_error('time_slot_' . $i, t('Time slot format should be 00:00-00:00 (in between 24 hrs).'));
      }

      // Slot Capacity.
      if ($time_capacity < 0) {
        form_set_error('time_slot_' . $i . '_capacity', t('Slot Capacity should be greater than 0.'));
      }

      // Timeslot Check.
      if (empty($time_capacity)) {
        form_set_error('time_slot_' . $i . '_capacity', t('Fill time slot capacity.'));
      }
      if (!empty($time_capacity) && !is_numeric($time_capacity)) {
        form_set_error('time_slot_' . $i . '_capacity', t('Time slot capacity should be Numeric.'));
      }

      // Checking duplicate slots.
      $time_slots_check[] = $time_slot;
      $vals = array_count_values($time_slots_check);
      if ($vals[$time_slot] > 1) {
        form_set_error('time_slot_' . $i, t('Time slot cannot redeclare twice or more.'));
      }
    }
  }
}