You are here

function apachesolr_date_search_date_range in Apache Solr Search 6.2

When faceting and filtering we need to infer ranges of dates. This function looks at a query and a facet field and returns a date range for use in querying.

Parameters

Drupal_Solr_Query_Interface $query: Current query object.

$facet_field: The field for which a range must be generated.

Return value

array $gap The array contains a start, end, and gap element.

Example return value: array( 0 => '2007-01-05T13:05:00Z/YEAR', 1 => '2013-11-17T15:04:00Z+1YEAR/YEAR', 2 => '+1YEAR', );

1 call to apachesolr_date_search_date_range()
apachesolr_search_add_facet_params in ./apachesolr_search.module
1 string reference to 'apachesolr_date_search_date_range'
apachesolr_search_add_facet_params in ./apachesolr_search.module

File

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

Code

function apachesolr_date_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 the query object, get initial
  // facet.date.* params from the DB and determine the best search
  // gap to use.
  if (!isset($start_iso)) {

    // NOTE: Finding the field namd and loading the field info is a hacky
    // bit of string manipulation. We look once with what comes in ($field_name),
    // and if that doesn't find any CCK field definition for us, we hack off
    // the last four characters (presumed to be '_end' for an ending date),
    // and try again. If that doesn't find anything we go home.
    $field_name = substr($facet_field, 8);
    $field = content_fields($field_name);
    $db_info = content_database_info($field);
    $column = $db_info['columns']['value']['column'];

    // This check is in place for the cases where the field name has
    // _end appended to the end, and signifies that it is and end date.
    if (!$field) {
      $field_name = substr($field_name, 0, strlen($field_name) - 4);
      $field = content_fields($field_name);
      $db_info = content_database_info($field);
      $column = $db_info['columns']['value2']['column'];
    }

    // By this point we should have the following:
    //   $field_name, a cck field name
    //   $field, a cck field definition
    //   $db_info, information from content.module about retrieving db data
    //   $column, the column name in the db table for this field
    if (!$field) {
      return;
    }
    if (!empty($field['timezone_db'])) {
      $tz = new DateTimeZone($field['timezone_db']);
    }
    else {

      // The commented code takes the TZ from the computer the site is on.

      //$tz = new DateTimeZone(date_default_timezone_get());
      $tz = new DateTimeZone('UTC');
    }
    $table = $db_info['table'];
    $start_value = db_result(db_query("SELECT MIN(cck.{$column}) FROM {{$table}} cck INNER JOIN {node} n ON cck.vid = n.vid WHERE n.status = 1"));
    if (is_numeric($start_value)) {
      $start_iso = apachesolr_date_iso($start_value);
    }
    elseif ($date = date_create($start_value, $tz)) {
      $start_iso = apachesolr_date_iso($date
        ->format('U'));
    }

    // Subtract one second, so that this range's $end_iso is not equal to the
    // next range's $start_iso.
    $end_value = db_result(db_query("SELECT MAX(cck.{$column}) FROM {{$table}} cck INNER JOIN {node} n ON cck.vid = n.vid WHERE n.status = 1"));
    if (is_numeric($end_value)) {
      $end_iso = apachesolr_date_iso($end_value - 1);
    }
    elseif ($date = date_create($end_value, $tz)) {
      $end_iso = apachesolr_date_iso($date
        ->format('U') - 1);
    }
    if (isset($start_iso) && isset($end_iso)) {
      $gap = apachesolr_date_determine_gap($start_iso, $end_iso);
    }
    else {

      // TODO: Find the gap.
      $end_iso = $start_iso;
      $gap = "YEAR";
    }
  }

  // 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}",
  );
}