You are here

public function SearchApiRangesWidgetSelect::execute in Search API ranges 7

Renders the links.

File

plugins/facetapi/widget_select.inc, line 13
Widget for facet ranges rendered as text links.

Class

SearchApiRangesWidgetSelect
@file Widget for facet ranges rendered as text links.

Code

public function execute() {
  static $count_select_widget;
  $count_select_widget++;
  $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();

  // Prepare variables for min/max query.
  $variables = array(
    'element' => $element,
    'index' => $index,
    'range_field' => $this->facet['field alias'],
    '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']);
  }
  $cp_element = $element;
  $first = array_slice($cp_element, 0, 1);
  $cp_first = $first;
  if (isset($cp_first[0]) && count($cp_first[0]['#query']['f']) > 0) {
    foreach ($cp_first[0]['#query']['f'] as $key => $qstring) {
      $values = explode(':', $qstring);
      if ($values[0] == $this->facet['field alias']) {
        $instring = TRUE;
        $cp_first[0]['#query']['f'][$key] = $this->facet['field alias'] . ':';
      }
      else {
        array_push($cp_first[0]['#query']['f'], $this->facet['field alias'] . ':');
      }
    }
  }
  $tmp_first = array_pop($cp_first);
  $remove_facet_url = url($tmp_first['#path']);
  if ($tmp_first['#active'] == TRUE) {
    $remove_facet_url = url($_GET['q'], array(
      'query' => $tmp_first['#query'],
    ));
  }
  $options[$remove_facet_url] = t('All');
  $active = $remove_facet_url;
  foreach ($element as $value => $item) {
    $instring = FALSE;
    if (count($item['#query']['f']) > 0) {
      foreach ($item['#query']['f'] as $qstring) {
        $values = explode(':', $qstring);
        if ($values[0] == $this->facet['field alias']) {
          $instring = TRUE;
        }
      }
    }
    if (!$instring) {
      if (!is_array($item['#query']['f'])) {
        $item['#query']['f'] = array();
      }
      array_push($item['#query']['f'], $this->facet['field alias'] . ':' . $value);
    }
    $url = url($_GET['q'], array(
      'query' => $item['#query'],
    ));
    $options[$url] = $item['#markup'] . ' (' . $item['#count'] . ')';
    if ($item['#active'] == TRUE) {
      $active = $url;
    }
  }
  if (end($options) !== '(-)') {
    if (!isset($form_state)) {
      $form_state = array();
    }
    $element = facetapi_ranges_select_facet_form($form_state, $options, $count_select_widget, $active);
  }
}