You are here

function views_handler_argument_term_node_tid_depth::query in Views (for Drupal 7) 6.3

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

Set up the query for this argument.

The argument sent may be found at $this->argument.

Overrides views_handler_argument::query

File

modules/taxonomy/views_handler_argument_term_node_tid_depth.inc, line 81

Class

views_handler_argument_term_node_tid_depth
Argument handler for taxonomy terms with depth.

Code

function query() {
  $this
    ->ensure_my_table();
  if (!empty($this->options['break_phrase'])) {
    $tids = new stdClass();
    $tids->value = $this->argument;
    $tids = views_break_phrase($this->argument, $tids);
    if ($tids->value == array(
      -1,
    )) {
      return FALSE;
    }
    if (count($tids->value) > 1) {
      $placeholder = " IN (" . implode(', ', array_fill(0, sizeof($tids->value), '%d')) . ")";
    }
    else {
      $placeholder = " = %d";
    }
    $tids = $tids->value;
  }
  else {
    $placeholder = "= %d";
    $tids = array(
      $this->argument,
    );
  }
  $subquery = "\n  SELECT tn.vid FROM {term_node} tn\n";
  $where = "  WHERE tn.tid {$placeholder}\n";
  $args = $tids;
  $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, $tids);
      $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, $tids);
        $last = "th{$count}";
      }
    }
  }
  $this->query
    ->add_where(0, "{$this->table_alias}.{$this->real_field} IN ({$subquery}{$where}  )", $args);
}