Skip to content

Commit

Permalink
Merge pull request #6 from ethercreative/dev
Browse files Browse the repository at this point in the history
Merge v1.2.0 to Master
  • Loading branch information
Tam committed Jun 7, 2016
2 parents 2225c5a + b2aa642 commit 2c43fd0
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 7 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
![Simple Map](resources/banner.jpg)

# Simple Map
A beautifully simple Google Map field type for Craft CMS, compatible with Matrix.
A beautifully simple Google Map field type for Craft CMS. Full localization support, compatible with Matrix, supports
searching by location and sorting by distance.

## Installation
Clone this repo into `craft/plugins/simplemap`.
Expand All @@ -15,10 +16,31 @@ The field type will return an array containing `lat`, `lng`, `zoom`, `address`,
This contains the locations address, broken down into its constituent parts. All values are optional so you'll need to have checks on any you use to make sure they exist.
A list of the available values can be found [here](https://developers.google.com/maps/documentation/geocoding/intro#Types).

**Searching and Sorting**

You can search for elements using the location specified in your map field. When searching by your map field you also have the option to sort the results by distance.

```twig
{% set entries = craft.entries.myMapField({
location: 'Maidstone, Kent, UK',
radius: 100,
unit: 'mi'
}).order('distance') %}
```

- `location`: Can either be an address string (requires a Google Maps Geocoding API key) or a Lat Lng Array (`{ 'lat': 51.27219908, 'lng': 0.51545620 }` or `craft.simpleMap.latLng(51.27219908, 0.51545620)`).
- `radius`: The radius around the location to search. Defaults to `50`.
- `unit`: The unit of measurement for the search. Can be either `km` (kilometers) or `mi` (miles). Defaults to `km`.

![How it looks](resources/preview.png)

## Changelog

### 1.2.0
- **Added search and sorting support.**
- Added optional Google API Server Key setting
- **_WARNING:_** This update will break any map fields that are NOT standalone (global) or in a Matrix field.

### 1.1.2
- Fix \#5 via @jripmeester - Fixed Lat / Lng being populated with function, not number.

Expand Down
20 changes: 17 additions & 3 deletions SimpleMapPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function getDescription()

public function getVersion()
{
return '1.1.2';
return '1.2.0';
}

public function getSchemaVersion()
{
return '0.0.1';
return '0.0.5';
}

public function getDeveloper()
Expand All @@ -52,4 +52,18 @@ public function getReleaseFeedUrl()
return 'https://raw.githubusercontent.com/ethercreative/SimpleMap/master/releases.json';
}

}
protected function defineSettings()
{
return array(
'serverApiKey' => array(AttributeType::String)
);
}

public function getSettingsHtml()
{
return craft()->templates->render('simplemap/plugin-settings', array(
'settings' => $this->getSettings()
));
}

}
23 changes: 21 additions & 2 deletions fieldtypes/SimpleMap_MapFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function getName()

public function defineContentAttribute()
{
return AttributeType::Mixed;
return false;
}

public function getInputHtml($name, $value)
Expand All @@ -34,7 +34,8 @@ public function getInputHtml($name, $value)
return craft()->templates->render('simplemap/map-fieldtype', array(
'id' => $id,
'name' => $name,
'value' => $value
'value' => $value,
'settings' => $settings
));
}

Expand All @@ -55,4 +56,22 @@ public function getSettingsHtml()
));
}

public function onAfterElementSave()
{
craft()->simpleMap->saveField($this);
}

public function prepValue($value)
{
return craft()->simpleMap->getField($this, $value);
}

public function modifyElementsQuery(DbCommand $query, $value)
{
if ($value !== null)
craft()->simpleMap->modifyQuery($query, $value);

return $query;
}

}
59 changes: 59 additions & 0 deletions migrations/m160606_162300_simpleMap_updateFieldStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Craft;

class m160606_162300_simpleMap_updateFieldStorage extends BaseMigration
{
public function safeUp()
{
if (!craft()->db->tableExists('simplemap_maps'))
craft()->plugins->getPlugin('simpleMap')->createTables();

$fields = craft()->fields->getAllFields();

foreach ($fields as $field)
{
if ($field->type === 'SimpleMap_Map')
$this->_transferData($field, 'content');

if ($field->type === 'Matrix')
foreach (craft()->matrix->getBlockTypesByFieldId($field->id) as $blockType)
foreach ($blockType->getFields() as $blockTypeField)
if ($blockTypeField->type == 'SimpleMap_Map')
$this->_transferData($blockTypeField, 'matrixcontent_' . $field->handle, $blockType->handle . '_' . $blockTypeField->handle);
}

return true;
}

private function _transferData(FieldModel $field, $tableName, $fieldHandle = "")
{
if (!$fieldHandle)
$fieldHandle = $field->handle;

$tableData = craft()->db->createCommand()
->select('elementId, locale, field_' . $fieldHandle)
->from($tableName)
->where('field_' . $fieldHandle . ' IS NOT NULL')
->queryAll();

foreach ($tableData as $row) {
$record = new SimpleMap_MapRecord;
$record->ownerId = $row['elementId'];
$record->fieldId = $field->id;
$record->ownerLocale = $row['locale'];

$f = json_decode($row['field_' . $fieldHandle], true);

$record->setAttribute('lat', $f['lat']);
$record->setAttribute('lng', $f['lng']);
$record->setAttribute('zoom', $f['zoom']);
$record->setAttribute('address', $f['address']);
if ($f !== null && array_key_exists('parts', $f))
$record->setAttribute('parts', json_encode($f['parts']));

$record->save();
}
}

}
23 changes: 23 additions & 0 deletions models/SimpleMap_MapModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Craft;

class SimpleMap_MapModel extends BaseModel {

protected function defineAttributes()
{
return array(
'ownerId' => AttributeType::Number,
'fieldId' => AttributeType::Number,
'ownerLocale' => AttributeType::Locale,
'lat' => SimpleMap_MapRecord::$dec,
'lng' => SimpleMap_MapRecord::$dec,
'zoom' => AttributeType::Number,
'address' => AttributeType::String,
'parts' => AttributeType::Mixed,

'distance' => SimpleMap_MapRecord::$dec,
);
}

}
44 changes: 44 additions & 0 deletions records/SimpleMap_MapRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Craft;

class SimpleMap_MapRecord extends BaseRecord {

public static $dec = array(AttributeType::Number, 'column' => ColumnType::Decimal, 'length' => 12, 'decimals' => 8);
const TABLE_NAME = 'simplemap_maps';

public function getTableName()
{
return static::TABLE_NAME;
}

public function defineRelations()
{
return array(
'owner' => array(static::BELONGS_TO, 'ElementRecord', 'required' => true, 'onDelete' => static::CASCADE),
'ownerLocale' => array(static::BELONGS_TO, 'LocaleRecord', 'ownerLocale', 'onDelete' => static::CASCADE, 'onUpdate' => static::CASCADE),
'field' => array(static::BELONGS_TO, 'FieldRecord', 'required' => true, 'onDelete' => static::CASCADE)
);
}

public function defineIndexes()
{
return array(
array('columns' => array('ownerId')),
array('columns' => array('fieldId'))
);
}

protected function defineAttributes()
{
return array(
'ownerLocale' => AttributeType::Locale,
'lat' => SimpleMap_MapRecord::$dec,
'lng' => SimpleMap_MapRecord::$dec,
'zoom' => AttributeType::Number,
'address' => AttributeType::String,
'parts' => AttributeType::Mixed
);
}

}
10 changes: 10 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,15 @@
"notes": [
"Fix #5 via @jripmeester - Fixed Lat / Lng being populated with function, not number."
]
},
{
"version": "1.2.0",
"downloadUrl": "https://github.com/ethercreative/SimpleMap/archive/v1.2.0.zip",
"date": "2016-06-07T10:00:00-08:00",
"notes": [
"**Added search and sorting support.**",
"Added optional Google API Server Key setting",
"**_WARNING:_** This update will break any map fields that are NOT standalone (global) or in a Matrix field."
]
}
]
Loading

0 comments on commit 2c43fd0

Please sign in to comment.