You are here

public function SearchApiRangesWidgetLinks::execute in Search API ranges 7

Renders the links.

File

plugins/facetapi/widget_links.inc, line 16
Widgets for facets rendered as test link ranges.

Class

SearchApiRangesWidgetLinks
Widget that renders facets as a series of text links.

Code

public function execute() {
  $element =& $this->build[$this->facet['field alias']];

  // Get Search API stuff
  $searcher = $this->facet
    ->getAdapter()
    ->getSearcher();
  $index_id = explode('@', $searcher);
  $index = search_api_index_load($index_id[1]);
  list($query, $results) = $this->facet
    ->getAdapter()
    ->getCurrentSearch();
  $range_field = $this->facet['field alias'];

  // Get facet path field/alias
  if (module_exists('facetapi_pretty_paths')) {
    $processor = new FacetapiUrlProcessorPrettyPaths($this->facet
      ->getAdapter());
    $range_field = $processor
      ->getFacetPrettyPathsAlias($this->facet
      ->getFacet());
  }

  // Prepare variables for min/max query
  $variables = array(
    'element' => $element,
    'index' => $index,
    'range_field' => $range_field,
    'target' => $this->facet
      ->getAdapter()
      ->getSearchPath(),
    'query' => $query,
    'prefix' => $this->settings->settings['prefix'],
    'suffix' => $this->settings->settings['suffix'],
  );

  // Generate the ranges to the be used for the text links
  if (strlen($this->settings->settings['range_advanced']) == 0) {
    $element = search_api_ranges_generate_ranges_simple($variables, $this->settings->settings['range_simple']);
  }
  else {
    $element = search_api_ranges_generate_ranges_advanced($variables, $this->settings->settings['range_advanced']);
  }

  // Create pretty path
  if (module_exists('facetapi_pretty_paths')) {
    foreach ($element as $key => $e) {
      if (isset($e['#query']['f'][0])) {
        $element[$key]['#query']['f'][0] = str_replace(':', '/', $e['#query']['f'][0]);

        // Remove the queries from the f array and add them to the path
        if (isset($e['#path']) && substr_count($e['#path'], '/' . $element[$key]['#query']['f'][0]) > 0) {
          $element[$key]['#active'] = TRUE;
          $element[$key]['#path'] = str_replace('/' . $element[$key]['#query']['f'][0], '', $e['#path']);
        }
        else {
          $element[$key]['#path'] = $element[$key]['#path'] . '/' . $element[$key]['#query']['f'][0];
        }
        unset($element[$key]['#query']['f']);
      }
    }
  }

  // Sets each item's theme hook, builds item list.
  $this
    ->setThemeHooks($element);
  $items_build = $this
    ->buildListItems($element);
  $element = array(
    '#theme' => 'item_list',
    '#items' => $items_build,
    '#attributes' => $this->build['#attributes'],
  );
}