Project

General

Profile

Paste
Download (6.67 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter_date.inc @ 8be7bf84

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_filter_date.
6
 */
7

    
8
/**
9
 * Filter to handle dates stored as a timestamp.
10
 *
11
 * @ingroup views_filter_handlers
12
 */
13
class views_handler_filter_date extends views_handler_filter_numeric {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function option_definition() {
19
    $options = parent::option_definition();
20

    
21
    // Value is already set up properly, we're just adding our new field to it.
22
    $options['value']['contains']['type']['default'] = 'date';
23

    
24
    return $options;
25
  }
26

    
27
  /**
28
   * Add a type selector to the value form.
29
   */
30
  public function value_form(&$form, &$form_state) {
31
    if (empty($form_state['exposed'])) {
32
      $form['value']['type'] = array(
33
        '#type' => 'radios',
34
        '#title' => t('Value type'),
35
        '#options' => array(
36
          'date' => t('A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred.'),
37
          'offset' => t('An offset from the current time such as "!example1" or "!example2"', array('!example1' => '+1 day', '!example2' => '-2 hours -30 minutes')),
38
        ),
39
        '#default_value' => !empty($this->value['type']) ? $this->value['type'] : 'date',
40
      );
41
    }
42
    parent::value_form($form, $form_state);
43
  }
44

    
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public function options_validate(&$form, &$form_state) {
49
    parent::options_validate($form, $form_state);
50

    
51
    if (!empty($this->options['exposed']) && empty($form_state['values']['options']['expose']['required'])) {
52
      // Who cares what the value is if it's exposed and non-required.
53
      return;
54
    }
55

    
56
    $this->validate_valid_time($form['value'], $form_state['values']['options']['operator'], $form_state['values']['options']['value']);
57
  }
58

    
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function exposed_validate(&$form, &$form_state) {
63
    if (empty($this->options['exposed'])) {
64
      return;
65
    }
66

    
67
    if (empty($this->options['expose']['required'])) {
68
      // Who cares what the value is if it's exposed and non-required.
69
      return;
70
    }
71

    
72
    $value = &$form_state['values'][$this->options['expose']['identifier']];
73
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
74
      $operator = $form_state['values'][$this->options['expose']['operator_id']];
75
    }
76
    else {
77
      $operator = $this->operator;
78
    }
79

    
80
    $this->validate_valid_time($this->options['expose']['identifier'], $operator, $value);
81

    
82
  }
83

    
84
  /**
85
   * Validate that the time values convert to something usable.
86
   */
87
  public function validate_valid_time(&$form, $operator, $value) {
88
    $operators = $this->operators();
89

    
90
    if ($operators[$operator]['values'] == 1) {
91
      $convert = strtotime($value['value']);
92
      if (!empty($form['value']) && ($convert == -1 || $convert === FALSE)) {
93
        form_error($form['value'], t('Invalid date format.'));
94
      }
95
    }
96
    elseif ($operators[$operator]['values'] == 2) {
97
      $min = strtotime($value['min']);
98
      if ($min == -1 || $min === FALSE) {
99
        form_error($form['min'], t('Invalid date format.'));
100
      }
101
      $max = strtotime($value['max']);
102
      if ($max == -1 || $max === FALSE) {
103
        form_error($form['max'], t('Invalid date format.'));
104
      }
105
    }
106
  }
107

    
108
  /**
109
   * Validate the build group options form.
110
   */
111
  public function build_group_validate($form, &$form_state) {
112
    // Special case to validate grouped date filters, this is because the
113
    // $group['value'] array contains the type of filter (date or offset)
114
    // and therefore the number of items the comparission has to be done
115
    // against 'one' instead of 'zero'.
116
    foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
117
      if (empty($group['remove'])) {
118
        // Check if the title is defined but value wasn't defined.
119
        if (!empty($group['title'])) {
120
          if ((!is_array($group['value']) && empty($group['value'])) || (is_array($group['value']) && count(array_filter($group['value'])) == 1)) {
121
            form_error($form['group_info']['group_items'][$id]['value'], t('The value is required if title for this item is defined.'));
122
          }
123
        }
124

    
125
        // Check if the value is defined but title wasn't defined.
126
        if ((!is_array($group['value']) && !empty($group['value'])) || (is_array($group['value']) && count(array_filter($group['value'])) > 1)) {
127
          if (empty($group['title'])) {
128
            form_error($form['group_info']['group_items'][$id]['title'], t('The title is required if value for this item is defined.'));
129
          }
130
        }
131
      }
132
    }
133
  }
134

    
135
  /**
136
   * {@inheritdoc}
137
   */
138
  public function accept_exposed_input($input) {
139
    if (empty($this->options['exposed'])) {
140
      return TRUE;
141
    }
142

    
143
    // Store this because it will get overwritten.
144
    $type = $this->value['type'];
145
    $rc = parent::accept_exposed_input($input);
146

    
147
    // Don't filter if value(s) are empty.
148
    $operators = $this->operators();
149
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
150
      $operator = $input[$this->options['expose']['operator_id']];
151
    }
152
    else {
153
      $operator = $this->operator;
154
    }
155

    
156
    if ($operators[$operator]['values'] == 1) {
157
      if ($this->value['value'] == '') {
158
        return FALSE;
159
      }
160
    }
161
    elseif ($operators[$operator]['values'] == 2) {
162
      if ($this->value['min'] == '' || $this->value['max'] == '') {
163
        return FALSE;
164
      }
165
    }
166

    
167
    // Restore what got overwritten by the parent.
168
    $this->value['type'] = $type;
169
    return $rc;
170
  }
171

    
172
  /**
173
   *
174
   */
175
  public function op_between($field) {
176
    // Use the substitutions to ensure a consistent timestamp.
177
    $query_substitutions = views_views_query_substitutions($this->view);
178
    $a = intval(strtotime($this->value['min'], $query_substitutions['***CURRENT_TIME***']));
179
    $b = intval(strtotime($this->value['max'], $query_substitutions['***CURRENT_TIME***']));
180

    
181
    // This is safe because we are manually scrubbing the values. It is
182
    // necessary to do it this way because $a and $b are formulas when using an
183
    // offset.
184
    $operator = strtoupper($this->operator);
185
    $this->query->add_where_expression($this->options['group'], "$field $operator $a AND $b");
186
  }
187

    
188
  /**
189
   *
190
   */
191
  public function op_simple($field) {
192
    // Use the substitutions to ensure a consistent timestamp.
193
    $query_substitutions = views_views_query_substitutions($this->view);
194
    $value = intval(strtotime($this->value['value'], $query_substitutions['***CURRENT_TIME***']));
195

    
196
    // This is safe because we are manually scrubbing the value. It is
197
    // necessary to do it this way because $value is a formula when using an
198
    // offset.
199
    $this->query->add_where_expression($this->options['group'], "$field $this->operator $value");
200
  }
201

    
202
}