You are here

function getlocations_fields_views_proximity_get_reference_location in Get Locations 7

Same name and namespace in other branches
  1. 7.2 modules/getlocations_fields/views/getlocations_fields.views.inc \getlocations_fields_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 getlocations_fields_views_proximity_get_reference_location()
getlocations_fields_handler_field_distance::click_sort in modules/getlocations_fields/handlers/getlocations_fields_handler_field_distance.inc
Called to determine what to tell the clicksorter.
getlocations_fields_handler_field_distance::query in modules/getlocations_fields/handlers/getlocations_fields_handler_field_distance.inc
Called to add the field to a query.
getlocations_fields_handler_filter_distance::query in modules/getlocations_fields/handlers/getlocations_fields_handler_filter_distance.inc
Add this filter to the query.
getlocations_fields_handler_sort_distance::query in modules/getlocations_fields/handlers/getlocations_fields_handler_sort_distance.inc
Called to add the sort to a query.

File

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

Code

function getlocations_fields_views_proximity_get_reference_location($view, $options) {
  $coordinates = array();
  switch ($options['origin']) {
    case 'user':
    case 'hybrid':
      global $user;
      $user_locations = getlocations_load_locations($user->uid, 'uid');
      if (isset($user_locations[0]['latitude']) || !empty($user_locations[0]['latitude'])) {
        $coordinates['latitude'] = (double) $user_locations[0]['latitude'];
        $coordinates['longitude'] = (double) $user_locations[0]['longitude'];
      }
      elseif ($options['origin'] == 'hybrid') {
        $coordinates['latitude'] = isset($options['latitude']) ? (double) $options['latitude'] : 0;
        $coordinates['longitude'] = isset($options['longitude']) ? (double) $options['longitude'] : 0;
      }
      break;
    case 'static':
      $coordinates['latitude'] = isset($options['latitude']) ? (double) $options['latitude'] : 0;
      $coordinates['longitude'] = isset($options['longitude']) ? (double) $options['longitude'] : 0;
      break;
    case 'search':
      $coordinates['latitude'] = isset($options['latitude']) ? (double) $options['latitude'] : 0;
      $coordinates['longitude'] = isset($options['longitude']) ? (double) $options['longitude'] : 0;
      break;
    case 'php':
      ob_start();
      $result = eval($options['php_code']);
      ob_end_clean();
      if ($result && isset($result['latitude']) && $result['latitude'] && isset($result['longitude']) && $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)) {
          $tempobj = node_load($nid);
          $field_name = $options['nid_loc_field'];
        }
      }
      break;
    case 'uid_arg':
      if ($userhandler = $view->display_handler
        ->get_handler('argument', $options['uid_arg'])) {
        $uid = $userhandler
          ->get_value();
        if ($uid && is_numeric($uid)) {
          $tempobj = user_load($uid);
          $field_name = $options['uid_loc_field'];
        }
      }
      break;
    case 'tid_arg':
      if ($termhandler = $view->display_handler
        ->get_handler('argument', $options['tid_arg'])) {
        $tid = $termhandler
          ->get_value();
        if ($tid && is_numeric($tid)) {
          $tempobj = taxonomy_term_load($tid);
          $field_name = $options['tid_loc_field'];
        }
      }
      break;
    case 'cid_arg':

      // not tested
      if ($commenthandler = $view->display_handler
        ->get_handler('argument', $options['cid_arg'])) {
        $cid = $commenthandler
          ->get_value();
        if ($cid && is_numeric($cid)) {
          $tempobj = comment_load($cid);
          $field_name = $options['cid_loc_field'];
        }
      }
      break;

    // getlocations_gps
    case 'gps':
      if (module_exists('getlocations_gps')) {
        $coordinates = getlocations_gps_coords();
      }
      break;

    // used by sort when argument is lat/lon
    case 'distance_arg':
      $coordinates = array();
      foreach ($view->argument as $argument) {
        if (isset($argument->field) && $argument->field == 'distance') {
          $a = explode('_', $view->args[$argument->position]);
          if (count($a) == 2) {
            $aa = explode(',', $a[0]);
            if (count($aa) == 2 && is_numeric($aa[0]) && is_numeric($aa[1])) {
              $lat = $aa[0];
              $lon = $aa[1];
            }
          }
          break;
        }
      }
      if (isset($lat)) {
        $coordinates['latitude'] = (double) $lat;
        $coordinates['longitude'] = (double) $lon;
      }
      break;
  }

  // end switch
  if (isset($tempobj) && $tempobj && isset($field_name) && $field_name) {
    if (isset($tempobj->{$field_name})) {
      if (!isset($tempobj->language)) {
        $lang = 'und';
      }
      else {
        $lang = $tempobj->language;
        if (!isset($tempobj->{$field_name}[$lang])) {
          $lang = 'und';
        }
      }
      $location = $tempobj->{$field_name};
      if (isset($location[$lang][0]['latitude']) && isset($location[$lang][0]['longitude'])) {
        $coordinates['latitude'] = (double) $location[$lang][0]['latitude'];
        $coordinates['longitude'] = (double) $location[$lang][0]['longitude'];
      }
    }
  }
  return $coordinates;
}