You are here

function facetapi_apachesolr_date_range in Facet API 6

Gets the range of dates we are using.

@todo integrate this into facetapi_apachesolr_facetapi_date_range_prepare() once we have a "range limit callback".

Parameters

$query: A Solr_Base_Query object.

$facet_field: A string containing the name of the facet field.

Return value

An array containing the gap and range information.

1 call to facetapi_apachesolr_date_range()
facetapi_apachesolr_facetapi_query_date_prepare in contrib/facetapi_apachesolr/facetapi_apachesolr.module
Implementation of hook_facetapi_query_QUERY_TYPE_prepare().

File

contrib/facetapi_apachesolr/facetapi_apachesolr.module, line 136
The Apache Solr Search Integration module's implementation of the the Facet API.

Code

function facetapi_apachesolr_date_range(Solr_Base_Query $query, $facet_field) {

  // Captures adapter, facet.
  // @todo Do we need some defensive coding here?
  // @todo Is facet field the facet name or the field alias?
  $adapter = facetapi_adapter_load('apachesolr_search');
  $enabled_facets = facetapi_enabled_facets_get('apachesolr_search');
  $facet = $enabled_facets[$facet_field];

  // Attempts to get next gap from passed date filters.
  $return = NULL;
  foreach ($adapter
    ->getActiveItems($facet) as $value => $item) {
    if ($gap = facetapi_date_gap_get($item['start'], $item['end'])) {
      $next_gap = facetapi_next_date_gap_get($gap, FACETAPI_DATE_MINUTE);
      if ($next_gap == $gap) {
        $next_gap = NULL;
      }
      $return = array(
        "{$item['start']}/{$next_gap}",
        "{$item['end']}+1{$next_gap}/{$next_gap}",
        "+1{$next_gap}",
      );
    }
  }

  // If no filters were passed, get default range.
  if (NULL === $return) {

    // Builds SQL that gets minimum and maximum values from node table.
    $minimum = $maximum = FALSE;
    if (!empty($facet['min callback']) && function_exists($facet['min callback'])) {
      $minimum = $facet['min callback']($facet);
    }
    if (!empty($facet['max callback']) && function_exists($facet['max callback'])) {
      $maximum = $facet['max callback']($facet);
    }

    // Gets the default gap.
    $gap = FACETAPI_DATE_YEAR;
    if ($minimum && $maximum) {
      $gap = facetapi_timestamp_gap_get($minimum, $maximum);
      $minimum = facetapi_isodate($minimum, $gap);
      $maximum = facetapi_isodate($maximum, $gap);
      $return = array(
        "{$minimum}/{$gap}",
        "{$maximum}+1{$gap}/{$gap}",
        "+1{$gap}",
      );
    }
  }

  // Returns the range information.
  return $return;
}