Projet

Général

Profil

Paste
Télécharger (7,26 ko) Statistiques
| Branche: | Révision:

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

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
  /**
46
   * Combines multiple date WHERE expressions into a single WHERE expression.
47
   *
48
   * @param string $function
49
   *   The function name to use to add individual conditions. Either 'op_simple'
50
   *   or 'op_between'.
51
   */
52
  protected function date_combine_conditions($function) {
53
    $this->get_query_fields();
54
    if (empty($this->query_fields)) {
55
      return;
56
    }
57

    
58
    // Create a custom filter group for the conditions.
59
    $this->query->set_where_group($this->options['date_method'], $this->options['date_group']);
60
    // Add each condition to the custom filter group.
61
    foreach ((array) $this->query_fields as $query_field) {
62
      $field = $query_field['field'];
63
      $this->date_handler = $query_field['date_handler'];
64

    
65
      // Respect relationships when determining the table alias.
66
      if ($field['table_name'] != $this->table || !empty($this->relationship)) {
67
        $this->related_table_alias = $this->query->ensure_table($field['table_name'], $this->relationship);
68
      }
69
      else {
70
        $this->related_table_alias = null;
71
      }
72
      $table_alias = !empty($this->related_table_alias) ? $this->related_table_alias : $field['table_name'];
73
      $field_name = $table_alias . '.' . $field['field_name'];
74

    
75
      // Call the appropriate function, either 'op_between' or 'op_simple'.
76
      parent::$function($field_name);
77
    }
78

    
79
    // Gather all of the condition strings and their placeholders.
80
    $conditions = array();
81
    $placeholders = array();
82
    foreach ($this->query->where[$this->options['date_group']]['conditions'] as $condition) {
83
      $conditions[] = $condition['field'];
84
      $placeholders += $condition['value'];
85
    }
86

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

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

    
93
    // Add it to the filter group chosen in the Views UI.
94
    $this->query->add_where_expression($this->options['group'], $conditions, $placeholders);
95
  }
96

    
97
  function extra_options_form(&$form, &$form_state) {
98
    parent::extra_options_form($form, $form_state);
99

    
100
    $fields = date_views_fields($this->base_table);
101
    $options = array();
102
    foreach ($fields['name'] as $name => $field) {
103
      $options[$name] = $field['label'];
104
    }
105

    
106
    $form['date_fields'] = array(
107
      '#title' => t('Date field(s)'),
108
      '#type' => 'checkboxes',
109
      '#options' => $options,
110
      '#default_value' => $this->options['date_fields'],
111
      '#multiple' => FALSE,
112
      '#description' => t('Select date field(s) to filter.'),
113
      '#required' => TRUE,
114
    );
115
    $form['date_method'] = array(
116
      '#title' => t('Method'),
117
      '#type' => 'radios',
118
      '#options' => array('OR' => t('OR'), 'AND' => t('AND')),
119
      '#default_value' => $this->options['date_method'],
120
      '#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).'),
121
      );
122
  }
123

    
124
  function extra_options_validate($form, &$form_state) {
125
    $check_fields = array_filter($form_state['values']['options']['date_fields']);
126
    if (empty($check_fields)) {
127
      form_error($form['date_fields'], t('You must select at least one date field for this filter.'));
128
    }
129
  }
130

    
131
  function extra_options_submit($form, &$form_state) {
132
    $form_state['values']['options']['date_fields'] = array_filter($form_state['values']['options']['date_fields']);
133
  }
134

    
135
  // Update the summary values to provide
136
  // meaningful information for each option.
137
  function admin_summary() {
138
    if (empty($this->options['date_fields'])) {
139
      return t('Missing date fields!');
140
    }
141
    $handler = $this->date_handler;
142

    
143
    $fields = date_views_fields($this->view->base_table);
144
    if (!empty($this->options['date_fields'])) {
145
      $output = array();
146
      foreach ($this->options['date_fields'] as $field) {
147
        if (array_key_exists($field, $fields['name'])) {
148
          $output[] = $fields['name'][$field]['label'];
149
        }
150
      }
151
    }
152
    $field = implode(' ' . $this->options['date_method'] . ' ', $output);
153
    $output = "$field " . check_plain($this->operator) . ' ';
154
    $parts = $handler->date_parts();
155
    $widget_options = $this->widget_options();
156
    // If the filter is exposed, display the granularity.
157
    if ($this->options['exposed']) {
158
      return t('(@field) <strong>Exposed</strong> @widget @format', array('@field' => $field, '@format' => $parts[$handler->granularity], '@widget' => $widget_options[$this->options['form_type']]));
159
    }
160
    // If not exposed, display the value.
161
    if (in_array($this->operator, $this->operator_values(2))) {
162
      $min = check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['min']);
163
      $max = check_plain(!empty($this->options['default_to_date']) ? $this->options['default_to_date'] : $this->options['value']['max']);
164
      $output .= t('@min and @max', array('@min' => $min, '@max' => $max));
165
    }
166
    else {
167
      $output .= check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['value']);
168
    }
169
    return $output;
170
  }
171

    
172
  function get_query_fields() {
173
    $fields = date_views_fields($this->base_table);
174
    $fields = $fields['name'];
175
    $this->query_fields = array();
176
    foreach ((array) $this->options['date_fields'] as $delta => $name) {
177
      if (array_key_exists($name, $fields) && $field = $fields[$name]) {
178
        $date_handler = new date_sql_handler($field['sql_type'], date_default_timezone());
179
        $date_handler->granularity = $this->options['granularity'];
180
        $date_handler->db_timezone = date_get_timezone_db($field['tz_handling']);
181
        $date_handler->local_timezone = date_get_timezone($field['tz_handling']);
182
        $this->query_fields[] = array('field' => $field, 'date_handler' => $date_handler);
183
      }
184
    }
185
  }
186
}
187
// @codingStandardsIgnoreEnd