1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains the 'taxonomy term' argument validator plugin.
|
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
|
function init(&$view, &$argument, $options) {
|
13
|
parent::init($view, $argument, $options);
|
14
|
|
15
|
// Convert legacy vids option to machine name vocabularies.
|
16
|
if (!empty($this->options['vids'])) {
|
17
|
$vocabularies = taxonomy_get_vocabularies();
|
18
|
foreach ($this->options['vids'] as $vid) {
|
19
|
if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
|
20
|
$this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
|
21
|
}
|
22
|
}
|
23
|
}
|
24
|
}
|
25
|
|
26
|
function option_definition() {
|
27
|
$options = parent::option_definition();
|
28
|
$options['vocabularies'] = array('default' => array());
|
29
|
$options['type'] = array('default' => 'tid');
|
30
|
$options['transform'] = array('default' => FALSE, 'bool' => TRUE);
|
31
|
|
32
|
return $options;
|
33
|
}
|
34
|
|
35
|
function options_form(&$form, &$form_state) {
|
36
|
$vocabularies = taxonomy_get_vocabularies();
|
37
|
$options = array();
|
38
|
foreach ($vocabularies as $voc) {
|
39
|
$options[$voc->machine_name] = check_plain($voc->name);
|
40
|
}
|
41
|
|
42
|
$form['vocabularies'] = array(
|
43
|
'#type' => 'checkboxes',
|
44
|
'#prefix' => '<div id="edit-options-validate-argument-vocabulary-wrapper">',
|
45
|
'#suffix' => '</div>',
|
46
|
'#title' => t('Vocabularies'),
|
47
|
'#options' => $options,
|
48
|
'#default_value' => $this->options['vocabularies'],
|
49
|
'#description' => t('If you wish to validate for specific vocabularies, check them; if none are checked, all terms will pass.'),
|
50
|
);
|
51
|
|
52
|
$form['type'] = array(
|
53
|
'#type' => 'select',
|
54
|
'#title' => t('Filter value type'),
|
55
|
'#options' => array(
|
56
|
'tid' => t('Term ID'),
|
57
|
'tids' => t('Term IDs separated by , or +'),
|
58
|
'name' => t('Term name'),
|
59
|
'convert' => t('Term name converted to Term ID'),
|
60
|
),
|
61
|
'#default_value' => $this->options['type'],
|
62
|
'#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.'),
|
63
|
);
|
64
|
|
65
|
$form['transform'] = array(
|
66
|
'#type' => 'checkbox',
|
67
|
'#title' => t('Transform dashes in URL to spaces in term name filter values'),
|
68
|
'#default_value' => $this->options['transform'],
|
69
|
);
|
70
|
}
|
71
|
|
72
|
function options_submit(&$form, &$form_state, &$options = array()) {
|
73
|
// Filter unselected items so we don't unnecessarily store giant arrays.
|
74
|
$options['vocabularies'] = array_filter($options['vocabularies']);
|
75
|
}
|
76
|
|
77
|
function convert_options(&$options) {
|
78
|
if (!isset($options['vocabularies']) && !empty($this->argument->options['validate_argument_vocabulary'])) {
|
79
|
$options['vocabularies'] = $this->argument->options['validate_argument_vocabulary'];
|
80
|
$options['type'] = $this->argument->options['validate_argument_type'];
|
81
|
$options['transform'] = isset($this->argument->options['validate_argument_transform']) ? $this->argument->options['validate_argument_transform'] : FALSE;
|
82
|
}
|
83
|
}
|
84
|
|
85
|
function validate_argument($argument) {
|
86
|
$vocabularies = array_filter($this->options['vocabularies']);
|
87
|
$type = $this->options['type'];
|
88
|
$transform = $this->options['transform'];
|
89
|
|
90
|
switch ($type) {
|
91
|
case 'tid':
|
92
|
if (!is_numeric($argument)) {
|
93
|
return FALSE;
|
94
|
}
|
95
|
|
96
|
$query = db_select('taxonomy_term_data', 'td');
|
97
|
$query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
|
98
|
$query->fields('td');
|
99
|
$query->condition('td.tid', $argument);
|
100
|
$query->addTag('term_access');
|
101
|
$term = $query->execute()->fetchObject();
|
102
|
if (!$term) {
|
103
|
return FALSE;
|
104
|
}
|
105
|
$term = taxonomy_term_load($term->tid);
|
106
|
$this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term));
|
107
|
return empty($vocabularies) || !empty($vocabularies[$term->vocabulary_machine_name]);
|
108
|
|
109
|
case 'tids':
|
110
|
// An empty argument is not a term so doesn't pass.
|
111
|
if (empty($argument)) {
|
112
|
return FALSE;
|
113
|
}
|
114
|
|
115
|
$tids = new stdClass();
|
116
|
$tids->value = $argument;
|
117
|
$tids = views_break_phrase($argument, $tids);
|
118
|
if ($tids->value == array(-1)) {
|
119
|
return FALSE;
|
120
|
}
|
121
|
|
122
|
$test = drupal_map_assoc($tids->value);
|
123
|
$titles = array();
|
124
|
|
125
|
// check, if some tids already verified
|
126
|
static $validated_cache = array();
|
127
|
foreach ($test as $tid) {
|
128
|
if (isset($validated_cache[$tid])) {
|
129
|
if ($validated_cache[$tid] === FALSE) {
|
130
|
return FALSE;
|
131
|
}
|
132
|
else {
|
133
|
$titles[] = $validated_cache[$tid];
|
134
|
unset($test[$tid]);
|
135
|
}
|
136
|
}
|
137
|
}
|
138
|
|
139
|
// if unverified tids left - verify them and cache results
|
140
|
if (count($test)) {
|
141
|
$query = db_select('taxonomy_term_data', 'td');
|
142
|
$query->addTag('term_access');
|
143
|
$query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
|
144
|
$query->fields('td');
|
145
|
$query->fields('tv', array('machine_name'));
|
146
|
$query->condition('td.tid', $test);
|
147
|
|
148
|
$result = $query->execute();
|
149
|
|
150
|
foreach ($result as $term) {
|
151
|
if ($vocabularies && empty($vocabularies[$term->machine_name])) {
|
152
|
$validated_cache[$term->tid] = FALSE;
|
153
|
return FALSE;
|
154
|
}
|
155
|
$term = taxonomy_term_load($term->tid);
|
156
|
$titles[] = $validated_cache[$term->tid] = check_plain(entity_label('taxonomy_term', $term));
|
157
|
unset($test[$term->tid]);
|
158
|
}
|
159
|
}
|
160
|
|
161
|
// Remove duplicate titles
|
162
|
$titles = array_unique($titles);
|
163
|
|
164
|
$this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);
|
165
|
// If this is not empty, we did not find a tid.
|
166
|
return empty($test);
|
167
|
|
168
|
case 'name':
|
169
|
case 'convert':
|
170
|
$query = db_select('taxonomy_term_data', 'td');
|
171
|
$query->addTag('term_access');
|
172
|
$query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
|
173
|
$query->fields('td');
|
174
|
$query->fields('tv', array('machine_name'));
|
175
|
if (!empty($vocabularies)) {
|
176
|
$query->condition('tv.machine_name', $vocabularies);
|
177
|
}
|
178
|
if ($transform) {
|
179
|
$query->where("replace(td.name, ' ', '-') = :name", array(':name' => $argument));
|
180
|
}
|
181
|
else {
|
182
|
$query->condition('td.name', $argument);
|
183
|
}
|
184
|
$term = $query->execute()->fetchObject();
|
185
|
|
186
|
if ($term && (empty($vocabularies) || !empty($vocabularies[$term->machine_name]))) {
|
187
|
if ($type == 'convert') {
|
188
|
$this->argument->argument = $term->tid;
|
189
|
}
|
190
|
$term = taxonomy_term_load($term->tid);
|
191
|
$this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term));
|
192
|
return TRUE;
|
193
|
}
|
194
|
return FALSE;
|
195
|
}
|
196
|
}
|
197
|
|
198
|
function process_summary_arguments(&$args) {
|
199
|
$type = $this->options['type'];
|
200
|
$transform = $this->options['transform'];
|
201
|
$vocabularies = array_filter($this->options['vocabularies']);
|
202
|
|
203
|
if ($type == 'convert') {
|
204
|
$arg_keys = array_flip($args);
|
205
|
|
206
|
$query = db_select('taxonomy_term_data', 'td');
|
207
|
$query->addTag('term_access');
|
208
|
$query->condition('tid', $args);
|
209
|
$query->addField('td', 'tid', 'tid');
|
210
|
if (!empty($vocabularies)) {
|
211
|
$query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
|
212
|
$query->condition('tv.machine_name', $vocabularies);
|
213
|
}
|
214
|
if ($transform) {
|
215
|
$query->addExpression("REPLACE(td.name, ' ', '-')", 'name');
|
216
|
}
|
217
|
else {
|
218
|
$query->addField('td', 'name', 'name');
|
219
|
}
|
220
|
|
221
|
foreach ($query->execute()->fetchAllKeyed() as $tid => $term) {
|
222
|
$args[$arg_keys[$tid]] = $term;
|
223
|
}
|
224
|
}
|
225
|
}
|
226
|
}
|