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