Skip to content

Instantly share code, notes, and snippets.

@drewfranz
Created September 17, 2019 23:59
Show Gist options
  • Save drewfranz/06392c602e1b334f792dfb79af399562 to your computer and use it in GitHub Desktop.
Save drewfranz/06392c602e1b334f792dfb79af399562 to your computer and use it in GitHub Desktop.
Drupal 7 Geocoder handler plugin file for adding custom integration with the Geocod.io geolocation API
<?php
/**
* @file
* Plugin to provide a Geocod.io geocoder.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t('Geocod.io'),
'description' => t('Geocodes via Geocod.io'),
'callback' => 'geocoder_geocodio',
'field_types' => array('text', 'text_long', 'addressfield', 'location', 'text_with_summary', 'computed', 'taxonomy_term_reference'),
'field_callback' => 'geocoder_geocodio_field',
'terms_of_service' => 'https://www.geocod.io/terms-of-use/',
);
/**
* Process Markup
*/
function geocoder_geocodio($address, $options = array()) {
// Check if there are 0, 1, or multiple day values.
if (_geocoder_geocodio_check_day_distinct_count()) {
// If only 1 value, check that today is the same day.
if (_geocoder_geocodio_check_day_value()) {
// It's the same day, so now get the count.
$count = _geocoder_geocodio_check_current_count();
// If we have hit the API call limit for today, do not continue..
if ($count >= variable_get('geocodio_api_daily_limit', '1500')) {
return;
}
}
}
$api_url = 'https://api.geocod.io/v1.3/geocode';
$address_str = str_replace(',', '+', $address);
$address_str = str_replace(' ', '+', $address_str);
$api_key = variable_get('geocodio_api_key', '');
if ($api_key == '') {
watchdog('Geocodio', 'No Valid API Key Configured');
return;
}
$params = array(
'q' => $address_str,
'api_key' => $api_key,
'format' => 'json',
);
$url = $api_url . '?' . urldecode(drupal_http_build_query($params));
$request = drupal_http_request($url);
$data = json_decode($request->data);
_geocoder_geocodio_increment_counter();
return _geocoder_geocodio_geometry($data);
}
function _geocoder_geocodio_check_day_distinct_count() {
$day_count_query = db_query("SELECT count(DISTINCT day) FROM geocodio");
$day_count_result = $day_count_query->fetchCol();
// We're checking if the day column values are all the same.
if (!empty($day_count_result)) {
if ($day_count_result[0] == 1) {
// The rows are all the same date, so next step is to count them.
return TRUE;
}
// There are multiple dates
// We have flipped into the next day. So reset the table and begin counting again.
$truncate_query = db_query("TRUNCATE TABLE geocodio");
return FALSE;
}
// No data in the table, so continue counting.
return FALSE;
}
function _geocoder_geocodio_check_day_value() {
$today = date('Y-m-d 00:00:00');
$day_query = db_query("SELECT DISTINCT day FROM geocodio");
$day_result = $day_query->fetchCol();
// We're checking to see if the day in the table is the same as today.
// If not, we can reset the table and begin counting again.
if (!empty($day_result) && $day_result[0] != $today) {
$truncate_query = db_query("TRUNCATE TABLE geocodio");
return FALSE;
}
return TRUE;
}
function _geocoder_geocodio_check_current_count() {
$query = db_query("SELECT SUM(count) FROM geocodio");
$result = $query->fetchCol();
// We are checking to see if we are still under the API call limit.
if (empty($result)) {
return 0;
} else {
return $result[0];
}
}
function _geocoder_geocodio_increment_counter() {
$query = db_query("INSERT INTO geocodio(cid, count, day) VALUES(RAND() * 100, 1, CURRENT_DATE) ON DUPLICATE KEY UPDATE count = count + 1");
}
function geocoder_geocodio_field($field, $field_item) {
if ($field['type'] == 'text' || $field['type'] == 'text_long' || $field['type'] == 'text_with_summary' || $field['type'] == 'computed') {
return geocoder_geocodio($field_item['value']);
}
if ($field['type'] == 'addressfield') {
$address = geocoder_widget_parse_addressfield($field_item);
return geocoder_geocodio($address);
}
if ($field['type'] == 'location') {
$address = geocoder_widget_parse_locationfield($field_item);
return geocoder_geocodio($address);
}
if ($field['type'] == 'taxonomy_term_reference') {
$term = taxonomy_term_load($field_item['tid']);
return geocoder_geocodio($term->name);
}
}
function _geocoder_geocodio_geometry(&$data) {
if (!isset($data->results[0]->location->lat)) {
return NULL;
}
geophp_load();
return new Point($data->results[0]->location->lng, $data->results[0]->location->lat);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment