Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * Expose term views/term forms by vocabulary as a context condition.
5
 */
6
class date_context_date_condition extends context_condition_node {
7
  function condition_values() {
8
    $values = array();
9
    $fields = field_info_fields();
10
    foreach ($fields as $field_name => $field) {
11
      if (in_array($field['type'], array('date', 'datetime', 'datestamp'))) {
12
        $values[$field_name] = $field_name;
13
      }
14
    }
15
    return $values;
16
  }
17

    
18
  function options_form($context) {
19
    $defaults = $this->fetch_from_context($context, 'options');
20
    $options = array(
21
      '<' =>  t('Is less than'),
22
      '<=' => t('Is less than or equal to'),
23
      '>=' => t('Is greater than or equal to'),
24
      '>' => t('Is greater than'),
25
      '=' => t('Is equal to'),
26
      '!=' => t('Is not equal to'),
27
      'empty' => t('Is empty'),
28
      'not empty' => t('Is not Empty'),
29
    );
30
    $form['operation'] = array(
31
      '#title' => t('Operation'),
32
      '#type' => 'select',
33
      '#options' => $options,
34
      '#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.'),
35
      '#default_value' => isset($defaults['operation']) ? $defaults['operation'] : '',
36
      '#required' => TRUE,
37
    );
38
    $form['value'] = array(
39
      '#title' => t('Value'),
40
      '#type' => 'textfield',
41
      '#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')),
42
      '#default_value' => isset($defaults['value']) ? $defaults['value'] : '',
43
      '#process' => array('ctools_dependent_process'),
44
      '#dependency' => array('edit-conditions-plugins-date-context-date-condition-options-operation' => array('<', '<=', '>', '>=', '=', '!=')),
45
    );
46
    return $form;
47
  }
48

    
49
  function execute($entity, $op) {
50
    if (in_array($op, array('view', 'form'))) {
51
      foreach ($this->get_contexts() as $context) {
52
        $options = $this->fetch_from_context($context, 'options');
53
        $fields = $this->fetch_from_context($context, 'values');
54

    
55
        foreach ($fields as $field_name => $label) {
56

    
57
          // If this field does not exist on this entity, just move along.
58
          if (empty($entity->{$field_name})) {
59
            continue;
60
          }
61

    
62
          $items = field_get_items('node', $entity, $field_name);
63

    
64
          // If there are no values, nothing to do unless we were looking for 'empty' or '!='.
65
          if (empty($items)) {
66
            if ($options['operation'] == '!=' || $options['operation'] == 'empty') {
67
              $this->condition_met($context, $field_name);
68
            }
69
          }
70

    
71
          // We don't have to construct the date values if we're just testing for 'not empty'.
72
          elseif ($options['operation'] == 'not empty') {
73
            $this->condition_met($context, $field_name);
74
          }
75

    
76
          // All other operations need to retrieve the date values for comparision.
77
          else {
78
            $field = field_info_field($field_name);
79
            $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
80
            foreach ($items as $delta => $item) {
81
              $timezone = date_get_timezone($field['settings']['tz_handling'], $item['timezone']);
82
              $date = new DateObject($item['value'], $timezone_db);
83
              date_timezone_set($date, timezone_open($timezone));
84
              $date1 = $date->format(DATE_FORMAT_DATETIME);
85
              if (empty($item['value2'])) {
86
                $item['value2'] = $item['value'];
87
              }
88
              $date = new DateObject($item['value2'], $timezone_db);
89
              date_timezone_set($date, timezone_open($timezone));
90
              $date2 = $date->format(DATE_FORMAT_DATETIME);
91
              str_replace('now', 'today', $options['value']);
92
              $date = date_create($options['value'], date_default_timezone_object());
93
              $compdate = $date->format(DATE_FORMAT_DATETIME);
94
              switch($options['operation']) {
95
                case '=':
96
                  if ($date2 >= $compdate && $date1 <= $compdate) {
97
                    $this->condition_met($context, $field_name);
98
                  }
99
                  break;
100
                case '>':
101
                  if ($date1 > $compdate) {
102
                    $this->condition_met($context, $field_name);
103
                  }
104
                  break;
105
                case '>=':
106
                  if ($date1 >= $compdate) {
107
                    $this->condition_met($context, $field_name);
108
                  }
109
                  break;
110
                case '<':
111
                  if ($date2 < $compdate) {
112
                    $this->condition_met($context, $field_name);
113
                  }
114
                  break;
115
                case '<=':
116
                  if ($date2 <= $compdate) {
117
                    $this->condition_met($context, $field_name);
118
                  }
119
                  break;
120
                case '!=':
121
                  if ($date1 < $compdate || $date2 > $compdate) {
122
                    $this->condition_met($context, $field_name);
123
                  }
124
                  break;
125
              }
126
            }
127
          }
128
        }
129
      }
130
    }
131
  }
132
}