You are here

function apachesolr_search_taxonomy_facet_block in Apache Solr Search 6.2

Same name and namespace in other branches
  1. 5.2 apachesolr_search.module \apachesolr_search_taxonomy_facet_block()
  2. 6 apachesolr_search.module \apachesolr_search_taxonomy_facet_block()

Generate the facet block for a taxonomy vid delta.

1 call to apachesolr_search_taxonomy_facet_block()
apachesolr_search_block in ./apachesolr_search.module
Implementation of hook_block().

File

./apachesolr_search.module, line 874
Provides a content search implementation for node content for use with the Apache Solr search application.

Code

function apachesolr_search_taxonomy_facet_block($response, $query, $delta) {
  $vid = substr($delta, 7);
  if (!module_exists('taxonomy') || !is_numeric($vid)) {
    return;
  }

  // Check that we have a response and a valid vid.
  if (is_object($response->facet_counts->facet_fields->{$delta}) && ($vocab = taxonomy_vocabulary_load($vid))) {
    $reflect_hierarchy = apachesolr_search_get_hierarchical_vocabularies();
    $contains_active = FALSE;
    $facets = array();
    foreach ($response->facet_counts->facet_fields->{$delta} as $tid => $count) {

      // TODO - for now we don't handle facet missing.
      if ($tid != '_empty_') {
        $active = $query
          ->has_filter('tid', $tid);
        if ($active) {
          $contains_active = TRUE;
        }
        $facets[$tid] = array(
          '#name' => 'tid',
          '#value' => $tid,
          '#exclude' => FALSE,
          '#count' => $count,
          '#parent' => 0,
          '#children' => array(),
          '#has_children' => FALSE,
          '#active' => $active,
        );
      }
    }
    if ($facets && $reflect_hierarchy[$vocab->vid]) {
      $placeholders = db_placeholders($facets);
      $tids = array_keys($facets);

      // @todo: faster as 2x separate queries?
      $result = db_query("SELECT tid, parent FROM {term_hierarchy} WHERE parent > 0 AND (tid IN ({$placeholders}) OR parent IN ({$placeholders}))", array_merge($tids, $tids));
      while ($term = db_fetch_object($result)) {

        // Mark all terms that are parents for later CSS class.
        // We assume data in the Solr index is complete - potential for some
        // breakage here.
        if (isset($facets[$term->parent])) {
          $facets[$term->parent]['#has_children'] = TRUE;
          if (isset($facets[$term->tid])) {
            $facets[$term->tid]['#parent'] = $term->parent;

            // Use a reference so we see the updated data.
            $facets[$term->parent]['#children'][] =& $facets[$term->tid];
          }
        }
      }

      // Check for the case like starting on a taxonomy/term/$tid page
      // where parents are not marked as active.
      // @todo: can we make this more efficient?
      do {
        $added_active = FALSE;
        foreach ($facets as $tid => $field) {
          if ($field['#active'] && $field['#parent'] && !$facets[$field['#parent']]['#active']) {

            // This parent has an active child.
            $added_active = TRUE;
            $query
              ->add_filter('tid', $field['#parent']);
            $facets[$field['#parent']]['#active'] = TRUE;
          }
        }
      } while ($added_active);
      foreach ($facets as $tid => $field) {
        if (!empty($field['#parent'])) {

          // We will render it via its parent.
          unset($facets[$tid]);
        }
      }
    }
    $items = apachesolr_search_nested_facet_items($query, $facets, $response->response->numFound);

    // Process all terms into an item list
    if ($items && ($response->response->numFound > 1 || $contains_active)) {
      $initial_limits = variable_get('apachesolr_facet_query_initial_limits', array());
      $limit = isset($initial_limits['apachesolr_search'][$delta]) ? $initial_limits['apachesolr_search'][$delta] : variable_get('apachesolr_facet_query_initial_limit_default', 10);
      return array(
        'subject' => t('Filter by @field', array(
          '@field' => $vocab->name,
        )),
        'content' => theme('apachesolr_facet_list', $items, $limit, $delta),
      );
    }
  }
}