You are here

function date_context_date_condition::execute in Date 8

Same name and namespace in other branches
  1. 7.3 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()

File

date_context/plugins/date_context_date_condition.inc, line 52

Class

date_context_date_condition
Expose term views/term forms by vocabulary as a context condition.

Code

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 DrupalDateTime($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 DrupalDateTime($item['value2'], $timezone_db);
            date_timezone_set($date, timezone_open($timezone));
            $date2 = $date
              ->format(DATE_FORMAT_DATETIME);
            str_replace('now', 'today', $options['value']);
            $date = new DrupalDateTime($options['value']);
            $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;
            }
          }
        }
      }
    }
  }
}