You are here

function views_handler_filter_term_node_tid_depth::query in Views (for Drupal 7) 6.2

Same name and namespace in other branches
  1. 6.3 modules/taxonomy/views_handler_filter_term_node_tid_depth.inc \views_handler_filter_term_node_tid_depth::query()
  2. 7.3 modules/taxonomy/views_handler_filter_term_node_tid_depth.inc \views_handler_filter_term_node_tid_depth::query()

Add this filter to the query.

Due to the nature of fapi, the value and the operator have an unintended level of indirection. You will find them in $this->operator and $this->value respectively.

Overrides views_handler_filter_in_operator::query

File

modules/taxonomy/views_handler_filter_term_node_tid_depth.inc, line 34

Class

views_handler_filter_term_node_tid_depth
Filter handler for taxonomy terms with depth.

Code

function query() {

  // If no filter values are present, then do nothing.
  if (count($this->value) == 0) {
    return;
  }
  else {
    if (count($this->value) == 1) {
      $placeholder = " = %d";
    }
    else {
      $placeholder = " IN (" . implode(', ', array_fill(0, sizeof($this->value), '%d')) . ")";
    }
  }

  // The normal use of ensure_my_table() here breaks Views.
  // So instead we trick the filter into using the alias of the base table.
  // See http://drupal.org/node/271833
  // If a relationship is set, we must use the alias it provides.
  if (!empty($this->relationship)) {
    $this->table_alias = $this->relationship;
  }
  else {
    if (isset($this->query->table_queue[$this->query->base_table]['alias'])) {
      $this->table_alias = $this->query->table_queue[$this->query->base_table]['alias'];
    }
    else {
      return;
    }
  }

  // Now build the subqueries.
  $subquery = "\n  SELECT tn.vid FROM {term_node} tn\n";
  $where = "  WHERE tn.tid {$placeholder}\n";
  $args = $this->value;
  $last = "tn";
  if ($this->options['depth'] > 0) {
    $subquery .= "    LEFT JOIN {term_hierarchy} th ON th.tid = tn.tid\n";
    $last = "th";
    foreach (range(1, abs($this->options['depth'])) as $count) {
      $subquery .= "    LEFT JOIN {term_hierarchy} th{$count} ON {$last}.parent = th{$count}.tid\n";
      $where .= "    OR th{$count}.tid {$placeholder}\n";
      $args = array_merge($args, $this->value);
      $last = "th{$count}";
    }
  }
  else {
    if ($this->options['depth'] < 0) {
      foreach (range(1, abs($this->options['depth'])) as $count) {
        $subquery .= "    LEFT JOIN {term_hierarchy} th{$count} ON {$last}.tid = th{$count}.parent\n";
        $where .= "    OR th{$count}.tid {$placeholder}\n";
        $args = array_merge($args, $this->value);
        $last = "th{$count}";
      }
    }
  }
  $this->query
    ->add_where(0, "{$this->table_alias}.{$this->real_field} IN ({$subquery}{$where}  )", $args);
}