Go to the documentation of this file.00001 <?php
00013 class views_handler_relationship_node_term_data extends views_handler_relationship {
00014 function init(&$view, &$options) {
00015 parent::init($view, $options);
00016
00017
00018 if (!empty($this->options['vids'])) {
00019 $vocabularies = taxonomy_get_vocabularies();
00020 foreach ($this->options['vids'] as $vid) {
00021 if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
00022 $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
00023 }
00024 }
00025 }
00026 }
00027
00028 function option_definition() {
00029 $options = parent::option_definition();
00030 $options['vocabularies'] = array('default' => array());
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 '#title' => t('Vocabularies'),
00044 '#options' => $options,
00045 '#default_value' => $this->options['vocabularies'],
00046 '#description' => t('Choose which vocabularies you wish to relate. Remember that every term found will create a new record, so this relationship is best used on just one vocabulary that has only one term per node.'),
00047 );
00048 parent::options_form($form, $form_state);
00049 }
00050
00054 function query() {
00055 $this->ensure_my_table();
00056
00057 $def = $this->definition;
00058 $def['table'] = 'taxonomy_term_data';
00059
00060 if (!array_filter($this->options['vocabularies'])) {
00061 $term_node = $this->query->add_table('taxonomy_index', $this->relationship);
00062 $def['left_table'] = 'taxonomy_index';
00063 $def['left_field'] = 'tid';
00064 $def['field'] = 'tid';
00065 $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
00066 }
00067 else {
00068
00069 $def['left_table'] = $this->table_alias;
00070 $def['left_field'] = 'nid';
00071 $def['field'] = 'nid';
00072 $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
00073
00074 $query = db_select('taxonomy_term_data', 'td');
00075 $query->addJoin($def['type'], 'taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
00076 $query->addJoin($def['type'], 'taxonomy_index', 'tn', 'tn.tid = td.tid');
00077 $query->condition('tv.machine_name', array_filter($this->options['vocabularies']));
00078 $query->addTag('term_access');
00079 $query->fields('td');
00080 $query->fields('tn', array('nid'));
00081 $def['table formula'] = $query;
00082 }
00083
00084 $join = new views_join();
00085
00086 $join->definition = $def;
00087 $join->construct();
00088 $join->adjusted = TRUE;
00089
00090
00091 $alias = $def['table'] . '_' . $this->table;
00092
00093 $this->alias = $this->query->add_relationship($alias, $join, 'taxonomy_term_data', $this->relationship);
00094 }
00095 }