Projet

Général

Profil

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

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

1
<?php
2
/**
3
 * @file
4
 * Date API views argument handler.
5
 * This argument combines multiple date arguments into a single argument
6
 * where all fields are controlled by the same date and can be combined with either AND or OR.
7
 */
8

    
9
/**
10
 * Date API argument handler.
11
 */
12
class date_views_argument_handler extends date_views_argument_handler_simple {
13

    
14
  /**
15
   * Get granularity and use it to create the formula and a format
16
   * for the results.
17
   */
18
  function init(&$view, &$options) {
19
    parent::init($view, $options);
20

    
21
    if (empty($this->view->date_info)) {
22
      $this->view->date_info = new stdClass();
23
    }
24
    if (empty($this->view->date_info->date_fields)) {
25
      $this->view->date_info->date_fields = array();
26
    }
27
    $this->view->date_info->date_fields = array_merge($this->view->date_info->date_fields, $this->options['date_fields']);
28
  }
29

    
30
  /**
31
   * Default value for the date_fields option.
32
   */
33
  function option_definition() {
34
    $options = parent::option_definition();
35
    $options['date_fields'] = array('default' => array());
36
    $options['date_method'] = array('default' => 'OR');
37
    $options['date_group'] = array('default' => 'date');
38
    return $options;
39
  }
40

    
41
  /**
42
   * Add a form element to select date_fields for this argument.
43
   */
44
  function options_form(&$form, &$form_state) {
45
    parent::options_form($form, $form_state);
46
    $fields = date_views_fields($this->base_table);
47
    $options = array();
48
    foreach ($fields['name'] as $name => $field) {
49
      $options[$name] = $field['label'];
50
    }
51

    
52
    $form['date_fields'] = array(
53
      '#title' => t('Date field(s)'),
54
      '#type' => 'checkboxes',
55
      '#options' => $options,
56
      '#default_value' => $this->options['date_fields'],
57
      '#multiple' => TRUE,
58
      '#description' => t("Select one or more date fields to filter with this argument."),
59
    );
60
    $form['date_method'] = array(
61
      '#title' => t('Method'),
62
      '#type' => 'radios',
63
      '#options' => array('OR' => t('OR'), 'AND' => t('AND')),
64
      '#default_value' => $this->options['date_method'],
65
      '#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). '),
66
      );
67
    $form['date_group'] = array(
68
      '#type' => 'hidden',
69
      '#value' => $this->options['date_group'],
70
    );
71
  }
72

    
73
  function options_validate(&$form, &$form_state) {
74

    
75
    // Views will whine if we don't have something for the these values even though we removed the option for summaries.
76
    $form_state['values']['options']['summary']['format'] = 'none';
77
    $form_state['values']['options']['summary']['options']['none'] = array();
78

    
79
    // It is very important to call the parent function here:
80
    parent::options_validate($form, $form_state);
81

    
82
    if ($form_state['values']['form_id'] == 'views_ui_config_item_form') {
83
      $check_fields = array_filter($form_state['values']['options']['date_fields']);
84
      if (empty($check_fields)) {
85
        form_error($form['date_fields'], t('You must select at least one date field for this argument.'));
86
      }
87
    }
88
  }
89

    
90
  function options_submit(&$form, &$form_state) {
91
    // It is very important to call the parent function here:
92
    parent::options_submit($form, $form_state);
93
    if ($form_state['values']['form_id'] == 'views_ui_config_item_form') {
94
      $form_state['values']['options']['date_fields'] = array_filter($form_state['values']['options']['date_fields']);
95
    }
96
  }
97

    
98
  // Update the summary values to show selected granularity.
99
  function admin_summary() {
100
    $fields = date_views_fields($this->base_table);
101
    if (!empty($this->options['date_fields'])) {
102
      $output = array();
103
      foreach ($this->options['date_fields'] as $field) {
104
        if (array_key_exists($field, $fields['name'])) {
105
          $output[] = $fields['name'][$field]['label'];
106
        }
107
      }
108
      return implode('<br />' . $this->options['date_method'] . ' ', $output);
109
    }
110
    else {
111
      return parent::admin_summary();
112
    }
113
  }
114

    
115
  /**
116
   * Provide a list of default behaviors for this argument if the argument
117
   * is not present.
118
   *
119
   * Override this method to provide additional (or fewer) default behaviors.
120
   */
121
  function default_actions($which = NULL) {
122
    $defaults = parent::default_actions();
123

    
124
    // There is no easy way to do summary queries on multiple fields, so remove that option.
125
    unset($defaults['summary']);
126

    
127
    if ($which) {
128
      if (!empty($defaults[$which])) {
129
        return $defaults[$which];
130
      }
131
    }
132
    else {
133
      return $defaults;
134
    }
135
  }
136

    
137
  /**
138
   * Set up the query for this argument.
139
   *
140
   * The argument sent may be found at $this->argument.
141
   */
142
  function query($group_by = FALSE) {
143

    
144
    // @TODO Not doing anything with $group_by yet, need to figure out what has to be done.
145

    
146
    if ($this->date_forbid()) {
147
      return;
148
    }
149
    $this->get_query_fields();
150
    $this->query->set_where_group($this->options['date_method'], $this->options['date_group']);
151
    $this->granularity = $this->date_handler->arg_granularity($this->argument);
152
    $format = $this->date_handler->views_formats($this->granularity, 'sql');
153

    
154
    $this->placeholders = array();
155

    
156
    if (!empty($this->query_fields)) {
157
      // Use set_where_group() with the selected date_method
158
      // of 'AND' or 'OR' to create the where clause.
159
      foreach ($this->query_fields as $count => $query_field) {
160
        $field = $query_field['field'];
161
        $this->date_handler = $query_field['date_handler'];
162
        $this->field = $field['field_name'];
163
        $this->real_field = $field['field_name'];
164
        $this->table = $field['table_name'];
165
        $this->original_table = $field['table_name'];
166
        if ($field['table_name'] != $this->table || !empty($this->relationship)) {
167
          $this->table = $this->query->ensure_table($field['table_name'], $this->relationship);
168
        }
169
        // $this->table_alias gets set when the first field is processed if otherwise empty.
170
        // For subsequent fields, we need to be sure it is emptied again.
171
        elseif (empty($this->relationship)) {
172
          $this->table_alias = NULL;
173
        }
174
        parent::query($group_by);
175

    
176
        $this->placeholders = array_merge($this->placeholders, $this->date_handler->placeholders);
177
      }
178
    }
179
  }
180

    
181
  /**
182
   * Collect information about our fields we will need to create the right query.
183
   */
184
  function get_query_fields() {
185
    $fields = date_views_fields($this->base_table);
186
    $fields = $fields['name'];
187
    $this->query_fields = array();
188
    foreach ($this->options['date_fields'] as $delta => $name) {
189
      if (array_key_exists($name, $fields) && $field = $fields[$name]) {
190
        $date_handler = new date_sql_handler($field['sql_type'], date_default_timezone());
191
        $date_handler->granularity = $this->options['granularity'];
192
        $date_handler->db_timezone = date_get_timezone_db($field['tz_handling']);
193
        $date_handler->local_timezone = date_get_timezone($field['tz_handling']);
194
        date_views_set_timezone($date_handler, $this, $field);
195
        $this->query_fields[] = array('field' => $field, 'date_handler' => $date_handler);
196
      }
197
    }
198
  }
199

    
200
}