diff --git a/README.md b/README.md
index e58b91b..d7eae3f 100644
--- a/README.md
+++ b/README.md
@@ -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`
diff --git a/simplemap/SimpleMapPlugin.php b/simplemap/SimpleMapPlugin.php
index 344bb5f..34168de 100644
--- a/simplemap/SimpleMapPlugin.php
+++ b/simplemap/SimpleMapPlugin.php
@@ -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,
));
}
diff --git a/simplemap/config.php b/simplemap/config.php
new file mode 100644
index 0000000..4527551
--- /dev/null
+++ b/simplemap/config.php
@@ -0,0 +1,6 @@
+ '',
+ 'serverApiKey' => '',
+];
\ No newline at end of file
diff --git a/simplemap/fieldtypes/SimpleMap_MapFieldType.php b/simplemap/fieldtypes/SimpleMap_MapFieldType.php
index 6e9ede8..7a05159 100644
--- a/simplemap/fieldtypes/SimpleMap_MapFieldType.php
+++ b/simplemap/fieldtypes/SimpleMap_MapFieldType.php
@@ -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;
diff --git a/simplemap/services/SimpleMapService.php b/simplemap/services/SimpleMapService.php
index 26a0450..a81ae09 100644
--- a/simplemap/services/SimpleMapService.php
+++ b/simplemap/services/SimpleMapService.php
@@ -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);
diff --git a/simplemap/templates/plugin-settings.twig b/simplemap/templates/plugin-settings.twig
index e6f8e3c..b6e5e5d 100644
--- a/simplemap/templates/plugin-settings.twig
+++ b/simplemap/templates/plugin-settings.twig
@@ -5,9 +5,11 @@
instructions: 'Get an API key.',
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({
@@ -15,7 +17,9 @@
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. Get an API key.',
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." : ''
}) }}
\ No newline at end of file
diff --git a/simplemap/variables/SimpleMapVariable.php b/simplemap/variables/SimpleMapVariable.php
index 29282ae..e935948 100644
--- a/simplemap/variables/SimpleMapVariable.php
+++ b/simplemap/variables/SimpleMapVariable.php
@@ -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;
}