You are here

public function date_context_date_condition::execute in Date 7.3

Same name and namespace in other branches
  1. 8 date_context/plugins/date_context_date_condition.inc \date_context_date_condition::execute()
  2. 7.2 date_context/plugins/date_context_date_condition.inc \date_context_date_condition::execute()

Overrides context_condition_node::execute

File

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

Class

date_context_date_condition
Context condition plugin for date values.

Code

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;
            }
          }
        }
      }
    }
  }
}