Projet

Général

Profil

Paste
Télécharger (5,74 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date_context / plugins / date_context_date_condition.inc @ b720ea3e

1
<?php
2

    
3
/**
4
 * @file
5
 * Context date condition plugin.
6
 */
7

    
8
/**
9
 * Expose term views/term forms by vocabulary as a context condition.
10
 */
11
// @codingStandardsIgnoreStart
12
class date_context_date_condition extends context_condition_node {
13

    
14
  /**
15
   * {@inheritdoc}
16
   */
17
  public function condition_values() {
18
    $values = array();
19
    $fields = field_info_fields();
20
    foreach ($fields as $field_name => $field) {
21
      if (in_array($field['type'], array('date', 'datetime', 'datestamp'))) {
22
        $values[$field_name] = $field_name;
23
      }
24
    }
25
    return $values;
26
  }
27

    
28
  /**
29
   * {@inheritdoc}
30
   */
31
  public function options_form($context) {
32
    $defaults = $this->fetch_from_context($context, 'options');
33
    $options = array(
34
      '<' => t('Is less than'),
35
      '<=' => t('Is less than or equal to'),
36
      '>=' => t('Is greater than or equal to'),
37
      '>' => t('Is greater than'),
38
      '=' => t('Is equal to'),
39
      '!=' => t('Is not equal to'),
40
      'empty' => t('Is empty'),
41
      'not empty' => t('Is not Empty'),
42
    );
43
    $dependency_options = array('<', '<=', '>', '>=', '=', '!=');
44

    
45
    $form['operation'] = array(
46
      '#title' => t('Operation'),
47
      '#type' => 'select',
48
      '#options' => $options,
49
      '#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.'),
50
      '#default_value' => isset($defaults['operation']) ? $defaults['operation'] : '',
51
      '#required' => TRUE,
52
    );
53
    $form['value'] = array(
54
      '#title' => t('Value'),
55
      '#type' => 'textfield',
56
      '#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')),
57
      '#default_value' => isset($defaults['value']) ? $defaults['value'] : '',
58
      '#process' => array('ctools_dependent_process'),
59
      '#dependency' => array('edit-conditions-plugins-date-context-date-condition-options-operation' => $dependency_options),
60
    );
61
    return $form;
62
  }
63

    
64
  /**
65
   * {@inheritdoc}
66
   */
67
  public function execute($entity, $op) {
68
    if (in_array($op, array('view', 'form'))) {
69
      foreach ($this->get_contexts() as $context) {
70
        $options = $this->fetch_from_context($context, 'options');
71
        $fields = $this->fetch_from_context($context, 'values');
72

    
73
        foreach ($fields as $field_name => $label) {
74

    
75
          // If this field does not exist on this entity, just move along.
76
          if (empty($entity->{$field_name})) {
77
            continue;
78
          }
79

    
80
          $items = field_get_items('node', $entity, $field_name);
81

    
82
          // If there are no values, nothing to do unless we were looking for 'empty' or '!='.
83
          if (empty($items)) {
84
            if ($options['operation'] == '!=' || $options['operation'] == 'empty') {
85
              $this->condition_met($context, $field_name);
86
            }
87
          }
88

    
89
          // We don't have to construct the date values if we're just testing for 'not empty'.
90
          elseif ($options['operation'] == 'not empty') {
91
            $this->condition_met($context, $field_name);
92
          }
93

    
94
          // All other operations need to retrieve the date values for comparision.
95
          else {
96
            $field = field_info_field($field_name);
97
            $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
98
            foreach ($items as $delta => $item) {
99
              $timezone = date_get_timezone($field['settings']['tz_handling'], $item['timezone']);
100
              $date = new DateObject($item['value'], $timezone_db);
101
              date_timezone_set($date, timezone_open($timezone));
102
              $date1 = $date->format(DATE_FORMAT_DATETIME);
103
              if (empty($item['value2'])) {
104
                $item['value2'] = $item['value'];
105
              }
106
              $date = new DateObject($item['value2'], $timezone_db);
107
              date_timezone_set($date, timezone_open($timezone));
108
              $date2 = $date->format(DATE_FORMAT_DATETIME);
109
              str_replace('now', 'today', $options['value']);
110
              $date = date_create($options['value'], date_default_timezone_object());
111
              $compdate = $date->format(DATE_FORMAT_DATETIME);
112
              switch ($options['operation']) {
113
                case '=':
114
                  if ($date2 >= $compdate && $date1 <= $compdate) {
115
                    $this->condition_met($context, $field_name);
116
                  }
117
                  break;
118

    
119
                case '>':
120
                  if ($date1 > $compdate) {
121
                    $this->condition_met($context, $field_name);
122
                  }
123
                  break;
124

    
125
                case '>=':
126
                  if ($date1 >= $compdate) {
127
                    $this->condition_met($context, $field_name);
128
                  }
129
                  break;
130

    
131
                case '<':
132
                  if ($date2 < $compdate) {
133
                    $this->condition_met($context, $field_name);
134
                  }
135
                  break;
136

    
137
                case '<=':
138
                  if ($date2 <= $compdate) {
139
                    $this->condition_met($context, $field_name);
140
                  }
141
                  break;
142

    
143
                case '!=':
144
                  if ($date1 < $compdate || $date2 > $compdate) {
145
                    $this->condition_met($context, $field_name);
146
                  }
147
                  break;
148
              }
149
            }
150
          }
151
        }
152
      }
153
    }
154
  }
155
}
156
// @codingStandardsIgnoreEnd