Projet

Général

Profil

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

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

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 with either AND or OR.
7
 */
8

    
9
class date_views_filter_handler extends date_views_filter_handler_simple {
10
  function init(&$view, &$options) {
11
    parent::init($view, $options);
12

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

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

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

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

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

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

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

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

    
70
      // Call the appropriate function, either 'op_between' or 'op_simple'.
71
      parent::$function($field_name);
72
    }
73

    
74
    // Gather all of the condition strings and their placeholders.
75
    $conditions = array();
76
    $placeholders = array();
77
    foreach ($this->query->where[$this->options['date_group']]['conditions'] as $condition) {
78
      $conditions[] = $condition['field'];
79
      $placeholders += $condition['value'];
80
    }
81

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

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

    
88
    // Add it to the filter group chosen in the Views UI.
89
    $this->query->add_where_expression($this->options['group'], $conditions, $placeholders);
90
  }
91

    
92
  function extra_options_form(&$form, &$form_state) {
93
    parent::extra_options_form($form, $form_state);
94

    
95
    $fields = date_views_fields($this->base_table);
96
    $options = array();
97
    foreach ($fields['name'] as $name => $field) {
98
      $options[$name] = $field['label'];
99
    }
100

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

    
119
  function extra_options_validate($form, &$form_state) {
120
    $check_fields = array_filter($form_state['values']['options']['date_fields']);
121
    if (empty($check_fields)) {
122
      form_error($form['date_fields'], t('You must select at least one date field for this filter.'));
123
    }
124
  }
125

    
126
  function extra_options_submit($form, &$form_state) {
127
    $form_state['values']['options']['date_fields'] = array_filter($form_state['values']['options']['date_fields']);
128
  }
129

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

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

    
167
  function get_query_fields() {
168
    $fields = date_views_fields($this->base_table);
169
    $fields = $fields['name'];
170
    $this->query_fields = array();
171
    foreach ((array) $this->options['date_fields'] as $delta => $name) {
172
      if (array_key_exists($name, $fields) && $field = $fields[$name]) {
173
        $date_handler = new date_sql_handler($field['sql_type'], date_default_timezone());
174
        $date_handler->granularity = $this->options['granularity'];
175
        $date_handler->db_timezone = date_get_timezone_db($field['tz_handling']);
176
        $date_handler->local_timezone = date_get_timezone($field['tz_handling']);
177
        $this->query_fields[] = array('field' => $field, 'date_handler' => $date_handler);
178
      }
179
    }
180
  }
181
}