function ip_geoloc_store_location in IP Geolocation Views & Maps 7
Same name and namespace in other branches
- 8 ip_geoloc_api.inc \ip_geoloc_store_location()
Store the supplied IP geolocation info on the database.
This will overwrite any existing info for the IP address in question.
Parameters
array $location: array with up to 13 location info fields; must at least contain a non-empty $location['ip_address'] and a non-empty $location['formatted_address'] for anything to happen
Return value
int|bool 0, when no insert or update was necessary SAVED_NEW (=1), when a new location record was inserted into the db SAVED_UPDATED (=2), when an existing location record was updated FALSE, when a db insert or db update failed
4 calls to ip_geoloc_store_location()
- ip_geoloc_current_location_ajax_recipient in ./
ip_geoloc.module - Data recipient for javascript function getLocation().
- ip_geoloc_get_location_by_ip in ./
ip_geoloc_api.inc - Returns the location details associated with the supplied IP address.
- ip_geoloc_init in ./
ip_geoloc.module - Implements hook_init().
- _ip_geoloc_process_access_log in ./
ip_geoloc.admin.inc - Callback for ip_geoloc_sync_with_accesslog batch process.
File
- ./
ip_geoloc_api.inc, line 35 - API functions of IP geolocation module
Code
function ip_geoloc_store_location($location) {
// Give contributed modules a chance to add their 2 cents by implementing
// hook_get_ip_geolocation_alter()
drupal_alter('get_ip_geolocation', $location);
if (!variable_get('ip_geoloc_store_addresses', TRUE)) {
return;
}
if (empty($location['ip_address']) || empty($location['formatted_address'])) {
// ip_geoloc_debug('IPGV&M: location object must contain both IP address
// and formatted address -- not stored.');
return 0;
}
if ($location['ip_address'] != '127.0.0.1' && (!isset($location['latitude']) || !isset($location['latitude']))) {
watchdog('IPGV&M', 'latitude or longitude missing for IP address %ip (location still stored)', array(
'%ip' => $location['ip_address'],
), WATCHDOG_WARNING);
}
// See if this IP is already on the db.
$result = db_query('SELECT * FROM {ip_geoloc} WHERE ip_address = :ip', array(
':ip' => $location['ip_address'],
));
$existing_location = $result
->fetchAssoc();
if (!$existing_location) {
// New entry, insert.
$location['city'] = utf8_encode($location['city']);
$location['formatted_address'] = utf8_encode($location['formatted_address']);
ip_geoloc_debug(t('IP Geolocaton: adding new record to db: !location', array(
'!location' => ip_geoloc_pretty_print($location),
)));
$full_location =& $location;
}
else {
// When updating, drupal_write_record() does not erase fields not present
// in $location.
$empty_location['latitude'] = '';
$empty_location['longitude'] = '';
$empty_location['country'] = '';
$empty_location['country_code'] = '';
$empty_location['region'] = '';
$empty_location['region_code'] = '';
$empty_location['city'] = '';
$empty_location['locality'] = '';
$empty_location['route'] = '';
$empty_location['street_number'] = '';
$empty_location['postal_code'] = '';
$empty_location['administrative_area_level_1'] = '';
$empty_location['formatted_address'] = '';
$location['id'] = $existing_location['id'];
$full_location = array_merge($empty_location, $location);
ip_geoloc_debug(t('IPGV&M: updating db with above location'));
}
try {
$result = drupal_write_record('ip_geoloc', $full_location, $existing_location ? array(
'id',
) : array());
} catch (PDOException $e) {
// May happen when a fields contains illegal characters.
drupal_set_message(check_plain($e
->getMessage()), 'error');
$result = FALSE;
}
if ($result === FALSE) {
drupal_set_message(t('IPGV&M: could not save location to db: !location', array(
'!location' => ip_geoloc_pretty_print($full_location),
)), 'error');
}
return $result;
}