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 @ a2bb1a14

1 85ad3d82 Assos Assos
<?php
2
3 b720ea3e Assos Assos
/**
4
 * @file
5
 * Context date condition plugin.
6
 */
7
8 85ad3d82 Assos Assos
/**
9
 * Expose term views/term forms by vocabulary as a context condition.
10
 */
11 b720ea3e Assos Assos
// @codingStandardsIgnoreStart
12 85ad3d82 Assos Assos
class date_context_date_condition extends context_condition_node {
13 b720ea3e Assos Assos
14
  /**
15
   * {@inheritdoc}
16
   */
17
  public function condition_values() {
18 85ad3d82 Assos Assos
    $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 b720ea3e Assos Assos
  /**
29
   * {@inheritdoc}
30
   */
31
  public function options_form($context) {
32 85ad3d82 Assos Assos
    $defaults = $this->fetch_from_context($context, 'options');
33
    $options = array(
34 b720ea3e Assos Assos
      '<' => t('Is less than'),
35 85ad3d82 Assos Assos
      '<=' => 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 b720ea3e Assos Assos
    $dependency_options = array('<', '<=', '>', '>=', '=', '!=');
44
45 85ad3d82 Assos Assos
    $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 b720ea3e Assos Assos
      '#dependency' => array('edit-conditions-plugins-date-context-date-condition-options-operation' => $dependency_options),
60 85ad3d82 Assos Assos
    );
61
    return $form;
62
  }
63
64 b720ea3e Assos Assos
  /**
65
   * {@inheritdoc}
66
   */
67
  public function execute($entity, $op) {
68 85ad3d82 Assos Assos
    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 b720ea3e Assos Assos
              switch ($options['operation']) {
113 85ad3d82 Assos Assos
                case '=':
114
                  if ($date2 >= $compdate && $date1 <= $compdate) {
115
                    $this->condition_met($context, $field_name);
116
                  }
117
                  break;
118 b720ea3e Assos Assos
119 85ad3d82 Assos Assos
                case '>':
120
                  if ($date1 > $compdate) {
121
                    $this->condition_met($context, $field_name);
122
                  }
123
                  break;
124 b720ea3e Assos Assos
125 85ad3d82 Assos Assos
                case '>=':
126
                  if ($date1 >= $compdate) {
127
                    $this->condition_met($context, $field_name);
128
                  }
129
                  break;
130 b720ea3e Assos Assos
131 85ad3d82 Assos Assos
                case '<':
132
                  if ($date2 < $compdate) {
133
                    $this->condition_met($context, $field_name);
134
                  }
135
                  break;
136 b720ea3e Assos Assos
137 85ad3d82 Assos Assos
                case '<=':
138
                  if ($date2 <= $compdate) {
139
                    $this->condition_met($context, $field_name);
140
                  }
141
                  break;
142 b720ea3e Assos Assos
143 85ad3d82 Assos Assos
                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 b720ea3e Assos Assos
// @codingStandardsIgnoreEnd