Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed how the map script was generated #222

Merged
merged 2 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 69 additions & 4 deletions docs/en/developerguide/customizing.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
## Customizing

### YAML Configuration
Some customization is done through a yml config.

### Limit
#### Limit
Will limit the amount of locations that can be shown at one time. Set to `-1` to have no limit. Having no limit can result in slow load times, or even timeouts when loading the page or new data.
```yaml
Dynamic\Locator\Locator:
limit: 50
```

### Radius
#### Radius
- `show_radius` will determine if a radius dropdown should be shown.
- `radii` is a list of radii to use in the radius dropdown.
```yaml
Expand All @@ -17,7 +19,7 @@ Dynamic\Locator\Locator:
radii: [30, 50, 100]
```

### Templates
#### Templates
Overriding the templates for the info window and list can be overridden by using `infoWindowTemplate` and `listTemplate`.
- `infoWindowTemplate` is the popup when a location is clicked.
- `listTemplate` is a single location in the list
Expand All @@ -28,7 +30,7 @@ Dynamic\Locator\Locator:
```
The `vendor/module: file` pattern can be used to locate files.

### Custom URL Variables
#### Custom URL Variables
Sometimes it is useful to override the defaults for url variables.
An example of this is when the module uses title case but you wrote everything for lowercase. It becomes seamless to switch out the variable names.
- `radius_var` is the variable used to send and receive the currently selected radius.
Expand All @@ -44,3 +46,66 @@ Dynamic\SilverStripeGeocoder\DistanceDataExtension:
address_var: 'Address'
unit_var: 'Unit'
```

### Extenison Points

#### Locator Form
##### overrideRadiusArray
Allows radius array to be changed outside of the yml config.
`overrideRadiusArray` gets passed the radius array.

##### updateRequiredFields
Alolows the required fields to be modified.
`updateRequiredFields` gets passed a Validator object.

##### updateLocatorFormFields
Allows form fields to be modified.
`updateLocatorFormFields` gets passed a field list.

##### updateLocatorActions
Allows form actions to be modified.
`updateLocatorActions` gets passed a field list.

#### Locator Admin
##### updateGetExportFields
Allows exported fields to be modified.
`updateGetExportFields` gets passed an associative array.

#### Location
##### updateLocationFields
Allows cms fields to be modified.
`updateLocationFields` gets passed a field list.

##### updateWebsiteURL
Allows modifying the wesite url of the location.
`updateWebsiteURL` gets passed the website url.

#### Locator Controller
##### modifyMapSettings
Allows modifying the map settings used for the locator javascript.
`modifyMapSettings` gets passed an associative array.

##### modifyMapSettingsEncoded
Allows modifying the map settings after it is turned into a string.
It is prefered to use the `modifyMapSettings` extension point.
`modifyMapSettingsEncoded` gets passed a string.

##### updateLocatorFilter
Allows updating the location list filter.
`updateLocatorFilter` gets passed the current filter and the current request.

##### updateLocatorFilterAny
Allows updating the location list filter any.
`updateLocatorFilterAny` gets passed the current filter any and the current request.

##### updateLocatorExclude
Allows updating the location list exlude.
`updateLocatorFilterAny` gets passed the current exlude and the current request.

##### updateLocationList
Allows updating the list of locations after distance calculations have been done.
`updateLocationList` is passed an array list.

##### updateListType
Allows updating the location list type.
`updateListType` is passed an array list.
159 changes: 80 additions & 79 deletions src/pages/LocatorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

/**
* Class LocatorController
*
* @mixin Locator
*/
class LocatorController extends \PageController
{
Expand Down Expand Up @@ -81,98 +83,97 @@ class LocatorController extends \PageController
public function init()
{
parent::init();

// prevent init of map if no query
$request = Controller::curr()->getRequest();
if (!$this->getTrigger($request)) {
return;
}

$locations = $this->getLocations();

// prevent init of map if there are no locations
if (!$locations) {
return;
}

// google maps api key
$key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key');
Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key);

// prevent init of map if no query
$request = Controller::curr()->getRequest();
$mapSettings = [
'fullMapStart' => true,
'maxDistance' => true,
'storeLimit' => -1,
'originMarker' => true,
'slideMap' => false,
'distanceAlert' => -1,
'featuredLocations' => false,
'defaultLat' => 0,
'defaultLng' => 0,
'mapSettings' => [
'zoom' => 12,
'mapTypeId' => 'google.maps.MapTypeId.ROADMAP',
'disableDoubleClickZoom' => true,
'scrollwheel' => false,
'navigationControl' => false,
'draggable' => false
],
];

$mapSettings['listTemplatePath'] = $this->getListTemplate();
$mapSettings['infowindowTemplatePath'] = $this->getInfoWindowTemplate();
$mapSettings['lengthUnit'] = $this->data()->Unit == 'km' ? 'km' : 'm';
$mapSettings['mapID'] = Config::inst()->get(LocatorController::class, 'map_container');
$mapSettings['locationList'] = Config::inst()->get(LocatorController::class, 'list_container');

if ($locations->filter('Featured', true)->count() > 0) {
$mapSettings['featuredLocations'] = true;
}

if ($this->getTrigger($request)) {
$locations = $this->getLocations();
// map config based on user input in Settings tab
$limit = Config::inst()->get(LocatorController::class, 'limit');
if (0 < $limit) {
$mapSettings['storeLimit'] = $limit;
}

if ($locations) {
$featuredInList = ($locations->filter('Featured', true)->count() > 0);
$defaultCoords = $this->getAddressSearchCoords() ?
$this->getAddressSearchCoords() :
new ArrayData([
"Lat" => 0,
"Lng" => 0,
]);

$featured = $featuredInList
? 'featuredLocations: true'
: 'featuredLocations: false';

// map config based on user input in Settings tab
$limit = Config::inst()->get(LocatorController::class, 'limit');
if ($limit < 1) {
$limit = -1;
}
$load = 'fullMapStart: true, storeLimit: ' . $limit . ', maxDistance: true,';
if ($coords = $this->getAddressSearchCoords()) {
$mapSettings['defaultLat'] = $coords->getField("Lat");
$mapSettings['defaultLat'] = $coords->getField("Lng");
}

$listTemplatePath = $this->getListTemplate();
$infowindowTemplatePath = $this->getInfoWindowTemplate();
// pass GET variables to xml action
$vars = $this->request->getVars();
unset($vars['url']);
$url = '';
if (count($vars)) {
$url .= '?' . http_build_query($vars);
}
$mapSettings['dataLocation'] = Controller::join_links($this->Link(), 'xml.xml', $url);

$kilometer = ($this->data()->Unit == 'km') ? "lengthUnit: 'km'" : "lengthUnit: 'm'";
if ($stylePath = $this->getMapStyleJSONPath()) {
if ($style = file_get_contents($stylePath)) {
$mapSettings['mapSettings']['styles'] = json_decode($style);
}
}

// pass GET variables to xml action
$vars = $this->request->getVars();
unset($vars['url']);
$url = '';
if (count($vars)) {
$url .= '?' . http_build_query($vars);
}
$link = Controller::join_links($this->Link(), 'xml.xml', $url);

// containers
$map_id = Config::inst()->get(LocatorController::class, 'map_container');
$list_class = Config::inst()->get(LocatorController::class, 'list_container');

$mapStyle = '';
if ($stylePath = $this->getMapStyleJSONPath()) {
if ($style = file_get_contents($stylePath)) {
$mapStyle = "styles: {$style},";
}
};

$markerImage = '';
if ($imagePath = $this->getMarkerIcon()) {
$markerImage = "markerImg: '{$imagePath}',";
}
if ($imagePath = $this->getMarkerIcon()) {
$mapSettings['markerImg'] = $imagePath;
}

$this->extend('modifyMapSettings', $mapSettings);

// init map
Requirements::customScript("
$encoded = json_encode($mapSettings, JSON_UNESCAPED_SLASHES);
// mapTypeId is a javascript object
$encoded = preg_replace('/("mapTypeId"\s*:\s*)"(.+?)"/i', '$1$2', $encoded);

$this->extend('modifyMapSettingsEncoded', $encoded);
// init map
Requirements::customScript("
$(function(){
$('#map-container').storeLocator({
{$load}
dataLocation: '{$link}',
listTemplatePath: '{$listTemplatePath}',
infowindowTemplatePath: '{$infowindowTemplatePath}',
{$markerImage}
originMarker: true,
{$featured},
slideMap: false,
distanceAlert: -1,
{$kilometer},
defaultLat: {$defaultCoords->getField("Lat")},
defaultLng: {$defaultCoords->getField("Lng")},
mapID: '{$map_id}',
locationList: '{$list_class}',
mapSettings: {
{$mapStyle}
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDoubleClickZoom: true,
scrollwheel: false,
navigationControl: false,
draggable: false
}
});
$('#map-container').storeLocator({$encoded});
});
", 'jquery-locator');
}
}
}

/**
Expand Down