From 6cd037e32e2e400c85f3569b970b9a41c3b8baf8 Mon Sep 17 00:00:00 2001 From: Tam Date: Wed, 8 Nov 2017 17:28:58 +0000 Subject: [PATCH] Added ability to save map with only an address --- CHANGELOG.md | 7 ++++++ composer.json | 2 +- src/fields/MapField.php | 10 ++++++++ src/fields/MapValidator.php | 42 +++++++++++++++++++++++++++++++ src/services/MapService.php | 24 +++++++++++++++--- src/translations/en/simplemap.php | 4 +++ 6 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 src/fields/MapValidator.php diff --git a/CHANGELOG.md b/CHANGELOG.md index f210864..940a176 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/composer.json b/composer.json index 3262b35..b91ab31 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/fields/MapField.php b/src/fields/MapField.php index d870e17..916e0f0 100644 --- a/src/fields/MapField.php +++ b/src/fields/MapField.php @@ -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 */ diff --git a/src/fields/MapValidator.php b/src/fields/MapValidator.php new file mode 100644 index 0000000..a6b3ad1 --- /dev/null +++ b/src/fields/MapValidator.php @@ -0,0 +1,42 @@ +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' + ), + [] + ]; + } + +} \ No newline at end of file diff --git a/src/services/MapService.php b/src/services/MapService.php index cf1f35b..f3defd6 100644 --- a/src/services/MapService.php +++ b/src/services/MapService.php @@ -34,6 +34,8 @@ class MapService extends Component /** @var string */ private static $_apiKey; + private static $_cachedAddressToLatLngs = []; + // Public Methods // ========================================================================= @@ -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; @@ -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 @@ -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); diff --git a/src/translations/en/simplemap.php b/src/translations/en/simplemap.php index 9156fa9..6460e99 100644 --- a/src/translations/en/simplemap.php +++ b/src/translations/en/simplemap.php @@ -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', ]; \ No newline at end of file