DeviceGeolocationController.php in Smart IP 8.3
File
modules/device_geolocation/src/Controller/DeviceGeolocationController.php
View source
<?php
namespace Drupal\device_geolocation\Controller;
use Drupal\device_geolocation\DeviceGeolocation;
use Drupal\smart_ip\SmartIp;
use Drupal\smart_ip\SmartIpEvents;
use Drupal\Component\Utility\Html;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
class DeviceGeolocationController extends ControllerBase {
public function saveLocation(Request $request) {
$shareLocation = SmartIp::getSession('smart_ip_user_share_location_permitted', TRUE);
if ($shareLocation) {
$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)) {
$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());
\Drupal::service('event_dispatcher')
->dispatch(SmartIpEvents::DATA_ACQUIRED, $event);
$location
->save();
}
}
}
return new JsonResponse();
}
public function check(Request $request) {
if (SmartIp::checkAllowedPage() && DeviceGeolocation::isNeedUpdate()) {
$json = [
'askGeolocate' => TRUE,
];
$location = \Drupal::service('smart_ip.smart_ip_location');
$json['device_geolocation'] = $location
->getData(FALSE);
$debugMode = SmartIp::isUserDebugMode();
$json['device_geolocation']['debugMode'] = $debugMode;
$json['device_geolocation']['askGeolocate'] = TRUE;
}
else {
$json = [
'askGeolocate' => FALSE,
];
}
return new JsonResponse($json);
}
}