Projet

Général

Profil

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

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

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

    
10
/**
11
 * Date API argument handler.
12
 */
13
// @codingStandardsIgnoreStart
14
class date_views_argument_handler extends date_views_argument_handler_simple {
15

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

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

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

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

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

    
75
  function options_validate(&$form, &$form_state) {
76

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

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

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

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

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

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

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

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

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

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

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

    
156
    $this->placeholders = array();
157

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

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

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

    
202
}
203
// @codingStandardsIgnoreEnd