You are here

function scheduler_admin_validate in Scheduler 7

Same name and namespace in other branches
  1. 6 scheduler.module \scheduler_admin_validate()

Form validation handler for scheduler_admin().

File

./scheduler.admin.inc, line 121
Administration forms for the Scheduler module.

Code

function scheduler_admin_validate($form, &$form_state) {

  // Replace all contiguous whitespaces (including tabs and newlines) with a
  // single plain space.
  $form_state['values']['scheduler_date_format'] = trim(preg_replace('/\\s+/', ' ', $form_state['values']['scheduler_date_format']));

  // Validate the letters used in the scheduler date format. All punctuation is
  // accepted, so remove everything except word characters then check that there
  // is nothing else which is not in the list of acceptable date/time letters.
  $no_punctuation = preg_replace('/[^\\w+]/', '', $form_state['values']['scheduler_date_format']);
  if (preg_match_all('/[^' . SCHEDULER_DATE_LETTERS . SCHEDULER_TIME_LETTERS . ']/', $no_punctuation, $extra)) {
    form_set_error('scheduler_date_format', t('You may only use the letters $date_letters for the date and $time_letters for the time. Remove the extra characters $extra', array(
      '$date_letters' => SCHEDULER_DATE_LETTERS,
      '$time_letters' => SCHEDULER_TIME_LETTERS,
      '$extra' => implode(' ', $extra[0]),
    )));
  }
  $time_format = scheduler_get_time_only_format($form_state['values']['scheduler_date_format']);
  if ($form_state['values']['scheduler_field_type'] == 'date_popup') {

    // The Date Popup function date_popup_time_formats() only returns the values
    // 'H:i:s' and 'h:i:sA' but Scheduler can accept more variations than just
    // these. Firstly, we add the lowercase 'a' alternative. Secondly timepicker
    // always requires hours and minutes, but seconds are optional. Spaces are
    // allowed before the 'A' or 'a'.
    $acceptable = array(
      'H:i:s',
      'h:i:sA',
      'h:i:s A',
      'h:i:sa',
      'h:i:s a',
      'H:i',
      'h:iA',
      'h:i A',
      'h:ia',
      'h:i a',
    );
    if ($time_format && !in_array($time_format, $acceptable)) {
      form_set_error('scheduler_date_format', t('When using the Date Popup module, the allowed time formats are: !formats', array(
        '!formats' => implode(', ', $acceptable),
      )));
    }
  }

  // If date-only is enabled then check if a valid default time was entered.
  // Leading zeros and seconds can be omitted, eg. 6:30 is considered valid.
  if ($form_state['values']['scheduler_allow_date_only']) {
    $default_time = date_parse($form_state['values']['scheduler_default_time']);
    if ($default_time['error_count']) {
      form_set_error('scheduler_default_time', t('The default time should be in the format HH:MM:SS'));
    }
    else {

      // Insert any possibly omitted leading zeroes.
      $unix_time = mktime($default_time['hour'], $default_time['minute'], $default_time['second']);
      $form_state['values']['scheduler_default_time'] = format_date($unix_time, 'custom', 'H:i:s');
    }
  }

  // Check that either the date format has a time part or the date-only option
  // is turned on.
  if ($time_format == '' && !$form_state['values']['scheduler_allow_date_only']) {
    form_set_error('scheduler_date_format', t('You must either include a time within the date format or enable the date-only option.'));
  }
}