00001 <?php
00002
00003
00007 class views_handler_filter_term_node_tid extends views_handler_filter_many_to_one {
00008 function has_extra_options() { return TRUE; }
00009
00010 function get_value_options() { }
00011
00012 function option_definition() {
00013 $options = parent::option_definition();
00014
00015 $options['type'] = array('default' => 'textfield');
00016 $options['limit'] = array('default' => TRUE);
00017 $options['vid'] = array('default' => 0);
00018
00019 return $options;
00020 }
00021
00022 function extra_options_form(&$form, &$form_state) {
00023 $vocabularies = taxonomy_get_vocabularies();
00024 foreach ($vocabularies as $voc) {
00025 $options[$voc->vid] = check_plain($voc->name);
00026 }
00027
00028 if ($this->options['limit']) {
00029
00030
00031 if ($this->options['vid'] == 0) {
00032 $this->options['vid'] = db_result(db_query('SELECT MIN(vid) FROM {vocabulary} v'));
00033 }
00034
00035 $form['vid'] = array(
00036 '#prefix' => '<div class="views-left-40">',
00037 '#suffix' => '</div>',
00038 '#type' => 'radios',
00039 '#title' => t('Vocabulary'),
00040 '#options' => $options,
00041 '#description' => t('Select which vocabulary to show terms for in the regular options.'),
00042 '#default_value' => $this->options['vid'],
00043 );
00044 }
00045
00046 $form['markup_start'] = array(
00047 '#value' => '<div class="views-left-40">',
00048 );
00049
00050 $form['type'] = array(
00051 '#type' => 'radios',
00052 '#title' => t('Selection type'),
00053 '#options' => array('select' => t('Dropdown'), 'textfield' => t('Autocomplete')),
00054 '#default_value' => $this->options['type'],
00055 );
00056
00057 $form['hierarchy'] = array(
00058 '#type' => 'checkbox',
00059 '#title' => t('Show hierarchy in dropdown'),
00060 '#default_value' => !empty($this->options['hierarchy']),
00061 '#process' => array('views_process_dependency'),
00062 '#dependency' => array('radio:options[type]' => array('select')),
00063 );
00064
00065 $form['markup_end'] = array(
00066 '#value' => '</div>',
00067 );
00068 }
00069
00070 function value_form(&$form, &$form_state) {
00071 $vocabulary = taxonomy_vocabulary_load($this->options['vid']);
00072 if (empty($vocabulary) && $this->options['limit']) {
00073 $form['markup'] = array(
00074 '#prefix' => '<div class="form-item">',
00075 '#suffix' => '</div>',
00076 '#value' => t('An invalid vocabulary is selected. Please change it in the options.'),
00077 );
00078 return;
00079 }
00080
00081 if ($this->options['type'] == 'textfield') {
00082 $default = '';
00083 if ($this->value) {
00084 $result = db_query("SELECT * FROM {term_data} td WHERE td.tid IN (" . implode(', ', $this->value) . ')');
00085 while ($term = db_fetch_object($result)) {
00086 if ($default) {
00087 $default .= ', ';
00088 }
00089 $default .= $term->name;
00090 }
00091 }
00092
00093 $form['value'] = array(
00094 '#title' => $this->options['limit'] ? t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->name)) : t('Select terms'),
00095 '#type' => 'textfield',
00096 '#default_value' => $default,
00097 );
00098
00099 if ($this->options['limit']) {
00100 $form['value']['#autocomplete_path'] = 'taxonomy/autocomplete/' . $vocabulary->vid;
00101 }
00102 }
00103 else {
00104 if (!empty($this->options['hierarchy']) && $this->options['limit']) {
00105 $tree = taxonomy_get_tree($vocabulary->vid);
00106 $options = array();
00107
00108 if ($tree) {
00109 foreach ($tree as $term) {
00110 $choice = new stdClass();
00111 $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name);
00112 $options[] = $choice;
00113 }
00114 }
00115 }
00116 else {
00117 $options = array();
00118 if ($this->options['limit']) {
00119 $result = db_query("SELECT * FROM {term_data} WHERE vid = %d ORDER BY weight, name", $vocabulary->vid);
00120 }
00121 else {
00122 $result = db_query("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");
00123 }
00124 while ($term = db_fetch_object($result)) {
00125 $options[$term->tid] = $term->name;
00126 }
00127 }
00128
00129 $default_value = (array) $this->value;
00130
00131 if (!empty($form_state['exposed'])) {
00132 $identifier = $this->options['expose']['identifier'];
00133
00134 if (!empty($this->options['expose']['reduce'])) {
00135 $options = $this->reduce_value_options($options);
00136
00137 if (empty($this->options['expose']['single']) && !empty($this->options['expose']['optional'])) {
00138 $default_value = array();
00139 }
00140 }
00141
00142 if (!empty($this->options['expose']['single'])) {
00143 if (!empty($this->options['expose']['optional']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
00144 $default_value = 'All';
00145 }
00146 else if (empty($default_value)) {
00147 $keys = array_keys($options);
00148 $default_value = array_shift($keys);
00149 }
00150 else {
00151 $copy = $default_value;
00152 $default_value = array_shift($copy);
00153 }
00154 }
00155 }
00156 $form['value'] = array(
00157 '#type' => 'select',
00158 '#title' => $this->options['limit'] ? t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->name)) : t('Select terms'),
00159 '#multiple' => TRUE,
00160 '#options' => $options,
00161 '#size' => min(9, count($options)),
00162 '#default_value' => $default_value,
00163 );
00164
00165 if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
00166 $form_state['input'][$identifier] = $default_value;
00167 }
00168 }
00169
00170
00171 if (empty($form_state['exposed'])) {
00172
00173 $this->helper->options_form($form, $form_state);
00174 }
00175 }
00176
00177 function value_validate(&$form, &$form_state) {
00178
00179 if ($this->options['type'] != 'textfield') {
00180 return;
00181 }
00182
00183 $values = drupal_explode_tags($form_state['values']['options']['value']);
00184 $tids = $this->validate_term_strings($form['value'], $values);
00185
00186 if ($tids) {
00187 $form_state['values']['options']['value'] = $tids;
00188 }
00189 }
00190
00191 function accept_exposed_input($input) {
00192 if (empty($this->options['exposed'])) {
00193 return TRUE;
00194 }
00195
00196
00197 if ($this->options['expose']['optional'] && empty($this->validated_exposed_input)) {
00198 return FALSE;
00199 }
00200
00201 $rc = parent::accept_exposed_input($input);
00202 if ($rc) {
00203
00204 if (isset($this->validated_exposed_input)) {
00205 $this->value = $this->validated_exposed_input;
00206 }
00207 }
00208
00209 return $rc;
00210 }
00211
00212 function exposed_validate(&$form, &$form_state) {
00213 if (empty($this->options['exposed'])) {
00214 return;
00215 }
00216
00217 $identifier = $this->options['expose']['identifier'];
00218
00219
00220 if ($this->options['type'] != 'textfield') {
00221 if ($form_state['values'][$identifier] != 'All') {
00222 $this->validated_exposed_input = (array) $form_state['values'][$identifier];
00223 }
00224 return;
00225 }
00226
00227 if (empty($this->options['expose']['identifier'])) {
00228 return;
00229 }
00230
00231 $values = drupal_explode_tags($form_state['values'][$identifier]);
00232
00233 $tids = $this->validate_term_strings($form[$identifier], $values);
00234 if ($tids) {
00235 $this->validated_exposed_input = $tids;
00236 }
00237 }
00238
00244 function validate_term_strings(&$form, $values) {
00245 if (empty($values)) {
00246 return array();
00247 }
00248
00249 $tids = array();
00250 $placeholders = array();
00251 $args = array();
00252 $results = array();
00253 foreach ($values as $value) {
00254 $missing[strtolower($value)] = TRUE;
00255 $args[] = $value;
00256 $placeholders[] = "'%s'";
00257 }
00258
00259 if (!$args) {
00260 return;
00261 }
00262
00263
00264 $args[] = $this->options['vid'];
00265
00266 $result = db_query("SELECT * FROM {term_data} WHERE name IN (" . implode(', ', $placeholders) . ") AND vid = %d", $args);
00267 while ($term = db_fetch_object($result)) {
00268 unset($missing[strtolower($term->name)]);
00269 $tids[] = $term->tid;
00270 }
00271
00272 if ($missing) {
00273 form_error($form, format_plural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array('@terms' => implode(', ', array_keys($missing)))));
00274 }
00275
00276 return $tids;
00277 }
00278
00279 function value_submit($form, &$form_state) {
00280
00281 }
00282
00283 function expose_form_right(&$form, &$form_state) {
00284 parent::expose_form_right($form, $form_state);
00285 if ($this->options['type'] != 'select') {
00286 unset($form['expose']['reduce']);
00287 }
00288 }
00289
00290 function admin_summary() {
00291
00292 $this->value_options = array();
00293
00294 if ($this->value) {
00295 $result = db_query("SELECT * FROM {term_data} td WHERE td.tid IN (" . implode(', ', $this->value) . ")");
00296
00297 while ($term = db_fetch_object($result)) {
00298 $this->value_options[$term->tid] = $term->name;
00299 }
00300 }
00301 return parent::admin_summary();
00302 }
00303 }