You are here

function date_validation_field_validation_validate in Field Validation 7

Implements hook_field_validation_validate().

File

date_validation/date_validation.validators.inc, line 40
Provides validation functionality and hooks

Code

function date_validation_field_validation_validate($validator_name, $rule, $entity, $langcode, $items, &$errors) {
  if (!empty($items)) {
    switch ($validator_name) {
      case "date_range":
        $date_range = _date_validation_date_range_check_data($rule['data']);
        $unit = isset($date_range['unit']) ? $date_range['unit'] : 'global';
        $start = isset($date_range['start']) ? $date_range['start'] : '';
        $end = isset($date_range['end']) ? $date_range['end'] : '';
        $start_contain = isset($date_range['start_contain']) ? $date_range['start_contain'] : TRUE;
        $end_contain = isset($date_range['end_contain']) ? $date_range['end_contain'] : TRUE;
        $less = isset($date_range['less']) ? $date_range['less'] : FALSE;

        //debug($date_range);
        foreach ($items as $delta => $item) {
          $flag = FALSE;
          $data = $item[$rule['col']];
          if ($data != '') {

            // support date, datetime
            if (!is_numeric($data)) {
              $data = strtotime($data);
            }
            $date_str = date("Y-m-d H:i:s", $data);

            //drupal_set_message('123:'.$date_str);

            //$unit= 'global';
            if ($unit == 'year') {
              $date_str = substr($date_str, 5);
            }
            elseif ($unit == 'month') {
              $date_str = substr($date_str, 8);
            }
            elseif ($unit == 'week') {
              $week_day = date('w', strtotime($date_str));
              $date_str = substr($date_str, 10);
              $date_str = $week_day . $date_str;
            }
            elseif ($unit == 'day') {
              $date_str = substr($date_str, 11);
            }
            elseif ($unit == 'hour') {
              $date_str = substr($date_str, 14);
            }
            elseif ($unit == 'minute') {
              $date_str = substr($date_str, 17);
            }
            if ($date_str < $start) {
              $flag = TRUE;
            }
            if ($date_str > $end) {
              $flag = TRUE;
            }
            if (!$start_contain && $date_str == $start) {
              $flag = TRUE;
            }
            if (!$end_contain && $date_str == $end) {
              $flag = TRUE;
            }
            if ($less) {
              $flag = $flag ? FALSE : TRUE;
            }
            if ($flag) {
              $error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
              $message = t($rule['error_message']);
              form_set_error($error, check_plain($message));
            }
          }
        }
        break;
    }
  }
}