You are here

public function SearchApiViewsHandlerFilterLocation::query in Search API Location 7.2

Add this filter to the query.

Overrides SearchApiViewsHandlerFilter::query

File

search_api_location_views/handler_filter_location.inc, line 119
Provides the views handler for location fields.

Class

SearchApiViewsHandlerFilterLocation
Handler class for a Views location filter.

Code

public function query() {
  while (is_array($this->value)) {
    $this->value = reset($this->value);
  }
  $this->value = trim($this->value);
  if (!$this->value || !($this->operator || is_numeric($this->operator))) {
    return;
  }
  if (empty($this->options['plugin'])) {
    $vars = array(
      '%filter' => $this
        ->ui_name(TRUE),
      '%view' => $this->view
        ->get_human_name(),
    );
    watchdog('search_api_location', 'Filter %filter in view %view has no location input plugin selected. Ignoring location filter.', $vars, WATCHDOG_WARNING);
    return;
  }
  $plugin = search_api_location_get_input_plugins($this->options['plugin']);
  if (!$plugin) {
    $vars = array(
      '%filter' => $this
        ->ui_name(TRUE),
      '%view' => $this->view
        ->get_human_name(),
      '%plugin' => $this->options['plugin'],
    );
    watchdog('search_api_location', 'Filter %filter in view %view uses unknown location input plugin %plugin. Ignoring location filter.', $vars, WATCHDOG_WARNING);
    return;
  }
  $location = $plugin['input callback']($this->value, $this->options['plugin-' . $this->options['plugin']]);
  if (!$location) {
    drupal_set_message(t('The location %address could not be resolved and was ignored.', array(
      '%address' => $this->value,
    )), 'warning');
    return;
  }
  $location = explode(',', $location, 2);
  $location_options = (array) $this->query
    ->getOption('search_api_location', array());

  // If the radius isn't numeric omit it. Necessary since "no radius" is "-".
  $radius = !is_numeric($this->operator) ? NULL : $this->operator;
  if ($this->options['radius_type'] == 'textfield' && is_numeric($this->options['radius_units'])) {
    $radius *= $this->options['radius_units'];
  }
  $location_options[] = array(
    'field' => $this->real_field,
    'lat' => $location[0],
    'lon' => $location[1],
    'radius' => $radius,
  );
  $this->query
    ->setOption('search_api_location', $location_options);
}