You are here

function apachesolr_search_nested_facet_items in Apache Solr Search 6.2

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

Recursive function that returns a nested array of facet values for use with theme_item_list().

Parameters

$query: The current Solr query.

$facets: Array of facet items to prepare for rendering, possibly as nested lists.

$num_found: The number of documents in the current response.

$sort: If true, the returned list will be sorted based on the count of each facets, it's text representation and wither it's active. If false, the facets will be returned in the order they were received.

2 calls to apachesolr_search_nested_facet_items()
apachesolr_search_currentsearch_block in ./apachesolr_search.module
Return the contents of the "Current search" block.
apachesolr_search_taxonomy_facet_block in ./apachesolr_search.module
Generate the facet block for a taxonomy vid delta.

File

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

Code

function apachesolr_search_nested_facet_items($query, $facets, $num_found, $sort = TRUE) {
  $facet_query_sorts = variable_get('apachesolr_facet_query_sorts', array());
  $facet_show_children = variable_get('apachesolr_facet_show_children', array());
  $items = array();
  foreach ($facets as $field) {
    $facet_text = '';
    $field_name = $field['#name'];
    if (function_exists('taxonomy_get_term')) {

      // Taxonomy filters in the query string use the tid field.
      if ($field_name == 'tid') {

        // Each taxonomy vocabulary in the Solr index uses its own field.
        $term = taxonomy_get_term($field['#value']);
        $field_name = 'im_vid_' . $term->vid;
      }
    }
    $facet_definition = apachesolr_get_facet_definition_by_field_name($field_name);
    if (isset($facet_definition['display_callback'])) {
      $function = $facet_definition['display_callback'];
      if (function_exists($function)) {
        $facet_text = $function($field['#value'], $field);
      }
    }
    if (!$facet_text) {
      $breadcrumb_name = 'apachesolr_breadcrumb_' . $field['#name'];
      drupal_alter('apachesolr_theme_breadcrumb', $breadcrumb_name);
      $facet_text = theme($breadcrumb_name, $field, $field['#exclude']);
    }
    if (!$facet_text) {
      $facet_text = $field['#value'];
    }
    $active = !empty($field['#active']);
    $link = array();
    $new_query = clone $query;
    if ($active) {
      foreach (apachesolr_search_collect_children($field) as $child) {
        $new_query
          ->remove_filter($child['#name'], $child['#value']);
      }
      $options['query'] = $new_query
        ->get_url_queryvalues();
      $link['data'] = theme('apachesolr_unclick_link', $facet_text, $new_query
        ->get_path(), $options);
    }
    else {
      $new_query
        ->add_filter($field['#name'], $field['#value']);
      $options = array(
        'query' => $new_query
          ->get_url_queryvalues(),
      );
      $link['data'] = theme('apachesolr_facet_link', $facet_text, $new_query
        ->get_path(), $options, $field['#count'], FALSE, $num_found);
    }
    if ($active) {

      // '*' sorts before all numbers.
      $sortpre = '*';
    }
    elseif (isset($facet_query_sorts['apachesolr_search'][$field_name]) && strpos($facet_query_sorts['apachesolr_search'][$field_name], 'index key') === 0) {

      // If this block is to be alphabetically sorted by key, change $sortpre.
      $sortpre = $field_name;
    }
    elseif (isset($facet_query_sorts['apachesolr_search'][$field_name]) && strpos($facet_query_sorts['apachesolr_search'][$field_name], 'index') === 0) {

      // If this block is to be alphabetically/numerically sorted by value, change $sortpre.
      $sortpre = $facet_text;
    }
    else {
      $sortpre = 1000000 - $field['#count'];
    }

    // Only display children if the block enables it or the parent is clicked.
    if (isset($facet_show_children['apachesolr_search'][$field_name]) && $facet_show_children['apachesolr_search'][$field_name] || !empty($field['#children']) && $field['#active'] == TRUE) {
      $link['children'] = apachesolr_search_nested_facet_items($query, $field['#children'], $num_found, $sort);
      $link['class'] = "expanded-facet";
    }
    elseif (!empty($field['#has_children'])) {
      $link['class'] = "collapsed";
    }
    $items[$sortpre . '*' . $facet_text . $field['#name'] . $field['#value']] = $link;
  }
  if ($sort && $items) {
    switch ($facet_query_sorts['apachesolr_search'][$field_name]) {
      case 'index numeric asc':
        ksort($items, SORT_NUMERIC);
        break;
      case 'index numeric desc':
        krsort($items, SORT_NUMERIC);
        break;
      case 'index desc':
      case 'index key desc':
        krsort($items, SORT_STRING);
        break;
      case 'index asc':
      case 'index key asc':
      default:
        ksort($items, SORT_STRING);
        break;
    }
  }
  return array_values($items);
}