You are here

function apachesolr_date_date_facet_block in Apache Solr Search 6.2

Helper function for displaying a date facet blocks.

1 string reference to 'apachesolr_date_date_facet_block'
apachesolr_date_apachesolr_cck_fields_alter in contrib/apachesolr_date/apachesolr_date.module
Implementation of hook_apachesolr_cck_fields_alter(). This function adds the CCK date fields' definitions to let them be recognized as facets.

File

contrib/apachesolr_date/apachesolr_date.module, line 343
Integration with the Apache Solr search application. Provides faceting for CCK Date fields.

Code

function apachesolr_date_date_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $facet_callback = FALSE) {

  // The items in the facet block (facet links and unclick links).
  $items = array();

  // The array that is ultimately sent into apachesolr_l(), @see apachesolr_l()
  $options = array();

  // The display text for any given facet or unclick link.
  $facet_text = '';

  // Clone the query as we either add or remove a filter before generating
  // the link.
  $new_query = clone $query;

  // Set the default gap.
  $gap = 'YEAR';
  foreach (array_reverse($new_query
    ->get_filters($facet_field)) as $filter) {
    $new_query
      ->remove_filter($facet_field, $filter['#value']);
    $gap = apachesolr_date_find_query_gap($filter['#start'], $filter['#end']);
    $facet_text = apachesolr_date_format_iso_by_gap($gap, $filter['#start']);
    $options['gap'] = $gap;
    $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.
  // NOTE: Date fields come in $response->facet_counts->facet_fields
  // but others come in $response->facet_counts->facet_dates.
  if (!empty($response->facet_counts->facet_dates->{$facet_field})) {
    $field = clone $response->facet_counts->facet_dates->{$facet_field};
  }
  elseif (!empty($response->facet_counts->facet_fields->{$facet_field})) {
    $field = clone $response->facet_counts->facet_fields->{$facet_field};
  }
  if ($field) {

    // A field will have this type of structure, where the main information
    // is in the form DATE => COUNT:
    // stdClass Object
    // (
    //    [2007-01-01T00:00:00Z] => 5
    //    [2008-01-01T00:00:00Z] => 4
    //    [2009-01-01T00:00:00Z] => 2
    //    [2010-01-01T00:00:00Z] => 6
    //    [2011-01-01T00:00:00Z] => 7
    //    [2012-01-01T00:00:00Z] => 4
    //    [2013-01-01T00:00:00Z] => 4
    //    [gap] => +1YEAR
    //    [end] => 2014-01-01T00:00:00Z
    // )
    // Isolate $end and $gap and clean the field to only have the actual
    // date values.
    $end = $field->end;
    unset($field->end);
    if (isset($field->gap)) {
      $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) {

      // 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, array(
          'gap' => $gap,
        ));
      }
      else {
        $facet_text = apachesolr_date_format_iso_by_gap($gap, $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, $delta);
    return array(
      'subject' => $filter_by,
      'content' => $output,
    );
  }
  return NULL;
}