ip_geoloc.install in IP Geolocation Views & Maps 7
Same filename and directory in other branches
Install and uninstall hooks for IP Geolocation Views & Maps.
File
ip_geoloc.installView source
<?php
/**
* @file
* Install and uninstall hooks for IP Geolocation Views & Maps.
*/
/**
* Implments hook_enable().
*/
function ip_geoloc_enable() {
drupal_set_message(t('IP Geolocation Views & Maps is now enabled. Please note that some of the canned Views that come with this module require the "<a target="project_page" href="!views_php">Views PHP</a>" module. The "Set my location" block requires <a target="project_page" href="!geocoder">Geocoder</a>. <br/>Module "<a target="project_page" href="!session_cache">Session Cache API</a>" is optional and recommended.', array(
'!views_php' => url('http://drupal.org/project/views_php'),
'!geocoder' => url('http://drupal.org/project/geocoder'),
'!session_cache' => url('http://drupal.org/project/session_cache'),
)));
}
/**
* Implements hook_requirements().
*
* $phase typically is one of 'install', 'runtime', 'update'.
*/
function ip_geoloc_requirements($phase) {
$requirements = array();
// Avoid 'undefined function: ip_geoloc_get_css_library_path' during 'install'
// and some warnings not relevant while executing an 'update'.
if ($phase != 'runtime') {
return $requirements;
}
$t = get_t();
$geolocation_methods = array();
if (variable_get('ip_geoloc_google_to_reverse_geocode', FALSE)) {
$geolocation_methods[] = $t('Google Maps reverse-geocode');
}
if (variable_get('ip_geoloc_smart_ip_as_backup', FALSE)) {
if (module_exists('smart_ip')) {
$geolocation_methods[] = 'Smart IP';
}
else {
$requirements['ip_geoloc_lbs']['severity'] = REQUIREMENT_WARNING;
$requirements['ip_geoloc_lbs']['description'] = $t('Smart IP is configured as a backup in case the Google Maps reverse-geocode fails. However the Smart IP module is not enabled.');
}
}
elseif (module_exists('geoip')) {
$geolocation_methods[] = 'GeoIP API';
}
$geolocation_methods = array_merge($geolocation_methods, module_implements('get_ip_geolocation_alter'));
$requirements['ip_geoloc_lbs']['title'] = $t('IP Geolocation Views & Maps,<br/>data collection methods employed');
$requirements['ip_geoloc_lbs']['value'] = implode(', ', $geolocation_methods);
if (empty($geolocation_methods)) {
$note = $t('You currently have no data collection methods enabled. Therefore no new address information can be added to the IP geolocation database.');
if (empty($requirements['ip_geoloc']['description'])) {
$requirements['ip_geoloc_lbs']['severity'] = REQUIREMENT_OK;
$requirements['ip_geoloc_lbs']['description'] = $note;
}
else {
$requirements['ip_geoloc_lbs']['description'] .= ' ' . $note;
}
}
$library = drupal_get_library('leaflet_markercluster', 'leaflet_markercluster');
if (isset($library['js'][0]['data'])) {
$requirements['ip_geoloc_markercluster']['title'] = t('IP Geolocation Views & Maps; Leaflet MarkerCluster library');
if (strpos($library['js'][0]['data'], IP_GEOLOC_LEAFLET_MARKERCLUSTER_REGIONBOUND_JS)) {
$requirements['ip_geoloc_markercluster']['value'] = t('Installed with RegionBound extension');
$requirements['ip_geoloc_markercluster']['severity'] = REQUIREMENT_OK;
}
else {
$requirements['ip_geoloc_markercluster']['value'] = t('Using basic version');
$requirements['ip_geoloc_markercluster']['severity'] = REQUIREMENT_INFO;
$requirements['ip_geoloc_markercluster']['description'] = t('Leaflet MarkerCluster installed without region-aware extension. You are not making the most of your clustering. We suggest you obtain the <a href="http://regionbound.com">RegionBound</a> marker cluster .js file and drop it in %directory for superior clustering.', array(
'%directory' => leaflet_markercluster_get_library_path(),
));
}
}
$requirements['ip_geoloc_font_libs']['title'] = $t('IP Geolocation Views & Maps,<br/>font icon libraries');
$requirements['ip_geoloc_font_libs']['severity'] = REQUIREMENT_OK;
$requirements['ip_geoloc_font_libs']['description'] = $t('When using Leaflet you may superimpose font icons on your markers. <a target="fsymbols" href="!url_fsymbols">fsymbols</a> do not require any files to be downloaded. Other font icons like those from <a target="fontello" href="!url_fontello">Fontello</a> do. See the <a target="readme" href="!url_readme">README</a> for more details on font icon repositories.', array(
'!url_fsymbols' => url('http://text-symbols.com'),
'!url_fontello' => url('http://fontello.com'),
'!url_readme' => url(drupal_get_path('module', 'ip_geoloc') . '/README.txt'),
));
$requirements['ip_geoloc_font_libs']['value'] = '';
$num_font_libs = 0;
foreach (ip_geoloc_get_font_icon_libs() as $i => $css_file) {
$css_file = trim($css_file);
if (!empty($css_file)) {
if (file_exists($css_file)) {
$msg = $t('Font icon library #@i found at %css_file', array(
'@i' => $i,
'%css_file' => $css_file,
));
$num_font_libs++;
}
else {
$msg = $t('File %file was entered on the <a target="config" href="!url_config">configuration page</a> but not found.', array(
'%file' => $css_file,
'!url_config' => url('admin/config/system/ip_geoloc'),
));
$requirements['ip_geoloc_font_libs']['severity'] = REQUIREMENT_WARNING;
}
$requirements['ip_geoloc_font_libs']['value'] .= $msg . '<br/>';
}
}
if ($num_font_libs == 0) {
$requirements['ip_geoloc_font_libs']['value'] .= $t('No font icon libraries found. However you can still copy and paste font icons from <a target="fsybmols" href="!url_fsymbols">fsymbols</a>.', array(
'!url_fsymbols' => url('http://text-symbols.com'),
));
}
switch (variable_get('ip_geoloc_auth_method', 0)) {
case 1:
$key_provided = variable_get('ip_geoloc_apikey', FALSE);
break;
case 2:
$key_provided = variable_get('ip_geoloc_client_id', FALSE);
break;
default:
}
if (empty($key_provided)) {
$requirements['ip_geoloc_map'] = array(
'title' => 'IP Geolocation Views & Maps',
'severity' => REQUIREMENT_WARNING,
'value' => t('No Google Maps API Key or Client ID. Keyless access to Google Maps is no longer supported and may impact rendering of maps. Obtain and enter an API Key or Client ID <a href="!url">here</a>.', array(
'!url' => url('admin/config/system/ip_geoloc'),
)),
);
}
return $requirements;
}
/**
* Implements hook_install().
*/
function ip_geoloc_install() {
// Set high weight so our implementation of hook init comes last.
db_update('system')
->fields(array(
'weight' => 10,
))
->condition('name', 'ip_geoloc')
->execute();
}
/**
* Implements hook_uninstall().
*/
function ip_geoloc_uninstall() {
// Delete all ip_geoloc_* variables at once.
db_query("DELETE FROM {variable} WHERE name LIKE 'ip_geoloc_%%'");
}
/**
* Implements hook_schema().
*
* Smart IP module provides:
* IP address
* Latitude/Longitude
* Country
* Country Code
* Region
* Region Code (usually empty for Australia)
* City
* ZIP (which we store under Postal Code)
*
* GeoIP City provides:
* TBA
*
* Google provides:
* TBA
*
* Note: some of the varchar should be varbinary, see drupal.org/node/1793674
*/
function ip_geoloc_schema() {
$schema['ip_geoloc'] = array(
'description' => 'Store visitor IP addresses and geolocation information',
'fields' => array(
'id' => array(
'description' => 'Unique id',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'ip_address' => array(
'description' => 'IP address',
'type' => 'varchar',
// Support IPv6 as well as standard IPv4 addresses, e.g.
// 2001:0db8:0000:0000:0000:ff00:0042:8329
// http://en.wikipedia.org/wiki/IPv6
'length' => '64',
'not null' => TRUE,
),
'latitude' => array(
'description' => 'Latitude',
'type' => 'float',
'size' => 'big',
'not null' => FALSE,
),
'longitude' => array(
'description' => 'Longitude',
'type' => 'float',
'size' => 'big',
'not null' => FALSE,
),
'country' => array(
'description' => 'Country',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
'country_code' => array(
'description' => 'ISO 3166 2-Character Country Code',
'type' => 'varchar',
'length' => 3,
'not null' => FALSE,
),
'region' => array(
'description' => 'Region',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
'region_code' => array(
'description' => '2-Character Region Code',
'type' => 'varchar',
'length' => 3,
'not null' => FALSE,
),
'city' => array(
'description' => 'City',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
'postal_code' => array(
'description' => 'Post code',
'type' => 'varchar',
'length' => 12,
'not null' => FALSE,
),
'locality' => array(
'description' => 'Suburb',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
'route' => array(
'description' => 'Street',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
'street_number' => array(
'description' => 'Street number',
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
),
'administrative_area_level_1' => array(
'description' => 'State or province',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
'formatted_address' => array(
'description' => 'Address',
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
),
),
'primary key' => array(
'id',
),
'indexes' => array(
'ip_address' => array(
'ip_address',
),
),
);
return $schema;
}
/**
* Allow for the storage of ipv6 as well as standard IP addresses.
*/
function ip_geoloc_update_7125() {
db_drop_index('ip_geoloc', 'ip_address');
$spec = array(
'description' => 'IP address',
'type' => 'varchar',
// Support IPv6 as well as standard IPv4 addresses.
'length' => '64',
'not null' => TRUE,
);
$index = array(
'indexes' => array(
'ip_address' => array(
'ip_address',
),
),
);
db_change_field('ip_geoloc', 'ip_address', 'ip_address', $spec, $index);
// db_add_index('ip_geoloc', 'ip_address', array('ip_address'));
return t('ip_address column updated.');
}
Functions
Name | Description |
---|---|
ip_geoloc_enable | Implments hook_enable(). |
ip_geoloc_install | Implements hook_install(). |
ip_geoloc_requirements | Implements hook_requirements(). |
ip_geoloc_schema | Implements hook_schema(). |
ip_geoloc_uninstall | Implements hook_uninstall(). |
ip_geoloc_update_7125 | Allow for the storage of ipv6 as well as standard IP addresses. |