You are here

function device_geolocation_detector_ajax in Smart IP 6

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

Google Geocoding ajax callback function data recipient.

1 string reference to 'device_geolocation_detector_ajax'
device_geolocation_menu in modules/device_geolocation/device_geolocation.module
Implements hook_menu().

File

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

Code

function device_geolocation_detector_ajax() {
  if (isset($_POST['latitude']) && isset($_POST['longitude'])) {
    global $user;
    $smart_ip_session = smart_ip_session_get('smart_ip');
    $ip_address = $smart_ip_session['location']['ip_address'];
    $smart_ip_session['maxmind_location'] = $smart_ip_session['location'];
    smart_ip_session_set('device_geolocation', NULL);
    unset($smart_ip_session['location']);
    $smart_ip_session['location']['ip_address'] = $ip_address;
    $smart_ip_session['location']['country'] = '';
    $smart_ip_session['location']['country_code'] = '';
    $smart_ip_session['location']['region'] = '';
    $smart_ip_session['location']['region_code'] = '';
    $smart_ip_session['location']['city'] = '';
    $smart_ip_session['location']['zip'] = '';
    $smart_ip_session['location']['latitude'] = '';
    $smart_ip_session['location']['longitude'] = '';
    $smart_ip_session['location']['time_zone'] = '';
    foreach ($_POST as $label => $address) {
      if (!empty($address)) {
        $label = check_plain($label);
        $smart_ip_session['location'][$label] = check_plain($address);
        smart_ip_session_set('device_geolocation', TRUE);
        smart_ip_session_set('device_geolocation_attempt', NULL);
      }
    }
    if (empty($smart_ip_session['location']['zip']) && isset($smart_ip_session['location']['postal_code'])) {
      $smart_ip_session['location']['zip'] = $smart_ip_session['location']['postal_code'];
    }
    if (empty($smart_ip_session['location']['city']) && isset($smart_ip_session['location']['locality'])) {
      $smart_ip_session['location']['city'] = $smart_ip_session['location']['locality'];
    }
    $smart_ip_session['location']['timestamp'] = time();

    // Allow other modules to modify result via hook_device_geolocation_detector_ajax_alter()
    drupal_alter('device_geolocation_detector_ajax', $smart_ip_session['location']);
    if (isset($smart_ip_session['location']) && $user->uid) {
      $user_obj = user_load($user->uid);
      user_save($user_obj, array(
        'geoip_location' => $smart_ip_session['location'],
      ));
    }
    smart_ip_session_set('smart_ip', $smart_ip_session);
  }
}