You are here

class date_context_date_condition in Date 7.2

Same name and namespace in other branches
  1. 8 date_context/plugins/date_context_date_condition.inc \date_context_date_condition
  2. 7.3 date_context/plugins/date_context_date_condition.inc \date_context_date_condition

Context condition plugin for date values.

Hierarchy

Expanded class hierarchy of date_context_date_condition

3 string references to 'date_context_date_condition'
date_context_context_node_condition_alter in date_context/date_context.module
Implements hook_context_node_condition_alter().
date_context_context_plugins in date_context/date_context.module
Implements hook_context_plugins().
date_context_context_registry in date_context/date_context.module
Implements hook_context_registry().

File

date_context/plugins/date_context_date_condition.inc, line 11
Context condition plugin for date values.

View source
class date_context_date_condition extends context_condition_node {

  /**
   * {@inheritdoc}
   */
  public function condition_values() {
    $values = array();
    $fields = field_info_fields();
    foreach ($fields as $field_name => $field) {
      if (in_array($field['type'], array(
        'date',
        'datetime',
        'datestamp',
      ))) {
        $values[$field_name] = $field_name;
      }
    }
    return $values;
  }

  /**
   * {@inheritdoc}
   */
  public function options_form($context) {
    $defaults = $this
      ->fetch_from_context($context, 'options');
    $options = array(
      '<' => t('Is less than'),
      '<=' => t('Is less than or equal to'),
      '>=' => t('Is greater than or equal to'),
      '>' => t('Is greater than'),
      '=' => t('Is equal to'),
      '!=' => t('Is not equal to'),
      'empty' => t('Is empty'),
      'not empty' => t('Is not Empty'),
    );
    $dependency_options = array(
      '<',
      '<=',
      '>',
      '>=',
      '=',
      '!=',
    );
    $form['operation'] = array(
      '#title' => t('Operation'),
      '#type' => 'select',
      '#options' => $options,
      '#description' => t('The comparison to perform to determine if the date field meets the condition. For multiple value date fields, all values will be checked to see if any meet the condition.'),
      '#default_value' => isset($defaults['operation']) ? $defaults['operation'] : '',
      '#required' => TRUE,
    );
    $form['value'] = array(
      '#title' => t('Value'),
      '#type' => 'textfield',
      '#description' => t("The value the field should contain to meet the condition. This can either be an absolute date in ISO format (YYYY-MM-DDTHH:MM:SS) or a relative string like '12AM today'. Examples: 2011-12-31T00:00:00, now, now +1 day, 12AM today, Monday next week. <a href=\"@relative_format\">More examples of relative date formats in the PHP documentation</a>.", array(
        '@relative_format' => 'http://www.php.net/manual/en/datetime.formats.relative.php',
      )),
      '#default_value' => isset($defaults['value']) ? $defaults['value'] : '',
      '#process' => array(
        'ctools_dependent_process',
      ),
      '#dependency' => array(
        'edit-conditions-plugins-date-context-date-condition-options-operation' => $dependency_options,
      ),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function execute($entity, $op) {
    if (in_array($op, array(
      'view',
      'form',
    ))) {
      foreach ($this
        ->get_contexts() as $context) {
        $options = $this
          ->fetch_from_context($context, 'options');
        $fields = $this
          ->fetch_from_context($context, 'values');
        foreach ($fields as $field_name => $label) {

          // If this field does not exist on this entity, just move along.
          if (empty($entity->{$field_name})) {
            continue;
          }
          $items = field_get_items('node', $entity, $field_name);

          // If there are no values, nothing to do unless we were looking for
          // 'empty' or '!='.
          if (empty($items)) {
            if ($options['operation'] == '!=' || $options['operation'] == 'empty') {
              $this
                ->condition_met($context, $field_name);
            }
          }
          elseif ($options['operation'] == 'not empty') {
            $this
              ->condition_met($context, $field_name);
          }
          else {
            $field = field_info_field($field_name);
            $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
            foreach ($items as $delta => $item) {
              $timezone = date_get_timezone($field['settings']['tz_handling'], $item['timezone']);
              $date = new DateObject($item['value'], $timezone_db);
              date_timezone_set($date, timezone_open($timezone));
              $date1 = $date
                ->format(DATE_FORMAT_DATETIME);
              if (empty($item['value2'])) {
                $item['value2'] = $item['value'];
              }
              $date = new DateObject($item['value2'], $timezone_db);
              date_timezone_set($date, timezone_open($timezone));
              $date2 = $date
                ->format(DATE_FORMAT_DATETIME);
              str_replace('now', 'today', $options['value']);
              $date = date_create($options['value'], date_default_timezone_object());
              $compdate = $date
                ->format(DATE_FORMAT_DATETIME);
              switch ($options['operation']) {
                case '=':
                  if ($date2 >= $compdate && $date1 <= $compdate) {
                    $this
                      ->condition_met($context, $field_name);
                  }
                  break;
                case '>':
                  if ($date1 > $compdate) {
                    $this
                      ->condition_met($context, $field_name);
                  }
                  break;
                case '>=':
                  if ($date1 >= $compdate) {
                    $this
                      ->condition_met($context, $field_name);
                  }
                  break;
                case '<':
                  if ($date2 < $compdate) {
                    $this
                      ->condition_met($context, $field_name);
                  }
                  break;
                case '<=':
                  if ($date2 <= $compdate) {
                    $this
                      ->condition_met($context, $field_name);
                  }
                  break;
                case '!=':
                  if ($date1 < $compdate || $date2 > $compdate) {
                    $this
                      ->condition_met($context, $field_name);
                  }
                  break;
              }
            }
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
context_condition::$description property
context_condition::$plugin property
context_condition::$title property
context_condition::$values property
context_condition::condition_form function Condition form. 3
context_condition::condition_form_submit function Condition form submit handler. 2
context_condition::condition_met function Marks a context as having met this particular condition.
context_condition::condition_used function Check whether this condition is used by any contexts. Can be used to prevent expensive condition checks from being triggered when no contexts use this condition.
context_condition::editor_form function Context editor form for conditions. 2
context_condition::editor_form_submit function Context editor form submit handler.
context_condition::fetch_from_context function Retrieve options from the context provided.
context_condition::get_contexts function Retrieve all contexts with the condition value provided. 2
context_condition::options_form_submit function Options form submit handler.
context_condition::settings_form function Settings form. Provide variable settings for your condition.
context_condition::__clone function Clone our references when we're being cloned.
context_condition::__construct function Constructor. Do not override.
date_context_date_condition::condition_values public function Condition values. Overrides context_condition_node::condition_values
date_context_date_condition::execute public function Overrides context_condition_node::execute
date_context_date_condition::options_form public function Options form. Provide additional options for your condition. Overrides context_condition_node::options_form