Skip to content

Commit

Permalink
Added ability to save map with only an address
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Nov 8, 2017
1 parent 5ac4d0e commit 6cd037e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 3.0.3 - 2017-11-08
### Added
- It's now possible to save the map field with only an address! Useful for populating the field from the front-end. (Requires the Geocoding API).

### Improved
- The address and lat/lng are now validated.

## 3.0.2 - 2017-11-03
### Fixed
- Fixed a bug where location searches would error if `orderBy` was not defined
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ether/simplemap",
"description": "A beautifully simple Google Map field type.",
"version": "3.0.2",
"version": "3.0.3",
"type": "craft-plugin",
"license": "MIT",
"minimum-stability": "dev",
Expand Down
10 changes: 10 additions & 0 deletions src/fields/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,16 @@ public function normalizeValue ($value, ElementInterface $element = null)
return SimpleMap::$plugin->getMap()->getField($this, $element, $value);
}

/**
* @inheritdoc
*/
public function getElementValidationRules (): array
{
return [
MapValidator::class,
];
}

/**
* @inheritdoc
*/
Expand Down
42 changes: 42 additions & 0 deletions src/fields/MapValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace ether\simplemap\fields;

use ether\simplemap\services\MapService;
use yii\validators\Validator;

class MapValidator extends Validator {

/**
* @inheritdoc
*/
protected function validateValue ($value)
{
if (!is_null($value->lat) && !is_null($value->lng))
return null;

if (!is_null($value->address)) {
$addressToLatLng = MapService::getLatLngFromAddress($value['address']);
if (is_null($addressToLatLng)) {
return [
\Craft::t(
'simplemap',
'Missing Lat/Lng or valid address'
),
[]
];
}

return null;
}

return [
\Craft::t(
'simplemap',
'Missing Lat/Lng'
),
[]
];
}

}
24 changes: 20 additions & 4 deletions src/services/MapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class MapService extends Component
/** @var string */
private static $_apiKey;

private static $_cachedAddressToLatLngs = [];

// Public Methods
// =========================================================================

Expand All @@ -49,6 +51,10 @@ class MapService extends Component
*/
public static function getLatLngFromAddress ($address)
{
if (array_key_exists($address, self::$_cachedAddressToLatLngs)) {
return self::$_cachedAddressToLatLngs[$address];
}

$browserApiKey = self::_getAPIKey();

if (!$browserApiKey) return null;
Expand All @@ -73,9 +79,12 @@ public static function getLatLngFromAddress ($address)
);
}

if (empty($resp['results'])) return null;
if (empty($resp['results'])) $latLng = null;
else $latLng = $resp['results'][0]['geometry']['location'];

self::$_cachedAddressToLatLngs[$address] = $latLng;

return $resp['results'][0]['geometry']['location'];
return $latLng;
}

// Public Methods: Instance
Expand Down Expand Up @@ -130,8 +139,15 @@ public function saveField (MapField $field, ElementInterface $owner): bool
/** @var Map $value */
$value = $owner->getFieldValue($field->handle);

// FIXME: All instances of `$field` should be pointing to the value
// (except `$field->id`)
if (
(is_null($value->lat) || is_null($value->lng))
&& !is_null($value->address)
) {
if ($addressToLatLng = self::getLatLngFromAddress($value->address)) {
$value->lat = $addressToLatLng['lat'];
$value->lng = $addressToLatLng['lng'];
}
}

$lat = number_format((float)$value->lat, 9);
$lng = number_format((float)$value->lng, 9);
Expand Down
4 changes: 4 additions & 0 deletions src/translations/en/simplemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@
'Boundary Restriction' => 'Boundary Restriction',
'Restrict the auto-complete to within a specific square boundary'
=> 'Restrict the auto-complete to within a specific square boundary',

// Field validation
'Missing Lat/Lng' => 'Missing Lat/Lng',
'Missing Lat/Lng or valid address' => 'Missing Lat/Lng or valid address',
];

0 comments on commit 6cd037e

Please sign in to comment.