From 6d82e2826fc667ff4cf4417551f4e834015ba17b Mon Sep 17 00:00:00 2001 From: Tony Air Date: Thu, 16 Jun 2022 01:33:01 +0200 Subject: [PATCH] FIX: MapAPI key --- src/Extensions/SiteConfigExtension.php | 272 +++++++++++++------------ 1 file changed, 147 insertions(+), 125 deletions(-) diff --git a/src/Extensions/SiteConfigExtension.php b/src/Extensions/SiteConfigExtension.php index f89de35..9e5229e 100755 --- a/src/Extensions/SiteConfigExtension.php +++ b/src/Extensions/SiteConfigExtension.php @@ -42,146 +42,168 @@ use SilverStripe\Core\Config\Config; */ class SiteConfigExtension extends DataExtension { - private static $db = [ - 'ExtraCode' => 'Text', - 'Longitude' => 'Decimal(10, 8)', - 'Latitude' => 'Decimal(11, 8)', - 'MapZoom' => 'Int', - 'Description' => 'Varchar(255)', - 'Address' => 'Varchar(255)', - 'Suburb' => 'Varchar(255)', - 'State' => 'Varchar(255)', - 'Country' => 'Varchar(255)', - 'ZipCode' => 'Varchar(6)', - ]; + private static $db = [ + 'ExtraCode' => 'Text', + 'Lng' => 'Decimal(10, 8)', + 'Lat' => 'Decimal(11, 8)', + 'MapZoom' => 'Int', + 'Description' => 'Varchar(255)', + 'Address' => 'Varchar(255)', + 'Suburb' => 'Varchar(255)', + 'State' => 'Varchar(255)', + 'Country' => 'Varchar(255)', + 'ZipCode' => 'Varchar(6)', + ]; - private static $has_one = [ - 'PrivacyPolicy' => SiteTree::class, - 'Sitemap' => SiteTree::class, - ]; + private static $has_one = [ + 'PrivacyPolicy' => SiteTree::class, + 'Sitemap' => SiteTree::class, + ]; - private static $many_many = [ - 'Navigation' => SiteTree::class, - ]; + private static $many_many = [ + 'Navigation' => SiteTree::class, + ]; - public function updateCMSFields(FieldList $fields) - { - $img = Image::get()->filter([ - 'ParentID' => 0, - 'FileFilename' => 'qrcode.png', - ])->first(); - if ($img) { - $fields->addFieldsToTab('Root.Main', [ - LiteralField::create('QRCode', 'QR code'), - ]); - } + public function updateCMSFields(FieldList $fields) + { + $img = Image::get()->filter([ + 'ParentID' => 0, + 'FileFilename' => 'qrcode.png', + ])->first(); + if ($img) { + $fields->addFieldsToTab('Root.Main', [ + LiteralField::create('QRCode', 'QR code'), + ]); + } - $fields->addFieldsToTab('Root.Main', [ - TreeMultiselectField::create( - 'Navigation', - 'Navigation', - SiteTree::class - )->setDisableFunction(static function ($el) { - return $el->getField('ParentID') !== 0; - }), - TextareaField::create('Description', 'Website Description'), - TextareaField::create('ExtraCode', 'Extra site-wide HTML code'), - DropdownField::create( - 'PrivacyPolicyID', - 'Privacy Policy Page', - SiteTree::get()->map()->toArray() - )->setEmptyString('(Select one)'), - DropdownField::create( - 'SitemapID', - 'Sitemap Page', - SitemapPage::get()->map()->toArray() - )->setEmptyString('(Select one)'), - ]); + $fields->addFieldsToTab('Root.Main', [ + TreeMultiselectField::create( + 'Navigation', + 'Navigation', + SiteTree::class + )->setDisableFunction(static function ($el) { + return $el->getField('ParentID') !== 0; + }), + TextareaField::create('Description', 'Website Description'), + TextareaField::create('ExtraCode', 'Extra site-wide HTML code'), + DropdownField::create( + 'PrivacyPolicyID', + 'Privacy Policy Page', + SiteTree::get()->map()->toArray() + )->setEmptyString('(Select one)'), + DropdownField::create( + 'SitemapID', + 'Sitemap Page', + SitemapPage::get()->map()->toArray() + )->setEmptyString('(Select one)'), + ]); - $mapTab = $fields->findOrMakeTab('Root.Maps'); - $mapTab->setTitle('Address / Map'); + $mapTab = $fields->findOrMakeTab('Root.Maps'); + $mapTab->setTitle('Address / Map'); - $addrFields =[ - TextField::create('Address'), - TextField::create('ZipCode'), - TextField::create('Suburb', 'City'), - ]; + $addrFields =[ + TextField::create('Address'), + TextField::create('ZipCode'), + TextField::create('Suburb', 'City'), + ]; - if (\class_exists(Addressable::class)) { - $stateLabel = _t('Addressable.STATE', 'State'); - $allowedStates = Config::inst()->get(SiteConfig::class, 'allowed_states'); - if ($allowedStates && count($allowedStates) >= 1) { - // If allowed states are restricted, only allow those - $addrFields[] = DropdownField::create('State', $stateLabel, $allowedStates); - } elseif (!$allowedStates) { - // If no allowed states defined, allow the user to type anything - $addrFields[] = TextField::create('State', $stateLabel); - } + if (\class_exists(Addressable::class)) { + $stateLabel = _t('Addressable.STATE', 'State'); + $allowedStates = Config::inst()->get(SiteConfig::class, 'allowed_states'); + if ($allowedStates && count($allowedStates) >= 1) { + // If allowed states are restricted, only allow those + $addrFields[] = DropdownField::create('State', $stateLabel, $allowedStates); + } elseif (!$allowedStates) { + // If no allowed states defined, allow the user to type anything + $addrFields[] = TextField::create('State', $stateLabel); + } - // Get country field - $countryLabel = _t('Addressable.COUNTRY', 'Country'); - $allowedCountries = Config::inst()->get(SiteConfig::class, 'allowed_countries'); - if($allowedCountries && count($allowedCountries) >= 1) { - $addrFields[] = DropdownField::create( - 'Country', - $countryLabel, - $allowedCountries - ); - } else { - $addrFields[] = TextField::create('Country', $countryLabel); - } - } else { - $addrFields[] = TextField::create('State'); - $addrFields[] = TextField::create('Country'); - } + // Get country field + $countryLabel = _t('Addressable.COUNTRY', 'Country'); + $allowedCountries = Config::inst()->get(SiteConfig::class, 'allowed_countries'); + if ($allowedCountries && count($allowedCountries) >= 1) { + $addrFields[] = DropdownField::create( + 'Country', + $countryLabel, + $allowedCountries + ); + } else { + $addrFields[] = TextField::create('Country', $countryLabel); + } + } else { + $addrFields[] = TextField::create('State'); + $addrFields[] = TextField::create('Country'); + } - $fields->addFieldsToTab('Root.Maps', $addrFields); + $fields->addFieldsToTab('Root.Maps', $addrFields); - if (MapboxField::getAccessToken()) { - $fields->addFieldsToTab('Root.Maps', [ - //TextField::create('MapAPIKey'), - TextField::create('MapZoom'), - MapboxField::create('Map', 'Choose a location', 'Latitude', 'Longitude'), - ]); - } else { - $fields->addFieldsToTab('Root.Maps', [ - LiteralField::create('MapNotice', '

No Map API keys specified.

') - ]); - } + if (MapboxField::getAccessToken()) { + $fields->addFieldsToTab('Root.Maps', [ + //TextField::create('MapAPIKey'), + TextField::create('MapZoom'), + MapboxField::create('Map', 'Choose a location', 'Latitude', 'Longitude'), + ]); + } else { + $fields->addFieldsToTab('Root.Maps', [ + LiteralField::create('MapNotice', '

No Map API keys specified.

') + ]); + } - /*GoogleMapField::create( - $this->owner, - 'Location', - [ - 'show_search_box' => true, - ] - )*/ - } + /*GoogleMapField::create( + $this->owner, + 'Location', + [ + 'show_search_box' => true, + ] + )*/ + } - public function MapStyle() - { - return MapboxField::config()->get('map_style'); - } + public function MapStyle() + { + return MapboxField::config()->get('map_style'); + } - public function getGeoJSON() - { - return '{"type": "MarkerCollection","features": [{"type": "Feature","icon": "",' - .'"properties": {"content": "'.$this->owner->getTitle().'"},"geometry": {"type": "Point",' - .'"coordinates": ['.$this->owner->getField('Lng').','.$this->owner->getField('Lat').']}}]}'; - } + public function getGeoJSON() + { + return \json_encode([ + 'type' => 'MarkerCollection', + 'features' => [ + [ + 'id' => 'SiteConfig' . $this->owner->ID, + 'type' => 'Feature', + 'icon' => '', + 'properties' => [ + 'content' => $this->owner->renderWith('A2nt/ElementalBasics/Models/MapPin'), + ], + 'geometry' => [ + 'type' => 'Point', + 'coordinates' => [ + $this->owner->Lng, + $this->owner->Lat, + ], + ], + ] + ] + ]); + } - public function DirectionsLink() - { - return '' - .' Get Directions'; - } + public function DirectionsLinkURL() + { + return 'https://www.google.com/maps/dir/Current+Location/' + .$this->owner->Lat.',' + .$this->owner->Lng; + } - public function getLatestBlogPosts() - { - return BlogPost::get()->sort('PublishDate DESC'); - } + public function DirectionsLink() + { + return '' + .' Get Directions'; + } + + public function getLatestBlogPosts() + { + return BlogPost::get()->sort('PublishDate DESC'); + } }