You are here

public function SearchFacetapiDate::execute in Faceted Navigation for Search 7

Adds the filter to the query object.

Parameters

$query: An object containing the query in the backend's native API.

File

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

Class

SearchFacetapiDate
Plugin for "date" query types.

Code

public function execute($query) {

  // Gets the last active date, bails if there isn't one.
  $active_items = $this->adapter
    ->getActiveItems($this->facet);
  if (!($active_item = end($active_items))) {
    return;
  }

  // Gets facet query and this facet's query info.
  $facet_query = $this->adapter
    ->getFacetQueryExtender();
  $query_info = $this->adapter
    ->getQueryInfo($this->facet);
  $tables_joined = array();

  // Formats start and end date.
  $start = strtotime($active_item['start']);
  $end = strtotime($active_item['end']);
  foreach ($query_info['fields'] as $field_info) {

    // Adds join to the facet query.
    $facet_query
      ->addFacetJoin($query_info, $field_info['table_alias']);

    // Adds adds join to search query, makes sure it is only added once.
    if (isset($query_info['joins'][$field_info['table_alias']])) {
      if (!isset($tables_joined[$field_info['table_alias']])) {
        $tables_joined[$field_info['table_alias']] = TRUE;
        $join_info = $query_info['joins'][$field_info['table_alias']];
        $query
          ->join($join_info['table'], $join_info['alias'], $join_info['condition']);
      }
    }

    // Adds field conditions to the facet and search query.
    $field = $field_info['table_alias'] . '.' . $this->facet['field'];
    $query
      ->condition($field, $start, '>=');
    $query
      ->condition($field, $end, '<');
    $facet_query
      ->condition($field, $start, '>=');
    $facet_query
      ->condition($field, $end, '<');
  }
}