You are here

function tagadelic_views_views_query_alter in Views Tagadelic 7

Implementation of hook_views_query_alter

File

./tagadelic_views.views.inc, line 36

Code

function tagadelic_views_views_query_alter(&$view, &$query) {
  if (get_class($view->style_plugin) != 'tagadelic_plugin_style') {
    return;
  }
  $field_tid = 'tid';
  $taxonomy_term_table_alias_default = 'taxonomy_term_data';
  $taxonomy_term_table_alias = null;

  // Base table is taxonomy_term_data
  if ($view->base_table == 'taxonomy_term_data') {
    $taxonomy_term_table_alias = $taxonomy_term_table_alias_default;
    $join = views_get_table_join('taxonomy_index', 'taxonomy_term_data');
    $query
      ->add_relationship("taxonomy_index_taxonomy_term_data", $join, 'taxonomy_index', NULL);
  }
  else {

    // If taxonomy is not at base table try to find a relation with it.
    // Actually I take in account only node
    $taxonomy_term_table_alias = isset($query->tables['taxonomy_term_data']) ? 'taxonomy_term_data' : null;

    // got taxonomy_term_data ?
    if (is_null($taxonomy_term_table_alias)) {

      // search if there is a reference, if so take the first
      foreach ($query->relationships as $alias => $relationship) {
        if ($relationship['base'] == 'taxonomy_term_data') {
          $taxonomy_term_table_alias = $alias;
          break;
        }
      }

      // set a default table defined later as join to $query->base_table
      // @TODO: Check what happens if base table is not a node
      if (!$taxonomy_term_table_alias) {
        $taxonomy_term_table_alias = 'tagadelic_taxonomy_term_data';
      }
      if (!is_null($taxonomy_term_table_alias)) {

        // TEST 2: add new relationships, seems OK
        if (!isset($query->table_queue['taxonomy_index'])) {
          $query
            ->ensure_table('taxonomy_index');
        }

        //            $reference_table = 'node';
        $reference_table = $query->base_table;

        // tagadelic_taxonomy_index
        $join = $query->table_queue['taxonomy_index']['join'];
        $query
          ->add_relationship('tagadelic_taxonomy_index', $join, 'taxonomy_index', $reference_table);

        // tagadelic_taxonomy_term_data
        $join = views_get_table_join('taxonomy_term_data', 'taxonomy_index');
        $query
          ->add_relationship('tagadelic_taxonomy_term_data', $join, 'tagadelic_taxonomy_index');

        // Add some term information needed by tagadelic (tid, vid)
        $field_tid = $query
          ->add_field($taxonomy_term_table_alias, 'tid', 'taxonomy_term_data_tag_tid');
        $query
          ->add_field($taxonomy_term_table_alias, 'vid', 'taxonomy_term_data_tag_vid');
      }
      else {
        views_debug(t("Consider adding at least a relationship to a taxonomy term to get (better) results."));
      }
    }
  }
  $field_name_count = $query
    ->add_field($taxonomy_term_table_alias, 'name', $alias = 'taxonomy_term_data_tag_count', $params = array(
    'function' => 'count',
  ));
  $field_name = $query
    ->add_field($taxonomy_term_table_alias, 'name', $alias = 'taxonomy_term_data_tag_name');
  $field_desc = $query
    ->add_field($taxonomy_term_table_alias, 'description', $alias = 'taxonomy_term_data_tag_description');
  $query->distinct = TRUE;
  $query->no_distinct = $field_tid;

  //  $query->set_distinct(TRUE, TRUE);
  $query
    ->add_groupby($field_tid);
  $query
    ->add_having_expression(0, 'taxonomy_term_data_tag_count > 0');

  //  $query->add_orderby( NULL, NULL, 'DESC', 'taxonomy_term_data_tag_count' );
  //  new views_plugin_query_default();
  //  dsm($view);
  //  dsm($query);
}