You are here

public function ApacheSolrFacetapiDate::build in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 plugins/facetapi/query_type_date.inc \ApacheSolrFacetapiDate::build()
  2. 7 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 117
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.
  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']};
  }
  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'] : '';

  // We cannot list anything below a minute (range of 00 seconds till 59
  // seconds. Milliseconds are not possible)
  if ($gap != "+1SECOND") {
    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;
}