You are here

public function IpGeoLocSubscriber::initIpGeoLoc in IP Geolocation Views & Maps 8

This method is called whenever the KernelEvents::REQUEST event is dispatched.

File

src/EventSubscriber/IpGeoLocSubscriber.php, line 49

Class

IpGeoLocSubscriber
Subscribe to KernelEvents::REQUEST events and redirect if site is currently in maintenance mode.

Namespace

Drupal\ip_geoloc\EventSubscriber

Code

public function initIpGeoLoc(FilterResponseEvent $event) {
  $response = $event
    ->getResponse();
  $currentPath = $this->currentPath
    ->getPath();
  $path_args = explode('/', $currentPath);

  // @ TODO check this views functionality
  foreach ($path_args as $arg) {

    // Only works on Views pages that have a (dummy) contextual filter defined.
    if ($arg == 'erase-location') {
      $location = $this->api
        ->getVisitorLocation();
      if (empty($location['is_updated'])) {

        // Wipe the current visitor location.
        $this->ipGeolocSession
          ->setSessionValue('location', NULL);

        // ... now pretend to everyone it never happened.
        $_GET['q'] = str_replace('/erase-location', '', $_GET['q']);
        return;
      }
    }
  }

  // @TODO review this   $this->geolocGlobal->logErrors();
  $location = $this->api
    ->getVisitorLocation();
  $reverse_geocode_client_timeout = $this->geolocGlobal
    ->reverseGeocodeTimeout();
  if ($reverse_geocode_client_timeout || $this->geolocGlobal
    ->isFirstClick()) {

    // Not convinced this is desirable.
    $this->geolocGlobal
      ->reinitLocation($location, $reverse_geocode_client_timeout);
    $this->api
      ->storeLocation($location);
    $this->ipGeolocSession
      ->setSessionValue('location', $location);
  }
}