You are here

public function DeviceGeolocationController::saveLocation in Smart IP 8.4

Same name and namespace in other branches
  1. 8.3 modules/device_geolocation/src/Controller/DeviceGeolocationController.php \Drupal\device_geolocation\Controller\DeviceGeolocationController::saveLocation()

Google Geocoding ajax callback function data handler.

1 string reference to 'DeviceGeolocationController::saveLocation'
device_geolocation.routing.yml in modules/device_geolocation/device_geolocation.routing.yml
modules/device_geolocation/device_geolocation.routing.yml

File

modules/device_geolocation/src/Controller/DeviceGeolocationController.php, line 28
Contains \Drupal\device_geolocation\Controller\DeviceGeolocationController.

Class

DeviceGeolocationController
Ajax callback handler for Device Geolocation module.

Namespace

Drupal\device_geolocation\Controller

Code

public function saveLocation(Request $request) {

  // Check if the user permitted to share location.
  $shareLocation = SmartIp::getSession('smart_ip_user_share_location_permitted', TRUE);
  if ($shareLocation) {

    // Save only if user has permission to share location.
    $post = $request->request
      ->all();
    if (isset($post['latitude']) && isset($post['longitude'])) {
      $data = [];
      SmartIp::setSession('device_geolocation', NULL);
      foreach ($post as $label => $address) {
        if (!empty($address)) {
          $label = Html::escape($label);
          $data[$label] = Html::escape($address);
          SmartIp::setSession('device_geolocation', TRUE);
        }
      }
      if (!empty($data)) {

        /** @var \Drupal\smart_ip\GetLocationEvent $event */
        $event = \Drupal::service('smart_ip.get_location_event');
        $location = $event
          ->getLocation();
        $ipAddress = $location
          ->get('ipAddress');
        $ipVersion = $location
          ->get('ipVersion');
        $location
          ->delete()
          ->setData($data)
          ->set('originalData', $data)
          ->set('ipAddress', $ipAddress)
          ->set('ipVersion', $ipVersion)
          ->set('timestamp', \Drupal::time()
          ->getRequestTime());

        // Allow other modules to modify the acquired location from client
        // side via Symfony Event Dispatcher.
        \Drupal::service('event_dispatcher')
          ->dispatch(SmartIpEvents::DATA_ACQUIRED, $event);
        $location
          ->save();
      }
    }
  }
  return new JsonResponse();
}