You are here

function _views_add_taxonomy in Views (for Drupal 7) 5

2 calls to _views_add_taxonomy()
views_handler_arg_taxid in modules/views_taxonomy.inc
views_handler_filter_tid_custom in modules/views_taxonomy.inc

File

modules/views_taxonomy.inc, line 491

Code

function _views_add_taxonomy($op, $value, $depth, &$query) {
  $value = array_map('intval', $value);

  // When filtering via depth, we have to add a chain. If it's an or query
  // we add 1 chain, but in an and query we actually basically have to add
  // a 2 dimensional array.
  if ($op == 'OR') {
    $num = $query
      ->add_table('term_node');
    $tablename = $query
      ->get_table_name('term_node', $num);
    $clause = "'" . implode("','", $value) . "'";
    $where = "{$tablename}.tid IN ({$clause})";

    // for each depth > 0, add the next parent in term_hierarchy to the join
    $thnum = $query
      ->add_table('term_hierarchy', false, 1, array(
      'left' => array(
        'table' => $tablename,
        'field' => 'tid',
      ),
      'right' => array(
        'field' => 'tid',
      ),
    ));
    $tablename = $query
      ->get_table_name('term_hierarchy', $thnum);
    for ($i = 0; $i < $depth; $i++) {
      $thnum = $query
        ->add_table('term_hierarchy', false, 1, array(
        'left' => array(
          'table' => $tablename,
          'field' => 'parent',
        ),
        'right' => array(
          'field' => 'tid',
        ),
      ));
      $tablename = $query
        ->get_table_name('term_hierarchy', $thnum);
      $where .= " OR {$tablename}.tid IN ({$clause})";
    }
    $query
      ->add_where($where);
  }
  else {
    if ($op == 'NOR') {

      //ignore the depth for this case
      $table_data = _views_get_tables();
      $joininfo = $table_data['term_node']['join'];
      $joininfo['extra']['tid'] = $value;
      $num = $query
        ->add_table('term_node', false, 1, $joininfo);
      $tablename = $query
        ->get_table_name('term_node', $num);
      $query
        ->add_where("{$tablename}.tid IS NULL");
    }
    else {
      foreach ($value as $tid) {

        // For every term we have to match add the depth chain
        $num = $query
          ->add_table('term_node');
        $tablename = $query
          ->get_table_name('term_node', $num);
        $where = "{$tablename}.tid = '{$tid}'";

        // for each depth > 0, add the next parent in term_hierarchy to the join
        $thnum = $query
          ->add_table('term_hierarchy', false, 1, array(
          'left' => array(
            'table' => $tablename,
            'field' => 'tid',
          ),
          'right' => array(
            'field' => 'tid',
          ),
        ));
        $tablename = $query
          ->get_table_name('term_hierarchy', $thnum);
        for ($i = 0; $i < $depth; $i++) {
          $thnum = $query
            ->add_table('term_hierarchy', false, 1, array(
            'left' => array(
              'table' => $tablename,
              'field' => 'parent',
            ),
            'right' => array(
              'field' => 'tid',
            ),
          ));
          $tablename = $query
            ->get_table_name('term_hierarchy', $thnum);
          $where .= " OR {$tablename}.tid = '{$tid}'";
        }
        $query
          ->add_where($where);
      }
    }
  }
}