Projet

Général

Profil

Paste
Télécharger (13,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / taxonomy / views_handler_filter_term_node_tid.inc @ 5d12d676

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

    
15
  /**
16
   * Stores the exposed input for this filter.
17
   */
18
  public $validated_exposed_input = NULL;
19

    
20
  /**
21
   * {@inheritdoc}
22
   */
23
  public function init(&$view, &$options) {
24
    parent::init($view, $options);
25
    if (!empty($this->definition['vocabulary'])) {
26
      $this->options['vocabulary'] = $this->definition['vocabulary'];
27
    }
28

    
29
    // Convert legacy vid option to machine name vocabulary.
30
    if (isset($this->options['vid']) && !empty($this->options['vid']) & empty($this->options['vocabulary'])) {
31
      $vocabularies = taxonomy_get_vocabularies();
32
      $vid = $this->options['vid'];
33
      if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
34
        $this->options['vocabulary'] = $vocabularies[$vid]->machine_name;
35
      }
36
    }
37
  }
38

    
39
  /**
40
   * {@inheritdoc}
41
   */
42
  public function has_extra_options() {
43
    return TRUE;
44
  }
45

    
46
  /**
47
   * {@inheritdoc}
48
   */
49
  public function get_value_options() {
50
    // Don't overwrite the value options.
51
  }
52

    
53
  /**
54
   * {@inheritdoc}
55
   */
56
  public function option_definition() {
57
    $options = parent::option_definition();
58

    
59
    $options['type'] = array('default' => 'textfield');
60
    $options['limit'] = array('default' => TRUE, 'bool' => TRUE);
61
    $options['vocabulary'] = array('default' => 0);
62
    $options['hierarchy'] = array('default' => 0);
63
    $options['error_message'] = array('default' => TRUE, 'bool' => TRUE);
64

    
65
    return $options;
66
  }
67

    
68
  /**
69
   * {@inheritdoc}
70
   */
71
  public function extra_options_form(&$form, &$form_state) {
72
    $vocabularies = taxonomy_get_vocabularies();
73
    $options = array();
74
    foreach ($vocabularies as $voc) {
75
      $options[$voc->machine_name] = check_plain($voc->name);
76
    }
77

    
78
    if ($this->options['limit']) {
79
      // We only do this when the form is displayed.
80
      if (empty($this->options['vocabulary'])) {
81
        $first_vocabulary = reset($vocabularies);
82
        $this->options['vocabulary'] = $first_vocabulary->machine_name;
83
      }
84

    
85
      if (empty($this->definition['vocabulary'])) {
86
        $form['vocabulary'] = array(
87
          '#type' => 'radios',
88
          '#title' => t('Vocabulary'),
89
          '#options' => $options,
90
          '#description' => t('Select which vocabulary to show terms for in the regular options.'),
91
          '#default_value' => $this->options['vocabulary'],
92
        );
93
      }
94
    }
95

    
96
    $form['type'] = array(
97
      '#type' => 'radios',
98
      '#title' => t('Selection type'),
99
      '#options' => array('select' => t('Dropdown'), 'textfield' => t('Autocomplete')),
100
      '#default_value' => $this->options['type'],
101
    );
102

    
103
    $form['hierarchy'] = array(
104
      '#type' => 'checkbox',
105
      '#title' => t('Show hierarchy in dropdown'),
106
      '#default_value' => !empty($this->options['hierarchy']),
107
      '#dependency' => array('radio:options[type]' => array('select')),
108
    );
109
  }
110

    
111
  /**
112
   * {@inheritdoc}
113
   */
114
  public function value_form(&$form, &$form_state) {
115
    $vocabulary = taxonomy_vocabulary_machine_name_load($this->options['vocabulary']);
116
    if (empty($vocabulary) && $this->options['limit']) {
117
      $form['markup'] = array(
118
        '#markup' => '<div class="form-item">' . t('An invalid vocabulary is selected. Please change it in the options.') . '</div>',
119
      );
120
      return;
121
    }
122

    
123
    if ($this->options['type'] == 'textfield') {
124
      $default = '';
125
      if ($this->value) {
126
        $result = taxonomy_term_load_multiple($this->value);
127
        foreach ($result as $entity_term) {
128
          if ($default) {
129
            $default .= ', ';
130
          }
131
          $default .= entity_label('taxonomy_term', $entity_term);
132
        }
133
      }
134

    
135
      $form['value'] = array(
136
        '#title' => $this->options['limit'] ? t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->name)) : t('Select terms'),
137
        '#type' => 'textfield',
138
        '#default_value' => $default,
139
      );
140

    
141
      if ($this->options['limit']) {
142
        $form['value']['#autocomplete_path'] = 'admin/views/ajax/autocomplete/taxonomy/' . $vocabulary->vid;
143
      }
144
    }
145
    else {
146
      if (!empty($this->options['hierarchy']) && $this->options['limit']) {
147
        $tree = taxonomy_get_tree($vocabulary->vid, 0, NULL, TRUE);
148
        $options = array();
149

    
150
        if ($tree) {
151
          // Translation system needs full entity objects, so we have access to
152
          // label.
153
          foreach ($tree as $term) {
154
            $choice = new stdClass();
155
            $choice->option = array($term->tid => str_repeat('-', $term->depth) . entity_label('taxonomy_term', $term));
156
            $options[] = $choice;
157
          }
158
        }
159
      }
160
      else {
161
        $options = array();
162
        $query = db_select('taxonomy_term_data', 'td');
163
        $query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
164
        $query->fields('td');
165
        $query->orderby('tv.weight');
166
        $query->orderby('tv.name');
167
        $query->orderby('td.weight');
168
        $query->orderby('td.name');
169
        $query->addTag('taxonomy_term_access');
170
        if ($this->options['limit']) {
171
          $query->condition('tv.machine_name', $vocabulary->machine_name);
172
        }
173
        $result = $query->execute();
174

    
175
        $tids = array();
176
        foreach ($result as $term) {
177
          $tids[] = $term->tid;
178
        }
179
        $entities = taxonomy_term_load_multiple($tids);
180
        foreach ($entities as $entity_term) {
181
          $options[$entity_term->tid] = entity_label('taxonomy_term', $entity_term);
182
        }
183
      }
184

    
185
      $default_value = (array) $this->value;
186

    
187
      if (!empty($form_state['exposed'])) {
188
        $identifier = $this->options['expose']['identifier'];
189

    
190
        if (!empty($this->options['expose']['reduce'])) {
191
          $options = $this->reduce_value_options($options);
192

    
193
          if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
194
            $default_value = array();
195
          }
196
        }
197

    
198
        if (empty($this->options['expose']['multiple'])) {
199
          if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
200
            $default_value = 'All';
201
          }
202
          elseif (empty($default_value)) {
203
            $keys = array_keys($options);
204
            $default_value = array_shift($keys);
205
          }
206
          // Due to #1464174 there is a chance that array('') was saved in the
207
          // admin UI. Let's choose a safe default value.
208
          elseif ($default_value == array('')) {
209
            $default_value = 'All';
210
          }
211
          else {
212
            $copy = $default_value;
213
            $default_value = array_shift($copy);
214
          }
215
        }
216
      }
217
      $form['value'] = array(
218
        '#type' => 'select',
219
        '#title' => $this->options['limit'] ? t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->name)) : t('Select terms'),
220
        '#multiple' => TRUE,
221
        '#options' => $options,
222
        '#size' => min(9, count($options)),
223
        '#default_value' => $default_value,
224
      );
225

    
226
      if (!empty($form_state['exposed']) && isset($identifier) && !isset($form_state['input'][$identifier])) {
227
        $form_state['input'][$identifier] = $default_value;
228
      }
229
    }
230

    
231

    
232
    if (empty($form_state['exposed'])) {
233
      // Retain the helper option
234
      $this->helper->options_form($form, $form_state);
235

    
236
      // Show help text if not exposed to end users.
237
      $form['value']['#description'] = t('Leave blank for all. Otherwise, the first selected term will be the default instead of "Any".');
238
    }
239
  }
240

    
241
  /**
242
   * {@inheritdoc}
243
   */
244
  public function value_validate($form, &$form_state) {
245
    // We only validate if they've chosen the text field style.
246
    if ($this->options['type'] != 'textfield') {
247
      return;
248
    }
249

    
250
    $values = drupal_explode_tags($form_state['values']['options']['value']);
251
    $tids = $this->validate_term_strings($form['value'], $values);
252

    
253
    if ($tids) {
254
      $form_state['values']['options']['value'] = $tids;
255
    }
256
  }
257

    
258
  /**
259
   * {@inheritdoc}
260
   */
261
  public function accept_exposed_input($input) {
262
    if (empty($this->options['exposed'])) {
263
      return TRUE;
264
    }
265

    
266
    // We need to know the operator, which is normally set in
267
    // views_handler_filter::accept_exposed_input(), before we actually call
268
    // the parent version of ourselves.
269
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
270
      $this->operator = $input[$this->options['expose']['operator_id']];
271
    }
272

    
273
    // If view is an attachment and is inheriting exposed filters, then assume
274
    // exposed input has already been validated
275
    if (!empty($this->view->is_attachment) && $this->view->display_handler->uses_exposed()) {
276
      $this->validated_exposed_input = (array) $this->view->exposed_raw_input[$this->options['expose']['identifier']];
277
    }
278

    
279
    // If we're checking for EMPTY or NOT, we don't need any input, and we can
280
    // say that our input conditions are met by just having the right operator.
281
    if ($this->operator == 'empty' || $this->operator == 'not empty') {
282
      return TRUE;
283
    }
284

    
285
    // If it's non-required and there's no value don't bother filtering.
286
    if (!$this->options['expose']['required'] && empty($this->validated_exposed_input)) {
287
      return FALSE;
288
    }
289

    
290
    $rc = parent::accept_exposed_input($input);
291
    if ($rc) {
292
      // If we have previously validated input, override.
293
      if (!$this->is_a_group() && isset($this->validated_exposed_input)) {
294
        $this->value = $this->validated_exposed_input;
295
      }
296
    }
297

    
298
    return $rc;
299
  }
300

    
301
  /**
302
   * {@inheritdoc}
303
   */
304
  public function exposed_validate(&$form, &$form_state) {
305
    if (empty($this->options['exposed'])) {
306
      return;
307
    }
308

    
309
    $identifier = $this->options['expose']['identifier'];
310

    
311
    // We only validate if they've chosen the text field style.
312
    if ($this->options['type'] != 'textfield') {
313
      if (isset($form_state['values'][$identifier]) && $form_state['values'][$identifier] != 'All') {
314
        $this->validated_exposed_input = (array) $form_state['values'][$identifier];
315
      }
316
      return;
317
    }
318

    
319
    if (empty($this->options['expose']['identifier'])) {
320
      return;
321
    }
322

    
323
    $values = drupal_explode_tags($form_state['values'][$identifier]);
324

    
325
    $tids = $this->validate_term_strings($form[$identifier], $values);
326
    if ($tids) {
327
      $this->validated_exposed_input = $tids;
328
    }
329
  }
330

    
331
  /**
332
   * Validate the user string. Since this can come from either the form
333
   * or the exposed filter, this is abstracted out a bit so it can
334
   * handle the multiple input sources.
335
   *
336
   * @param array $form
337
   *   The form which is used, either the views ui or the exposed filters.
338
   * @param array $values
339
   *   The taxonomy names which will be converted to tids.
340
   *
341
   * @return array
342
   *   The taxonomy ids fo all validated terms.
343
   */
344
  public function validate_term_strings(&$form, $values) {
345
    if (empty($values)) {
346
      return array();
347
    }
348

    
349
    $tids = array();
350
    $names = array();
351
    $missing = array();
352
    foreach ($values as $value) {
353
      $missing[strtolower($value)] = TRUE;
354
      $names[] = $value;
355
    }
356

    
357
    if (!$names) {
358
      return FALSE;
359
    }
360

    
361
    $query = db_select('taxonomy_term_data', 'td');
362
    $query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
363
    $query->fields('td');
364
    $query->condition('td.name', $names);
365
    $query->condition('tv.machine_name', $this->options['vocabulary']);
366
    $query->addTag('taxonomy_term_access');
367
    $result = $query->execute();
368
    foreach ($result as $term) {
369
      unset($missing[strtolower($term->name)]);
370
      $tids[] = $term->tid;
371
    }
372

    
373
    if ($missing && !empty($this->options['error_message'])) {
374
      form_error($form, format_plural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array('@terms' => implode(', ', array_keys($missing)))));
375
    }
376
    elseif ($missing && empty($this->options['error_message'])) {
377
      $tids = array(0);
378
    }
379

    
380
    return $tids;
381
  }
382

    
383
  /**
384
   * {@inheritdoc}
385
   */
386
  public function value_submit($form, &$form_state) {
387
    // prevent array_filter from messing up our arrays in parent submit.
388
  }
389

    
390
  /**
391
   * {@inheritdoc}
392
   */
393
  public function expose_form(&$form, &$form_state) {
394
    parent::expose_form($form, $form_state);
395
    if ($this->options['type'] != 'select') {
396
      unset($form['expose']['reduce']);
397
    }
398
    $form['error_message'] = array(
399
      '#type' => 'checkbox',
400
      '#title' => t('Display error message'),
401
      '#default_value' => !empty($this->options['error_message']),
402
    );
403
  }
404

    
405
  /**
406
   * {@inheritdoc}
407
   */
408
  public function admin_summary() {
409
    // set up $this->value_options for the parent summary
410
    $this->value_options = array();
411

    
412
    if ($this->value) {
413
      $this->value = array_filter($this->value);
414
      $result = taxonomy_term_load_multiple($this->value);
415
      foreach ($result as $entity_term) {
416
        $this->value_options[$entity_term->tid] = entity_label('taxonomy_term', $entity_term);
417
      }
418
    }
419
    return parent::admin_summary();
420
  }
421

    
422
}