You are here

function apachesolr_facet_block in Apache Solr Search 6

Same name and namespace in other branches
  1. 5.2 apachesolr.module \apachesolr_facet_block()
  2. 5 apachesolr.module \apachesolr_facet_block()
  3. 6.2 apachesolr.module \apachesolr_facet_block()

Helper function for displaying a facet block.

2 calls to apachesolr_facet_block()
apachesolr_og_block in contrib/apachesolr_og/apachesolr_og.module
Implementation of hook_block().
apachesolr_search_block in ./apachesolr_search.module
Implementation of hook_block().

File

./apachesolr.module, line 860
Integration with the Apache Solr search application.

Code

function apachesolr_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $facet_callback = FALSE) {
  if (!empty($response->facet_counts->facet_fields->{$facet_field})) {
    $contains_active = FALSE;
    $items = array();
    foreach ($response->facet_counts->facet_fields->{$facet_field} as $facet => $count) {
      $options = array();
      $exclude = FALSE;

      // Solr sends this back if it's empty.
      if ($facet == '_empty_') {
        $exclude = TRUE;
        $facet = '[* TO *]';
        $options['html'] = TRUE;
      }
      if ($facet_callback && function_exists($facet_callback)) {
        $facet_text = $facet_callback($facet, $options);
      }
      elseif ($exclude) {
        $facet_text = theme('placeholder', t('Missing this field'));
      }
      else {
        $facet_text = $facet;
      }
      $active = $query
        ->has_filter($facet_field, $facet);
      if ($active) {

        // '*' sorts before all numbers.
        $sortpre = '*';
      }
      elseif ($exclude) {

        // '-' sorts before all numbers, but after '*'.
        $sortpre = '-';
      }
      else {
        $sortpre = 1000000 - $count;
      }
      $new_query = clone $query;
      if ($active) {
        $contains_active = TRUE;
        $new_query
          ->remove_filter($facet_field, $facet);
        $options['query'] = $new_query
          ->get_url_queryvalues();
        $link = theme('apachesolr_unclick_link', $facet_text, $new_query
          ->get_path(), $options);
      }
      else {
        $new_query
          ->add_filter($facet_field, $facet, $exclude);
        $options['query'] = $new_query
          ->get_url_queryvalues();
        $link = theme('apachesolr_facet_link', $facet_text, $new_query
          ->get_path(), $options, $count, FALSE, $response->response->numFound);
      }
      if ($count || $active) {
        $items[$sortpre . '*' . $facet_text] = $link;
      }
    }

    // Unless a facet is active only display 2 or more.
    if ($items && ($response->response->numFound > 1 || $contains_active)) {
      ksort($items, SORT_STRING);

      // Get information needed by the rest of the blocks about limits.
      $initial_limits = variable_get('apachesolr_facet_query_initial_limits', array());
      $limit = isset($initial_limits[$module][$delta]) ? $initial_limits[$module][$delta] : variable_get('apachesolr_facet_query_initial_limit_default', 10);
      $output = theme('apachesolr_facet_list', $items, $limit);
      return array(
        'subject' => $filter_by,
        'content' => $output,
      );
    }
  }
  return NULL;
}