You are here

public function SearchApiFacetapiTerm::execute in Search API 7

Adds the filter to the query object.

Parameters

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

1 method overrides SearchApiFacetapiTerm::execute()
SearchApiFacetapiDate::execute in contrib/search_api_facetapi/plugins/facetapi/query_type_date.inc
Adds the filter to the query object.

File

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

Class

SearchApiFacetapiTerm
Plugin for "term" query types.

Code

public function execute($query) {

  // Return terms for this facet.
  $this->adapter
    ->addFacet($this->facet, $query);
  $settings = $this
    ->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;
  }

  // Retrieve the active facet filters.
  $active = $this->adapter
    ->getActiveItems($this->facet);
  if (empty($active)) {
    return;
  }

  // Create the facet filter, and add a tag to it so that it can be easily
  // identified down the line by services when they need to exclude facets.
  $operator = $settings['operator'];
  if ($operator == FACETAPI_OPERATOR_AND) {
    $conjunction = 'AND';
  }
  elseif ($operator == FACETAPI_OPERATOR_OR) {
    $conjunction = 'OR';

    // When the operator is OR, remove parent terms from the active ones if
    // children are active. If we don't do this, sending a term and its
    // parent will produce the same results as just sending the parent.
    if (is_callable($this->facet['hierarchy callback']) && !$settings['flatten']) {

      // Check the filters in reverse order, to avoid checking parents that
      // will afterwards be removed anyways.
      $values = array_keys($active);
      $parents = call_user_func($this->facet['hierarchy callback'], $values);
      foreach (array_reverse($values) as $filter) {

        // Skip this filter if it was already removed, or if it is the
        // "missing value" filter ("!").
        if (!isset($active[$filter]) || $filter == '!') {
          continue;
        }

        // Go through the entire hierarchy of the value and remove all its
        // ancestors.
        while (!empty($parents[$filter])) {
          $ancestor = array_shift($parents[$filter]);
          if (isset($active[$ancestor])) {
            unset($active[$ancestor]);
            if (!empty($parents[$ancestor])) {
              $parents[$filter] = array_merge($parents[$filter], $parents[$ancestor]);
            }
          }
        }
      }
    }
  }
  else {
    $vars = array(
      '%operator' => $operator,
      '%facet' => !empty($this->facet['label']) ? $this->facet['label'] : $this->facet['name'],
    );
    watchdog('search_api_facetapi', 'Unknown facet operator %operator used for facet %facet.', $vars, WATCHDOG_WARNING);
    return;
  }
  $tags = array(
    'facet:' . $this->facet['field'],
  );
  $facet_filter = $query
    ->createFilter($conjunction, $tags);
  foreach ($active as $filter => $filter_array) {
    $field = $this->facet['field'];
    $this
      ->addFacetFilter($facet_filter, $field, $filter, $query);
  }

  // Now add the filter to the query.
  $query
    ->filter($facet_filter);
}