Projet

Général

Profil

Paste
Télécharger (8,18 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / taxonomy / views_plugin_argument_validate_taxonomy_term.inc @ 4003efde

1
<?php
2

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

    
8
/**
9
 * Validate whether an argument is an acceptable node.
10
 */
11
class views_plugin_argument_validate_taxonomy_term extends views_plugin_argument_validate {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public function init(&$view, &$argument, $options) {
17
    parent::init($view, $argument, $options);
18

    
19
    // Convert legacy vids option to machine name vocabularies.
20
    if (!empty($this->options['vids'])) {
21
      $vocabularies = taxonomy_get_vocabularies();
22
      foreach ($this->options['vids'] as $vid) {
23
        if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
24
          $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
25
        }
26
      }
27
    }
28
  }
29

    
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public function option_definition() {
34
    $options = parent::option_definition();
35
    $options['vocabularies'] = array('default' => array());
36
    $options['type'] = array('default' => 'tid');
37
    $options['transform'] = array('default' => FALSE, 'bool' => TRUE);
38

    
39
    return $options;
40
  }
41

    
42
  /**
43
   * {@inheritdoc}
44
   */
45
  public function options_form(&$form, &$form_state) {
46
    $vocabularies = taxonomy_get_vocabularies();
47
    $options = array();
48
    foreach ($vocabularies as $voc) {
49
      $options[$voc->machine_name] = check_plain($voc->name);
50
    }
51

    
52
    $form['vocabularies'] = array(
53
      '#type' => 'checkboxes',
54
      '#prefix' => '<div id="edit-options-validate-argument-vocabulary-wrapper">',
55
      '#suffix' => '</div>',
56
      '#title' => t('Vocabularies'),
57
      '#options' => $options,
58
      '#default_value' => $this->options['vocabularies'],
59
      '#description' => t('If you wish to validate for specific vocabularies, check them; if none are checked, all terms will pass.'),
60
    );
61

    
62
    $form['type'] = array(
63
      '#type' => 'select',
64
      '#title' => t('Filter value type'),
65
      '#options' => array(
66
        'tid' => t('Term ID'),
67
        'tids' => t('Term IDs separated by , or +'),
68
        'name' => t('Term name'),
69
        'convert' => t('Term name converted to Term ID'),
70
      ),
71
      '#default_value' => $this->options['type'],
72
      '#description' => t('Select the form of this filter value; if using term name, it is generally more efficient to convert it to a term ID and use Taxonomy: Term ID rather than Taxonomy: Term Name" as the filter.'),
73
    );
74

    
75
    $form['transform'] = array(
76
      '#type' => 'checkbox',
77
      '#title' => t('Transform dashes in URL to spaces in term name filter values'),
78
      '#default_value' => $this->options['transform'],
79
    );
80
  }
81

    
82
  /**
83
   * {@inheritdoc}
84
   */
85
  public function options_submit(&$form, &$form_state, &$options = array()) {
86
    // Filter unselected items so we don't unnecessarily store giant arrays.
87
    $options['vocabularies'] = array_filter($options['vocabularies']);
88
  }
89

    
90
  /**
91
   * {@inheritdoc}
92
   */
93
  public function convert_options(&$options) {
94
    if (!isset($options['vocabularies']) && !empty($this->argument->options['validate_argument_vocabulary'])) {
95
      $options['vocabularies'] = $this->argument->options['validate_argument_vocabulary'];
96
      $options['type'] = $this->argument->options['validate_argument_type'];
97
      $options['transform'] = isset($this->argument->options['validate_argument_transform']) ? $this->argument->options['validate_argument_transform'] : FALSE;
98
    }
99
  }
100

    
101
  /**
102
   * {@inheritdoc}
103
   */
104
  public function validate_argument($argument) {
105
    $vocabularies = array_filter($this->options['vocabularies']);
106
    $type = $this->options['type'];
107
    $transform = $this->options['transform'];
108

    
109
    switch ($type) {
110
      case 'tid':
111
        if (!is_numeric($argument)) {
112
          return FALSE;
113
        }
114

    
115
        $query = db_select('taxonomy_term_data', 'td');
116
        $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
117
        $query->fields('td');
118
        $query->condition('td.tid', $argument);
119
        $query->addTag('taxonomy_term_access');
120
        $term = $query->execute()->fetchObject();
121
        if (!$term) {
122
          return FALSE;
123
        }
124
        $term = taxonomy_term_load($term->tid);
125
        $this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term));
126
        return empty($vocabularies) || !empty($vocabularies[$term->vocabulary_machine_name]);
127

    
128
      case 'tids':
129
        // An empty argument is not a term so doesn't pass.
130
        if (empty($argument)) {
131
          return FALSE;
132
        }
133

    
134
        $tids = new stdClass();
135
        $tids->value = $argument;
136
        $tids = views_break_phrase($argument, $tids);
137
        if ($tids->value == array(-1)) {
138
          return FALSE;
139
        }
140

    
141
        $test = drupal_map_assoc($tids->value);
142
        $titles = array();
143

    
144
        // check, if some tids already verified.
145
        static $validated_cache = array();
146
        foreach ($test as $tid) {
147
          if (isset($validated_cache[$tid])) {
148
            if ($validated_cache[$tid] === FALSE) {
149
              return FALSE;
150
            }
151
            else {
152
              $titles[] = $validated_cache[$tid];
153
              unset($test[$tid]);
154
            }
155
          }
156
        }
157

    
158
        // if unverified tids left - verify them and cache results.
159
        if (count($test)) {
160
          $query = db_select('taxonomy_term_data', 'td');
161
          $query->addTag('taxonomy_term_access');
162
          $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
163
          $query->fields('td');
164
          $query->fields('tv', array('machine_name'));
165
          $query->condition('td.tid', $test);
166

    
167
          $result = $query->execute();
168

    
169
          foreach ($result as $term) {
170
            if ($vocabularies && empty($vocabularies[$term->machine_name])) {
171
              $validated_cache[$term->tid] = FALSE;
172
              return FALSE;
173
            }
174
            $term = taxonomy_term_load($term->tid);
175
            $titles[] = $validated_cache[$term->tid] = check_plain(entity_label('taxonomy_term', $term));
176
            unset($test[$term->tid]);
177
          }
178
        }
179

    
180
        // Remove duplicate titles.
181
        $titles = array_unique($titles);
182

    
183
        $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);
184
        // If this is not empty, we did not find a tid.
185
        return empty($test);
186

    
187
      case 'name':
188
      case 'convert':
189
        $query = db_select('taxonomy_term_data', 'td');
190
        $query->addTag('taxonomy_term_access');
191
        $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
192
        $query->fields('td');
193
        $query->fields('tv', array('machine_name'));
194
        if (!empty($vocabularies)) {
195
          $query->condition('tv.machine_name', $vocabularies);
196
        }
197
        if ($transform) {
198
          $query->where("replace(td.name, ' ', '-') = :name", array(':name' => $argument));
199
        }
200
        else {
201
          $query->condition('td.name', $argument);
202
        }
203
        $term = $query->execute()->fetchObject();
204

    
205
        if ($term && (empty($vocabularies) || !empty($vocabularies[$term->machine_name]))) {
206
          if ($type == 'convert') {
207
            $this->argument->argument = $term->tid;
208
          }
209
          $term = taxonomy_term_load($term->tid);
210
          $this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term));
211
          return TRUE;
212
        }
213
        return FALSE;
214
    }
215
  }
216

    
217
  /**
218
   * {@inheritdoc}
219
   */
220
  public function process_summary_arguments(&$args) {
221
    $type = $this->options['type'];
222
    $transform = $this->options['transform'];
223
    $vocabularies = array_filter($this->options['vocabularies']);
224

    
225
    if ($type == 'convert') {
226
      $arg_keys = array_flip($args);
227

    
228
      $query = db_select('taxonomy_term_data', 'td');
229
      $query->addTag('taxonomy_term_access');
230
      $query->condition('td.tid', $args);
231
      $query->addField('td', 'tid', 'tid');
232
      if (!empty($vocabularies)) {
233
        $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
234
        $query->condition('tv.machine_name', $vocabularies);
235
      }
236
      if ($transform) {
237
        $query->addExpression("REPLACE(td.name, ' ', '-')", 'name');
238
      }
239
      else {
240
        $query->addField('td', 'name', 'name');
241
      }
242

    
243
      foreach ($query->execute()->fetchAllKeyed() as $tid => $term) {
244
        $args[$arg_keys[$tid]] = $term;
245
      }
246
    }
247
  }
248

    
249
}