Skip to content

Commit

Permalink
Merge branch 'release/4.0.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Jul 22, 2024
2 parents 22c4748 + 5bcf328 commit f8b75cd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Release Notes for Easy Address Field for Craft CMS

## 4.0.9 - 2024-07-22
### Fixed
- Better error catching on Nomanatim errors ([#37](https://github.com/studioespresso/craft-easyaddressfield/issues/37))

## 4.0.8 - 2023-11-23
### Changed
- Fixed missing return statement in AddressFieldModel
- Moved getDirectionsUrl from the twig function to the field model and added deprecation notice



## 4.0.7 - 2023-03-22
### Fixed
- Improved the position of the map in the field and added an extra label [#36](https://github.com/studioespresso/craft3-easyaddressfield/issues/36)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "studioespresso/craft-easyaddressfield",
"description": "The only address field you need",
"type": "craft-plugin",
"version": "4.0.8",
"version": "4.0.9",
"keywords": [
"cms",
"craftcms",
Expand Down
7 changes: 0 additions & 7 deletions src/fields/EasyAddressFieldField.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,9 @@ public function normalizeValue($value, ElementInterface $element = null): mixed
*/
public function serializeValue($value, ElementInterface $element = null): mixed
{
$settings = $this->getSettings();
if (!$value) {
return $value;
}
if (!ElementHelper::isDraftOrRevision($element)) {

if ($settings['geoCode'] and empty($value['latitude']) and empty($value['longitude'])) {
$value = EasyAddressField::getInstance()->geoLocation->locate($value);
}
}

return Db::prepareValueForDb($value);
}
Expand Down
5 changes: 4 additions & 1 deletion src/services/FieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Craft;
use craft\base\Component;
use craft\base\ElementInterface;
use craft\helpers\ElementHelper;
use studioespresso\easyaddressfield\EasyAddressField;
use studioespresso\easyaddressfield\fields\EasyAddressFieldField;
use studioespresso\easyaddressfield\models\EasyAddressFieldModel;
Expand Down Expand Up @@ -40,7 +41,9 @@ public function saveField(EasyAddressFieldField $field, ElementInterface $elemen
$record->field = $field->id;
}

$value = EasyAddressField::$plugin->geoLocation()->locate($value);
if(!ElementHelper::isDraftOrRevision($element)) {
$value = EasyAddressField::$plugin->geoLocation()->locate($value);
}


$record->name = $value->name;
Expand Down
20 changes: 12 additions & 8 deletions src/services/GeoLocationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ public function init(): void
*/
public function locate(EasyAddressFieldModel $model)
{
if (!$model->latitude && !$model->longitude and strlen($model->toString()) >= 2) {
$model = $this->geocodeOSM($model);
}
try {
if (!$model->latitude && !$model->longitude and strlen($model->toString()) >= 2) {
$model = $this->geocodeOSM($model);
}

return $model;
return $model;
} catch (\Throwable $e) {
Craft::error($e->getMessage());
}
}

private function geocodeOSM(EasyAddressFieldModel $model)
Expand All @@ -51,17 +55,17 @@ private function geocodeOSM(EasyAddressFieldModel $model)
->addressDetails();

$result = $nominatim->find($search);
if(empty($result)) {
if (empty($result)) {
return $model;
}

if(isset($result[0]['lat']) && isset($result[0]['lon'])) {
if (isset($result[0]['lat']) && isset($result[0]['lon'])) {
$model->longitude = $result[0]['lon'];
$model->latitude = $result[0]['lat'];
} elseif(is_array($result[0]['geojson']['coordinates'][0]) && is_array($result[0]['geojson']['coordinates'][0][0])) {
} elseif (is_array($result[0]['geojson']['coordinates'][0]) && is_array($result[0]['geojson']['coordinates'][0][0])) {
$model->longitude = $result[0]['geojson']['coordinates'][0][0][0];
$model->latitude = $result[0]['geojson']['coordinates'][0][0][1];
} elseif(is_array($result[0]['geojson']['coordinates'][0])) {
} elseif (is_array($result[0]['geojson']['coordinates'][0])) {
$model->longitude = $result[0]['geojson']['coordinates'][0][0];
$model->latitude = $result[0]['geojson']['coordinates'][0][1];
} else {
Expand Down

0 comments on commit f8b75cd

Please sign in to comment.