@@ -20,338 +20,338 @@ |
||
20 | 20 | */ |
21 | 21 | class LocatorController extends \PageController |
22 | 22 | { |
23 | - /** |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - private static $allowed_actions = [ |
|
27 | - 'xml', |
|
28 | - 'json', |
|
29 | - ]; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var array |
|
33 | - */ |
|
34 | - private static $base_filter = []; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - private static $base_exclude = [ |
|
40 | - 'Lat' => 0, |
|
41 | - 'Lng' => 0, |
|
42 | - ]; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var array |
|
46 | - */ |
|
47 | - private static $base_filter_any = []; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var bool |
|
51 | - */ |
|
52 | - private static $bootstrapify = true; |
|
53 | - |
|
54 | - /** |
|
55 | - * ID of map container |
|
56 | - * |
|
57 | - * @var string |
|
58 | - */ |
|
59 | - private static $map_container = 'map'; |
|
60 | - |
|
61 | - /** |
|
62 | - * class of location list container |
|
63 | - * |
|
64 | - * @var string |
|
65 | - */ |
|
66 | - private static $list_container = 'loc-list'; |
|
67 | - |
|
68 | - /** |
|
69 | - * @var DataList|ArrayList |
|
70 | - */ |
|
71 | - protected $locations; |
|
72 | - |
|
73 | - /** |
|
74 | - * Set Requirements based on input from CMS |
|
75 | - */ |
|
76 | - public function init() |
|
77 | - { |
|
78 | - parent::init(); |
|
79 | - |
|
80 | - // prevent init of map if no query |
|
81 | - $request = Controller::curr()->getRequest(); |
|
82 | - if (!$this->getTrigger($request)) { |
|
83 | - return; |
|
84 | - } |
|
85 | - |
|
86 | - $locations = $this->getLocations(); |
|
87 | - |
|
88 | - // prevent init of map if there are no locations |
|
89 | - if (!$locations) { |
|
90 | - return; |
|
91 | - } |
|
92 | - |
|
93 | - // google maps api key |
|
94 | - $key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key'); |
|
95 | - Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key); |
|
96 | - |
|
97 | - $mapSettings = [ |
|
98 | - 'fullMapStart' => true, |
|
99 | - 'maxDistance' => true, |
|
100 | - 'storeLimit' => -1, |
|
101 | - 'originMarker' => true, |
|
102 | - 'slideMap' => false, |
|
103 | - 'distanceAlert' => -1, |
|
104 | - 'featuredLocations' => false, |
|
105 | - 'defaultLat' => 0, |
|
106 | - 'defaultLng' => 0, |
|
107 | - 'mapSettings' => [ |
|
108 | - 'zoom' => 12, |
|
109 | - 'mapTypeId' => 'google.maps.MapTypeId.ROADMAP', |
|
110 | - 'disableDoubleClickZoom' => true, |
|
111 | - 'scrollwheel' => false, |
|
112 | - 'navigationControl' => false, |
|
113 | - 'draggable' => false |
|
114 | - ], |
|
115 | - ]; |
|
116 | - |
|
117 | - $mapSettings['listTemplatePath'] = $this->getListTemplate(); |
|
118 | - $mapSettings['infowindowTemplatePath'] = $this->getInfoWindowTemplate(); |
|
119 | - $mapSettings['lengthUnit'] = $this->data()->Unit == 'km' ? 'km' : 'm'; |
|
120 | - $mapSettings['mapID'] = Config::inst()->get(LocatorController::class, 'map_container'); |
|
121 | - $mapSettings['locationList'] = Config::inst()->get(LocatorController::class, 'list_container'); |
|
122 | - |
|
123 | - if ($locations->filter('Featured', true)->count() > 0) { |
|
124 | - $mapSettings['featuredLocations'] = true; |
|
125 | - } |
|
126 | - |
|
127 | - // map config based on user input in Settings tab |
|
128 | - $limit = Config::inst()->get(LocatorController::class, 'limit'); |
|
129 | - if (0 < $limit) { |
|
130 | - $mapSettings['storeLimit'] = $limit; |
|
131 | - } |
|
132 | - |
|
133 | - if ($coords = $this->getAddressSearchCoords()) { |
|
134 | - $mapSettings['defaultLat'] = $coords->getField("Lat"); |
|
135 | - $mapSettings['defaultLat'] = $coords->getField("Lng"); |
|
136 | - } |
|
137 | - |
|
138 | - // pass GET variables to xml action |
|
139 | - $vars = $this->request->getVars(); |
|
140 | - unset($vars['url']); |
|
141 | - $url = ''; |
|
142 | - if (count($vars)) { |
|
143 | - $url .= '?' . http_build_query($vars); |
|
144 | - } |
|
145 | - $mapSettings['dataLocation'] = Controller::join_links($this->Link(), 'xml.xml', $url); |
|
146 | - |
|
147 | - if ($stylePath = $this->getMapStyleJSONPath()) { |
|
148 | - if ($style = file_get_contents($stylePath)) { |
|
149 | - $mapSettings['mapSettings']['styles'] = json_decode($style); |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - if ($imagePath = $this->getMarkerIcon()) { |
|
154 | - $mapSettings['markerImg'] = $imagePath; |
|
155 | - } |
|
156 | - |
|
157 | - $this->extend('modifyMapSettings', $mapSettings); |
|
158 | - |
|
159 | - $encoded = json_encode($mapSettings, JSON_UNESCAPED_SLASHES); |
|
160 | - // mapTypeId is a javascript object |
|
161 | - $encoded = preg_replace('/("mapTypeId"\s*:\s*)"(.+?)"/i', '$1$2', $encoded); |
|
162 | - |
|
163 | - $this->extend('modifyMapSettingsEncoded', $encoded); |
|
164 | - // init map |
|
165 | - Requirements::customScript(" |
|
23 | + /** |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + private static $allowed_actions = [ |
|
27 | + 'xml', |
|
28 | + 'json', |
|
29 | + ]; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var array |
|
33 | + */ |
|
34 | + private static $base_filter = []; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + private static $base_exclude = [ |
|
40 | + 'Lat' => 0, |
|
41 | + 'Lng' => 0, |
|
42 | + ]; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var array |
|
46 | + */ |
|
47 | + private static $base_filter_any = []; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var bool |
|
51 | + */ |
|
52 | + private static $bootstrapify = true; |
|
53 | + |
|
54 | + /** |
|
55 | + * ID of map container |
|
56 | + * |
|
57 | + * @var string |
|
58 | + */ |
|
59 | + private static $map_container = 'map'; |
|
60 | + |
|
61 | + /** |
|
62 | + * class of location list container |
|
63 | + * |
|
64 | + * @var string |
|
65 | + */ |
|
66 | + private static $list_container = 'loc-list'; |
|
67 | + |
|
68 | + /** |
|
69 | + * @var DataList|ArrayList |
|
70 | + */ |
|
71 | + protected $locations; |
|
72 | + |
|
73 | + /** |
|
74 | + * Set Requirements based on input from CMS |
|
75 | + */ |
|
76 | + public function init() |
|
77 | + { |
|
78 | + parent::init(); |
|
79 | + |
|
80 | + // prevent init of map if no query |
|
81 | + $request = Controller::curr()->getRequest(); |
|
82 | + if (!$this->getTrigger($request)) { |
|
83 | + return; |
|
84 | + } |
|
85 | + |
|
86 | + $locations = $this->getLocations(); |
|
87 | + |
|
88 | + // prevent init of map if there are no locations |
|
89 | + if (!$locations) { |
|
90 | + return; |
|
91 | + } |
|
92 | + |
|
93 | + // google maps api key |
|
94 | + $key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key'); |
|
95 | + Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key); |
|
96 | + |
|
97 | + $mapSettings = [ |
|
98 | + 'fullMapStart' => true, |
|
99 | + 'maxDistance' => true, |
|
100 | + 'storeLimit' => -1, |
|
101 | + 'originMarker' => true, |
|
102 | + 'slideMap' => false, |
|
103 | + 'distanceAlert' => -1, |
|
104 | + 'featuredLocations' => false, |
|
105 | + 'defaultLat' => 0, |
|
106 | + 'defaultLng' => 0, |
|
107 | + 'mapSettings' => [ |
|
108 | + 'zoom' => 12, |
|
109 | + 'mapTypeId' => 'google.maps.MapTypeId.ROADMAP', |
|
110 | + 'disableDoubleClickZoom' => true, |
|
111 | + 'scrollwheel' => false, |
|
112 | + 'navigationControl' => false, |
|
113 | + 'draggable' => false |
|
114 | + ], |
|
115 | + ]; |
|
116 | + |
|
117 | + $mapSettings['listTemplatePath'] = $this->getListTemplate(); |
|
118 | + $mapSettings['infowindowTemplatePath'] = $this->getInfoWindowTemplate(); |
|
119 | + $mapSettings['lengthUnit'] = $this->data()->Unit == 'km' ? 'km' : 'm'; |
|
120 | + $mapSettings['mapID'] = Config::inst()->get(LocatorController::class, 'map_container'); |
|
121 | + $mapSettings['locationList'] = Config::inst()->get(LocatorController::class, 'list_container'); |
|
122 | + |
|
123 | + if ($locations->filter('Featured', true)->count() > 0) { |
|
124 | + $mapSettings['featuredLocations'] = true; |
|
125 | + } |
|
126 | + |
|
127 | + // map config based on user input in Settings tab |
|
128 | + $limit = Config::inst()->get(LocatorController::class, 'limit'); |
|
129 | + if (0 < $limit) { |
|
130 | + $mapSettings['storeLimit'] = $limit; |
|
131 | + } |
|
132 | + |
|
133 | + if ($coords = $this->getAddressSearchCoords()) { |
|
134 | + $mapSettings['defaultLat'] = $coords->getField("Lat"); |
|
135 | + $mapSettings['defaultLat'] = $coords->getField("Lng"); |
|
136 | + } |
|
137 | + |
|
138 | + // pass GET variables to xml action |
|
139 | + $vars = $this->request->getVars(); |
|
140 | + unset($vars['url']); |
|
141 | + $url = ''; |
|
142 | + if (count($vars)) { |
|
143 | + $url .= '?' . http_build_query($vars); |
|
144 | + } |
|
145 | + $mapSettings['dataLocation'] = Controller::join_links($this->Link(), 'xml.xml', $url); |
|
146 | + |
|
147 | + if ($stylePath = $this->getMapStyleJSONPath()) { |
|
148 | + if ($style = file_get_contents($stylePath)) { |
|
149 | + $mapSettings['mapSettings']['styles'] = json_decode($style); |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + if ($imagePath = $this->getMarkerIcon()) { |
|
154 | + $mapSettings['markerImg'] = $imagePath; |
|
155 | + } |
|
156 | + |
|
157 | + $this->extend('modifyMapSettings', $mapSettings); |
|
158 | + |
|
159 | + $encoded = json_encode($mapSettings, JSON_UNESCAPED_SLASHES); |
|
160 | + // mapTypeId is a javascript object |
|
161 | + $encoded = preg_replace('/("mapTypeId"\s*:\s*)"(.+?)"/i', '$1$2', $encoded); |
|
162 | + |
|
163 | + $this->extend('modifyMapSettingsEncoded', $encoded); |
|
164 | + // init map |
|
165 | + Requirements::customScript(" |
|
166 | 166 | $(function(){ |
167 | 167 | $('#map-container').storeLocator({$encoded}); |
168 | 168 | }); |
169 | 169 | ", 'jquery-locator'); |
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @param HTTPRequest $request |
|
174 | - * |
|
175 | - * @return bool |
|
176 | - */ |
|
177 | - public function getTrigger(HTTPRequest $request = null) |
|
178 | - { |
|
179 | - if ($request === null) { |
|
180 | - $request = $this->getRequest(); |
|
181 | - } |
|
182 | - return !empty($this->getRequest()->getVars()) || $this->data()->ResultsOnLoad; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * @return ArrayList|DataList |
|
187 | - */ |
|
188 | - public function getLocations() |
|
189 | - { |
|
190 | - if (!$this->locations) { |
|
191 | - $this->setLocations($this->request); |
|
192 | - } |
|
193 | - |
|
194 | - return $this->locations; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @param HTTPRequest|null $request |
|
199 | - * |
|
200 | - * @return $this |
|
201 | - */ |
|
202 | - public function setLocations(HTTPRequest $request = null) |
|
203 | - { |
|
204 | - |
|
205 | - if ($request === null) { |
|
206 | - $request = $this->request; |
|
207 | - } |
|
208 | - $filter = $this->config()->get('base_filter'); |
|
209 | - |
|
210 | - $categoryVar = $this->data()->config()->get('category_var'); |
|
211 | - if ($request->getVar($categoryVar)) { |
|
212 | - $filter['Categories.ID'] = $request->getVar($categoryVar); |
|
213 | - } else { |
|
214 | - if ($this->getPageCategories()->exists()) { |
|
215 | - foreach ($this->getPageCategories() as $category) { |
|
216 | - $filter['Categories.ID'][] = $category->ID; |
|
217 | - } |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - $this->extend('updateLocatorFilter', $filter, $request); |
|
222 | - |
|
223 | - $filterAny = $this->config()->get('base_filter_any'); |
|
224 | - $this->extend('updateLocatorFilterAny', $filterAny, $request); |
|
225 | - |
|
226 | - $exclude = $this->config()->get('base_exclude'); |
|
227 | - $this->extend('updateLocatorExclude', $exclude, $request); |
|
228 | - |
|
229 | - $class = $this->data()->ClassName; |
|
230 | - $locations = $class::get_locations($filter, $filterAny, $exclude); |
|
231 | - $locations = DataToArrayListHelper::to_array_list($locations); |
|
232 | - |
|
233 | - //allow for adjusting list post possible distance calculation |
|
234 | - $this->extend('updateLocationList', $locations); |
|
235 | - |
|
236 | - if ($locations->canSortBy('Distance')) { |
|
237 | - $locations = $locations->sort('Distance'); |
|
238 | - } |
|
239 | - |
|
240 | - if ($this->getShowRadius()) { |
|
241 | - $radiusVar = $this->data()->config()->get('radius_var'); |
|
242 | - |
|
243 | - if ($radius = (int)$request->getVar($radiusVar)) { |
|
244 | - $locations = $locations->filterByCallback(function ($location) use (&$radius) { |
|
245 | - return $location->Distance <= $radius; |
|
246 | - }); |
|
247 | - } |
|
248 | - } |
|
249 | - |
|
250 | - //allow for returning list to be set as |
|
251 | - $this->extend('updateListType', $locations); |
|
252 | - |
|
253 | - $limit = $this->getLimit(); |
|
254 | - if ($limit > 0) { |
|
255 | - $locations = $locations->limit($limit); |
|
256 | - } |
|
257 | - |
|
258 | - $this->locations = $locations; |
|
259 | - |
|
260 | - return $this; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * @return ArrayData|boolean |
|
265 | - */ |
|
266 | - public function getAddressSearchCoords() |
|
267 | - { |
|
268 | - $addressVar = Config::inst()->get(DistanceDataExtension::class, 'address_var'); |
|
269 | - if (!$this->request->getVar($addressVar)) { |
|
270 | - return false; |
|
271 | - } |
|
272 | - if (class_exists(GoogleGeocoder::class)) { |
|
273 | - $geocoder = new GoogleGeocoder($this->request->getVar($addressVar)); |
|
274 | - $response = $geocoder->getResult(); |
|
275 | - $lat = $response->getLatitude(); |
|
276 | - $lng = $response->getLongitude(); |
|
277 | - |
|
278 | - return new ArrayData([ |
|
279 | - "Lat" => $lat, |
|
280 | - "Lng" => $lng, |
|
281 | - ]); |
|
282 | - } |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * @param HTTPRequest $request |
|
287 | - * |
|
288 | - * @return \SilverStripe\View\ViewableData_Customised |
|
289 | - */ |
|
290 | - public function index(HTTPRequest $request) |
|
291 | - { |
|
292 | - if ($this->getTrigger($request)) { |
|
293 | - $locations = $this->getLocations(); |
|
294 | - } else { |
|
295 | - $locations = ArrayList::create(); |
|
296 | - } |
|
297 | - |
|
298 | - return $this->customise(array( |
|
299 | - 'Locations' => $locations, |
|
300 | - )); |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Renders locations in xml format |
|
305 | - * |
|
306 | - * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
307 | - */ |
|
308 | - public function xml() |
|
309 | - { |
|
310 | - $this->getResponse()->addHeader("Content-Type", "application/xml"); |
|
311 | - $data = new ArrayData(array( |
|
312 | - "Locations" => $this->getLocations(), |
|
313 | - "AddressCoords" => $this->getAddressSearchCoords(), |
|
314 | - )); |
|
315 | - |
|
316 | - return $data->renderWith('Dynamic/Locator/Data/XML'); |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * Renders locations in json format |
|
321 | - * |
|
322 | - * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
323 | - */ |
|
324 | - public function json() |
|
325 | - { |
|
326 | - $this->getResponse()->addHeader("Content-Type", "application/json"); |
|
327 | - $data = new ArrayData(array( |
|
328 | - "Locations" => $this->getLocations(), |
|
329 | - "AddressCoords" => $this->getAddressSearchCoords(), |
|
330 | - )); |
|
331 | - |
|
332 | - return $data->renderWith('Dynamic/Locator/Data/JSON'); |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * LocationSearch form. |
|
337 | - * |
|
338 | - * Search form for locations, updates map and results list via AJAX |
|
339 | - * |
|
340 | - * @return \SilverStripe\Forms\Form |
|
341 | - */ |
|
342 | - public function LocationSearch() |
|
343 | - { |
|
344 | - |
|
345 | - $form = LocatorForm::create($this, 'LocationSearch'); |
|
346 | - if (class_exists(BootstrapForm::class) && $this->config()->get('bootstrapify')) { |
|
347 | - $form->Fields()->bootstrapify(); |
|
348 | - $form->Actions()->bootstrapify(); |
|
349 | - } |
|
350 | - |
|
351 | - return $form |
|
352 | - ->setFormMethod('GET') |
|
353 | - ->setFormAction($this->Link()) |
|
354 | - ->disableSecurityToken() |
|
355 | - ->loadDataFrom($this->request->getVars()); |
|
356 | - } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @param HTTPRequest $request |
|
174 | + * |
|
175 | + * @return bool |
|
176 | + */ |
|
177 | + public function getTrigger(HTTPRequest $request = null) |
|
178 | + { |
|
179 | + if ($request === null) { |
|
180 | + $request = $this->getRequest(); |
|
181 | + } |
|
182 | + return !empty($this->getRequest()->getVars()) || $this->data()->ResultsOnLoad; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * @return ArrayList|DataList |
|
187 | + */ |
|
188 | + public function getLocations() |
|
189 | + { |
|
190 | + if (!$this->locations) { |
|
191 | + $this->setLocations($this->request); |
|
192 | + } |
|
193 | + |
|
194 | + return $this->locations; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @param HTTPRequest|null $request |
|
199 | + * |
|
200 | + * @return $this |
|
201 | + */ |
|
202 | + public function setLocations(HTTPRequest $request = null) |
|
203 | + { |
|
204 | + |
|
205 | + if ($request === null) { |
|
206 | + $request = $this->request; |
|
207 | + } |
|
208 | + $filter = $this->config()->get('base_filter'); |
|
209 | + |
|
210 | + $categoryVar = $this->data()->config()->get('category_var'); |
|
211 | + if ($request->getVar($categoryVar)) { |
|
212 | + $filter['Categories.ID'] = $request->getVar($categoryVar); |
|
213 | + } else { |
|
214 | + if ($this->getPageCategories()->exists()) { |
|
215 | + foreach ($this->getPageCategories() as $category) { |
|
216 | + $filter['Categories.ID'][] = $category->ID; |
|
217 | + } |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + $this->extend('updateLocatorFilter', $filter, $request); |
|
222 | + |
|
223 | + $filterAny = $this->config()->get('base_filter_any'); |
|
224 | + $this->extend('updateLocatorFilterAny', $filterAny, $request); |
|
225 | + |
|
226 | + $exclude = $this->config()->get('base_exclude'); |
|
227 | + $this->extend('updateLocatorExclude', $exclude, $request); |
|
228 | + |
|
229 | + $class = $this->data()->ClassName; |
|
230 | + $locations = $class::get_locations($filter, $filterAny, $exclude); |
|
231 | + $locations = DataToArrayListHelper::to_array_list($locations); |
|
232 | + |
|
233 | + //allow for adjusting list post possible distance calculation |
|
234 | + $this->extend('updateLocationList', $locations); |
|
235 | + |
|
236 | + if ($locations->canSortBy('Distance')) { |
|
237 | + $locations = $locations->sort('Distance'); |
|
238 | + } |
|
239 | + |
|
240 | + if ($this->getShowRadius()) { |
|
241 | + $radiusVar = $this->data()->config()->get('radius_var'); |
|
242 | + |
|
243 | + if ($radius = (int)$request->getVar($radiusVar)) { |
|
244 | + $locations = $locations->filterByCallback(function ($location) use (&$radius) { |
|
245 | + return $location->Distance <= $radius; |
|
246 | + }); |
|
247 | + } |
|
248 | + } |
|
249 | + |
|
250 | + //allow for returning list to be set as |
|
251 | + $this->extend('updateListType', $locations); |
|
252 | + |
|
253 | + $limit = $this->getLimit(); |
|
254 | + if ($limit > 0) { |
|
255 | + $locations = $locations->limit($limit); |
|
256 | + } |
|
257 | + |
|
258 | + $this->locations = $locations; |
|
259 | + |
|
260 | + return $this; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * @return ArrayData|boolean |
|
265 | + */ |
|
266 | + public function getAddressSearchCoords() |
|
267 | + { |
|
268 | + $addressVar = Config::inst()->get(DistanceDataExtension::class, 'address_var'); |
|
269 | + if (!$this->request->getVar($addressVar)) { |
|
270 | + return false; |
|
271 | + } |
|
272 | + if (class_exists(GoogleGeocoder::class)) { |
|
273 | + $geocoder = new GoogleGeocoder($this->request->getVar($addressVar)); |
|
274 | + $response = $geocoder->getResult(); |
|
275 | + $lat = $response->getLatitude(); |
|
276 | + $lng = $response->getLongitude(); |
|
277 | + |
|
278 | + return new ArrayData([ |
|
279 | + "Lat" => $lat, |
|
280 | + "Lng" => $lng, |
|
281 | + ]); |
|
282 | + } |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * @param HTTPRequest $request |
|
287 | + * |
|
288 | + * @return \SilverStripe\View\ViewableData_Customised |
|
289 | + */ |
|
290 | + public function index(HTTPRequest $request) |
|
291 | + { |
|
292 | + if ($this->getTrigger($request)) { |
|
293 | + $locations = $this->getLocations(); |
|
294 | + } else { |
|
295 | + $locations = ArrayList::create(); |
|
296 | + } |
|
297 | + |
|
298 | + return $this->customise(array( |
|
299 | + 'Locations' => $locations, |
|
300 | + )); |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Renders locations in xml format |
|
305 | + * |
|
306 | + * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
307 | + */ |
|
308 | + public function xml() |
|
309 | + { |
|
310 | + $this->getResponse()->addHeader("Content-Type", "application/xml"); |
|
311 | + $data = new ArrayData(array( |
|
312 | + "Locations" => $this->getLocations(), |
|
313 | + "AddressCoords" => $this->getAddressSearchCoords(), |
|
314 | + )); |
|
315 | + |
|
316 | + return $data->renderWith('Dynamic/Locator/Data/XML'); |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * Renders locations in json format |
|
321 | + * |
|
322 | + * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
323 | + */ |
|
324 | + public function json() |
|
325 | + { |
|
326 | + $this->getResponse()->addHeader("Content-Type", "application/json"); |
|
327 | + $data = new ArrayData(array( |
|
328 | + "Locations" => $this->getLocations(), |
|
329 | + "AddressCoords" => $this->getAddressSearchCoords(), |
|
330 | + )); |
|
331 | + |
|
332 | + return $data->renderWith('Dynamic/Locator/Data/JSON'); |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * LocationSearch form. |
|
337 | + * |
|
338 | + * Search form for locations, updates map and results list via AJAX |
|
339 | + * |
|
340 | + * @return \SilverStripe\Forms\Form |
|
341 | + */ |
|
342 | + public function LocationSearch() |
|
343 | + { |
|
344 | + |
|
345 | + $form = LocatorForm::create($this, 'LocationSearch'); |
|
346 | + if (class_exists(BootstrapForm::class) && $this->config()->get('bootstrapify')) { |
|
347 | + $form->Fields()->bootstrapify(); |
|
348 | + $form->Actions()->bootstrapify(); |
|
349 | + } |
|
350 | + |
|
351 | + return $form |
|
352 | + ->setFormMethod('GET') |
|
353 | + ->setFormAction($this->Link()) |
|
354 | + ->disableSecurityToken() |
|
355 | + ->loadDataFrom($this->request->getVars()); |
|
356 | + } |
|
357 | 357 | } |
@@ -27,258 +27,258 @@ |
||
27 | 27 | */ |
28 | 28 | class Locator extends \Page |
29 | 29 | { |
30 | - /** |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - private static $singular_name = 'Locator'; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - private static $plural_name = 'Locators'; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - private static $description = 'Display locations on a map'; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - private static $db = array( |
|
49 | - 'Unit' => 'Enum("m,km","m")', |
|
50 | - 'ResultsOnLoad' => 'Boolean', |
|
51 | - ); |
|
52 | - |
|
53 | - /** |
|
54 | - * @var array |
|
55 | - */ |
|
56 | - private static $many_many = array( |
|
57 | - 'Categories' => LocationCategory::class, |
|
58 | - ); |
|
59 | - |
|
60 | - /** |
|
61 | - * @var array |
|
62 | - */ |
|
63 | - private static $defaults = [ |
|
64 | - 'ResultsOnLoad' => 0, |
|
65 | - ]; |
|
66 | - |
|
67 | - /** |
|
68 | - * @var string |
|
69 | - */ |
|
70 | - private static $table_name = 'Locator'; |
|
71 | - |
|
72 | - /** |
|
73 | - * @var string |
|
74 | - */ |
|
75 | - private static $location_class = Location::class; |
|
76 | - |
|
77 | - /** |
|
78 | - * @return FieldList |
|
79 | - */ |
|
80 | - public function getCMSFields() |
|
81 | - { |
|
82 | - $this->beforeUpdateCMSFields(function ($fields) { |
|
83 | - // Settings |
|
84 | - $fields->addFieldsToTab('Root.Settings', array( |
|
85 | - HeaderField::create('DisplayOptions', 'Display Options', 3), |
|
86 | - OptionsetField::create('Unit', 'Unit of measure', array('m' => 'Miles', 'km' => 'Kilometers')), |
|
87 | - CheckboxField::create('ResultsOnLoad', 'Show results on page load') |
|
88 | - ->setDescription('For larger collections of locations, it is |
|
30 | + /** |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + private static $singular_name = 'Locator'; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + private static $plural_name = 'Locators'; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + private static $description = 'Display locations on a map'; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + private static $db = array( |
|
49 | + 'Unit' => 'Enum("m,km","m")', |
|
50 | + 'ResultsOnLoad' => 'Boolean', |
|
51 | + ); |
|
52 | + |
|
53 | + /** |
|
54 | + * @var array |
|
55 | + */ |
|
56 | + private static $many_many = array( |
|
57 | + 'Categories' => LocationCategory::class, |
|
58 | + ); |
|
59 | + |
|
60 | + /** |
|
61 | + * @var array |
|
62 | + */ |
|
63 | + private static $defaults = [ |
|
64 | + 'ResultsOnLoad' => 0, |
|
65 | + ]; |
|
66 | + |
|
67 | + /** |
|
68 | + * @var string |
|
69 | + */ |
|
70 | + private static $table_name = 'Locator'; |
|
71 | + |
|
72 | + /** |
|
73 | + * @var string |
|
74 | + */ |
|
75 | + private static $location_class = Location::class; |
|
76 | + |
|
77 | + /** |
|
78 | + * @return FieldList |
|
79 | + */ |
|
80 | + public function getCMSFields() |
|
81 | + { |
|
82 | + $this->beforeUpdateCMSFields(function ($fields) { |
|
83 | + // Settings |
|
84 | + $fields->addFieldsToTab('Root.Settings', array( |
|
85 | + HeaderField::create('DisplayOptions', 'Display Options', 3), |
|
86 | + OptionsetField::create('Unit', 'Unit of measure', array('m' => 'Miles', 'km' => 'Kilometers')), |
|
87 | + CheckboxField::create('ResultsOnLoad', 'Show results on page load') |
|
88 | + ->setDescription('For larger collections of locations, it is |
|
89 | 89 | recommended to only show a limited amount of results after a location |
90 | 90 | search.') |
91 | - )); |
|
92 | - |
|
93 | - // Filter categories |
|
94 | - $config = GridFieldConfig_RelationEditor::create(); |
|
95 | - $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class); |
|
96 | - $config->addComponent(new GridFieldAddExistingSearchButton()); |
|
97 | - $categories = $this->Categories(); |
|
98 | - $categoriesField = GridField::create('Categories', 'Categories', $categories, $config) |
|
99 | - ->setDescription('only show locations from the selected category'); |
|
100 | - |
|
101 | - // Filter |
|
102 | - $fields->addFieldsToTab('Root.Filter', array( |
|
103 | - HeaderField::create('CategoryOptionsHeader', 'Location Filtering', 3), |
|
104 | - $categoriesField, |
|
105 | - )); |
|
106 | - }); |
|
107 | - |
|
108 | - return parent::getCMSFields(); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @param array $filter |
|
113 | - * @param array $filterAny |
|
114 | - * @param array $exclude |
|
115 | - * @param null|callable $callback |
|
116 | - * |
|
117 | - * @return DataList|ArrayList |
|
118 | - */ |
|
119 | - public static function get_locations( |
|
120 | - $filter = [], |
|
121 | - $filterAny = [], |
|
122 | - $exclude = [], |
|
123 | - $callback = null |
|
124 | - ) { |
|
125 | - $locationClass = Config::inst()->get(static::class, 'location_class'); |
|
126 | - $locations = $locationClass::get()->filter($filter)->exclude($exclude); |
|
127 | - |
|
128 | - if (!empty($filterAny)) { |
|
129 | - $locations = $locations->filterAny($filterAny); |
|
130 | - } |
|
131 | - if (!empty($exclude)) { |
|
132 | - $locations = $locations->exclude($exclude); |
|
133 | - } |
|
134 | - |
|
135 | - if ($callback !== null && is_callable($callback)) { |
|
136 | - $locations->filterByCallback($callback); |
|
137 | - } |
|
138 | - |
|
139 | - return $locations; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @return DataList |
|
144 | - */ |
|
145 | - public static function get_all_categories() |
|
146 | - { |
|
147 | - return LocationCategory::get(); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @return bool |
|
152 | - */ |
|
153 | - public function getPageCategories() |
|
154 | - { |
|
155 | - return self::locator_categories_by_locator($this->ID); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @param int $id |
|
160 | - * |
|
161 | - * @return bool| |
|
162 | - */ |
|
163 | - public static function locator_categories_by_locator($id = 0) |
|
164 | - { |
|
165 | - if ($id == 0) { |
|
166 | - return false; |
|
167 | - } |
|
168 | - |
|
169 | - return static::get()->byID($id)->getUsedCategories(); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Gets the list of radii |
|
174 | - * |
|
175 | - * @return ArrayList |
|
176 | - */ |
|
177 | - public function getRadii() |
|
178 | - { |
|
179 | - $radii = [ |
|
180 | - '0' => '25', |
|
181 | - '1' => '50', |
|
182 | - '2' => '75', |
|
183 | - '3' => '100', |
|
184 | - ]; |
|
185 | - $config_radii = $this->config()->get('radii'); |
|
186 | - if ($config_radii) { |
|
187 | - $radii = $config_radii; |
|
188 | - } |
|
189 | - |
|
190 | - return $radii; |
|
191 | - } |
|
192 | - |
|
193 | - public function getRadiiArrayList() |
|
194 | - { |
|
195 | - $list = []; |
|
196 | - |
|
197 | - foreach ($this->getRadii() as $radius) { |
|
198 | - $list[] = new ArrayData(array( |
|
199 | - 'Radius' => $radius, |
|
200 | - )); |
|
201 | - } |
|
202 | - |
|
203 | - return new ArrayList($list); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Gets the limit of locations |
|
208 | - * @return mixed |
|
209 | - */ |
|
210 | - public function getLimit() |
|
211 | - { |
|
212 | - return $this->config()->get('limit'); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Gets if the radius drop down should be shown |
|
217 | - * @return mixed |
|
218 | - */ |
|
219 | - public function getShowRadius() |
|
220 | - { |
|
221 | - return $this->config()->get('show_radius'); |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * @return mixed |
|
226 | - */ |
|
227 | - public function getUsedCategories() |
|
228 | - { |
|
229 | - return $this->Categories()->filter([ |
|
230 | - 'LocationSet.ID:GreaterThan' => 0, |
|
231 | - ]); |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Gets the path of the info window template |
|
236 | - * |
|
237 | - * @return string |
|
238 | - */ |
|
239 | - public function getInfoWindowTemplate() |
|
240 | - { |
|
241 | - return ModuleResourceLoader::singleton()->resolveURL( |
|
242 | - Config::inst()->get( |
|
243 | - static::class, |
|
244 | - 'infoWindowTemplate' |
|
245 | - ) |
|
246 | - ); |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * Gets the path of the list template |
|
251 | - * |
|
252 | - * @return string |
|
253 | - */ |
|
254 | - public function getListTemplate() |
|
255 | - { |
|
256 | - return ModuleResourceLoader::singleton()->resolveURL( |
|
257 | - Config::inst()->get( |
|
258 | - static::class, |
|
259 | - 'listTemplate' |
|
260 | - ) |
|
261 | - ); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @return null|string |
|
266 | - */ |
|
267 | - public function getMapStyle() |
|
268 | - { |
|
269 | - return AddressDataExtension::getMapStyleJSON(); |
|
270 | - } |
|
271 | - |
|
272 | - public function getMapStyleJSONPath() |
|
273 | - { |
|
274 | - return AddressDataExtension::getMapStyleJSONPath(); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * @return null|string |
|
279 | - */ |
|
280 | - public function getMarkerIcon() |
|
281 | - { |
|
282 | - return AddressDataExtension::getIconImage(); |
|
283 | - } |
|
91 | + )); |
|
92 | + |
|
93 | + // Filter categories |
|
94 | + $config = GridFieldConfig_RelationEditor::create(); |
|
95 | + $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class); |
|
96 | + $config->addComponent(new GridFieldAddExistingSearchButton()); |
|
97 | + $categories = $this->Categories(); |
|
98 | + $categoriesField = GridField::create('Categories', 'Categories', $categories, $config) |
|
99 | + ->setDescription('only show locations from the selected category'); |
|
100 | + |
|
101 | + // Filter |
|
102 | + $fields->addFieldsToTab('Root.Filter', array( |
|
103 | + HeaderField::create('CategoryOptionsHeader', 'Location Filtering', 3), |
|
104 | + $categoriesField, |
|
105 | + )); |
|
106 | + }); |
|
107 | + |
|
108 | + return parent::getCMSFields(); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @param array $filter |
|
113 | + * @param array $filterAny |
|
114 | + * @param array $exclude |
|
115 | + * @param null|callable $callback |
|
116 | + * |
|
117 | + * @return DataList|ArrayList |
|
118 | + */ |
|
119 | + public static function get_locations( |
|
120 | + $filter = [], |
|
121 | + $filterAny = [], |
|
122 | + $exclude = [], |
|
123 | + $callback = null |
|
124 | + ) { |
|
125 | + $locationClass = Config::inst()->get(static::class, 'location_class'); |
|
126 | + $locations = $locationClass::get()->filter($filter)->exclude($exclude); |
|
127 | + |
|
128 | + if (!empty($filterAny)) { |
|
129 | + $locations = $locations->filterAny($filterAny); |
|
130 | + } |
|
131 | + if (!empty($exclude)) { |
|
132 | + $locations = $locations->exclude($exclude); |
|
133 | + } |
|
134 | + |
|
135 | + if ($callback !== null && is_callable($callback)) { |
|
136 | + $locations->filterByCallback($callback); |
|
137 | + } |
|
138 | + |
|
139 | + return $locations; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @return DataList |
|
144 | + */ |
|
145 | + public static function get_all_categories() |
|
146 | + { |
|
147 | + return LocationCategory::get(); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @return bool |
|
152 | + */ |
|
153 | + public function getPageCategories() |
|
154 | + { |
|
155 | + return self::locator_categories_by_locator($this->ID); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @param int $id |
|
160 | + * |
|
161 | + * @return bool| |
|
162 | + */ |
|
163 | + public static function locator_categories_by_locator($id = 0) |
|
164 | + { |
|
165 | + if ($id == 0) { |
|
166 | + return false; |
|
167 | + } |
|
168 | + |
|
169 | + return static::get()->byID($id)->getUsedCategories(); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Gets the list of radii |
|
174 | + * |
|
175 | + * @return ArrayList |
|
176 | + */ |
|
177 | + public function getRadii() |
|
178 | + { |
|
179 | + $radii = [ |
|
180 | + '0' => '25', |
|
181 | + '1' => '50', |
|
182 | + '2' => '75', |
|
183 | + '3' => '100', |
|
184 | + ]; |
|
185 | + $config_radii = $this->config()->get('radii'); |
|
186 | + if ($config_radii) { |
|
187 | + $radii = $config_radii; |
|
188 | + } |
|
189 | + |
|
190 | + return $radii; |
|
191 | + } |
|
192 | + |
|
193 | + public function getRadiiArrayList() |
|
194 | + { |
|
195 | + $list = []; |
|
196 | + |
|
197 | + foreach ($this->getRadii() as $radius) { |
|
198 | + $list[] = new ArrayData(array( |
|
199 | + 'Radius' => $radius, |
|
200 | + )); |
|
201 | + } |
|
202 | + |
|
203 | + return new ArrayList($list); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Gets the limit of locations |
|
208 | + * @return mixed |
|
209 | + */ |
|
210 | + public function getLimit() |
|
211 | + { |
|
212 | + return $this->config()->get('limit'); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Gets if the radius drop down should be shown |
|
217 | + * @return mixed |
|
218 | + */ |
|
219 | + public function getShowRadius() |
|
220 | + { |
|
221 | + return $this->config()->get('show_radius'); |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * @return mixed |
|
226 | + */ |
|
227 | + public function getUsedCategories() |
|
228 | + { |
|
229 | + return $this->Categories()->filter([ |
|
230 | + 'LocationSet.ID:GreaterThan' => 0, |
|
231 | + ]); |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Gets the path of the info window template |
|
236 | + * |
|
237 | + * @return string |
|
238 | + */ |
|
239 | + public function getInfoWindowTemplate() |
|
240 | + { |
|
241 | + return ModuleResourceLoader::singleton()->resolveURL( |
|
242 | + Config::inst()->get( |
|
243 | + static::class, |
|
244 | + 'infoWindowTemplate' |
|
245 | + ) |
|
246 | + ); |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * Gets the path of the list template |
|
251 | + * |
|
252 | + * @return string |
|
253 | + */ |
|
254 | + public function getListTemplate() |
|
255 | + { |
|
256 | + return ModuleResourceLoader::singleton()->resolveURL( |
|
257 | + Config::inst()->get( |
|
258 | + static::class, |
|
259 | + 'listTemplate' |
|
260 | + ) |
|
261 | + ); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @return null|string |
|
266 | + */ |
|
267 | + public function getMapStyle() |
|
268 | + { |
|
269 | + return AddressDataExtension::getMapStyleJSON(); |
|
270 | + } |
|
271 | + |
|
272 | + public function getMapStyleJSONPath() |
|
273 | + { |
|
274 | + return AddressDataExtension::getMapStyleJSONPath(); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * @return null|string |
|
279 | + */ |
|
280 | + public function getMarkerIcon() |
|
281 | + { |
|
282 | + return AddressDataExtension::getIconImage(); |
|
283 | + } |
|
284 | 284 | } |