You are here

function apachesolr_search_date_range in Apache Solr Search 5.2

Same name and namespace in other branches
  1. 6 apachesolr_search.module \apachesolr_search_date_range()
  2. 6.2 apachesolr_search.module \apachesolr_search_date_range()
1 call to apachesolr_search_date_range()
apachesolr_search_add_facet_params in ./apachesolr_search.module

File

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

Code

function apachesolr_search_date_range($query, $facet_field) {
  foreach ($query
    ->get_filters($facet_field) as $filter) {

    // If we had an ISO date library we could use ISO dates
    // directly.  Instead, we convert to Unix timestamps for comparison.
    // Only use dates if we are able to parse into timestamps.
    $start = strtotime($filter['#start']);
    $end = strtotime($filter['#end']);
    if ($start && $end && $start < $end) {
      $start_iso = $filter['#start'];
      $end_iso = $filter['#end'];

      // Determine the drilldown gap for this range.
      $gap = apachesolr_date_gap_drilldown(apachesolr_date_find_query_gap($start_iso, $end_iso));
    }
  }

  // If there is no $delta field in query object, get initial
  // facet.date.* params from the DB and determine the best search
  // gap to use.  This callback assumes $delta is 'changed' or 'created'.
  if (!isset($start_iso)) {
    $start_iso = apachesolr_date_iso(db_result(db_query("SELECT MIN({$facet_field}) FROM {node} WHERE status = 1")));

    // Subtract one second, so that this range's $end_iso is not equal to the
    // next range's $start_iso.
    $end_iso = apachesolr_date_iso(db_result(db_query("SELECT MAX({$facet_field}) FROM {node} WHERE status = 1")) - 1);
    $gap = apachesolr_date_determine_gap($start_iso, $end_iso);
  }

  // Return a query range from the beginning of a gap period to the beginning
  // of the next gap period.  We ALWAYS generate query ranges of this form
  // and the apachesolr_date_*() helper functions require it.
  return array(
    "{$start_iso}/{$gap}",
    "{$end_iso}+1{$gap}/{$gap}",
    "+1{$gap}",
  );
}