Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / modules / taxonomy / views_handler_filter_term_node_tid.inc @ 6eb8d15f

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_filter_term_node_tid.
6
 */
7

    
8
/**
9
 * Filter by term id.
10
 *
11
 * @ingroup views_filter_handlers
12
 */
13
class views_handler_filter_term_node_tid extends views_handler_filter_many_to_one {
14
  // Stores the exposed input for this filter.
15
  var $validated_exposed_input = NULL;
16

    
17
  function init(&$view, &$options) {
18
    parent::init($view, $options);
19
    if (!empty($this->definition['vocabulary'])) {
20
      $this->options['vocabulary'] = $this->definition['vocabulary'];
21
    }
22

    
23
    // Convert legacy vid option to machine name vocabulary.
24
    if (isset($this->options['vid']) && !empty($this->options['vid']) & empty($this->options['vocabulary'])) {
25
      $vocabularies = taxonomy_get_vocabularies();
26
      $vid = $this->options['vid'];
27
      if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
28
        $this->options['vocabulary'] = $vocabularies[$vid]->machine_name;
29
      }
30
    }
31
  }
32

    
33
  function has_extra_options() { return TRUE; }
34

    
35
  function get_value_options() { /* don't overwrite the value options */ }
36

    
37
  function option_definition() {
38
    $options = parent::option_definition();
39

    
40
    $options['type'] = array('default' => 'textfield');
41
    $options['limit'] = array('default' => TRUE, 'bool' => TRUE);
42
    $options['vocabulary'] = array('default' => 0);
43
    $options['hierarchy'] = array('default' => 0);
44
    $options['error_message'] = array('default' => TRUE, 'bool' => TRUE);
45

    
46
    return $options;
47
  }
48

    
49
  function extra_options_form(&$form, &$form_state) {
50
    $vocabularies = taxonomy_get_vocabularies();
51
    $options = array();
52
    foreach ($vocabularies as $voc) {
53
      $options[$voc->machine_name] = check_plain($voc->name);
54
    }
55

    
56
    if ($this->options['limit']) {
57
      // We only do this when the form is displayed.
58
      if (empty($this->options['vocabulary'])) {
59
        $first_vocabulary = reset($vocabularies);
60
        $this->options['vocabulary'] = $first_vocabulary->machine_name;
61
      }
62

    
63
      if (empty($this->definition['vocabulary'])) {
64
        $form['vocabulary'] = array(
65
          '#type' => 'radios',
66
          '#title' => t('Vocabulary'),
67
          '#options' => $options,
68
          '#description' => t('Select which vocabulary to show terms for in the regular options.'),
69
          '#default_value' => $this->options['vocabulary'],
70
        );
71
      }
72
    }
73

    
74
    $form['type'] = array(
75
      '#type' => 'radios',
76
      '#title' => t('Selection type'),
77
      '#options' => array('select' => t('Dropdown'), 'textfield' => t('Autocomplete')),
78
      '#default_value' => $this->options['type'],
79
    );
80

    
81
    $form['hierarchy'] = array(
82
      '#type' => 'checkbox',
83
      '#title' => t('Show hierarchy in dropdown'),
84
      '#default_value' => !empty($this->options['hierarchy']),
85
      '#dependency' => array('radio:options[type]' => array('select')),
86
    );
87
  }
88

    
89
  function value_form(&$form, &$form_state) {
90
    $vocabulary = taxonomy_vocabulary_machine_name_load($this->options['vocabulary']);
91
    if (empty($vocabulary) && $this->options['limit']) {
92
      $form['markup'] = array(
93
        '#markup' => '<div class="form-item">' . t('An invalid vocabulary is selected. Please change it in the options.') . '</div>',
94
      );
95
      return;
96
    }
97

    
98
    if ($this->options['type'] == 'textfield') {
99
      $default = '';
100
      if ($this->value) {
101
        $result = taxonomy_term_load_multiple($this->value);
102
        foreach ($result as $entity_term) {
103
          if ($default) {
104
            $default .= ', ';
105
          }
106
          $default .= entity_label('taxonomy_term', $entity_term);
107
        }
108
      }
109

    
110
      $form['value'] = array(
111
        '#title' => $this->options['limit'] ? t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->name)) : t('Select terms'),
112
        '#type' => 'textfield',
113
        '#default_value' => $default,
114
      );
115

    
116
      if ($this->options['limit']) {
117
        $form['value']['#autocomplete_path'] = 'admin/views/ajax/autocomplete/taxonomy/' . $vocabulary->vid;
118
      }
119
    }
120
    else {
121
      if (!empty($this->options['hierarchy']) && $this->options['limit']) {
122
        $tree = taxonomy_get_tree($vocabulary->vid, 0, NULL, TRUE);
123
        $options = array();
124

    
125
        if ($tree) {
126
          // Translation system needs full entity objects, so we have access to label.
127
          foreach ($tree as $term) {
128
            $choice = new stdClass();
129
            $choice->option = array($term->tid => str_repeat('-', $term->depth) . entity_label('taxonomy_term', $term));
130
            $options[] = $choice;
131
          }
132
        }
133
      }
134
      else {
135
        $options = array();
136
        $query = db_select('taxonomy_term_data', 'td');
137
        $query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
138
        $query->fields('td');
139
        $query->orderby('tv.weight');
140
        $query->orderby('tv.name');
141
        $query->orderby('td.weight');
142
        $query->orderby('td.name');
143
        $query->addTag('term_access');
144
        if ($this->options['limit']) {
145
          $query->condition('tv.machine_name', $vocabulary->machine_name);
146
        }
147
        $result = $query->execute();
148

    
149
        $tids = array();
150
        foreach ($result as $term) {
151
          $tids[] = $term->tid;
152
        }
153
        $entities = taxonomy_term_load_multiple($tids);
154
        foreach ($entities as $entity_term) {
155
          $options[$entity_term->tid] = entity_label('taxonomy_term', $entity_term);
156
        }
157
      }
158

    
159
      $default_value = (array) $this->value;
160

    
161
      if (!empty($form_state['exposed'])) {
162
        $identifier = $this->options['expose']['identifier'];
163

    
164
        if (!empty($this->options['expose']['reduce'])) {
165
          $options = $this->reduce_value_options($options);
166

    
167
          if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
168
            $default_value = array();
169
          }
170
        }
171

    
172
        if (empty($this->options['expose']['multiple'])) {
173
          if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
174
            $default_value = 'All';
175
          }
176
          elseif (empty($default_value)) {
177
            $keys = array_keys($options);
178
            $default_value = array_shift($keys);
179
          }
180
          // Due to #1464174 there is a chance that array('') was saved in the admin ui.
181
          // Let's choose a safe default value.
182
          elseif ($default_value == array('')) {
183
            $default_value = 'All';
184
          }
185
          else {
186
            $copy = $default_value;
187
            $default_value = array_shift($copy);
188
          }
189
        }
190
      }
191
      $form['value'] = array(
192
        '#type' => 'select',
193
        '#title' => $this->options['limit'] ? t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->name)) : t('Select terms'),
194
        '#multiple' => TRUE,
195
        '#options' => $options,
196
        '#size' => min(9, count($options)),
197
        '#default_value' => $default_value,
198
      );
199

    
200
      if (!empty($form_state['exposed']) && isset($identifier) && !isset($form_state['input'][$identifier])) {
201
        $form_state['input'][$identifier] = $default_value;
202
      }
203
    }
204

    
205

    
206
    if (empty($form_state['exposed'])) {
207
      // Retain the helper option
208
      $this->helper->options_form($form, $form_state);
209
    }
210
  }
211

    
212
  function value_validate($form, &$form_state) {
213
    // We only validate if they've chosen the text field style.
214
    if ($this->options['type'] != 'textfield') {
215
      return;
216
    }
217

    
218
    $values = drupal_explode_tags($form_state['values']['options']['value']);
219
    $tids = $this->validate_term_strings($form['value'], $values);
220

    
221
    if ($tids) {
222
      $form_state['values']['options']['value'] = $tids;
223
    }
224
  }
225

    
226
  function accept_exposed_input($input) {
227
    if (empty($this->options['exposed'])) {
228
      return TRUE;
229
    }
230

    
231
    // If view is an attachment and is inheriting exposed filters, then assume
232
    // exposed input has already been validated
233
    if (!empty($this->view->is_attachment) && $this->view->display_handler->uses_exposed()) {
234
      $this->validated_exposed_input = (array) $this->view->exposed_raw_input[$this->options['expose']['identifier']];
235
    }
236

    
237
    // If it's non-required and there's no value don't bother filtering.
238
    if (!$this->options['expose']['required'] && empty($this->validated_exposed_input)) {
239
      return FALSE;
240
    }
241

    
242
    $rc = parent::accept_exposed_input($input);
243
    if ($rc) {
244
      // If we have previously validated input, override.
245
      if (!$this->is_a_group() && isset($this->validated_exposed_input)) {
246
        $this->value = $this->validated_exposed_input;
247
      }
248
    }
249

    
250
    return $rc;
251
  }
252

    
253
  function exposed_validate(&$form, &$form_state) {
254
    if (empty($this->options['exposed'])) {
255
      return;
256
    }
257

    
258
    $identifier = $this->options['expose']['identifier'];
259

    
260
    // We only validate if they've chosen the text field style.
261
    if ($this->options['type'] != 'textfield') {
262
      if ($form_state['values'][$identifier] != 'All')  {
263
        $this->validated_exposed_input = (array) $form_state['values'][$identifier];
264
      }
265
      return;
266
    }
267

    
268
    if (empty($this->options['expose']['identifier'])) {
269
      return;
270
    }
271

    
272
    $values = drupal_explode_tags($form_state['values'][$identifier]);
273

    
274
    $tids = $this->validate_term_strings($form[$identifier], $values);
275
    if ($tids) {
276
      $this->validated_exposed_input = $tids;
277
    }
278
  }
279

    
280
  /**
281
   * Validate the user string. Since this can come from either the form
282
   * or the exposed filter, this is abstracted out a bit so it can
283
   * handle the multiple input sources.
284
   *
285
   * @param $form
286
   *   The form which is used, either the views ui or the exposed filters.
287
   * @param $values
288
   *   The taxonomy names which will be converted to tids.
289
   *
290
   * @return array
291
   *   The taxonomy ids fo all validated terms.
292
   */
293
  function validate_term_strings(&$form, $values) {
294
    if (empty($values)) {
295
      return array();
296
    }
297

    
298
    $tids = array();
299
    $names = array();
300
    $missing = array();
301
    foreach ($values as $value) {
302
      $missing[strtolower($value)] = TRUE;
303
      $names[] = $value;
304
    }
305

    
306
    if (!$names) {
307
      return FALSE;
308
    }
309

    
310
    $query = db_select('taxonomy_term_data', 'td');
311
    $query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
312
    $query->fields('td');
313
    $query->condition('td.name', $names);
314
    $query->condition('tv.machine_name', $this->options['vocabulary']);
315
    $query->addTag('term_access');
316
    $result = $query->execute();
317
    foreach ($result as $term) {
318
      unset($missing[strtolower($term->name)]);
319
      $tids[] = $term->tid;
320
    }
321

    
322
    if ($missing && !empty($this->options['error_message'])) {
323
      form_error($form, format_plural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array('@terms' => implode(', ', array_keys($missing)))));
324
    }
325
    elseif ($missing && empty($this->options['error_message'])) {
326
      $tids = array(0);
327
    }
328

    
329
    return $tids;
330
  }
331

    
332
  function value_submit($form, &$form_state) {
333
    // prevent array_filter from messing up our arrays in parent submit.
334
  }
335

    
336
  function expose_form(&$form, &$form_state) {
337
    parent::expose_form($form, $form_state);
338
    if ($this->options['type'] != 'select') {
339
      unset($form['expose']['reduce']);
340
    }
341
    $form['error_message'] = array(
342
      '#type' => 'checkbox',
343
      '#title' => t('Display error message'),
344
      '#default_value' => !empty($this->options['error_message']),
345
    );
346
  }
347

    
348
  function admin_summary() {
349
    // set up $this->value_options for the parent summary
350
    $this->value_options = array();
351

    
352
    if ($this->value) {
353
      $this->value = array_filter($this->value);
354
      $result = taxonomy_term_load_multiple($this->value);
355
      foreach ($result as $entity_term) {
356
        $this->value_options[$entity_term->tid] = entity_label('taxonomy_term', $entity_term);
357
      }
358
    }
359
    return parent::admin_summary();
360
  }
361
}