Projet

Général

Profil

Paste
Télécharger (12,6 ko) Statistiques
| Branche: | Révision:

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

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('taxonomy_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
      // Show help text if not exposed to end users.
211
      $form['value']['#description'] = t('Leave blank for all. Otherwise, the first selected term will be the default instead of "Any".');
212
    }
213
  }
214

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

    
221
    $values = drupal_explode_tags($form_state['values']['options']['value']);
222
    $tids = $this->validate_term_strings($form['value'], $values);
223

    
224
    if ($tids) {
225
      $form_state['values']['options']['value'] = $tids;
226
    }
227
  }
228

    
229
  function accept_exposed_input($input) {
230
    if (empty($this->options['exposed'])) {
231
      return TRUE;
232
    }
233

    
234
    // We need to know the operator, which is normally set in
235
    // views_handler_filter::accept_exposed_input(), before we actually call
236
    // the parent version of ourselves.
237
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
238
      $this->operator = $input[$this->options['expose']['operator_id']];
239
    }
240

    
241
    // If view is an attachment and is inheriting exposed filters, then assume
242
    // exposed input has already been validated
243
    if (!empty($this->view->is_attachment) && $this->view->display_handler->uses_exposed()) {
244
      $this->validated_exposed_input = (array) $this->view->exposed_raw_input[$this->options['expose']['identifier']];
245
    }
246

    
247
    // If we're checking for EMPTY or NOT, we don't need any input, and we can
248
    // say that our input conditions are met by just having the right operator.
249
    if ($this->operator == 'empty' || $this->operator == 'not empty') {
250
      return TRUE;
251
    }
252

    
253
    // If it's non-required and there's no value don't bother filtering.
254
    if (!$this->options['expose']['required'] && empty($this->validated_exposed_input)) {
255
      return FALSE;
256
    }
257

    
258
    $rc = parent::accept_exposed_input($input);
259
    if ($rc) {
260
      // If we have previously validated input, override.
261
      if (!$this->is_a_group() && isset($this->validated_exposed_input)) {
262
        $this->value = $this->validated_exposed_input;
263
      }
264
    }
265

    
266
    return $rc;
267
  }
268

    
269
  function exposed_validate(&$form, &$form_state) {
270
    if (empty($this->options['exposed'])) {
271
      return;
272
    }
273

    
274
    $identifier = $this->options['expose']['identifier'];
275

    
276
    // We only validate if they've chosen the text field style.
277
    if ($this->options['type'] != 'textfield') {
278
      if (isset($form_state['values'][$identifier]) && $form_state['values'][$identifier] != 'All') {
279
        $this->validated_exposed_input = (array) $form_state['values'][$identifier];
280
      }
281
      return;
282
    }
283

    
284
    if (empty($this->options['expose']['identifier'])) {
285
      return;
286
    }
287

    
288
    $values = drupal_explode_tags($form_state['values'][$identifier]);
289

    
290
    $tids = $this->validate_term_strings($form[$identifier], $values);
291
    if ($tids) {
292
      $this->validated_exposed_input = $tids;
293
    }
294
  }
295

    
296
  /**
297
   * Validate the user string. Since this can come from either the form
298
   * or the exposed filter, this is abstracted out a bit so it can
299
   * handle the multiple input sources.
300
   *
301
   * @param $form
302
   *   The form which is used, either the views ui or the exposed filters.
303
   * @param $values
304
   *   The taxonomy names which will be converted to tids.
305
   *
306
   * @return array
307
   *   The taxonomy ids fo all validated terms.
308
   */
309
  function validate_term_strings(&$form, $values) {
310
    if (empty($values)) {
311
      return array();
312
    }
313

    
314
    $tids = array();
315
    $names = array();
316
    $missing = array();
317
    foreach ($values as $value) {
318
      $missing[strtolower($value)] = TRUE;
319
      $names[] = $value;
320
    }
321

    
322
    if (!$names) {
323
      return FALSE;
324
    }
325

    
326
    $query = db_select('taxonomy_term_data', 'td');
327
    $query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
328
    $query->fields('td');
329
    $query->condition('td.name', $names);
330
    $query->condition('tv.machine_name', $this->options['vocabulary']);
331
    $query->addTag('taxonomy_term_access');
332
    $result = $query->execute();
333
    foreach ($result as $term) {
334
      unset($missing[strtolower($term->name)]);
335
      $tids[] = $term->tid;
336
    }
337

    
338
    if ($missing && !empty($this->options['error_message'])) {
339
      form_error($form, format_plural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array('@terms' => implode(', ', array_keys($missing)))));
340
    }
341
    elseif ($missing && empty($this->options['error_message'])) {
342
      $tids = array(0);
343
    }
344

    
345
    return $tids;
346
  }
347

    
348
  function value_submit($form, &$form_state) {
349
    // prevent array_filter from messing up our arrays in parent submit.
350
  }
351

    
352
  function expose_form(&$form, &$form_state) {
353
    parent::expose_form($form, $form_state);
354
    if ($this->options['type'] != 'select') {
355
      unset($form['expose']['reduce']);
356
    }
357
    $form['error_message'] = array(
358
      '#type' => 'checkbox',
359
      '#title' => t('Display error message'),
360
      '#default_value' => !empty($this->options['error_message']),
361
    );
362
  }
363

    
364
  function admin_summary() {
365
    // set up $this->value_options for the parent summary
366
    $this->value_options = array();
367

    
368
    if ($this->value) {
369
      $this->value = array_filter($this->value);
370
      $result = taxonomy_term_load_multiple($this->value);
371
      foreach ($result as $entity_term) {
372
        $this->value_options[$entity_term->tid] = entity_label('taxonomy_term', $entity_term);
373
      }
374
    }
375
    return parent::admin_summary();
376
  }
377
}