You are here

public function IpGeoLocGlobal::useSmartIpIfEnabled in IP Geolocation Views & Maps 8

Use Smart IP (if enabled) to retrieve lat/long and address info.

Note that smart_ip_get_location() will invoke hook_smart_ip_get_location_alter($location), which we use to format the address.

Parameters

array $location: If $location['ip_address'] isn't filled out the current user's IP address will be used.

Return value

bool TRUE upon success, FALSE otherwise.

1 call to IpGeoLocGlobal::useSmartIpIfEnabled()
IpGeoLocGlobal::reinitLocation in src/Services/IpGeoLocGlobal.php
Reinitialises the supplied location array.

File

src/Services/IpGeoLocGlobal.php, line 102

Class

IpGeoLocGlobal
Class IpGeoLocGlobal.

Namespace

Drupal\ip_geoloc\Services

Code

public function useSmartIpIfEnabled(array &$location) {
  $config = \Drupal::config('ip_geoloc.settings');
  $smart_ip = $this->config
    ->get('ip_geoloc_smart_ip_as_backup') ? $this->config
    ->get('ip_geoloc_smart_ip_as_backup') : FALSE;
  if ($smart_ip) {
    if (function_exists('smart_ip_get_location')) {
      if (empty($location['ip_address'])) {
        $location['ip_address'] = ip_address();
      }
      $fixed_address = isset($location['fixed_address']) ? $location['fixed_address'] : 0;
      $region = isset($location['region']) ? $location['region'] : 0;

      // See also: ip_geoloc_smart_ip_get_location_alter().
      $location = [
        'provider' => 'smart_ip',
        'fixed_address' => $fixed_address,
        'region' => $region,
      ] + smart_ip_get_location($location['ip_address']);
      return TRUE;
    }
    $this
      ->debug($this->stringTranslation
      ->translate('IPGV&M: Smart IP configured as a backup, but is not enabled.'));
  }

  // $location['formatted_address'] = '';.
  return FALSE;
}