00001 <?php
00010 class views_handler_argument_term_node_tid_depth extends views_handler_argument {
00011 function option_definition() {
00012 $options = parent::option_definition();
00013
00014 $options['depth'] = array('default' => 0);
00015 $options['break_phrase'] = array('default' => FALSE);
00016 $options['set_breadcrumb'] = array('default' => FALSE);
00017 $options['use_taxonomy_term_path'] = array('default' => FALSE);
00018
00019 return $options;
00020 }
00021
00022 function options_form(&$form, &$form_state) {
00023 $form['depth'] = array(
00024 '#type' => 'weight',
00025 '#title' => t('Depth'),
00026 '#default_value' => $this->options['depth'],
00027 '#description' => t('The depth will match nodes tagged with terms in the hierarchy. For example, if you have the term "fruit" and a child term "apple", with a depth of 1 (or higher) then filtering for the term "fruit" will get nodes that are tagged with "apple" as well as "fruit". If negative, the reverse is true; searching for "apple" will also pick up nodes tagged with "fruit" if depth is -1 (or lower).'),
00028 );
00029
00030 $form['break_phrase'] = array(
00031 '#type' => 'checkbox',
00032 '#title' => t('Allow multiple values'),
00033 '#description' => t('If selected, users can enter multiple values in the form of 1+2+3. Due to the number of JOINs it would require, AND will be treated as OR with this filter.'),
00034 '#default_value' => !empty($this->options['break_phrase']),
00035 );
00036
00037 $form['set_breadcrumb'] = array(
00038 '#type' => 'checkbox',
00039 '#title' => t("Set the breadcrumb for the term parents"),
00040 '#description' => t('If selected, the breadcrumb trail will include all parent terms, each one linking to this view. Note that this only works if just one term was received.'),
00041 '#default_value' => !empty($this->options['set_breadcrumb']),
00042 );
00043
00044 $form['use_taxonomy_term_path'] = array(
00045 '#type' => 'checkbox',
00046 '#title' => t("Use Drupal's taxonomy term path to create breadcrumb links"),
00047 '#description' => t('If selected, the links in the breadcrumb trail will be created using the standard drupal method instead of the custom views method. This is useful if you are using modules like taxonomy redirect to modify your taxonomy term links.'),
00048 '#default_value' => !empty($this->options['use_taxonomy_term_path']),
00049 '#dependency' => array('edit-options-set-breadcrumb' => array(TRUE)),
00050 );
00051 parent::options_form($form, $form_state);
00052 }
00053
00054 function set_breadcrumb(&$breadcrumb) {
00055 if (empty($this->options['set_breadcrumb']) || !is_numeric($this->argument)) {
00056 return;
00057 }
00058
00059 return views_taxonomy_set_breadcrumb($breadcrumb, $this);
00060 }
00061
00065 function default_actions($which = NULL) {
00066 if ($which) {
00067 if (in_array($which, array('ignore', 'not found', 'empty', 'default'))) {
00068 return parent::default_actions($which);
00069 }
00070 return;
00071 }
00072 $actions = parent::default_actions();
00073 unset($actions['summary asc']);
00074 unset($actions['summary desc']);
00075 unset($actions['summary asc by count']);
00076 unset($actions['summary desc by count']);
00077 return $actions;
00078 }
00079
00080 function query($group_by = FALSE) {
00081 $this->ensure_my_table();
00082
00083 if (!empty($this->options['break_phrase'])) {
00084 $tids = new stdClass();
00085 $tids->value = $this->argument;
00086 $tids = views_break_phrase($this->argument, $tids);
00087 if ($tids->value == array(-1)) {
00088 return FALSE;
00089 }
00090
00091 if (count($tids->value) > 1) {
00092 $operator = 'IN';
00093 }
00094 else {
00095 $operator = '=';
00096 }
00097
00098 $tids = $tids->value;
00099 }
00100 else {
00101 $operator = "=";
00102 $tids = $this->argument;
00103 }
00104
00105 $subquery = db_select('taxonomy_index', 'tn');
00106 $subquery->addField('tn', 'nid');
00107 $where = db_or()->condition('tn.tid', $tids, $operator);
00108 $last = "tn";
00109
00110 if ($this->options['depth'] > 0) {
00111 $subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
00112 $last = "th";
00113 foreach (range(1, abs($this->options['depth'])) as $count) {
00114 $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
00115 $where->condition("th$count.tid", $tids, $operator);
00116 $last = "th$count";
00117 }
00118 }
00119 elseif ($this->options['depth'] < 0) {
00120 foreach (range(1, abs($this->options['depth'])) as $count) {
00121 $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
00122 $where->condition("th$count.tid", $tids, $operator);
00123 $last = "th$count";
00124 }
00125 }
00126
00127 $subquery->condition($where);
00128 $this->query->add_where(0, "$this->table_alias.$this->real_field", $subquery, 'IN');
00129 }
00130
00131 function title() {
00132 $term = taxonomy_term_load($this->argument);
00133 if (!empty($term)) {
00134 return check_plain($term->name);
00135 }
00136
00137 return t('No name');
00138 }
00139 }