Skip to content

Commit

Permalink
Merge branch 'v2-dev' into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Nov 8, 2017
2 parents 63ed76d + be8ed9e commit 0200b44
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ var map = new mapboxgl.Map({

## Changelog

### 1.7.1
- It is now possible to save the field using only an address (useful for saving via the front-end, requires Geocoding).
- Improved the field's validation.

### 1.7.0
- Improved the efficiency of the location search query.

Expand Down
9 changes: 9 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,14 @@
"notes": [
"[Improved] Improved the efficiency of the location search query."
]
},
{
"version": "1.7.1",
"downloadUrl": "https://github.com/ethercreative/simplemap/archive/v1.7.1.zip",
"date": "2017-11-08T16:05:00-08:00",
"notes": [
"[Added] It is now possible to save the field using only an address (useful for saving via the front-end, requires Geocoding)",
"[Improved] Improved the field's validation"
]
}
]
2 changes: 1 addition & 1 deletion simplemap/SimpleMapPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getDescription()

public function getVersion()
{
return '1.7.0';
return '1.7.1';
}

public function getSchemaVersion()
Expand Down
5 changes: 5 additions & 0 deletions simplemap/fieldtypes/SimpleMap_MapFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ public function getSettingsHtml()
));
}

public function validate ($value)
{
return craft()->simpleMap->validateField($this);
}

public function onAfterElementSave()
{
craft()->simpleMap->saveField($this);
Expand Down
39 changes: 39 additions & 0 deletions simplemap/services/SimpleMapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,45 @@ public function getField (SimpleMap_MapFieldType $fieldType, $value)
return $model;
}

/**
* Validates the field
*
* @param SimpleMap_MapFieldType $fieldType
*
* @return bool
*/
public function validateField (SimpleMap_MapFieldType $fieldType)
{
$owner = $fieldType->element;
$field = $fieldType->model;
$content = $fieldType->element->getContent();

$handle = $field->handle;
$data = $content->getAttribute($handle);

if (
!array_key_exists('lat', $data)
|| !array_key_exists('lng', $data)
) {
if (!array_key_exists('address', $data)) {
$owner->addError($handle, 'Missing lat/lng');
return false;
}

$addressToLatLng = self::getLatLngFromAddress($data['address']);
if ($addressToLatLng == null) {
$owner->addError($handle, 'Missing lat/lng or valid address');
return false;
}

$data['lat'] = $addressToLatLng['lat'];
$data['lng'] = $addressToLatLng['lng'];
}

$content->setAttribute($handle, $data);
return true;
}

/**
* Save Map Field
*
Expand Down

0 comments on commit 0200b44

Please sign in to comment.