You are here

class property_validation_date_range_validator in Field Validation 7.2

Hierarchy

Expanded class hierarchy of property_validation_date_range_validator

1 string reference to 'property_validation_date_range_validator'
property_validation_date_range_validator.inc in property_validation/plugins/validator/property_validation_date_range_validator.inc

File

property_validation/plugins/validator/property_validation_date_range_validator.inc, line 15

View source
class property_validation_date_range_validator extends property_validation_validator {

  /**
   * Validate property.
   */
  public function validate() {
    $data = $this->value;
    if ($data != '') {
      $flag = FALSE;
      $settings = $this->rule->settings;
      $cycle = $settings['cycle'];

      // support date, datetime
      if (!is_numeric($data)) {
        $data = strtotime($data);
      }
      $date_str = date("Y-m-d H:i:s", $data);
      if ($cycle == 'global') {
        if (!empty($settings['min'])) {
          $settings['min'] = strtotime($settings['min']);
          $settings['min'] = date("Y-m-d H:i:s", $settings['min']);
        }
        if (!empty($settings['max'])) {
          $settings['max'] = strtotime($settings['max']);
          $settings['max'] = date("Y-m-d H:i:s", $settings['max']);
        }
      }
      if ($cycle == 'year') {
        $date_str = substr($date_str, 5);
      }
      elseif ($cycle == 'month') {
        $date_str = substr($date_str, 8);
      }
      elseif ($cycle == 'week') {
        $week_day = date('w', strtotime($date_str));
        $date_str = substr($date_str, 10);
        $date_str = $week_day . $date_str;
      }
      elseif ($cycle == 'day') {
        $date_str = substr($date_str, 11);
      }
      elseif ($cycle == 'hour') {
        $date_str = substr($date_str, 14);
      }
      elseif ($cycle == 'minute') {
        $date_str = substr($date_str, 17);
      }
      if (!empty($settings['min']) && $date_str < $settings['min']) {
        $flag = TRUE;
      }
      if (!empty($settings['max']) && $date_str > $settings['max']) {
        $flag = TRUE;
      }
      if (!empty($settings['reverse'])) {
        $flag = $flag ? FALSE : TRUE;
      }
      if ($flag) {
        $this
          ->set_error();
      }
    }
  }

  /**
   * Provide settings option
   */
  function settings_form(&$form, &$form_state) {
    $default_settings = $this
      ->get_default_settings($form, $form_state);

    //print debug($default_settings);
    $form['settings']['cycle'] = array(
      '#title' => t('Cycle of date'),
      '#description' => t("Specify the cycle of date, support: global, year, month, week, day, hour, minute."),
      '#type' => 'select',
      '#options' => array(
        'global' => t('Global'),
        'year' => t('Year'),
        'month' => t('Month'),
        'week' => t('Week'),
        'day' => t('Day'),
        'hour' => t('Hour'),
        'minute' => t('Minute'),
      ),
      '#default_value' => isset($default_settings['cycle']) ? $default_settings['cycle'] : '',
    );
    $form['settings']['min'] = array(
      '#title' => t('Minimum date'),
      '#description' => t("Optionally specify the minimum date."),
      '#type' => 'textfield',
      '#default_value' => isset($default_settings['min']) ? $default_settings['min'] : '',
    );
    $form['settings']['max'] = array(
      '#title' => t('Maximum date'),
      '#description' => t("Optionally specify the maximum date."),
      '#type' => 'textfield',
      '#default_value' => isset($default_settings['max']) ? $default_settings['max'] : '',
    );
    $form['settings']['help'] = array(
      '#markup' => t('For minimum and maximum time, we only support date format "Y-m-d H:i:s", here is the relation between cycle and minimum/maximum date format:') . theme('item_list', array(
        'items' => array(
          t('global - [Y-m-d H:i:s]'),
          t('year - [m-d H:i:s]'),
          t('month - [d H:i:s]'),
          t('week - [w H:i:s]'),
          t('day - [H:i:s]'),
          t('hour - [i:s]'),
          t('minute - [s]'),
        ),
      )) . t('If cycle is "global", it support more date formats which could be converted through strtotime(), such as "now", "+1 month", "+1 day".'),
    );
    $form['settings']['reverse'] = array(
      '#title' => t('Reverse'),
      '#description' => t("If it is checked, it means must not match the range."),
      '#type' => 'checkbox',
      '#default_value' => isset($default_settings['reverse']) ? $default_settings['reverse'] : FALSE,
    );
    parent::settings_form($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
property_validation_date_range_validator::settings_form function Provide settings option Overrides property_validation_validator::settings_form
property_validation_date_range_validator::validate public function Validate property. Overrides property_validation_validator::validate
property_validation_validator::$entity protected property
property_validation_validator::$entity_type protected property
property_validation_validator::$errors protected property
property_validation_validator::$field protected property
property_validation_validator::$rule protected property
property_validation_validator::$value protected property
property_validation_validator::get_default_settings public function Return default settingsfor the validator.
property_validation_validator::get_error_element public function Return error element for the validation rule.
property_validation_validator::get_error_message public function Return error message string for the validation rule.
property_validation_validator::set_error public function Set error message.
property_validation_validator::token_help public function Provide token help info for error message. 4
property_validation_validator::__construct function Save arguments locally.