You are here

function smart_ip_update_fields in Smart IP 7.2

Same name and namespace in other branches
  1. 6.2 smart_ip.module \smart_ip_update_fields()

Update the values of location fields.

Parameters

array $fields: Location fields.

3 calls to smart_ip_update_fields()
smart_ip_get_location in ./smart_ip.module
Get the geo location from the IP address
smart_ip_session_set in ./smart_ip.module
Write session variable.
smart_ip_set_user_data in ./smart_ip.module
Set the $user data

File

./smart_ip.module, line 927
Determines country, geo location (longitude/latitude), region, city and postal code of the user, based on IP address

Code

function smart_ip_update_fields(&$fields) {
  $coordinates_exist = isset($fields['latitude']) && isset($fields['longitude']);
  if ($coordinates_exist) {

    // If coordinates are (0, 0) there was no match
    if ($fields['latitude'] === 0 && $fields['longitude'] === 0) {
      $fields['latitude'] = NULL;
      $fields['longitude'] = NULL;
    }
  }

  // Determine if EU member country.
  if (isset($fields['country_code']) && (!isset($fields['is_eu_country']) || isset($fields['is_eu_country']) && empty($fields['is_eu_country']))) {
    $eu_country = smart_ip_is_eu_gdpr_country($fields['country_code']);
    $fields['is_eu_country'] = !empty($eu_country);
    if ($fields['is_eu_country']) {
      $fields['is_gdpr_country'] = TRUE;
    }
  }

  // Determine if GDPR country.
  if (isset($fields['country_code']) && (!isset($fields['is_gdpr_country']) || isset($fields['is_gdpr_country']) && empty($fields['is_gdpr_country']))) {
    $gdpr_country = smart_ip_is_eu_gdpr_country($fields['country_code'], FALSE);
    $fields['is_gdpr_country'] = !empty($gdpr_country);
  }

  // Update the format of time zone field according to user's preference.
  $tz_format = variable_get('smart_ip_timezone_format', 'identifier');
  $geotimezone_exists = module_exists('geotimezone');
  if ($geotimezone_exists && empty($fields['time_zone']) && ($coordinates_exist || isset($fields['country_code']))) {

    // Time zone is empty, get value from Geo Time Zone.
    $location = $fields;
    $location['countryCode'] = $fields['country_code'];
    $location['regionCode'] = $fields['region_code'];
    $time_zone = geotimezone_query($location, $tz_format);
    if (is_array($time_zone)) {
      $fields['time_zone'] = !empty($time_zone) ? implode(', ', $time_zone) : '';
    }
    else {
      $fields['time_zone'] = $time_zone;
    }
  }
  elseif (!empty($fields['time_zone'])) {
    if ($geotimezone_exists && $tz_format == 'identifier' && (strpos($fields['time_zone'], '+') === 0 || strpos($fields['time_zone'], '-') === 0) && ($coordinates_exist || isset($fields['country_code']))) {

      // Convert to time zone identifier.
      $location = $fields;
      $location['countryCode'] = $fields['country_code'];
      $location['regionCode'] = $fields['region_code'];
      $tz_format = geotimezone_query($location, $tz_format);
      if (is_array($tz_format)) {
        $fields['time_zone'] = !empty($tz_format) ? implode(', ', $tz_format) : '';
      }
      else {
        $fields['time_zone'] = $tz_format;
      }
    }
    elseif ($tz_format == 'offset' && strpos($fields['time_zone'], '+') === FALSE) {

      // Convert to time zone offset.
      $time = new \DateTime('now', new \DateTimeZone($fields['time_zone']));
      $fields['time_zone'] = $time
        ->format('P');
    }
  }

  // Make sure external data in UTF-8.
  if (!empty($fields)) {
    foreach ($fields as &$item) {
      if (is_string($item) && !mb_detect_encoding($item, 'UTF-8', TRUE)) {
        $item = mb_convert_encoding($item, 'UTF-8', 'ISO-8859-1');
      }
    }
  }
}