Projet

Général

Profil

Paste
Télécharger (8,12 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date_views / includes / date_views_filter_handler.inc @ ee46a8ed

1
<?php
2
/**
3
 * @file
4
 * A flexible, configurable date filter.
5
 * This filter combines multiple date filters into a single filter
6
 * where all fields are controlled by the same date and can be combined
7
 * with either AND or OR.
8
 */
9

    
10
// @codingStandardsIgnoreStart
11
class date_views_filter_handler extends date_views_filter_handler_simple {
12
  function init(&$view, &$options) {
13
    parent::init($view, $options);
14

    
15
    if (empty($this->view->date_info)) {
16
      $this->view->date_info = new stdClass();
17
    }
18
    if (empty($this->view->date_info->date_fields)) {
19
      $this->view->date_info->date_fields = array();
20
    }
21
    $this->view->date_info->date_fields = array_merge($this->view->date_info->date_fields, $this->options['date_fields']);
22
  }
23

    
24
  // Set default values for the date filter.
25
  function option_definition() {
26
    $options = parent::option_definition();
27
    $options['date_fields'] = array('default' => array());
28
    $options['date_method'] = array('default' => 'OR');
29
    $options['date_group'] = array('default' => 'date');
30
    return $options;
31
  }
32

    
33
  function op_between($field) {
34
    $this->date_combine_conditions('op_between');
35
  }
36

    
37
  function op_simple($field) {
38
    $this->date_combine_conditions('op_simple');
39
  }
40

    
41
  function op_contains($field) {
42
    $this->date_combine_conditions('op_contains');
43
  }
44

    
45
  function op_empty($field) {
46
    $this->get_query_fields();
47
    if (empty($this->query_fields)) {
48
      return;
49
    }
50

    
51
    // Add each condition to the custom filter group.
52
    foreach ((array) $this->query_fields as $query_field) {
53
      $field = $query_field['field'];
54
      $this->date_handler = $query_field['date_handler'];
55

    
56
      // Respect relationships when determining the table alias.
57
      if ($field['table_name'] != $this->table || !empty($this->relationship)) {
58
        $this->related_table_alias = $this->query->ensure_table($field['table_name'], $this->relationship);
59
      }
60
      else {
61
        $this->related_table_alias = NULL;
62
      }
63

    
64
      $table_alias = !empty($this->related_table_alias) ? $this->related_table_alias : $field['table_name'];
65
      $field_name = $table_alias . '.' . $field['field_name'];
66

    
67
      parent::op_empty($field_name);
68
    }
69
  }
70

    
71
  /**
72
   * Combines multiple date WHERE expressions into a single WHERE expression.
73
   *
74
   * @param string $function
75
   *   The function name to use to add individual conditions. Either 'op_simple'
76
   *   or 'op_between'.
77
   */
78
  protected function date_combine_conditions($function) {
79
    $this->get_query_fields();
80
    if (empty($this->query_fields)) {
81
      return;
82
    }
83

    
84
    // Create a custom filter group for the conditions.
85
    $this->query->set_where_group($this->options['date_method'], $this->options['date_group']);
86
    // Add each condition to the custom filter group.
87
    foreach ((array) $this->query_fields as $query_field) {
88
      $field = $query_field['field'];
89
      $this->date_handler = $query_field['date_handler'];
90

    
91
      // Respect relationships when determining the table alias.
92
      if ($field['table_name'] != $this->table || !empty($this->relationship)) {
93
        $this->related_table_alias = $this->query->ensure_table($field['table_name'], $this->relationship);
94
      }
95
      else {
96
        $this->related_table_alias = null;
97
      }
98
      $table_alias = !empty($this->related_table_alias) ? $this->related_table_alias : $field['table_name'];
99
      $field_name = $table_alias . '.' . $field['field_name'];
100

    
101
      // Call the appropriate function, either 'op_between' or 'op_simple'.
102
      parent::$function($field_name);
103
    }
104

    
105
    // Gather all of the condition strings and their placeholders.
106
    $conditions = array();
107
    $placeholders = array();
108
    foreach ($this->query->where[$this->options['date_group']]['conditions'] as $condition) {
109
      $conditions[] = $condition['field'];
110
      $placeholders += $condition['value'];
111
    }
112

    
113
    // Remove the conditions from the custom filter group.
114
    unset($this->query->where[$this->options['date_group']]);
115

    
116
    // Combine all of the conditions into one string.
117
    $conditions = implode(' ' . $this->options['date_method'] . ' ', $conditions);
118

    
119
    // Add it to the filter group chosen in the Views UI.
120
    $this->query->add_where_expression($this->options['group'], $conditions, $placeholders);
121
  }
122

    
123
  function extra_options_form(&$form, &$form_state) {
124
    parent::extra_options_form($form, $form_state);
125

    
126
    $fields = date_views_fields($this->base_table);
127
    $options = array();
128
    foreach ($fields['name'] as $name => $field) {
129
      $options[$name] = $field['label'];
130
    }
131

    
132
    $form['date_fields'] = array(
133
      '#title' => t('Date field(s)'),
134
      '#type' => 'checkboxes',
135
      '#options' => $options,
136
      '#default_value' => $this->options['date_fields'],
137
      '#multiple' => FALSE,
138
      '#description' => t('Select date field(s) to filter.'),
139
      '#required' => TRUE,
140
    );
141
    $form['date_method'] = array(
142
      '#title' => t('Method'),
143
      '#type' => 'radios',
144
      '#options' => array('OR' => t('OR'), 'AND' => t('AND')),
145
      '#default_value' => $this->options['date_method'],
146
      '#description' => t('Method of handling multiple date fields in the same query. Return items that have any matching date field (date = field_1 OR field_2), or only those with matches in all selected date fields (date = field_1 AND field_2).'),
147
      );
148
  }
149

    
150
  function extra_options_validate($form, &$form_state) {
151
    $check_fields = array_filter($form_state['values']['options']['date_fields']);
152
    if (empty($check_fields)) {
153
      form_error($form['date_fields'], t('You must select at least one date field for this filter.'));
154
    }
155
  }
156

    
157
  function extra_options_submit($form, &$form_state) {
158
    $form_state['values']['options']['date_fields'] = array_filter($form_state['values']['options']['date_fields']);
159
  }
160

    
161
  // Update the summary values to provide
162
  // meaningful information for each option.
163
  function admin_summary() {
164
    if (empty($this->options['date_fields'])) {
165
      return t('Missing date fields!');
166
    }
167
    $handler = $this->date_handler;
168

    
169
    $fields = date_views_fields($this->view->base_table);
170
    if (!empty($this->options['date_fields'])) {
171
      $output = array();
172
      foreach ($this->options['date_fields'] as $field) {
173
        if (array_key_exists($field, $fields['name'])) {
174
          $output[] = $fields['name'][$field]['label'];
175
        }
176
      }
177
    }
178
    $field = implode(' ' . $this->options['date_method'] . ' ', $output);
179
    $output = "$field " . check_plain($this->operator) . ' ';
180
    $parts = $handler->date_parts();
181
    $widget_options = $this->widget_options();
182
    // If the filter is exposed, display the granularity.
183
    if ($this->options['exposed']) {
184
      return t('(@field) <strong>Exposed</strong> @widget @format', array('@field' => $field, '@format' => $parts[$handler->granularity], '@widget' => $widget_options[$this->options['form_type']]));
185
    }
186
    // If not exposed, display the value.
187
    if (in_array($this->operator, $this->operator_values(2))) {
188
      $min = check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['min']);
189
      $max = check_plain(!empty($this->options['default_to_date']) ? $this->options['default_to_date'] : $this->options['value']['max']);
190
      $output .= t('@min and @max', array('@min' => $min, '@max' => $max));
191
    }
192
    else {
193
      $output .= check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['value']);
194
    }
195
    return $output;
196
  }
197

    
198
  function get_query_fields() {
199
    $fields = date_views_fields($this->base_table);
200
    $fields = $fields['name'];
201
    $this->query_fields = array();
202
    foreach ((array) $this->options['date_fields'] as $delta => $name) {
203
      if (array_key_exists($name, $fields) && $field = $fields[$name]) {
204
        $date_handler = new date_sql_handler($field['sql_type'], date_default_timezone());
205
        $date_handler->granularity = $this->options['granularity'];
206
        $date_handler->db_timezone = date_get_timezone_db($field['tz_handling']);
207
        $date_handler->local_timezone = date_get_timezone($field['tz_handling']);
208
        $this->query_fields[] = array('field' => $field, 'date_handler' => $date_handler);
209
      }
210
    }
211
  }
212
}
213
// @codingStandardsIgnoreEnd