Project

General

Profile

Paste
Download (12.4 KB) Statistics
| Branch: | Revision:

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

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
        '#description' => t('Leave blank for all. Otherwise, the first selected term will be the default instead of "Any".'),
199
      );
200

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

    
206

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

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

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

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

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

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

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

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

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

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

    
264
    return $rc;
265
  }
266

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

    
272
    $identifier = $this->options['expose']['identifier'];
273

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

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

    
286
    $values = drupal_explode_tags($form_state['values'][$identifier]);
287

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

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

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

    
320
    if (!$names) {
321
      return FALSE;
322
    }
323

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

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

    
343
    return $tids;
344
  }
345

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

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

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

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