You are here

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

Return whether a the visitor's location is due for an update.

Updates are only performed on selected configured pages. An update is due when more than a configurable number of seconds have elapsed. If that number is set to zero, then the user's location will be requested until at least the location's country is known, which is normally immediately at the start of the session.

Parameters

array $location: Array of location components.

Return value

bool TRUE if an update is due.

File

src/Services/IpGeoLocGlobal.php, line 172

Class

IpGeoLocGlobal
Class IpGeoLocGlobal.

Namespace

Drupal\ip_geoloc\Services

Code

public function checkLocation(array $location = NULL) {
  if (!$this->config
    ->get('ip_geoloc_google_to_reverse_geocode') ? $this->config
    ->get('ip_geoloc_google_to_reverse_geocode') : FALSE) {
    return FALSE;
  }
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $path_alias = \Drupal::service('path.alias_manager')
    ->getAliasByPath($current_path);
  $path_matcher = \Drupal::service('path.matcher');
  $include_pages = $this->config
    ->get('ip_geoloc_include_pages') ? $this->config
    ->get('ip_geoloc_include_pages') : '*';
  if (!$path_matcher
    ->matchPath($path_alias, $include_pages)) {
    return FALSE;
  }
  $exclude_pages = $this->config
    ->get('ip_geoloc_exclude_pages') ? $this->config
    ->get('ip_geoloc_exclude_pages') : '*';
  if ($path_matcher
    ->matchPath($path_alias, $exclude_pages)) {
    return FALSE;
  }
  $roles_to_reverse_geocode = $this->config
    ->get('ip_geoloc_roles_to_reverse_geocode') ? $this->config
    ->get('ip_geoloc_roles_to_reverse_geocode') : [
    DRUPAL_ANONYMOUS_RID,
    DRUPAL_AUTHENTICATED_RID,
  ];
  $roles_applicable = array_intersect($roles_to_reverse_geocode, array_keys(user_role_names()));
  if (empty($roles_applicable)) {
    return FALSE;
  }
  $interval = (int) $this->config
    ->get('ip_geoloc_location_check_interval') ? $this->config
    ->get('ip_geoloc_location_check_interval') : IP_GEOLOC_LOCATION_CHECK_INTERVAL;
  if ($interval == 0) {
    return !isset($location['latitude']);
  }
  $last_position_check = $this->ipGeolocSession
    ->getSessionValue('last_position_check');
  if (isset($last_position_check)) {
    $time_elapsed = time() - $last_position_check;
    if ($time_elapsed < $interval) {
      $this
        ->debug($this->stringTranslation
        ->translate('IPGV&M: next update scheduled for first click after %seconds seconds (unless overridden or on excluded page).', [
        '%seconds' => $interval - $time_elapsed,
      ]));
      return FALSE;
    }
  }
  return TRUE;
}