You are here

function device_geolocation_get_coordinates in Smart IP 6.2

Same name and namespace in other branches
  1. 6 modules/device_geolocation/device_geolocation.module \device_geolocation_get_coordinates()
  2. 7.2 modules/device_geolocation/device_geolocation.module \device_geolocation_get_coordinates()
  3. 7 modules/device_geolocation/device_geolocation.module \device_geolocation_get_coordinates()

Get Visitor's coordinates.

1 call to device_geolocation_get_coordinates()
device_geolocation_init in modules/device_geolocation/device_geolocation.module
Implements hook_init().

File

modules/device_geolocation/device_geolocation.module, line 278
Provides visitor's geographical location using client device location source that implements W3C Geolocation API and Google Geocoding service.

Code

function device_geolocation_get_coordinates() {
  global $user;
  $latitude = 0;
  $longitude = 0;
  $user_data = unserialize($user->data);
  $smart_ip_session = smart_ip_session_get('smart_ip');
  $is_allowed_page = smart_ip_check_allowed_page();

  // Smart IP user $_SESSION
  if (!is_null($smart_ip_session) && !empty($smart_ip_session['location']['latitude'])) {
    $latitude = $smart_ip_session['location']['latitude'];
    $longitude = $smart_ip_session['location']['longitude'];
  }
  elseif (isset($user_data['geoip_location']) && !empty($user_data['geoip_location']['latitude'])) {
    $smart_ip_session['location'] = $user_data['geoip_location'];
    $latitude = $smart_ip_session['location']['latitude'];
    $longitude = $smart_ip_session['location']['longitude'];
    if ($is_allowed_page) {
      smart_ip_session_set('smart_ip', $smart_ip_session);
    }
  }
  else {
    $smart_ip_session['location'] = smart_ip_get_current_visitor_location_data();
    if ($is_allowed_page) {
      smart_ip_session_set('smart_ip', $smart_ip_session);
    }
    if ($user->uid) {
      $user_obj = user_load($user->uid);
      user_save($user_obj, array(
        'geoip_location' => $smart_ip_session['location'],
      ));
    }
    if (!empty($smart_ip_session['location']['latitude'])) {
      $latitude = $smart_ip_session['location']['latitude'];
      $longitude = $smart_ip_session['location']['longitude'];
    }
    elseif ($is_allowed_page) {
      smart_ip_session_set('smart_ip', NULL);
    }
  }
  $debug_mode = is_user_debug_mode();

  //$debug_mode = FALSE;
  $coordinates = array(
    'latitude' => $latitude,
    'longitude' => $longitude,
    'debug_mode' => $debug_mode,
  );

  // Send also to javascript
  drupal_add_js(array(
    'device_geolocation' => $coordinates,
  ), 'setting');
  return $coordinates;
}