You are here

function apachesolr_date_facet_block in Apache Solr Search 5.2

Same name and namespace in other branches
  1. 6 apachesolr.module \apachesolr_date_facet_block()
  2. 6.2 apachesolr.module \apachesolr_date_facet_block()

Helper function for displaying a date facet block.

TODO: Refactor with apachesolr_facet_block().

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

File

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

Code

function apachesolr_date_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $facet_callback = FALSE) {
  $items = array();
  $new_query = clone $query;
  foreach (array_reverse($new_query
    ->get_filters($facet_field)) as $filter) {
    $options = array();

    // Iteratively remove the date facets.
    $new_query
      ->remove_filter($facet_field, $filter['#value']);
    if ($facet_callback && function_exists($facet_callback)) {
      $facet_text = $facet_callback($filter['#start'], $options);
    }
    else {
      $facet_text = apachesolr_date_format_iso_by_gap(apachesolr_date_find_query_gap($filter['#start'], $filter['#end']), $filter['#start']);
    }
    $options['query'] = $new_query
      ->get_url_queryvalues();
    array_unshift($items, theme('apachesolr_unclick_link', $facet_text, $new_query
      ->get_path(), $options));
  }

  // Add links for additional date filters.
  if (!empty($response->facet_counts->facet_dates->{$facet_field})) {
    $field = clone $response->facet_counts->facet_dates->{$facet_field};
    $end = $field->end;
    unset($field->end);
    $gap = $field->gap;
    unset($field->gap);

    // Treat each date facet as a range start, and use the next date
    // facet as range end.  Use 'end' for the final end.
    $range_end = array();
    foreach ($field as $facet => $count) {
      if (isset($prev_facet)) {
        $range_end[$prev_facet] = $facet;
      }
      $prev_facet = $facet;
    }
    $range_end[$prev_facet] = $end;
    foreach ($field as $facet => $count) {
      $options = array();

      // Solr sends this back if it's empty.
      if ($facet == '_empty_' || $count == 0) {
        continue;
      }
      if ($facet_callback && function_exists($facet_callback)) {
        $facet_text = $facet_callback($facet, $options);
      }
      else {
        $facet_text = apachesolr_date_format_iso_by_gap(substr($gap, 2), $facet);
      }
      $new_query = clone $query;
      $new_query
        ->add_filter($facet_field, '[' . $facet . ' TO ' . $range_end[$facet] . ']');
      $options['query'] = $new_query
        ->get_url_queryvalues();
      $items[] = theme('apachesolr_facet_link', $facet_text, $new_query
        ->get_path(), $options, $count, FALSE, $response->response->numFound);
    }
  }
  if (count($items) > 0) {

    // 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;
}