00001 <?php
00007 class views_plugin_argument_default_taxonomy_tid extends views_plugin_argument_default {
00008 function init(&$view, &$argument, $options) {
00009 parent::init($view, $argument, $options);
00010
00011
00012 if (!empty($this->options['vids'])) {
00013 $vocabularies = taxonomy_get_vocabularies();
00014 foreach ($this->options['vids'] as $vid) {
00015 if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
00016 $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
00017 }
00018 }
00019 }
00020 }
00021
00022 function option_definition() {
00023 $options = parent::option_definition();
00024
00025 $options['term_page'] = array('default' => TRUE);
00026 $options['node'] = array('default' => FALSE);
00027 $options['anyall'] = array('default' => ',');
00028 $options['limit'] = array('default' => FALSE);
00029 $options['vocabularies'] = array('default' => array());
00030
00031 return $options;
00032 }
00033
00034 function options_form(&$form, &$form_state) {
00035 $form['term_page'] = array(
00036 '#type' => 'checkbox',
00037 '#title' => t('Load default filter from term page'),
00038 '#default_value' => $this->options['term_page'],
00039 );
00040 $form['node'] = array(
00041 '#type' => 'checkbox',
00042 '#title' => t('Load default filter from node page, that\'s good for related taxonomy blocks'),
00043 '#default_value' => $this->options['node'],
00044 );
00045
00046 $form['limit'] = array(
00047 '#type' => 'checkbox',
00048 '#title' => t('Limit terms by vocabulary'),
00049 '#default_value'=> $this->options['limit'],
00050 '#process' => array('form_process_checkbox', 'ctools_dependent_process'),
00051 '#dependency' => array(
00052 'edit-options-argument-default-taxonomy-tid-node' => array(1),
00053 ),
00054 );
00055
00056 $options = array();
00057 $vocabularies = taxonomy_get_vocabularies();
00058 foreach ($vocabularies as $voc) {
00059 $options[$voc->machine_name] = check_plain($voc->name);
00060 }
00061
00062 $form['vocabularies'] = array(
00063 '#prefix' => '<div><div id="edit-options-vids">',
00064 '#suffix' => '</div></div>',
00065 '#type' => 'checkboxes',
00066 '#title' => t('Vocabularies'),
00067 '#options' => $options,
00068 '#default_value' => $this->options['vocabularies'],
00069 '#process' => array('form_process_checkboxes', 'ctools_dependent_process'),
00070 '#dependency' => array(
00071 'edit-options-argument-default-taxonomy-tid-limit' => array(1),
00072 'edit-options-argument-default-taxonomy-tid-node' => array(1),
00073 ),
00074 );
00075
00076 $form['anyall'] = array(
00077 '#type' => 'radios',
00078 '#title' => t('Multiple-value handling'),
00079 '#default_value'=> $this->options['anyall'],
00080 '#process' => array('form_process_radios', 'ctools_dependent_process'),
00081 '#options' => array(
00082 ',' => t('Filter to items that share all terms'),
00083 '+' => t('Filter to items that share any term'),
00084 ),
00085 '#dependency' => array(
00086 'edit-options-argument-default-taxonomy-tid-node' => array(1),
00087 ),
00088 );
00089 }
00090
00091 function options_submit(&$form, &$form_state, &$options = array()) {
00092
00093 $options['vocabularies'] = array_filter($options['vocabularies']);
00094 }
00095
00096 function get_argument() {
00097
00098 if (!empty($this->options['term_page'])) {
00099 if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
00100 return arg(2);
00101 }
00102 }
00103
00104 if (!empty($this->options['node'])) {
00105 foreach (range(1, 3) as $i) {
00106 $node = menu_get_object('node', $i);
00107 if (!empty($node)) {
00108 break;
00109 }
00110 }
00111
00112 if ($node) {
00113 $taxonomy = array();
00114 $fields = field_info_instances('node', $node->type);
00115 foreach ($fields as $name => $info) {
00116 $field_info = field_info_field($name);
00117 if ($field_info['type'] == 'taxonomy_term_reference') {
00118 $items = field_get_items('node', $node, $name);
00119 if (is_array($items)) {
00120 foreach ($items as $item) {
00121 $taxonomy[$item['tid']] = $field_info['settings']['allowed_values'][0]['vocabulary'];
00122 }
00123 }
00124 }
00125 }
00126 if (!empty($this->options['limit'])) {
00127 $tids = array();
00128
00129 foreach ($taxonomy as $tid => $vocab) {
00130 if (!empty($this->options['vocabularies'][$vocab])) {
00131 $tids[] = $tid;
00132 }
00133 }
00134 return implode($this->options['anyall'], $tids);
00135 }
00136
00137 else {
00138 return implode($this->options['anyall'], array_keys($taxonomy));
00139 }
00140 }
00141 }
00142
00143
00144
00145 $views_page = views_get_page_view();
00146 if ($views_page && isset($views_page->argument['tid'])) {
00147 return $views_page->argument['tid']->argument;
00148 }
00149 }
00150 }