00001 <?php
00010 class views_plugin_argument_validate_taxonomy_term extends views_plugin_argument_validate {
00011 function init(&$view, &$argument, $options) {
00012 parent::init($view, $argument, $options);
00013
00014
00015 if (!empty($this->options['vids'])) {
00016 $vocabularies = taxonomy_get_vocabularies();
00017 foreach ($this->options['vids'] as $vid) {
00018 if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
00019 $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
00020 }
00021 }
00022 }
00023 }
00024
00025 function option_definition() {
00026 $options = parent::option_definition();
00027 $options['vocabularies'] = array('default' => array());
00028 $options['type'] = array('default' => 'tid');
00029 $options['transform'] = array('default' => FALSE);
00030
00031 return $options;
00032 }
00033
00034 function options_form(&$form, &$form_state) {
00035 $vocabularies = taxonomy_get_vocabularies();
00036 $options = array();
00037 foreach ($vocabularies as $voc) {
00038 $options[$voc->machine_name] = check_plain($voc->name);
00039 }
00040
00041 $form['vocabularies'] = array(
00042 '#type' => 'checkboxes',
00043 '#prefix' => '<div id="edit-options-validate-argument-vocabulary-wrapper">',
00044 '#suffix' => '</div>',
00045 '#title' => t('Vocabularies'),
00046 '#options' => $options,
00047 '#default_value' => $this->options['vocabularies'],
00048 '#description' => t('If you wish to validate for specific vocabularies, check them; if none are checked, all terms will pass.'),
00049 );
00050
00051 $form['type'] = array(
00052 '#type' => 'select',
00053 '#title' => t('Filter value type'),
00054 '#options' => array(
00055 'tid' => t('Term ID'),
00056 'tids' => t('Term IDs separated by , or +'),
00057 'name' => t('Term name'),
00058 'convert' => t('Term name converted to Term ID'),
00059 ),
00060 '#default_value' => $this->options['type'],
00061 '#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.'),
00062 );
00063
00064 $form['transform'] = array(
00065 '#type' => 'checkbox',
00066 '#title' => t('Transform dashes in URL to spaces in term name filter values'),
00067 '#default_value' => $this->options['transform'],
00068 );
00069 }
00070
00071 function options_submit(&$form, &$form_state, &$options = array()) {
00072
00073 $options['vocabularies'] = array_filter($options['vocabularies']);
00074 }
00075
00076 function convert_options(&$options) {
00077 if (!isset($options['vocabularies']) && !empty($this->argument->options['validate_argument_vocabulary'])) {
00078 $options['vocabularies'] = $this->argument->options['validate_argument_vocabulary'];
00079 $options['type'] = $this->argument->options['validate_argument_type'];
00080 $options['transform'] = isset($this->argument->options['validate_argument_transform']) ? $this->argument->options['validate_argument_transform'] : FALSE;
00081 }
00082 }
00083
00084 function validate_argument($argument) {
00085 $vocabularies = array_filter($this->options['vocabularies']);
00086 $type = $this->options['type'];
00087 $transform = $this->options['transform'];
00088
00089 switch ($type) {
00090 case 'tid':
00091 if (!is_numeric($argument)) {
00092 return FALSE;
00093 }
00094
00095 $query = db_select('taxonomy_term_data', 'td');
00096 $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
00097 $query->fields('td');
00098 $query->fields('tv', array('machine_name'));
00099 $query->condition('td.tid', $argument);
00100 $query->addTag('term_access');
00101 $term = $query->execute()->fetchObject();
00102 if (!$term) {
00103 return FALSE;
00104 }
00105 $this->argument->validated_title = check_plain($term->name);
00106 return empty($vocabularies) || !empty($vocabularies[$term->machine_name]);
00107
00108 case 'tids':
00109
00110 if (empty($argument)) {
00111 return FALSE;
00112 }
00113
00114 $tids = new stdClass();
00115 $tids->value = $argument;
00116 $tids = views_break_phrase($argument, $tids);
00117 if ($tids->value == array(-1)) {
00118 return FALSE;
00119 }
00120
00121 $test = drupal_map_assoc($tids->value);
00122 $titles = array();
00123
00124
00125 static $validated_cache = array();
00126 foreach ($test as $tid) {
00127 if (isset($validated_cache[$tid])) {
00128 if ($validated_cache[$tid] === FALSE) {
00129 return FALSE;
00130 }
00131 else {
00132 $titles[] = $validated_cache[$tid];
00133 unset($test[$tid]);
00134 }
00135 }
00136 }
00137
00138
00139 if (count($test)) {
00140 $query = db_select('taxonomy_term_data', 'td');
00141 $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
00142 $query->fields('td');
00143 $query->fields('tv', array('machine_name'));
00144 $query->condition('td.tid', $test);
00145
00146 $result = $query->execute();
00147
00148 foreach ($result as $term) {
00149 if ($vocabularies && empty($vocabularies[$term->machine_name])) {
00150 $validated_cache[$term->tid] = FALSE;
00151 return FALSE;
00152 }
00153
00154 $titles[] = $validated_cache[$term->tid] = check_plain($term->name);
00155 unset($test[$term->tid]);
00156 }
00157 }
00158
00159
00160 $titles = array_unique($titles);
00161
00162 $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);
00163
00164 return empty($test);
00165
00166 case 'name':
00167 case 'convert':
00168 $query = db_select('taxonomy_term_data', 'td');
00169 $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
00170 $query->fields('td');
00171 $query->fields('tv', array('machine_name'));
00172 if (!empty($vocabularies)) {
00173 $query->condition('tv.machine_name', $vocabularies);
00174 }
00175 if ($transform) {
00176 $query->where("replace(td.name, ' ', '-') = :name", array(':name' => $argument));
00177 }
00178 else {
00179 $query->condition('td.name', $argument);
00180 }
00181 $term = $query->execute()->fetchObject();
00182
00183 if ($term && (empty($vocabularies) || !empty($vocabularies[$term->machine_name]))) {
00184 if ($type == 'convert') {
00185 $this->argument->argument = $term->tid;
00186 }
00187 $this->argument->validated_title = check_plain($term->name);
00188 return TRUE;
00189 }
00190 return FALSE;
00191 }
00192 }
00193
00194 function process_summary_arguments(&$args) {
00195 $type = $this->options['type'];
00196 $transform = $this->options['transform'];
00197 $vocabularies = array_filter($this->options['vocabularies']);
00198
00199 if ($type == 'convert') {
00200 $arg_keys = array_flip($args);
00201
00202 $query = db_select('taxonomy_term_data', 'td');
00203 $query->condition('tid', $args);
00204 $query->addField('td', 'tid', 'tid');
00205 if (!empty($vocabularies)) {
00206 $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
00207 $query->condition('tv.machine_name', $vocabularies);
00208 }
00209 if ($transform) {
00210 $query->addExpression("REPLACE(td.name, ' ', '-')", 'name');
00211 }
00212 else {
00213 $query->addField('td', 'name', 'name');
00214 }
00215
00216 foreach ($query->execute()->fetchAllKeyed() as $tid => $term) {
00217 $args[$arg_keys[$tid]] = $term;
00218 }
00219 }
00220 }
00221 }