FIX: MapAPI key

This commit is contained in:
Tony Air 2022-06-16 01:33:01 +02:00
parent 8f8306cc9e
commit 6d82e2826f

View File

@ -42,146 +42,168 @@ use SilverStripe\Core\Config\Config;
*/ */
class SiteConfigExtension extends DataExtension class SiteConfigExtension extends DataExtension
{ {
private static $db = [ private static $db = [
'ExtraCode' => 'Text', 'ExtraCode' => 'Text',
'Longitude' => 'Decimal(10, 8)', 'Lng' => 'Decimal(10, 8)',
'Latitude' => 'Decimal(11, 8)', 'Lat' => 'Decimal(11, 8)',
'MapZoom' => 'Int', 'MapZoom' => 'Int',
'Description' => 'Varchar(255)', 'Description' => 'Varchar(255)',
'Address' => 'Varchar(255)', 'Address' => 'Varchar(255)',
'Suburb' => 'Varchar(255)', 'Suburb' => 'Varchar(255)',
'State' => 'Varchar(255)', 'State' => 'Varchar(255)',
'Country' => 'Varchar(255)', 'Country' => 'Varchar(255)',
'ZipCode' => 'Varchar(6)', 'ZipCode' => 'Varchar(6)',
]; ];
private static $has_one = [ private static $has_one = [
'PrivacyPolicy' => SiteTree::class, 'PrivacyPolicy' => SiteTree::class,
'Sitemap' => SiteTree::class, 'Sitemap' => SiteTree::class,
]; ];
private static $many_many = [ private static $many_many = [
'Navigation' => SiteTree::class, 'Navigation' => SiteTree::class,
]; ];
public function updateCMSFields(FieldList $fields) public function updateCMSFields(FieldList $fields)
{ {
$img = Image::get()->filter([ $img = Image::get()->filter([
'ParentID' => 0, 'ParentID' => 0,
'FileFilename' => 'qrcode.png', 'FileFilename' => 'qrcode.png',
])->first(); ])->first();
if ($img) { if ($img) {
$fields->addFieldsToTab('Root.Main', [ $fields->addFieldsToTab('Root.Main', [
LiteralField::create('QRCode', '<img src="'.$img->Link().'" alt="QR code" width="200" style="float:left" />'), LiteralField::create('QRCode', '<img src="'.$img->Link().'" alt="QR code" width="200" style="float:left" />'),
]); ]);
} }
$fields->addFieldsToTab('Root.Main', [ $fields->addFieldsToTab('Root.Main', [
TreeMultiselectField::create( TreeMultiselectField::create(
'Navigation', 'Navigation',
'Navigation', 'Navigation',
SiteTree::class SiteTree::class
)->setDisableFunction(static function ($el) { )->setDisableFunction(static function ($el) {
return $el->getField('ParentID') !== 0; return $el->getField('ParentID') !== 0;
}), }),
TextareaField::create('Description', 'Website Description'), TextareaField::create('Description', 'Website Description'),
TextareaField::create('ExtraCode', 'Extra site-wide HTML code'), TextareaField::create('ExtraCode', 'Extra site-wide HTML code'),
DropdownField::create( DropdownField::create(
'PrivacyPolicyID', 'PrivacyPolicyID',
'Privacy Policy Page', 'Privacy Policy Page',
SiteTree::get()->map()->toArray() SiteTree::get()->map()->toArray()
)->setEmptyString('(Select one)'), )->setEmptyString('(Select one)'),
DropdownField::create( DropdownField::create(
'SitemapID', 'SitemapID',
'Sitemap Page', 'Sitemap Page',
SitemapPage::get()->map()->toArray() SitemapPage::get()->map()->toArray()
)->setEmptyString('(Select one)'), )->setEmptyString('(Select one)'),
]); ]);
$mapTab = $fields->findOrMakeTab('Root.Maps'); $mapTab = $fields->findOrMakeTab('Root.Maps');
$mapTab->setTitle('Address / Map'); $mapTab->setTitle('Address / Map');
$addrFields =[ $addrFields =[
TextField::create('Address'), TextField::create('Address'),
TextField::create('ZipCode'), TextField::create('ZipCode'),
TextField::create('Suburb', 'City'), TextField::create('Suburb', 'City'),
]; ];
if (\class_exists(Addressable::class)) { if (\class_exists(Addressable::class)) {
$stateLabel = _t('Addressable.STATE', 'State'); $stateLabel = _t('Addressable.STATE', 'State');
$allowedStates = Config::inst()->get(SiteConfig::class, 'allowed_states'); $allowedStates = Config::inst()->get(SiteConfig::class, 'allowed_states');
if ($allowedStates && count($allowedStates) >= 1) { if ($allowedStates && count($allowedStates) >= 1) {
// If allowed states are restricted, only allow those // If allowed states are restricted, only allow those
$addrFields[] = DropdownField::create('State', $stateLabel, $allowedStates); $addrFields[] = DropdownField::create('State', $stateLabel, $allowedStates);
} elseif (!$allowedStates) { } elseif (!$allowedStates) {
// If no allowed states defined, allow the user to type anything // If no allowed states defined, allow the user to type anything
$addrFields[] = TextField::create('State', $stateLabel); $addrFields[] = TextField::create('State', $stateLabel);
} }
// Get country field // Get country field
$countryLabel = _t('Addressable.COUNTRY', 'Country'); $countryLabel = _t('Addressable.COUNTRY', 'Country');
$allowedCountries = Config::inst()->get(SiteConfig::class, 'allowed_countries'); $allowedCountries = Config::inst()->get(SiteConfig::class, 'allowed_countries');
if($allowedCountries && count($allowedCountries) >= 1) { if ($allowedCountries && count($allowedCountries) >= 1) {
$addrFields[] = DropdownField::create( $addrFields[] = DropdownField::create(
'Country', 'Country',
$countryLabel, $countryLabel,
$allowedCountries $allowedCountries
); );
} else { } else {
$addrFields[] = TextField::create('Country', $countryLabel); $addrFields[] = TextField::create('Country', $countryLabel);
} }
} else { } else {
$addrFields[] = TextField::create('State'); $addrFields[] = TextField::create('State');
$addrFields[] = TextField::create('Country'); $addrFields[] = TextField::create('Country');
} }
$fields->addFieldsToTab('Root.Maps', $addrFields); $fields->addFieldsToTab('Root.Maps', $addrFields);
if (MapboxField::getAccessToken()) { if (MapboxField::getAccessToken()) {
$fields->addFieldsToTab('Root.Maps', [ $fields->addFieldsToTab('Root.Maps', [
//TextField::create('MapAPIKey'), //TextField::create('MapAPIKey'),
TextField::create('MapZoom'), TextField::create('MapZoom'),
MapboxField::create('Map', 'Choose a location', 'Latitude', 'Longitude'), MapboxField::create('Map', 'Choose a location', 'Latitude', 'Longitude'),
]); ]);
} else { } else {
$fields->addFieldsToTab('Root.Maps', [ $fields->addFieldsToTab('Root.Maps', [
LiteralField::create('MapNotice', '<p class="alert alert-info">No Map API keys specified.</p>') LiteralField::create('MapNotice', '<p class="alert alert-info">No Map API keys specified.</p>')
]); ]);
} }
/*GoogleMapField::create( /*GoogleMapField::create(
$this->owner, $this->owner,
'Location', 'Location',
[ [
'show_search_box' => true, 'show_search_box' => true,
] ]
)*/ )*/
} }
public function MapStyle() public function MapStyle()
{ {
return MapboxField::config()->get('map_style'); return MapboxField::config()->get('map_style');
} }
public function getGeoJSON() public function getGeoJSON()
{ {
return '{"type": "MarkerCollection","features": [{"type": "Feature","icon": "<i class=\'fas fa-map-marker-alt\'></i>",' return \json_encode([
.'"properties": {"content": "'.$this->owner->getTitle().'"},"geometry": {"type": "Point",' 'type' => 'MarkerCollection',
.'"coordinates": ['.$this->owner->getField('Lng').','.$this->owner->getField('Lat').']}}]}'; 'features' => [
} [
'id' => 'SiteConfig' . $this->owner->ID,
'type' => 'Feature',
'icon' => '<i class="fa-icon fas fa-map-marker-alt"></i>',
'properties' => [
'content' => $this->owner->renderWith('A2nt/ElementalBasics/Models/MapPin'),
],
'geometry' => [
'type' => 'Point',
'coordinates' => [
$this->owner->Lng,
$this->owner->Lat,
],
],
]
]
]);
}
public function DirectionsLink() public function DirectionsLinkURL()
{ {
return '<a href="https://www.google.com/maps/dir/Current+Location/' return 'https://www.google.com/maps/dir/Current+Location/'
.$this->owner->getField('Lat').',' .$this->owner->Lat.','
.$this->owner->getField('Lng').'" class="btn btn-primary btn-directions" target="_blank">' .$this->owner->Lng;
.'<i class="fas fa-road"></i> Get Directions</a>'; }
}
public function getLatestBlogPosts() public function DirectionsLink()
{ {
return BlogPost::get()->sort('PublishDate DESC'); return '<a href="'.$this->DirectionsLinkURL().'" class="btn btn-primary btn-directions" target="_blank">'
} .'<i class="fas fa-road"></i> Get Directions</a>';
}
public function getLatestBlogPosts()
{
return BlogPost::get()->sort('PublishDate DESC');
}
} }