You are here

function apachesolr_search_currentsearch_block in Apache Solr Search 6.2

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

Return the contents of the "Current search" block.

Parameters

$response: The Solr response object.

$query: The Solr query object.

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

File

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

Code

function apachesolr_search_currentsearch_block($response, $query) {
  $fields = $query
    ->get_filters();
  $links = array();
  $facets = array();

  // If current search has keys, offer current search without them
  if ($keys = $query
    ->get_query_basic()) {
    $links[] = theme('apachesolr_unclick_link', $keys, $query
      ->get_path(''), array(
      'query' => $query
        ->get_url_queryvalues(),
    ));
  }

  // Find all taxonomy terms to be treated in a hierarchy.
  if (module_exists('taxonomy')) {
    $reflect_hierarchy = apachesolr_search_get_hierarchical_vocabularies();
    $facets = array();
    foreach ($fields as $index => $field) {
      if ($field['#name'] && 'tid' == $field['#name']) {
        $term = taxonomy_get_term($field['#value']);
        if ($reflect_hierarchy[$term->vid]) {
          $fields[$index] += array(
            '#parent' => 0,
            '#children' => array(),
          );

          // Just save the index for later lookup.
          $facets[$term->tid] = $index;
        }
      }
    }
    if ($facets) {

      // Get all term hierarchy information.
      $all_terms = apachesolr_get_parent_terms(array_keys($facets));
      foreach ($all_terms as $tid => $term) {
        if (!isset($facets[$tid])) {

          // This is a parent that is missing from the query.  E.g. we started
          // on a taxonomy/term/$tid page.
          $query
            ->add_filter('tid', $tid);

          // Ordering is wonky, but oh well...
          $fields[] = array(
            '#name' => 'tid',
            '#value' => $tid,
            '#exclude' => FALSE,
            '#parent' => 0,
            '#children' => array(),
          );

          // Get the index of the newly added facet.
          end($fields);
          $facets[$tid] = key($fields);
        }
      }
      foreach ($all_terms as $tid => $term) {
        $index = $facets[$term->tid];
        if (isset($facets[$term->parent])) {

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

  // We don't directly render any items with a parent.
  foreach ($fields as $index => $field) {
    $fields[$index]['#active'] = TRUE;
    if (!empty($fields[$index]['#parent']) || !$field['#name']) {

      // We will render it via its parent.
      unset($fields[$index]);
    }
  }
  $links = array_merge($links, apachesolr_search_nested_facet_items($query, $fields, $response->response->numFound, FALSE));
  if ($links) {
    $content = theme('apachesolr_currentsearch', $response->response->numFound, $links);
    return array(
      'subject' => t('Current search'),
      'content' => $content,
    );
  }
}