Projet

Général

Profil

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

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

1
<?php
2

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

    
8
/**
9
 * Context condition plugin for date values.
10
 */
11
class date_context_date_condition extends context_condition_node {
12

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

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

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

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

    
72
        foreach ($fields as $field_name => $label) {
73
          // If this field does not exist on this entity, just move along.
74
          if (empty($entity->{$field_name})) {
75
            continue;
76
          }
77

    
78
          $items = field_get_items('node', $entity, $field_name);
79

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

    
88
          // We don't have to construct the date values if we're just testing
89
          // 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
95
          // comparision.
96
          else {
97
            $field = field_info_field($field_name);
98
            $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
99
            foreach ($items as $delta => $item) {
100
              $timezone = date_get_timezone($field['settings']['tz_handling'], $item['timezone']);
101
              $date = new DateObject($item['value'], $timezone_db);
102
              date_timezone_set($date, timezone_open($timezone));
103
              $date1 = $date->format(DATE_FORMAT_DATETIME);
104
              if (empty($item['value2'])) {
105
                $item['value2'] = $item['value'];
106
              }
107
              $date = new DateObject($item['value2'], $timezone_db);
108
              date_timezone_set($date, timezone_open($timezone));
109
              $date2 = $date->format(DATE_FORMAT_DATETIME);
110
              str_replace('now', 'today', $options['value']);
111
              $date = date_create($options['value'], date_default_timezone_object());
112
              $compdate = $date->format(DATE_FORMAT_DATETIME);
113
              switch ($options['operation']) {
114
                case '=':
115
                  if ($date2 >= $compdate && $date1 <= $compdate) {
116
                    $this->condition_met($context, $field_name);
117
                  }
118
                  break;
119

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

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

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

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

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

    
157
}