You are here

public function ApacheSolrFacetapiDate::build in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 plugins/facetapi/query_type_date.inc \ApacheSolrFacetapiDate::build()
  2. 6.3 plugins/facetapi/query_type_date.inc \ApacheSolrFacetapiDate::build()

Initializes the facet's build array.

Return value

array The initialized render array.

File

plugins/facetapi/query_type_date.inc, line 120
Date query type plugin for the Apache Solr adapter.

Class

ApacheSolrFacetapiDate
Plugin for "date" query types.

Code

public function build() {

  // Initializes build and gets static response.
  if (!($response = apachesolr_static_response_cache($this->adapter
    ->getSearcher()))) {
    return array();
  }
  $build = array();

  // Gets total number of documents matched in search.
  $total = $response->response->numFound;

  // Gets the active date facets, starts to builds the "parent - child"
  // relationships.
  $parent = NULL;
  foreach ($this->adapter
    ->getActiveItems($this->facet) as $value => $item) {

    // Builds the raw facet "value", the count for selected items will be the
    // total number of rows returned in the query.
    $build[$value] = array(
      '#count' => $total,
    );

    // If there is a previous item, there is a parent, uses a reference so the
    // arrays are populated when they are updated.
    if (NULL !== $parent) {
      $build[$parent]['#item_children'][$value] =& $build[$value];
      $build[$value]['#item_parents'][$parent] = $parent;
    }

    // Stores the last value iterated over.
    $parent = $value;
  }

  // Gets raw facet data from the Solr server.
  // Check older versions first, then newer ones.
  if (isset($response->facet_counts->facet_dates) && isset($response->facet_counts->facet_dates->{$this->facet['field']})) {
    $raw_data = (array) $response->facet_counts->facet_dates->{$this->facet['field']};
    $raw_data['gap'] = FACETAPI_DATE_MINUTE;
    if (isset($response->facet_counts->facet_dates->{$this->facet['field']}->gap)) {
      $raw_data['gap'] = $response->facet_counts->facet_dates->{$this->facet['field']}->gap;
    }
  }
  elseif (isset($response->facet_counts->facet_ranges) && isset($response->facet_counts->facet_ranges->{$this->facet['field']}->counts)) {
    $raw_data = (array) $response->facet_counts->facet_ranges->{$this->facet['field']}->counts;
    $raw_data['gap'] = FACETAPI_DATE_MINUTE;
    if (isset($response->facet_counts->facet_ranges->{$this->facet['field']}->gap)) {
      $raw_data['gap'] = $response->facet_counts->facet_ranges->{$this->facet['field']}->gap;
    }
  }
  else {
    $raw_data = array();
  }

  //$end = (!empty($raw_data['end'])) ? $raw_data['end'] : '';

  //$start = (!empty($raw_data['start'])) ? $raw_data['start'] : '';
  $gap = !empty($raw_data['gap']) ? $raw_data['gap'] : '';
  $settings = $this
    ->getSettings()->settings;
  $granularity = isset($settings['date_granularity']) ? $settings['date_granularity'] : FACETAPI_DATE_MINUTE;

  // We cannot list anything below a minute (range of 00 seconds till 59
  // seconds). Milliseconds are not possible. Also, anything below the set
  // granularity should not be shown either.
  if ($gap != "+1" . FACETAPI_DATE_SECOND && (!$gap || strtotime($gap, 0) >= strtotime("+1" . $granularity, 0))) {
    unset($raw_data['start']);
    unset($raw_data['end']);
    unset($raw_data['gap']);

    // Treat each date facet as a range start, and use the next date facet
    // as range end.  Use 'end' for the final end.
    $previous = NULL;

    // Builds facet counts object used by the server.
    foreach ($raw_data as $value => $count) {
      if ($count) {
        $from = $value;
        $to = facetapi_isodate(strtotime($value . $gap));
        $new_value = '[' . $from . ' TO ' . $to . ']';
        $build[$new_value] = array(
          '#count' => $count,
          '#active' => 0,
        );
        if (NULL !== $parent) {
          $build[$parent]['#item_children'][$new_value] =& $build[$new_value];
          $build[$new_value]['#item_parents'][$parent] = $parent;
        }
      }
    }
  }
  return $build;
}