diff --git a/docs/en/developerguide/customizing.md b/docs/en/developerguide/customizing.md index c4403f2..aa919d6 100644 --- a/docs/en/developerguide/customizing.md +++ b/docs/en/developerguide/customizing.md @@ -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 @@ -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 @@ -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. @@ -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. diff --git a/src/pages/LocatorController.php b/src/pages/LocatorController.php index 5a7e7b7..00aba4c 100644 --- a/src/pages/LocatorController.php +++ b/src/pages/LocatorController.php @@ -15,6 +15,8 @@ /** * Class LocatorController + * + * @mixin Locator */ class LocatorController extends \PageController { @@ -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'); - } - } } /**