From 46b5e04c3ae9652e96c5961874f70775455e0078 Mon Sep 17 00:00:00 2001 From: Colin Tucker Date: Wed, 4 Apr 2018 16:29:00 +1000 Subject: [PATCH] Added field for Google API key. --- src/API/GoogleAPI.php | 26 ++++++++++++++++++++++++++ src/Extensions/GoogleConfig.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/API/GoogleAPI.php b/src/API/GoogleAPI.php index 673fb06..76baf85 100644 --- a/src/API/GoogleAPI.php +++ b/src/API/GoogleAPI.php @@ -35,6 +35,32 @@ class GoogleAPI use Injectable; use Configurable; + /** + * Answers the API key from site or YAML configuration. + * + * @return string + */ + public function getAPIKey() + { + $key = SiteConfig::current_site_config()->GoogleAPIKey; + + if (!$key) { + $key = self::config()->api_key; + } + + return $key; + } + + /** + * Answers true if the receiver has an API key. + * + * @return boolean + */ + public function hasAPIKey() + { + return (boolean) $this->getAPIKey(); + } + /** * Answers the API language from site or YAML configuration. * diff --git a/src/Extensions/GoogleConfig.php b/src/Extensions/GoogleConfig.php index 41fa8f4..fdef0db 100644 --- a/src/Extensions/GoogleConfig.php +++ b/src/Extensions/GoogleConfig.php @@ -43,6 +43,7 @@ class GoogleConfig extends ServicesConfig * @config */ private static $db = [ + 'GoogleAPIKey' => 'Varchar(128)', 'GoogleAPILanguage' => 'Varchar(8)', 'GoogleVerificationCode' => 'Varchar(64)', 'GoogleAnalyticsTrackingID' => 'Varchar(64)', @@ -82,6 +83,15 @@ public function updateCMSFields(FieldList $fields) 'GoogleAPIConfig', $this->owner->fieldLabel('GoogleAPIConfig'), [ + TextField::create( + 'GoogleAPIKey', + $this->owner->fieldLabel('GoogleAPIKey') + )->setRightTitle( + _t( + __CLASS__ . '.GOOGLEAPIKEYRIGHTTITLE', + 'Create credentials using the Google API Manager and paste the API key here.' + ) + ), DropdownField::create( 'GoogleAPILanguage', $this->owner->fieldLabel('GoogleAPILanguage'), @@ -133,6 +143,7 @@ public function updateFieldLabels(&$labels) // Update Field Labels: $labels['Google'] = _t(__CLASS__ . '.GOOGLE', 'Google'); + $labels['GoogleAPIKey'] = _t(__CLASS__ . '.GOOGLEAPIKEY', 'Google API Key'); $labels['GoogleAPIConfig'] = _t(__CLASS__ . '.GOOGLEAPI', 'Google API'); $labels['GoogleAPILanguage'] = _t(__CLASS__ . '.LANGUAGE', 'Language'); $labels['GoogleAnalyticsConfig'] = _t(__CLASS__ . '.GOOGLEANALYTICS', 'Google Analytics'); @@ -164,6 +175,10 @@ public function getBodyAttributes() $api = GoogleAPI::singleton(); + if ($key = $api->getAPIKey()) { + $attributes['data-google-api-key'] = $key; + } + if ($lang = $api->getAPILanguage()) { $attributes['data-google-api-lang'] = $lang; } @@ -185,6 +200,23 @@ public function getGoogleLanguageOptions() return $this->owner->config()->google_languages; } + /** + * Answers a status message array for the CMS interface. + * + * @return string + */ + public function getGoogleStatusMessage() + { + if (!GoogleAPI::singleton()->hasAPIKey()) { + + return _t( + __CLASS__ . '.GOOGLEAPIKEYMISSING', + 'Google API Key has not been entered into site configuration.' + ); + + } + } + /** * Removes any HTML present in the pasted value and answers the verification code. *