You are here

public static function SmartIp::updateFields in Smart IP 8.4

Same name and namespace in other branches
  1. 8.3 src/SmartIp.php \Drupal\smart_ip\SmartIp::updateFields()

Update the values of location fields.

Parameters

array $fields: Location fields.

2 calls to SmartIp::updateFields()
SmartIp::query in src/SmartIp.php
Get the geolocation from the IP address.
SmartIpLocation::save in src/SmartIpLocation.php
Saves the Smart IP location data to user data and session (for anonymous, saves to session only).

File

src/SmartIp.php, line 74
Contains \Drupal\smart_ip\SmartIp.

Class

SmartIp
Smart IP static basic methods wrapper.

Namespace

Drupal\smart_ip

Code

public static function updateFields(&$fields) {
  $coordinatesExist = isset($fields['latitude']) && isset($fields['longitude']);
  if ($coordinatesExist) {

    // 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['countryCode']) && (!isset($fields['isEuCountry']) || isset($fields['isEuCountry']) && empty($fields['isEuCountry']))) {
    $euCountry = smart_ip_is_eu_gdpr_country($fields['countryCode']);
    $fields['isEuCountry'] = !empty($euCountry);
    if ($fields['isEuCountry']) {
      $fields['isGdprCountry'] = TRUE;
    }
  }

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

  // Update the format of time zone field according to user's preference.
  $config = \Drupal::config('smart_ip.settings');
  $tzFormat = $config
    ->get('timezone_format');
  $geotimezoneExists = \Drupal::moduleHandler()
    ->moduleExists('geotimezone');
  if ($geotimezoneExists && empty($fields['timeZone']) && ($coordinatesExist || isset($fields['countryCode']))) {

    // Time zone is empty, get value from Geo Time Zone.
    $timeZone = geotimezone_query($fields, $tzFormat);
    if (is_array($timeZone)) {
      $fields['timeZone'] = !empty($timeZone) ? Tags::implode($timeZone) : '';
    }
    else {
      $fields['timeZone'] = $timeZone;
    }
  }
  elseif (!empty($fields['timeZone'])) {
    $a = strpos($fields['timeZone'], '+') === 0;
    $b = strpos($fields['timeZone'], '-') === 0;
    if ($geotimezoneExists && $tzFormat == 'identifier' && (strpos($fields['timeZone'], '+') === 0 || strpos($fields['timeZone'], '-') === 0) && ($coordinatesExist || isset($fields['countryCode']))) {

      // Convert to time zone identifier.
      $timeZone = geotimezone_query($fields, $tzFormat);
      if (is_array($timeZone)) {
        $fields['timeZone'] = !empty($timeZone) ? Tags::implode($timeZone) : '';
      }
      else {
        $fields['timeZone'] = $timeZone;
      }
    }
    elseif ($tzFormat == 'offset' && strpos($fields['timeZone'], '+') === FALSE) {

      // Convert to time zone offset.
      $time = new \DateTime('now', new \DateTimeZone($fields['timeZone']));
      $fields['timeZone'] = $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');
      }
    }
  }
}