Skip to content

Commit

Permalink
Merge pull request #2525 from jeroendesloovere/integrate-geolocation-…
Browse files Browse the repository at this point in the history
…vendor

Integrate Geolocation in Backend Location Model
  • Loading branch information
carakas committed Apr 25, 2018
2 parents 7403cf6 + 2c3ab5b commit 11607b9
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 47 deletions.
5 changes: 5 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ services:
tags:
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

ForkCMS\Utility\Geolocation:
public: true
arguments:
- "@fork.settings"

templating:
class: Frontend\Core\Engine\TwigTemplate
public: true
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"spoon/library": "^3.0",
"league/flysystem-aws-s3-v3": "^1.0.13",
"league/flysystem-cached-adapter": "^1.0.6",
"pimple/pimple": "^3.2"
"pimple/pimple": "^3.2",
"jeroendesloovere/geolocation-php-api": "^2.1"
},
"require-dev": {
"jdorn/sql-formatter": "1.2.17",
Expand Down
52 changes: 51 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/Backend/Modules/Location/Actions/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Backend\Core\Language\Language as BL;
use Backend\Core\Engine\Model as BackendModel;
use Backend\Modules\Location\Engine\Model as BackendLocationModel;
use ForkCMS\Utility\Geolocation;
use Symfony\Component\Intl\Intl as Intl;

/**
Expand Down Expand Up @@ -58,7 +59,7 @@ private function validateForm(): void
$item['country'] = $this->form->getField('country')->getValue();

// define coordinates
$coordinates = BackendLocationModel::getCoordinates(
$coordinates = BackendModel::get(Geolocation::class)->getCoordinates(
$item['street'],
$item['number'],
$item['city'],
Expand Down
3 changes: 2 additions & 1 deletion src/Backend/Modules/Location/Actions/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Backend\Core\Engine\Model as BackendModel;
use Backend\Form\Type\DeleteType;
use Backend\Modules\Location\Engine\Model as BackendLocationModel;
use ForkCMS\Utility\Geolocation;
use Symfony\Component\Intl\Intl as Intl;
use Frontend\Modules\Location\Engine\Model as FrontendLocationModel;

Expand Down Expand Up @@ -194,7 +195,7 @@ private function validateForm(): void
// check if it's necessary to geocode again
if ($this->record['lat'] === null || $this->record['lng'] === null || $item['street'] != $this->record['street'] || $item['number'] != $this->record['number'] || $item['zip'] != $this->record['zip'] || $item['city'] != $this->record['city'] || $item['country'] != $this->record['country']) {
// define coordinates
$coordinates = BackendLocationModel::getCoordinates(
$coordinates = BackendModel::get(Geolocation::class)->getCoordinates(
$item['street'],
$item['number'],
$item['city'],
Expand Down
53 changes: 10 additions & 43 deletions src/Backend/Modules/Location/Engine/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Backend\Core\Language\Language as BL;
use Backend\Core\Engine\Model as BackendModel;
use Common\ModuleExtraType;
use Symfony\Component\Intl\Intl as Intl;
use ForkCMS\Utility\Geolocation;

/**
* In this file we store all generic functions that we will be using in the location module
Expand Down Expand Up @@ -90,6 +90,8 @@ public static function getAll(): array
/**
* Get coordinates latitude/longitude
*
* @deprecated
*
* @param string $street
* @param string $streetNumber
* @param string $city
Expand All @@ -105,48 +107,13 @@ public static function getCoordinates(
string $zip = null,
string $country = null
): array {
// init item
$item = [];

// building item
if (!empty($street)) {
$item[] = $street;
}

if (!empty($streetNumber)) {
$item[] = $streetNumber;
}

if (!empty($city)) {
$item[] = $city;
}

if (!empty($zip)) {
$item[] = $zip;
}

if (!empty($country)) {
$item[] = Intl::getRegionBundle()->getCountryName($country, BL::getInterfaceLanguage());
}

// define address
$address = implode(' ', $item);

// fetch the geo coordinates
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . rawurlencode($address);
$geocodes = json_decode(file_get_contents($url), true);

// return coordinates latitude/longitude
return [
'latitude' => array_key_exists(
0,
$geocodes['results']
) ? $geocodes['results'][0]['geometry']['location']['lat'] : null,
'longitude' => array_key_exists(
0,
$geocodes['results']
) ? $geocodes['results'][0]['geometry']['location']['lng'] : null,
];
return BackendModel::get(Geolocation::class)->getCoordinates(
$street,
$streetNumber,
$city,
$zip,
$country
);
}

/**
Expand Down
59 changes: 59 additions & 0 deletions src/ForkCMS/Utility/Geolocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace ForkCMS\Utility;

use Backend\Core\Language\Language;
use Common\ModulesSettings;
use JeroenDesloovere\Geolocation\Geolocation as API;
use JeroenDesloovere\Geolocation\Result\Coordinates;
use JeroenDesloovere\Geolocation\Exception;
use Symfony\Component\Intl\Intl;

class Geolocation
{
/** @var API */
private $api;

public function __construct(ModulesSettings $settings)
{
$this->api = new API($settings->get('Core', 'google_maps_key'));
}

/**
* @param string|null $street
* @param string|null $streetNumber
* @param string|null $city
* @param string|null $zip
* @param string|null $country
* @return array - Example: ['latitude' => 50.8864, 'longitude' => 3.42928]
*/
public function getCoordinates(
string $street = null,
string $streetNumber = null,
string $city = null,
string $zip = null,
string $country = null
): array {
if (!empty($country)) {
$country = Intl::getRegionBundle()->getCountryName($country, Language::getInterfaceLanguage());
}

try {
/** @var Coordinates $coordinates */
$coordinates = $this->api->getCoordinates(
$street,
$streetNumber,
$city,
$zip,
$country
);
} catch (Exception $e) {
$coordinates = null;
}

return [
'latitude' => ($coordinates instanceof Coordinates) ? $coordinates->getLatitude() : null,
'longitude' => ($coordinates instanceof Coordinates) ? $coordinates->getLongitude() : null,
];
}
}

0 comments on commit 11607b9

Please sign in to comment.