Skip to content

Commit

Permalink
Added ability to define settings via config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Mar 2, 2018
1 parent 2f31a1d commit 989df77
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,15 @@ Lat/Lng, to display it on a map.
{{ location.lng }}
```

## Configuration

In addition to the plugins settings withing the Craft CP, you can use a config file. Copy `config.php` from the plugins folder to `craft/config/simplemap.php` and change the settings as needed.

## Changelog

### 1.8.3
- Fixed error when saving #92 #94 (via @taylordaughtry)
- Added ability to define settings via config file

### 1.8.2
- Fixed bug where distance was always `null`
Expand Down
12 changes: 11 additions & 1 deletion simplemap/SimpleMapPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,20 @@ protected function defineSettings()
);
}

/**
* @return null|string
* @throws Exception
*/
public function getSettingsHtml()
{
$configFileSettings = [
'browserApiKey' => craft()->config->get('browserApiKey', 'simplemap'),
'serverApiKey' => craft()->config->get('serverApiKey', 'simplemap'),
];

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

Expand Down
6 changes: 6 additions & 0 deletions simplemap/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'browserApiKey' => '',
'serverApiKey' => '',
];
4 changes: 3 additions & 1 deletion simplemap/fieldtypes/SimpleMap_MapFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function getInputHtml($name, $value)
$boundary = JsonHelper::encode(array('ne' => $ne, 'sw' => $sw));
}

$key = craft()->plugins->getPlugin('SimpleMap')->getSettings()->browserApiKey;
$key = craft()->config->get('browserApiKey', 'simplemap');
if (!$key)
$key = craft()->plugins->getPlugin('SimpleMap')->getSettings()->browserApiKey;

$locale = $value ? $value->ownerLocale : craft()->language;

Expand Down
15 changes: 10 additions & 5 deletions simplemap/services/SimpleMapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,15 +472,20 @@ private static function getAPIKey ()
if (self::$apiKey)
return self::$apiKey;

$apiKey = craft()->plugins
->getPlugin('SimpleMap')
->getSettings()['serverApiKey'];
$apiKey = craft()->config->get('serverApiKey', 'simplemap');

if (!$apiKey) {
if (!$apiKey)
$apiKey = craft()->plugins
->getPlugin('SimpleMap')
->getSettings()['serverApiKey'];

if (!$apiKey)
$apiKey = craft()->config->get('browserApiKey', 'simplemap');

if (!$apiKey)
$apiKey = craft()->plugins
->getPlugin('SimpleMap')
->getSettings()['browserApiKey'];
}

if (!$apiKey) {
SimpleMapPlugin::log("Missing API Key", LogLevel::Error);
Expand Down
12 changes: 8 additions & 4 deletions simplemap/templates/plugin-settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
instructions: '<a href="https://developers.google.com/maps/documentation/javascript/get-api-key#get-an-api-key" target="_blank">Get an API key.</a>',
id: 'browserApiKey',
name: 'browserApiKey',
value: settings.browserApiKey,
value: configFileSettings.browserApiKey ?: settings.browserApiKey,
type: 'text',
required: true
required: true,
readonly: configFileSettings.browserApiKey ? true : false,
warning: configFileSettings.browserApiKey ? "This is being overridden by the browserApiKey config setting." : ''
}) }}

{{ forms.textField({
label: "Alternate Server API Key"|t,
instructions: 'If you are using the above API key publically and need to add restrictions to it, you will need to pass an unrestricted API key here for exclusive use by the server. <a href="https://developers.google.com/maps/documentation/javascript/get-api-key#get-an-api-key" target="_blank">Get an API key.</a>',
id: 'serverApiKey',
name: 'serverApiKey',
value: settings.serverApiKey,
value: configFileSettings.serverApiKey ?: settings.serverApiKey,
type: 'text',
required: false
required: false,
readonly: configFileSettings.serverApiKey ? true : false,
warning: configFileSettings.serverApiKey ? "This is being overridden by the browserApiKey config setting." : ''
}) }}
5 changes: 5 additions & 0 deletions simplemap/variables/SimpleMapVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public function latLng ($lat, $lng)
}

public function apiKey () {
$apiKey = craft()->config->get('browserApiKey', 'simplemap');

if ($apiKey)
return $apiKey;

return craft()->plugins->getPlugin('simpleMap')->getSettings()->browserApiKey;
}

Expand Down

0 comments on commit 989df77

Please sign in to comment.