You are here

function _ip_geoloc_check_location in IP Geolocation Views & Maps 8

Same name and namespace in other branches
  1. 7 ip_geoloc.module \_ip_geoloc_check_location()

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

./ip_geoloc.module, line 413
IPGV&M is a mapping engine for Views that contain locations of entities and/or visitors. Google Maps, Leaflet and OpenLayers2 maps are all supported and available through this module. Using a number of optional sources IPGV&M also retrieves…

Code

function _ip_geoloc_check_location(array $location = NULL) {
  if (!\Drupal::state()
    ->get('ip_geoloc_google_to_reverse_geocode', FALSE)) {
    return FALSE;
  }
  $path_alias = drupal_get_path_alias();
  $include_pages = \Drupal::state()
    ->get('ip_geoloc_include_pages', '*');
  if (!drupal_match_path($path_alias, $include_pages)) {
    return FALSE;
  }
  $exclude_pages = \Drupal::state()
    ->get('ip_geoloc_exclude_pages', IP_GEOLOC_DEFAULT_PAGE_EXCLUSIONS);
  if (drupal_match_path($path_alias, $exclude_pages)) {
    return FALSE;
  }
  global $user;
  $roles_to_reverse_geocode = \Drupal::state()
    ->get('ip_geoloc_roles_to_reverse_geocode', [
    DRUPAL_ANONYMOUS_RID,
    DRUPAL_AUTHENTICATED_RID,
  ]);
  $roles_applicable = array_intersect($roles_to_reverse_geocode, array_keys($user->roles));
  if (empty($roles_applicable)) {
    return FALSE;
  }
  $interval = (int) \Drupal::state()
    ->get('ip_geoloc_location_check_interval', IP_GEOLOC_LOCATION_CHECK_INTERVAL);
  if ($interval == 0) {
    return !isset($location['latitude']);
  }
  $last_position_check = _ip_geoloc_get_session_value('last_position_check');
  if (isset($last_position_check)) {
    $time_elapsed = time() - $last_position_check;
    if ($time_elapsed < $interval) {
      ip_geoloc_debug(t('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;
}