6 class views_handler_filter_term_node_tid
extends views_handler_filter_many_to_one
{
7 function can_group() { return FALSE
; }
9 function has_extra_options() { return TRUE
; }
11 function get_value_options() { /* don't overwrite the value options */ }
13 function option_definition() {
14 $options = parent
::option_definition();
16 $options['type'] = array('default' => 'textfield');
17 $options['limit'] = array('default' => TRUE
);
18 $options['vid'] = array('default' => 0);
19 $options['hierarchy'] = array('default' => 0);
20 $options['error_message'] = array('default' => TRUE
);
25 function extra_options_form(&$form, &$form_state) {
26 $vocabularies = taxonomy_get_vocabularies();
27 foreach ($vocabularies as
$voc) {
28 $options[$voc->vid
] = check_plain($voc->name
);
31 if ($this->options
['limit']) {
32 // We only do this when the form is displayed.
33 if ($this->options
['vid'] == 0) {
34 $first_vocabulary = reset($vocabularies);
35 $this->options
['vid'] = $first_vocabulary->vid
;
39 '#prefix' => '<div class="views-left-40">',
40 '#suffix' => '</div>',
42 '#title' => t('Vocabulary'),
43 '#options' => $options,
44 '#description' => t('Select which vocabulary to show terms for in the regular options.'),
45 '#default_value' => $this->options
['vid'],
49 $form['markup_start'] = array(
50 '#value' => '<div class="views-left-40">',
53 $form['type'] = array(
55 '#title' => t('Selection type'),
56 '#options' => array('select' => t('Dropdown'), 'textfield' => t('Autocomplete')),
57 '#default_value' => $this->options
['type'],
60 $form['hierarchy'] = array(
61 '#type' => 'checkbox',
62 '#title' => t('Show hierarchy in dropdown'),
63 '#default_value' => !empty($this->options
['hierarchy']),
64 '#process' => array('views_process_dependency'),
65 '#dependency' => array('radio:options[type]' => array('select')),
68 $form['markup_end'] = array(
73 function value_form(&$form, &$form_state) {
74 $vocabulary = taxonomy_vocabulary_load($this->options
['vid']);
75 if (empty($vocabulary) && $this->options
['limit']) {
76 $form['markup'] = array(
77 '#prefix' => '<div class="form-item">',
78 '#suffix' => '</div>',
79 '#value' => t('An invalid vocabulary is selected. Please change it in the options.'),
84 if ($this->options
['type'] == 'textfield') {
87 $result = db_query("SELECT * FROM {term_data} td WHERE td.tid IN (" .
implode(', ', $this->value
) .
')');
88 while ($term = db_fetch_object($result)) {
92 $default .
= $term->name
;
96 $form['value'] = array(
97 '#title' => $this->options
['limit'] ?
t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->name
)) : t('Select terms'),
98 '#type' => 'textfield',
99 '#default_value' => $default,
102 if ($this->options
['limit']) {
103 $form['value']['#autocomplete_path'] = 'taxonomy/autocomplete/' .
$vocabulary->vid
;
107 if (!empty($this->options
['hierarchy']) && $this->options
['limit']) {
108 $tree = taxonomy_get_tree($vocabulary->vid
);
112 foreach ($tree as
$term) {
113 $choice = new
stdClass();
114 $choice->option
= array($term->tid
=> str_repeat('-', $term->depth
) .
$term->name
);
115 $options[] = $choice;
121 if ($this->options
['limit']) {
122 $result = db_query(db_rewrite_sql("SELECT t.* FROM {term_data} t WHERE t.vid = %d ORDER BY t.weight, t.name", 't', 'tid'), $vocabulary->vid
);
125 $result = db_query(db_rewrite_sql("SELECT td.* FROM {term_data} td INNER JOIN {vocabulary} v ON td.vid = v.vid ORDER BY v.weight, v.name, td.weight, td.name", 'td', 'tid'));
127 while ($term = db_fetch_object($result)) {
128 $options[$term->tid
] = $term->name
;
132 $default_value = (array) $this->value
;
134 if (!empty($form_state['exposed'])) {
135 $identifier = $this->options
['expose']['identifier'];
137 if (!empty($this->options
['expose']['reduce'])) {
138 $options = $this->reduce_value_options($options);
140 if (empty($this->options
['expose']['single']) && !empty($this->options
['expose']['optional'])) {
141 $default_value = array();
145 if (!empty($this->options
['expose']['single'])) {
146 if (!empty($this->options
['expose']['optional']) && (empty($default_value) || !empty($this->options
['expose']['reduce']))) {
147 $default_value = 'All';
149 else if (empty($default_value)) {
150 $keys = array_keys($options);
151 $default_value = array_shift($keys);
154 $copy = $default_value;
155 $default_value = array_shift($copy);
159 $form['value'] = array(
161 '#title' => $this->options
['limit'] ?
t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->name
)) : t('Select terms'),
163 '#options' => $options,
164 '#size' => min(9, count($options)),
165 '#default_value' => $default_value,
168 if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
169 $form_state['input'][$identifier] = $default_value;
174 if (empty($form_state['exposed'])) {
175 // Retain the helper option
176 $this->helper
->options_form($form, $form_state);
180 function value_validate($form, &$form_state) {
181 // We only validate if they've chosen the text field style.
182 if ($this->options
['type'] != 'textfield') {
186 $values = drupal_explode_tags($form_state['values']['options']['value']);
187 $tids = $this->validate_term_strings($form['value'], $values);
190 $form_state['values']['options']['value'] = $tids;
194 function accept_exposed_input($input) {
195 if (empty($this->options
['exposed'])) {
199 // If view is an attachment and is inheriting exposed filters, then assume
200 // exposed input has already been validated
201 if (isset($this->view
->is_attachment
) && $this->view
->display_handler
->uses_exposed()) {
202 $this->validated_exposed_input
= (array)$this->view
->exposed_raw_input
[$this->options
['expose']['identifier']];
205 // If it's optional and there's no value don't bother filtering.
206 if ($this->options
['expose']['optional'] && empty($this->validated_exposed_input
)) {
210 $rc = parent
::accept_exposed_input($input);
212 // If we have previously validated input, override.
213 if (isset($this->validated_exposed_input
)) {
214 $this->value
= $this->validated_exposed_input
;
221 function exposed_validate(&$form, &$form_state) {
222 if (empty($this->options
['exposed'])) {
226 $identifier = $this->options
['expose']['identifier'];
228 // We only validate if they've chosen the text field style.
229 if ($this->options
['type'] != 'textfield') {
230 if ($form_state['values'][$identifier] != 'All') {
231 $this->validated_exposed_input
= (array) $form_state['values'][$identifier];
236 if (empty($this->options
['expose']['identifier'])) {
240 $values = drupal_explode_tags($form_state['values'][$identifier]);
242 $tids = $this->validate_term_strings($form[$identifier], $values);
244 $this->validated_exposed_input
= $tids;
249 * Validate the user string. Since this can come from either the form
250 * or the exposed filter, this is abstracted out a bit so it can
251 * handle the multiple input sources.
253 function validate_term_strings(&$form, $values) {
254 if (empty($values)) {
259 $placeholders = array();
262 foreach ($values as
$value) {
263 $missing[strtolower($value)] = TRUE
;
265 $placeholders[] = "'%s'";
272 // add the taxonomy vid to the argument list.
273 $args[] = $this->options
['vid'];
275 $result = db_query("SELECT * FROM {term_data} td WHERE td.name IN (" .
implode(', ', $placeholders) .
") AND td.vid = %d", $args);
276 while ($term = db_fetch_object($result)) {
277 unset($missing[strtolower($term->name
)]);
278 $tids[] = $term->tid
;
281 if ($missing && !empty($this->options
['error_message'])) {
282 form_error($form, format_plural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array('@terms' => implode(', ', array_keys($missing)))));
284 elseif ($missing && empty($this->options
['error_message'])) {
291 function value_submit($form, &$form_state) {
292 // prevent array_filter from messing up our arrays in parent submit.
295 function expose_form_right(&$form, &$form_state) {
296 parent
::expose_form_right($form, $form_state);
297 if ($this->options
['type'] != 'select') {
298 unset($form['expose']['reduce']);
300 $form['error_message'] = array(
301 '#type' => 'checkbox',
302 '#title' => t('Display error message'),
303 '#default_value' => !empty($this->options
['error_message']),
307 function admin_summary() {
308 // set up $this->value_options for the parent summary
309 $this->value_options
= array();
312 $result = db_query("SELECT * FROM {term_data} td WHERE td.tid IN (" .
implode(', ', $this->value
) .
")");
314 while ($term = db_fetch_object($result)) {
315 $this->value_options
[$term->tid
] = $term->name
;
318 return parent
::admin_summary();