@@ -21,379 +21,379 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class LocatorController extends \PageController |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - private static $allowed_actions = [ |
|
| 28 | - 'xml', |
|
| 29 | - 'json', |
|
| 30 | - ]; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - private static $base_filter = []; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var array |
|
| 39 | - */ |
|
| 40 | - private static $base_exclude = [ |
|
| 41 | - 'Lat' => 0, |
|
| 42 | - 'Lng' => 0, |
|
| 43 | - ]; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var array |
|
| 47 | - */ |
|
| 48 | - private static $base_filter_any = []; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var bool |
|
| 52 | - */ |
|
| 53 | - private static $bootstrapify = true; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * ID of map container |
|
| 57 | - * |
|
| 58 | - * @var string |
|
| 59 | - */ |
|
| 60 | - private static $map_container = 'map'; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * class of location list container |
|
| 64 | - * |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - private static $list_container = 'loc-list'; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The zoom level of the map |
|
| 71 | - * @var int |
|
| 72 | - */ |
|
| 73 | - private static $zoom = 12; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * The minimum zoom level the map can have |
|
| 77 | - * @var int |
|
| 78 | - */ |
|
| 79 | - private static $min_zoom = 6; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * The maximum zoom level the map can have |
|
| 83 | - * @var int |
|
| 84 | - */ |
|
| 85 | - private static $max_zoom = 18; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * If double clicking the map should not zoom |
|
| 89 | - * @var bool |
|
| 90 | - */ |
|
| 91 | - private static $disable_double_click_zoom = true; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * If the map should disable zoom by scrollwheel |
|
| 95 | - * @var bool |
|
| 96 | - */ |
|
| 97 | - private static $scrollwheel = false; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * If the map should show naviagtion controls |
|
| 101 | - * @var bool |
|
| 102 | - */ |
|
| 103 | - private static $navigation_control = false; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @var bool |
|
| 107 | - */ |
|
| 108 | - private static $draggable = false; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @var DataList|ArrayList |
|
| 112 | - */ |
|
| 113 | - protected $locations; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Set Requirements based on input from CMS |
|
| 117 | - */ |
|
| 118 | - public function init() |
|
| 119 | - { |
|
| 120 | - parent::init(); |
|
| 121 | - |
|
| 122 | - // prevent init of map if no query |
|
| 123 | - $request = Controller::curr()->getRequest(); |
|
| 124 | - if (!$this->getTrigger($request)) { |
|
| 125 | - return; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - $locations = $this->getLocations(); |
|
| 129 | - |
|
| 130 | - // prevent init of map if there are no locations |
|
| 131 | - if (!$locations) { |
|
| 132 | - return; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // google maps api key |
|
| 136 | - $key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key'); |
|
| 137 | - Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key); |
|
| 138 | - |
|
| 139 | - $mapSettings = [ |
|
| 140 | - 'fullMapStart' => true, |
|
| 141 | - 'maxDistance' => true, |
|
| 142 | - 'storeLimit' => -1, |
|
| 143 | - 'originMarker' => true, |
|
| 144 | - 'slideMap' => false, |
|
| 145 | - 'distanceAlert' => -1, |
|
| 146 | - 'featuredLocations' => false, |
|
| 147 | - 'defaultLat' => 0, |
|
| 148 | - 'defaultLng' => 0, |
|
| 149 | - 'mapSettings' => [ |
|
| 150 | - 'zoom' => Config::inst()->get(LocatorController::class, 'zoom'), |
|
| 151 | - 'minZoom' => Config::inst()->get(LocatorController::class, 'min_zoom'), |
|
| 152 | - 'maxZoom' => Config::inst()->get(LocatorController::class, 'max_zoom'), |
|
| 153 | - 'mapTypeId' => 'google.maps.MapTypeId.ROADMAP', |
|
| 154 | - 'disableDoubleClickZoom' => Config::inst()->get(LocatorController::class, 'disable_double_click_zoom'), |
|
| 155 | - 'scrollwheel' => Config::inst()->get(LocatorController::class, 'scrollwheel'), |
|
| 156 | - 'navigationControl' => Config::inst()->get(LocatorController::class, 'navigation_control'), |
|
| 157 | - 'draggable' => Config::inst()->get(LocatorController::class, 'draggable'), |
|
| 158 | - ], |
|
| 159 | - ]; |
|
| 160 | - |
|
| 161 | - $mapSettings['listTemplatePath'] = $this->getListTemplate(); |
|
| 162 | - $mapSettings['infowindowTemplatePath'] = $this->getInfoWindowTemplate(); |
|
| 163 | - $mapSettings['lengthUnit'] = $this->data()->Unit == 'km' ? 'km' : 'm'; |
|
| 164 | - $mapSettings['mapID'] = Config::inst()->get(LocatorController::class, 'map_container'); |
|
| 165 | - $mapSettings['locationList'] = Config::inst()->get(LocatorController::class, 'list_container'); |
|
| 166 | - |
|
| 167 | - if ($locations->filter('Featured', true)->count() > 0) { |
|
| 168 | - $mapSettings['featuredLocations'] = true; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - // map config based on user input in Settings tab |
|
| 172 | - $limit = Config::inst()->get(LocatorController::class, 'limit'); |
|
| 173 | - if (0 < $limit) { |
|
| 174 | - $mapSettings['storeLimit'] = $limit; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if ($coords = $this->getAddressSearchCoords()) { |
|
| 178 | - $mapSettings['defaultLat'] = $coords->getField("Lat"); |
|
| 179 | - $mapSettings['defaultLat'] = $coords->getField("Lng"); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - // pass GET variables to xml action |
|
| 183 | - $vars = $this->request->getVars(); |
|
| 184 | - unset($vars['url']); |
|
| 185 | - $url = ''; |
|
| 186 | - if (count($vars)) { |
|
| 187 | - $url .= '?' . http_build_query($vars); |
|
| 188 | - } |
|
| 189 | - $mapSettings['dataLocation'] = Controller::join_links($this->Link(), 'xml.xml', $url); |
|
| 190 | - |
|
| 191 | - if ($stylePath = $this->getMapStyleJSONPath()) { |
|
| 192 | - if ($style = file_get_contents($stylePath)) { |
|
| 193 | - $mapSettings['mapSettings']['styles'] = json_decode($style); |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - if ($imagePath = $this->getMarkerIcon()) { |
|
| 198 | - $mapSettings['markerImg'] = $imagePath; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $this->extend('modifyMapSettings', $mapSettings); |
|
| 202 | - |
|
| 203 | - $encoded = json_encode($mapSettings, JSON_UNESCAPED_SLASHES); |
|
| 204 | - // mapTypeId is a javascript object |
|
| 205 | - $encoded = preg_replace('/("mapTypeId"\s*:\s*)"(.+?)"/i', '$1$2', $encoded); |
|
| 206 | - |
|
| 207 | - $this->extend('modifyMapSettingsEncoded', $encoded); |
|
| 208 | - // init map |
|
| 209 | - Requirements::customScript(" |
|
| 24 | + /** |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + private static $allowed_actions = [ |
|
| 28 | + 'xml', |
|
| 29 | + 'json', |
|
| 30 | + ]; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + private static $base_filter = []; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var array |
|
| 39 | + */ |
|
| 40 | + private static $base_exclude = [ |
|
| 41 | + 'Lat' => 0, |
|
| 42 | + 'Lng' => 0, |
|
| 43 | + ]; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var array |
|
| 47 | + */ |
|
| 48 | + private static $base_filter_any = []; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var bool |
|
| 52 | + */ |
|
| 53 | + private static $bootstrapify = true; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * ID of map container |
|
| 57 | + * |
|
| 58 | + * @var string |
|
| 59 | + */ |
|
| 60 | + private static $map_container = 'map'; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * class of location list container |
|
| 64 | + * |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + private static $list_container = 'loc-list'; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The zoom level of the map |
|
| 71 | + * @var int |
|
| 72 | + */ |
|
| 73 | + private static $zoom = 12; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * The minimum zoom level the map can have |
|
| 77 | + * @var int |
|
| 78 | + */ |
|
| 79 | + private static $min_zoom = 6; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * The maximum zoom level the map can have |
|
| 83 | + * @var int |
|
| 84 | + */ |
|
| 85 | + private static $max_zoom = 18; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * If double clicking the map should not zoom |
|
| 89 | + * @var bool |
|
| 90 | + */ |
|
| 91 | + private static $disable_double_click_zoom = true; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * If the map should disable zoom by scrollwheel |
|
| 95 | + * @var bool |
|
| 96 | + */ |
|
| 97 | + private static $scrollwheel = false; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * If the map should show naviagtion controls |
|
| 101 | + * @var bool |
|
| 102 | + */ |
|
| 103 | + private static $navigation_control = false; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @var bool |
|
| 107 | + */ |
|
| 108 | + private static $draggable = false; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @var DataList|ArrayList |
|
| 112 | + */ |
|
| 113 | + protected $locations; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Set Requirements based on input from CMS |
|
| 117 | + */ |
|
| 118 | + public function init() |
|
| 119 | + { |
|
| 120 | + parent::init(); |
|
| 121 | + |
|
| 122 | + // prevent init of map if no query |
|
| 123 | + $request = Controller::curr()->getRequest(); |
|
| 124 | + if (!$this->getTrigger($request)) { |
|
| 125 | + return; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + $locations = $this->getLocations(); |
|
| 129 | + |
|
| 130 | + // prevent init of map if there are no locations |
|
| 131 | + if (!$locations) { |
|
| 132 | + return; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // google maps api key |
|
| 136 | + $key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key'); |
|
| 137 | + Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key); |
|
| 138 | + |
|
| 139 | + $mapSettings = [ |
|
| 140 | + 'fullMapStart' => true, |
|
| 141 | + 'maxDistance' => true, |
|
| 142 | + 'storeLimit' => -1, |
|
| 143 | + 'originMarker' => true, |
|
| 144 | + 'slideMap' => false, |
|
| 145 | + 'distanceAlert' => -1, |
|
| 146 | + 'featuredLocations' => false, |
|
| 147 | + 'defaultLat' => 0, |
|
| 148 | + 'defaultLng' => 0, |
|
| 149 | + 'mapSettings' => [ |
|
| 150 | + 'zoom' => Config::inst()->get(LocatorController::class, 'zoom'), |
|
| 151 | + 'minZoom' => Config::inst()->get(LocatorController::class, 'min_zoom'), |
|
| 152 | + 'maxZoom' => Config::inst()->get(LocatorController::class, 'max_zoom'), |
|
| 153 | + 'mapTypeId' => 'google.maps.MapTypeId.ROADMAP', |
|
| 154 | + 'disableDoubleClickZoom' => Config::inst()->get(LocatorController::class, 'disable_double_click_zoom'), |
|
| 155 | + 'scrollwheel' => Config::inst()->get(LocatorController::class, 'scrollwheel'), |
|
| 156 | + 'navigationControl' => Config::inst()->get(LocatorController::class, 'navigation_control'), |
|
| 157 | + 'draggable' => Config::inst()->get(LocatorController::class, 'draggable'), |
|
| 158 | + ], |
|
| 159 | + ]; |
|
| 160 | + |
|
| 161 | + $mapSettings['listTemplatePath'] = $this->getListTemplate(); |
|
| 162 | + $mapSettings['infowindowTemplatePath'] = $this->getInfoWindowTemplate(); |
|
| 163 | + $mapSettings['lengthUnit'] = $this->data()->Unit == 'km' ? 'km' : 'm'; |
|
| 164 | + $mapSettings['mapID'] = Config::inst()->get(LocatorController::class, 'map_container'); |
|
| 165 | + $mapSettings['locationList'] = Config::inst()->get(LocatorController::class, 'list_container'); |
|
| 166 | + |
|
| 167 | + if ($locations->filter('Featured', true)->count() > 0) { |
|
| 168 | + $mapSettings['featuredLocations'] = true; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + // map config based on user input in Settings tab |
|
| 172 | + $limit = Config::inst()->get(LocatorController::class, 'limit'); |
|
| 173 | + if (0 < $limit) { |
|
| 174 | + $mapSettings['storeLimit'] = $limit; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if ($coords = $this->getAddressSearchCoords()) { |
|
| 178 | + $mapSettings['defaultLat'] = $coords->getField("Lat"); |
|
| 179 | + $mapSettings['defaultLat'] = $coords->getField("Lng"); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + // pass GET variables to xml action |
|
| 183 | + $vars = $this->request->getVars(); |
|
| 184 | + unset($vars['url']); |
|
| 185 | + $url = ''; |
|
| 186 | + if (count($vars)) { |
|
| 187 | + $url .= '?' . http_build_query($vars); |
|
| 188 | + } |
|
| 189 | + $mapSettings['dataLocation'] = Controller::join_links($this->Link(), 'xml.xml', $url); |
|
| 190 | + |
|
| 191 | + if ($stylePath = $this->getMapStyleJSONPath()) { |
|
| 192 | + if ($style = file_get_contents($stylePath)) { |
|
| 193 | + $mapSettings['mapSettings']['styles'] = json_decode($style); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + if ($imagePath = $this->getMarkerIcon()) { |
|
| 198 | + $mapSettings['markerImg'] = $imagePath; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $this->extend('modifyMapSettings', $mapSettings); |
|
| 202 | + |
|
| 203 | + $encoded = json_encode($mapSettings, JSON_UNESCAPED_SLASHES); |
|
| 204 | + // mapTypeId is a javascript object |
|
| 205 | + $encoded = preg_replace('/("mapTypeId"\s*:\s*)"(.+?)"/i', '$1$2', $encoded); |
|
| 206 | + |
|
| 207 | + $this->extend('modifyMapSettingsEncoded', $encoded); |
|
| 208 | + // init map |
|
| 209 | + Requirements::customScript(" |
|
| 210 | 210 | $(function(){ |
| 211 | 211 | $('#map-container').storeLocator({$encoded}); |
| 212 | 212 | }); |
| 213 | 213 | ", 'jquery-locator'); |
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @param HTTPRequest $request |
|
| 218 | - * |
|
| 219 | - * @return bool |
|
| 220 | - */ |
|
| 221 | - public function getTrigger(HTTPRequest $request = null) |
|
| 222 | - { |
|
| 223 | - if ($request === null) { |
|
| 224 | - $request = $this->getRequest(); |
|
| 225 | - } |
|
| 226 | - return !empty($this->getRequest()->getVars()) || $this->data()->ResultsOnLoad; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * @return ArrayList|DataList |
|
| 231 | - */ |
|
| 232 | - public function getLocations() |
|
| 233 | - { |
|
| 234 | - if (!$this->locations) { |
|
| 235 | - $this->setLocations($this->request); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return $this->locations; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * @param HTTPRequest|null $request |
|
| 243 | - * |
|
| 244 | - * @return $this |
|
| 245 | - */ |
|
| 246 | - public function setLocations(HTTPRequest $request = null) |
|
| 247 | - { |
|
| 248 | - |
|
| 249 | - if ($request === null) { |
|
| 250 | - $request = $this->request; |
|
| 251 | - } |
|
| 252 | - $filter = $this->config()->get('base_filter'); |
|
| 253 | - |
|
| 254 | - $categoryVar = $this->data()->config()->get('category_var'); |
|
| 255 | - if ($request->getVar($categoryVar)) { |
|
| 256 | - $filter['Categories.ID'] = $request->getVar($categoryVar); |
|
| 257 | - } else { |
|
| 258 | - if ($this->getPageCategories()->exists()) { |
|
| 259 | - foreach ($this->getPageCategories() as $category) { |
|
| 260 | - $filter['Categories.ID'][] = $category->ID; |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - $this->extend('updateLocatorFilter', $filter, $request); |
|
| 266 | - |
|
| 267 | - $filterAny = $this->config()->get('base_filter_any'); |
|
| 268 | - $this->extend('updateLocatorFilterAny', $filterAny, $request); |
|
| 269 | - |
|
| 270 | - $exclude = $this->config()->get('base_exclude'); |
|
| 271 | - $this->extend('updateLocatorExclude', $exclude, $request); |
|
| 272 | - |
|
| 273 | - $class = $this->data()->ClassName; |
|
| 274 | - $locations = $class::get_locations($filter, $filterAny, $exclude); |
|
| 275 | - $locations = DataToArrayListHelper::to_array_list($locations); |
|
| 276 | - |
|
| 277 | - //allow for adjusting list post possible distance calculation |
|
| 278 | - $this->extend('updateLocationList', $locations); |
|
| 279 | - |
|
| 280 | - if ($locations->canSortBy('Distance')) { |
|
| 281 | - $locations = $locations->sort('Distance'); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - if ($this->getShowRadius()) { |
|
| 285 | - $radiusVar = $this->data()->config()->get('radius_var'); |
|
| 286 | - |
|
| 287 | - if ($radius = (int)$request->getVar($radiusVar)) { |
|
| 288 | - $locations = $locations->filterByCallback(function ($location) use (&$radius) { |
|
| 289 | - return $location->Distance <= $radius; |
|
| 290 | - }); |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - //allow for returning list to be set as |
|
| 295 | - $this->extend('updateListType', $locations); |
|
| 296 | - |
|
| 297 | - $limit = $this->getLimit(); |
|
| 298 | - if ($limit > 0) { |
|
| 299 | - $locations = $locations->limit($limit); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - $this->locations = $locations; |
|
| 303 | - |
|
| 304 | - return $this; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * @return ArrayData|boolean |
|
| 309 | - */ |
|
| 310 | - public function getAddressSearchCoords() |
|
| 311 | - { |
|
| 312 | - $addressVar = Config::inst()->get(DistanceDataExtension::class, 'address_var'); |
|
| 313 | - if (!$this->request->getVar($addressVar)) { |
|
| 314 | - return false; |
|
| 315 | - } |
|
| 316 | - if (class_exists(GoogleGeocoder::class)) { |
|
| 317 | - $geocoder = new GoogleGeocoder($this->request->getVar($addressVar)); |
|
| 318 | - $response = $geocoder->getResult(); |
|
| 319 | - $lat = $response->getLatitude(); |
|
| 320 | - $lng = $response->getLongitude(); |
|
| 321 | - |
|
| 322 | - return new ArrayData([ |
|
| 323 | - "Lat" => $lat, |
|
| 324 | - "Lng" => $lng, |
|
| 325 | - ]); |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * @param HTTPRequest $request |
|
| 331 | - * |
|
| 332 | - * @return \SilverStripe\View\ViewableData_Customised |
|
| 333 | - */ |
|
| 334 | - public function index(HTTPRequest $request) |
|
| 335 | - { |
|
| 336 | - if ($this->getTrigger($request)) { |
|
| 337 | - $locations = $this->getLocations(); |
|
| 338 | - } else { |
|
| 339 | - $locations = ArrayList::create(); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - return $this->customise([ |
|
| 343 | - 'Locations' => $locations, |
|
| 344 | - ]); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Renders locations in xml format |
|
| 349 | - * |
|
| 350 | - * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 351 | - */ |
|
| 352 | - public function xml() |
|
| 353 | - { |
|
| 354 | - $this->getResponse()->addHeader("Content-Type", "application/xml"); |
|
| 355 | - $data = new ArrayData([ |
|
| 356 | - "Locations" => $this->getLocations(), |
|
| 357 | - "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 358 | - ]); |
|
| 359 | - |
|
| 360 | - return $data->renderWith('Dynamic/Locator/Data/XML'); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * Renders locations in json format |
|
| 365 | - * |
|
| 366 | - * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 367 | - */ |
|
| 368 | - public function json() |
|
| 369 | - { |
|
| 370 | - $this->getResponse()->addHeader("Content-Type", "application/json"); |
|
| 371 | - $data = new ArrayData([ |
|
| 372 | - "Locations" => $this->getLocations(), |
|
| 373 | - "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 374 | - ]); |
|
| 375 | - |
|
| 376 | - return $data->renderWith('Dynamic/Locator/Data/JSON'); |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * LocationSearch form. |
|
| 381 | - * |
|
| 382 | - * Search form for locations, updates map and results list via AJAX |
|
| 383 | - * |
|
| 384 | - * @return \SilverStripe\Forms\Form |
|
| 385 | - */ |
|
| 386 | - public function LocationSearch() |
|
| 387 | - { |
|
| 388 | - |
|
| 389 | - $form = LocatorForm::create($this, 'LocationSearch'); |
|
| 390 | - |
|
| 391 | - $this->extend('updateLocationSearch', $form); |
|
| 392 | - |
|
| 393 | - return $form |
|
| 394 | - ->setFormMethod('GET') |
|
| 395 | - ->setFormAction($this->Link()) |
|
| 396 | - ->disableSecurityToken() |
|
| 397 | - ->loadDataFrom($this->request->getVars()); |
|
| 398 | - } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @param HTTPRequest $request |
|
| 218 | + * |
|
| 219 | + * @return bool |
|
| 220 | + */ |
|
| 221 | + public function getTrigger(HTTPRequest $request = null) |
|
| 222 | + { |
|
| 223 | + if ($request === null) { |
|
| 224 | + $request = $this->getRequest(); |
|
| 225 | + } |
|
| 226 | + return !empty($this->getRequest()->getVars()) || $this->data()->ResultsOnLoad; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * @return ArrayList|DataList |
|
| 231 | + */ |
|
| 232 | + public function getLocations() |
|
| 233 | + { |
|
| 234 | + if (!$this->locations) { |
|
| 235 | + $this->setLocations($this->request); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + return $this->locations; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * @param HTTPRequest|null $request |
|
| 243 | + * |
|
| 244 | + * @return $this |
|
| 245 | + */ |
|
| 246 | + public function setLocations(HTTPRequest $request = null) |
|
| 247 | + { |
|
| 248 | + |
|
| 249 | + if ($request === null) { |
|
| 250 | + $request = $this->request; |
|
| 251 | + } |
|
| 252 | + $filter = $this->config()->get('base_filter'); |
|
| 253 | + |
|
| 254 | + $categoryVar = $this->data()->config()->get('category_var'); |
|
| 255 | + if ($request->getVar($categoryVar)) { |
|
| 256 | + $filter['Categories.ID'] = $request->getVar($categoryVar); |
|
| 257 | + } else { |
|
| 258 | + if ($this->getPageCategories()->exists()) { |
|
| 259 | + foreach ($this->getPageCategories() as $category) { |
|
| 260 | + $filter['Categories.ID'][] = $category->ID; |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + $this->extend('updateLocatorFilter', $filter, $request); |
|
| 266 | + |
|
| 267 | + $filterAny = $this->config()->get('base_filter_any'); |
|
| 268 | + $this->extend('updateLocatorFilterAny', $filterAny, $request); |
|
| 269 | + |
|
| 270 | + $exclude = $this->config()->get('base_exclude'); |
|
| 271 | + $this->extend('updateLocatorExclude', $exclude, $request); |
|
| 272 | + |
|
| 273 | + $class = $this->data()->ClassName; |
|
| 274 | + $locations = $class::get_locations($filter, $filterAny, $exclude); |
|
| 275 | + $locations = DataToArrayListHelper::to_array_list($locations); |
|
| 276 | + |
|
| 277 | + //allow for adjusting list post possible distance calculation |
|
| 278 | + $this->extend('updateLocationList', $locations); |
|
| 279 | + |
|
| 280 | + if ($locations->canSortBy('Distance')) { |
|
| 281 | + $locations = $locations->sort('Distance'); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + if ($this->getShowRadius()) { |
|
| 285 | + $radiusVar = $this->data()->config()->get('radius_var'); |
|
| 286 | + |
|
| 287 | + if ($radius = (int)$request->getVar($radiusVar)) { |
|
| 288 | + $locations = $locations->filterByCallback(function ($location) use (&$radius) { |
|
| 289 | + return $location->Distance <= $radius; |
|
| 290 | + }); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + //allow for returning list to be set as |
|
| 295 | + $this->extend('updateListType', $locations); |
|
| 296 | + |
|
| 297 | + $limit = $this->getLimit(); |
|
| 298 | + if ($limit > 0) { |
|
| 299 | + $locations = $locations->limit($limit); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + $this->locations = $locations; |
|
| 303 | + |
|
| 304 | + return $this; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * @return ArrayData|boolean |
|
| 309 | + */ |
|
| 310 | + public function getAddressSearchCoords() |
|
| 311 | + { |
|
| 312 | + $addressVar = Config::inst()->get(DistanceDataExtension::class, 'address_var'); |
|
| 313 | + if (!$this->request->getVar($addressVar)) { |
|
| 314 | + return false; |
|
| 315 | + } |
|
| 316 | + if (class_exists(GoogleGeocoder::class)) { |
|
| 317 | + $geocoder = new GoogleGeocoder($this->request->getVar($addressVar)); |
|
| 318 | + $response = $geocoder->getResult(); |
|
| 319 | + $lat = $response->getLatitude(); |
|
| 320 | + $lng = $response->getLongitude(); |
|
| 321 | + |
|
| 322 | + return new ArrayData([ |
|
| 323 | + "Lat" => $lat, |
|
| 324 | + "Lng" => $lng, |
|
| 325 | + ]); |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * @param HTTPRequest $request |
|
| 331 | + * |
|
| 332 | + * @return \SilverStripe\View\ViewableData_Customised |
|
| 333 | + */ |
|
| 334 | + public function index(HTTPRequest $request) |
|
| 335 | + { |
|
| 336 | + if ($this->getTrigger($request)) { |
|
| 337 | + $locations = $this->getLocations(); |
|
| 338 | + } else { |
|
| 339 | + $locations = ArrayList::create(); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + return $this->customise([ |
|
| 343 | + 'Locations' => $locations, |
|
| 344 | + ]); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Renders locations in xml format |
|
| 349 | + * |
|
| 350 | + * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 351 | + */ |
|
| 352 | + public function xml() |
|
| 353 | + { |
|
| 354 | + $this->getResponse()->addHeader("Content-Type", "application/xml"); |
|
| 355 | + $data = new ArrayData([ |
|
| 356 | + "Locations" => $this->getLocations(), |
|
| 357 | + "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 358 | + ]); |
|
| 359 | + |
|
| 360 | + return $data->renderWith('Dynamic/Locator/Data/XML'); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * Renders locations in json format |
|
| 365 | + * |
|
| 366 | + * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 367 | + */ |
|
| 368 | + public function json() |
|
| 369 | + { |
|
| 370 | + $this->getResponse()->addHeader("Content-Type", "application/json"); |
|
| 371 | + $data = new ArrayData([ |
|
| 372 | + "Locations" => $this->getLocations(), |
|
| 373 | + "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 374 | + ]); |
|
| 375 | + |
|
| 376 | + return $data->renderWith('Dynamic/Locator/Data/JSON'); |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * LocationSearch form. |
|
| 381 | + * |
|
| 382 | + * Search form for locations, updates map and results list via AJAX |
|
| 383 | + * |
|
| 384 | + * @return \SilverStripe\Forms\Form |
|
| 385 | + */ |
|
| 386 | + public function LocationSearch() |
|
| 387 | + { |
|
| 388 | + |
|
| 389 | + $form = LocatorForm::create($this, 'LocationSearch'); |
|
| 390 | + |
|
| 391 | + $this->extend('updateLocationSearch', $form); |
|
| 392 | + |
|
| 393 | + return $form |
|
| 394 | + ->setFormMethod('GET') |
|
| 395 | + ->setFormAction($this->Link()) |
|
| 396 | + ->disableSecurityToken() |
|
| 397 | + ->loadDataFrom($this->request->getVars()); |
|
| 398 | + } |
|
| 399 | 399 | } |