You are here

protected function SearchApiFacetapiTerm::addFacetFilter in Search API 7

Helper method for setting a facet filter on a query or query filter object.

Parameters

SearchApiQueryInterface|SearchApiQueryFilterInterface $query_filter: The query or filter to which apply the filter.

string $field: The field to apply the filter to.

string $filter: The filter, in the internal string representation used by this module.

SearchApiQuery|null $query: (optional) If available, the search query object should be passed as the fourth parameter.

2 calls to SearchApiFacetapiTerm::addFacetFilter()
SearchApiFacetapiDate::execute in contrib/search_api_facetapi/plugins/facetapi/query_type_date.inc
Adds the filter to the query object.
SearchApiFacetapiTerm::execute in contrib/search_api_facetapi/plugins/facetapi/query_type_term.inc
Adds the filter to the query object.

File

contrib/search_api_facetapi/plugins/facetapi/query_type_term.inc, line 118
Term query type plugin for the Apache Solr adapter.

Class

SearchApiFacetapiTerm
Plugin for "term" query types.

Code

protected function addFacetFilter($query_filter, $field, $filter) {

  // Test if this filter should be negated.
  $settings = $this->adapter
    ->getFacet($this->facet)
    ->getSettings();
  $exclude = !empty($settings->settings['exclude']);

  // Integer (or other non-string) filters might mess up some of the following
  // comparison expressions.
  $filter = (string) $filter;
  if ($filter == '!') {
    $query_filter
      ->condition($field, NULL, $exclude ? '<>' : '=');
  }
  elseif ($filter && $filter[0] == '[' && $filter[strlen($filter) - 1] == ']' && ($pos = strpos($filter, ' TO '))) {
    $lower = trim(substr($filter, 1, $pos));
    $upper = trim(substr($filter, $pos + 4, -1));
    $supports_between = FALSE;
    if (func_num_args() > 3) {
      $query = func_get_arg(3);
      if ($query instanceof SearchApiQuery) {
        try {
          $supports_between = $query
            ->getIndex()
            ->server()
            ->supportsFeature('search_api_between');
        } catch (SearchApiException $e) {

          // Ignore, really not that important (and rather unlikely).
        }
      }
    }
    if ($lower == '*' && $upper == '*') {
      $query_filter
        ->condition($field, NULL, $exclude ? '=' : '<>');
    }
    elseif ($supports_between && $lower != '*' && $upper != '*') {
      $operator = $exclude ? 'NOT BETWEEN' : 'BETWEEN';
      $query_filter
        ->condition($field, array(
        $lower,
        $upper,
      ), $operator);
    }
    elseif (!$exclude) {
      if ($lower != '*') {

        // Iff we have a range with two finite boundaries, we set two
        // conditions (larger than the lower bound and less than the upper
        // bound) and therefore have to make sure that we have an AND
        // conjunction for those.
        if ($upper != '*' && !($query_filter instanceof SearchApiQueryInterface || $query_filter
          ->getConjunction() === 'AND')) {
          $original_query_filter = $query_filter;
          $query_filter = new SearchApiQueryFilter('AND');
        }
        $query_filter
          ->condition($field, $lower, '>=');
      }
      if ($upper != '*') {
        $query_filter
          ->condition($field, $upper, '<=');
      }
    }
    else {

      // Same as above, but with inverted logic.
      if ($lower != '*') {
        if ($upper != '*' && ($query_filter instanceof SearchApiQueryInterface || $query_filter
          ->getConjunction() === 'AND')) {
          $original_query_filter = $query_filter;
          $query_filter = new SearchApiQueryFilter('OR');
        }
        $query_filter
          ->condition($field, $lower, '<');
      }
      if ($upper != '*') {
        $query_filter
          ->condition($field, $upper, '>');
      }
    }
  }
  else {
    $query_filter
      ->condition($field, $filter, $exclude ? '<>' : '=');
  }
  if (isset($original_query_filter)) {
    $original_query_filter
      ->filter($query_filter);
  }
}