You are here

function _ip_geoloc_get_location_field_value in IP Geolocation Views & Maps 7

Same name and namespace in other branches
  1. 8 plugins/ip_geoloc.statistics.inc \_ip_geoloc_get_location_field_value()

Return the value of an ip_geoloc key, for the target database field name.

Parameters

string $db_field_name: The name of the database field for which to return data.

array $location: The location array.

Return value

string|bool|int|float The value of the ip_geoloc key required.

2 calls to _ip_geoloc_get_location_field_value()
_ip_geoloc_get_field_value in plugins/ip_geoloc.statistics.inc
Better Statistics field callback for IP Geoloc.
_ip_geoloc_statistics_backfill in plugins/ip_geoloc.statistics.inc
Backfills position information to past {accesslog} entries.

File

plugins/ip_geoloc.statistics.inc, line 363
Capture IP Geoloc statistics in the access log.

Code

function _ip_geoloc_get_location_field_value($db_field_name, $location) {

  // Sanity check, is the required database field mapped to a ip_geoloc
  // array key?
  if (!($ip_geoloc_item = _ip_geoloc_key_map($db_field_name))) {
    return NULL;
  }
  list($ip_geoloc_key, $type, $size) = $ip_geoloc_item;

  // Is there data for this key. If not, check viable alternatives,
  // or return NULL as last resort.
  if (!isset($location[$ip_geoloc_key]) || empty($location[$ip_geoloc_key])) {
    switch ($db_field_name) {
      case 'geoloc_region':
        return _ip_geoloc_get_location_field_value('geoloc_admin_area_level_1', $location);
      case 'geoloc_city':
        return _ip_geoloc_get_location_field_value('geoloc_locality', $location);
      default:
        return NULL;
    }
  }

  // Return data, ensuring right type and size.
  switch ($type) {
    case 'string':
      if ($size) {
        return drupal_substr($location[$ip_geoloc_key], 0, $size);
      }
      else {
        return (string) $location[$ip_geoloc_key];
      }
    case 'boolean':
      return empty($location[$ip_geoloc_key]) ? 0 : 1;
    case 'float':
    case 'int':
      settype($location[$ip_geoloc_key], $type);
      return $location[$ip_geoloc_key];
    default:
      return NULL;
  }
}