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