You are here

function location_views_proximity_get_reference_location in Location 7.5

Same name and namespace in other branches
  1. 6.3 location.views.inc \location_views_proximity_get_reference_location()
  2. 7.3 location.views.inc \location_views_proximity_get_reference_location()
  3. 7.4 location.views.inc \location_views_proximity_get_reference_location()

Helper function for proximity handlers. Retrieves the coordinates of the location that this field measures distances against.

Parameters

$view: The view object.

$options: An array of the options (or values in the case of the filter) of the handler.

Return value

An array with keys 'latitude' and 'longitude' or an empty array.

4 calls to location_views_proximity_get_reference_location()
location_handler_field_location_distance::click_sort in handlers/location_handler_field_location_distance.inc
Called to determine what to tell the clicksorter.
location_handler_field_location_distance::query in handlers/location_handler_field_location_distance.inc
Called to add the field to a query.
location_handler_sort_location_distance::query in handlers/location_handler_sort_location_distance.inc
Called to add the sort to a query.
location_views_handler_filter_proximity::query in handlers/location_views_handler_filter_proximity.inc
Add this filter to the query.

File

./location.views.inc, line 456
Views 3 support for Location.

Code

function location_views_proximity_get_reference_location($view, $options) {
  $coordinates = array();
  switch ($options['origin']) {
    case 'user':
    case 'hybrid':
      global $user;
      $user_locations = isset($user->locations) ? $user->locations : location_load_locations($user->uid, 'uid');

      // This user_location_delta will only possibly be set if we are dealing with the filter.
      $i = isset($options['user_location_delta']) && !empty($options['user_location_delta']) ? $options['user_location_delta'] : 0;
      if (isset($user_locations[$i]['latitude']) || !empty($user_locations[$i]['latitude'])) {
        $coordinates['latitude'] = (double) $user_locations[$i]['latitude'];
        $coordinates['longitude'] = (double) $user_locations[$i]['longitude'];
      }
      else {
        if ($options['origin'] == 'hybrid') {
          $coordinates['latitude'] = (double) $options['latitude'];
          $coordinates['longitude'] = (double) $options['longitude'];
        }
      }
      break;
    case 'static':
    case 'latlon_gmap':
      $coordinates['latitude'] = (double) $options['latitude'];
      $coordinates['longitude'] = (double) $options['longitude'];
      break;
    case 'tied':
      if (!empty($view->filter)) {
        foreach ($view->filter as $filter) {
          if ($filter->table == 'location' && $filter->field == 'distance' && $filter->options['relationship'] == $options['relationship']) {
            $filter_options = array_merge($filter->options, $filter->options['value'], $filter->value);
            if ($coords = location_views_proximity_get_reference_location($view, $filter_options)) {
              $coordinates['latitude'] = (double) $coords['latitude'];
              $coordinates['longitude'] = (double) $coords['longitude'];
            }
          }
        }
      }
      break;
    case 'postal':
    case 'postal_default':

      // Force default for country.
      if ($options['origin'] == 'postal_default') {
        $options['country'] = variable_get('location_default_country', 'us');
      }

      // Zip code lookup.
      if (!empty($options['postal_code']) && !empty($options['country'])) {
        $coords = location_latlon_rough($options);
        if ($coords) {
          $coordinates['latitude'] = (double) $coords['lat'];
          $coordinates['longitude'] = (double) $coords['lon'];
        }
      }
      break;
    case 'php':
      ob_start();
      $result = eval($options['php_code']);
      ob_end_clean();
      if ($result && $result['latitude'] && $result['longitude']) {
        $coordinates['latitude'] = (double) $result['latitude'];
        $coordinates['longitude'] = (double) $result['longitude'];
      }
      break;
    case 'nid_arg':
      if ($nodehandler = $view->display_handler
        ->get_handler('argument', $options['nid_arg'])) {
        $nid = $nodehandler
          ->get_value();
        if ($nid && is_numeric($nid) && ($tempnode = node_load($nid))) {
          $field_name = $options['nid_loc_field'];
          if ($field_name == 'node') {
            $coordinates['latitude'] = (double) $tempnode->location['latitude'];
            $coordinates['longitude'] = (double) $tempnode->location['longitude'];
          }
          else {
            if (isset($tempnode->{$field_name})) {
              $cck_location = $tempnode->{$field_name};
              if (isset($cck_location[LANGUAGE_NONE][0]['longitude']) && isset($cck_location[LANGUAGE_NONE][0]['latitude'])) {
                $coordinates['latitude'] = (double) $cck_location[LANGUAGE_NONE][0]['latitude'];
                $coordinates['longitude'] = (double) $cck_location[LANGUAGE_NONE][0]['longitude'];
              }
              else {
                if (isset($cck_location[0]['longitude']) && isset($cck_location[0]['latitude'])) {
                  $coordinates['latitude'] = (double) $cck_location[0]['latitude'];
                  $coordinates['longitude'] = (double) $cck_location[0]['longitude'];
                }
              }
            }
          }
        }
      }
      break;
    case 'uid_arg':
      if ($userhandler = $view->display_handler
        ->get_handler('argument', $options['uid_arg'])) {
        $uid = $userhandler
          ->get_value();
        if ($uid && is_numeric($uid) && ($tempuser = user_load(array(
          'uid' => $uid,
        )))) {
          $coordinates['latitude'] = (double) $tempuser->location['latitude'];
          $coordinates['longitude'] = (double) $tempuser->location['longitude'];
        }
      }
      break;
    case 'distance_arg':
      foreach ($view->argument as $argument) {
        if ($argument->field == 'distance') {
          list($coords, $search_distance) = explode('_', $view->args[$argument->position]);
          list($lat, $lon) = explode(',', $coords);
          break;
        }
      }
      $coordinates['latitude'] = (double) $lat;
      $coordinates['longitude'] = (double) $lon;
      break;
  }
  return $coordinates;
}