You are here

function getlocations_fields_handler_sort_distance::query in Get Locations 7.2

Same name and namespace in other branches
  1. 7 modules/getlocations_fields/handlers/getlocations_fields_handler_sort_distance.inc \getlocations_fields_handler_sort_distance::query()

Called to add the sort to a query.

Overrides views_handler_sort::query

File

modules/getlocations_fields/handlers/getlocations_fields_handler_sort_distance.inc, line 73
getlocations_fields_handler_sort_distance.inc @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Class

getlocations_fields_handler_sort_distance
@file getlocations_fields_handler_sort_distance.inc @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function query() {

  // Google Autocomplete data needs to be transferred to options
  if ($this->options['origin'] == 'search' && isset($this->view->exposed_data['distance']) && $this->view->exposed_data['distance']['latitude'] && $this->view->exposed_data['distance']['longitude']) {
    $this->options['latitude'] = $this->view->exposed_data['distance']['latitude'];
    $this->options['longitude'] = $this->view->exposed_data['distance']['longitude'];
  }
  $coordinates = getlocations_fields_views_proximity_get_reference_location($this->view, $this->options);
  $this
    ->ensure_my_table();

  // OK, so this part will need a little explanation.
  // Since the distance calculation is so icky, we try quite hard
  // to save some work for the database.
  // If someone has added a field that matches the sort, we just sort on that column!
  $alias = $this->table_alias . '_' . $this->field . '_sort';
  foreach ($this->view->field as $filter) {
    if ($filter->table == 'getlocations_fields' && $filter->field == 'distance' && $filter->options['relationship'] == $this->options['relationship']) {
      if ($filter->options['origin'] == $this->options['origin'] && $filter->options['latitude'] == $this->options['latitude'] && $filter->options['longitude'] == $this->options['longitude']) {

        // We have a match! Sync aliases to make it easier on the database.
        $alias = $filter->field_alias;

        #$distance = $filter->options['search_distance'];
      }
    }
  }
  if (!empty($coordinates) && $coordinates['latitude'] && $coordinates['longitude']) {

    //  // This is done exactly the same as the field version.
    //  // Views is ok with us redefining the formula for a field.
    //  // If ANYTHING differs in the configuration, we will use a new alias.
    $this->query
      ->add_orderby(NULL, getlocations_earth_distance_sql($coordinates['latitude'], $coordinates['longitude'], $this->table_alias), $this->options['order'], $alias);
  }
}