You are here

public function SearchApiFacetapiDate::execute in Search API 7

Adds the filter to the query object.

Parameters

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

Overrides SearchApiFacetapiTerm::execute

File

contrib/search_api_facetapi/plugins/facetapi/query_type_date.inc, line 37
Date query type plugin for the Search API adapter.

Class

SearchApiFacetapiDate
Plugin for "date" query types.

Code

public function execute($query) {

  // Return terms for this facet.
  $this->adapter
    ->addFacet($this->facet, $query);
  $settings = $this->adapter
    ->getFacet($this->facet)
    ->getSettings()->settings;

  // First check if the facet is enabled for this search.
  $default_true = isset($settings['default_true']) ? $settings['default_true'] : TRUE;
  $facet_search_ids = isset($settings['facet_search_ids']) ? $settings['facet_search_ids'] : array();
  if ($default_true != empty($facet_search_ids[$query
    ->getOption('search id')])) {

    // Facet is not enabled for this search ID.
    return;
  }

  // Change limit to "unlimited" (-1).
  $options =& $query
    ->getOptions();
  if (!empty($options['search_api_facets'][$this->facet['name']])) {
    $options['search_api_facets'][$this->facet['name']]['limit'] = -1;
  }
  if ($active_items = $this->adapter
    ->getActiveItems($this->facet)) {
    $field = $this->facet['field'];
    $operator = 'OR';
    if ($settings['operator'] !== FACETAPI_OPERATOR_OR) {
      $operator = 'AND';

      // If the operator is AND, we just need to apply the lowest-level
      // filter(s) to make this work correctly. For single-valued fields, this
      // will always just be the last value, so just use that to improve
      // performance for that case.
      $fields = $query
        ->getIndex()
        ->getFields();
      if (isset($fields[$field]['type']) && !search_api_is_list_type($fields[$field]['type'])) {
        $active_items = array(
          end($active_items),
        );
      }
    }
    $date_query = $query
      ->createFilter($operator, array(
      "facet:{$field}",
    ));
    foreach ($active_items as $active_item) {
      $filter = $this
        ->createRangeFilter($active_item['value']);
      if ($filter) {
        $this
          ->addFacetFilter($date_query, $field, $filter, $query);
      }
    }
    $query
      ->filter($date_query);
  }
}