Passed
Pull Request — 3.1 (#220)
by Jason
02:40
created
tests/pages/LocatorControllerTest.php 1 patch
Indentation   +63 added lines, -63 removed lines patch added patch discarded remove patch
@@ -17,78 +17,78 @@
 block discarded – undo
17 17
 class LocatorControllerTest extends FunctionalTest
18 18
 {
19 19
 
20
-    /**
21
-     * @var string
22
-     */
23
-    protected static $fixture_file = '../fixtures.yml';
20
+	/**
21
+	 * @var string
22
+	 */
23
+	protected static $fixture_file = '../fixtures.yml';
24 24
 
25
-    /**
26
-     * @var bool
27
-     */
28
-    protected static $use_draft_site = true;
25
+	/**
26
+	 * @var bool
27
+	 */
28
+	protected static $use_draft_site = true;
29 29
 
30
-    /**
31
-     *
32
-     */
33
-    public function testIndex()
34
-    {
35
-        $locator = $this->objFromFixture(Locator::class, 'locator1');
36
-        $controller = LocatorController::create($locator);
37
-        $this->assertInstanceOf(ViewableData::class, $controller->index($controller->getRequest()));
38
-    }
30
+	/**
31
+	 *
32
+	 */
33
+	public function testIndex()
34
+	{
35
+		$locator = $this->objFromFixture(Locator::class, 'locator1');
36
+		$controller = LocatorController::create($locator);
37
+		$this->assertInstanceOf(ViewableData::class, $controller->index($controller->getRequest()));
38
+	}
39 39
 
40
-    /**
41
-     *
42
-     */
43
-    public function testXml()
44
-    {
45
-        /** @var Locator $locator */
46
-        $locator = $this->objFromFixture(Locator::class, 'locator1');
47
-        $page = $this->get($locator->Link('xml'));
40
+	/**
41
+	 *
42
+	 */
43
+	public function testXml()
44
+	{
45
+		/** @var Locator $locator */
46
+		$locator = $this->objFromFixture(Locator::class, 'locator1');
47
+		$page = $this->get($locator->Link('xml'));
48 48
 
49
-        $this->assertEquals(200, $page->getStatusCode());
50
-        $this->assertEquals('application/xml', $page->getHeader('content-type'));
49
+		$this->assertEquals(200, $page->getStatusCode());
50
+		$this->assertEquals('application/xml', $page->getHeader('content-type'));
51 51
 
52
-        $dom = new \DOMDocument();
53
-        // true if it loads, false if it doesn't
54
-        $valid = $dom->loadXML($page->getBody());
55
-        $this->assertTrue($valid);
56
-    }
52
+		$dom = new \DOMDocument();
53
+		// true if it loads, false if it doesn't
54
+		$valid = $dom->loadXML($page->getBody());
55
+		$this->assertTrue($valid);
56
+	}
57 57
 
58
-    /**
59
-     *
60
-     */
61
-    public function testJson()
62
-    {
63
-        /** @var Locator $locator */
64
-        $locator = $this->objFromFixture(Locator::class, 'locator1');
65
-        $page = $this->get($locator->Link('json'));
58
+	/**
59
+	 *
60
+	 */
61
+	public function testJson()
62
+	{
63
+		/** @var Locator $locator */
64
+		$locator = $this->objFromFixture(Locator::class, 'locator1');
65
+		$page = $this->get($locator->Link('json'));
66 66
 
67
-        $this->assertEquals(200, $page->getStatusCode());
68
-        $this->assertEquals('application/json', $page->getHeader('content-type'));
67
+		$this->assertEquals(200, $page->getStatusCode());
68
+		$this->assertEquals('application/json', $page->getHeader('content-type'));
69 69
 
70
-        $json = json_decode($page->getBody());
71
-        // if it is null its not valid
72
-        $this->assertNotNull($json);
73
-    }
70
+		$json = json_decode($page->getBody());
71
+		// if it is null its not valid
72
+		$this->assertNotNull($json);
73
+	}
74 74
 
75
-    /**
76
-     *
77
-     */
78
-    public function testLocationSearch()
79
-    {
80
-        $locator = $this->objFromFixture(Locator::class, 'locator1');
81
-        $object = LocatorController::create($locator);
82
-        $form = $object->LocationSearch();
83
-        $this->assertInstanceOf(Form::class, $form);
75
+	/**
76
+	 *
77
+	 */
78
+	public function testLocationSearch()
79
+	{
80
+		$locator = $this->objFromFixture(Locator::class, 'locator1');
81
+		$object = LocatorController::create($locator);
82
+		$form = $object->LocationSearch();
83
+		$this->assertInstanceOf(Form::class, $form);
84 84
 
85
-        $category = $this->objFromFixture(LocationCategory::class, 'service');
86
-        $category2 = $this->objFromFixture(LocationCategory::class, 'manufacturing');
87
-        $locator->Categories()->add($category);
88
-        $locator->Categories()->add($category2);
85
+		$category = $this->objFromFixture(LocationCategory::class, 'service');
86
+		$category2 = $this->objFromFixture(LocationCategory::class, 'manufacturing');
87
+		$locator->Categories()->add($category);
88
+		$locator->Categories()->add($category2);
89 89
 
90
-        $form = $object->LocationSearch();
91
-        $fields = $form->Fields();
92
-        $this->assertInstanceOf(FieldList::class, $fields);
93
-    }
90
+		$form = $object->LocationSearch();
91
+		$fields = $form->Fields();
92
+		$this->assertInstanceOf(FieldList::class, $fields);
93
+	}
94 94
 }
Please login to merge, or discard this patch.
src/pages/LocatorController.php 1 patch
Indentation   +306 added lines, -306 removed lines patch added patch discarded remove patch
@@ -18,124 +18,124 @@  discard block
 block discarded – undo
18 18
  */
19 19
 class LocatorController extends \PageController
20 20
 {
21
-    /**
22
-     * @var array
23
-     */
24
-    private static $allowed_actions = [
25
-        'xml',
26
-        'json',
27
-    ];
28
-
29
-    /**
30
-     * @var array
31
-     */
32
-    private static $base_filter = [];
33
-
34
-    /**
35
-     * @var array
36
-     */
37
-    private static $base_exclude = [
38
-        'Lat' => 0,
39
-        'Lng' => 0,
40
-    ];
41
-
42
-    /**
43
-     * @var array
44
-     */
45
-    private static $base_filter_any = [];
46
-
47
-    /**
48
-     * @var bool
49
-     */
50
-    private static $bootstrapify = true;
51
-
52
-    /**
53
-     * ID of map container
54
-     *
55
-     * @var string
56
-     */
57
-    private static $map_container = 'map';
58
-
59
-    /**
60
-     * class of location list container
61
-     *
62
-     * @var string
63
-     */
64
-    private static $list_container = 'loc-list';
65
-
66
-    /**
67
-     * @var DataList|ArrayList
68
-     */
69
-    protected $locations;
70
-
71
-    /**
72
-     * Set Requirements based on input from CMS
73
-     */
74
-    public function init()
75
-    {
76
-        parent::init();
77
-        // google maps api key
78
-        $key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key');
79
-        Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key);
80
-
81
-        // prevent init of map if no query
82
-        $request = Controller::curr()->getRequest();
83
-
84
-        if ($this->getTrigger($request)) {
85
-            $locations = $this->getLocations();
86
-
87
-            if ($locations) {
88
-                $featuredInList = ($locations->filter('Featured', true)->count() > 0);
89
-                $defaultCoords = $this->getAddressSearchCoords() ?
90
-                    $this->getAddressSearchCoords() :
91
-                    new ArrayData([
92
-                        "Lat" => 0,
93
-                        "Lng" => 0,
94
-                    ]);
95
-
96
-                $featured = $featuredInList
97
-                    ? 'featuredLocations: true'
98
-                    : 'featuredLocations: false';
99
-
100
-                // map config based on user input in Settings tab
101
-                $limit = Config::inst()->get(LocatorController::class, 'limit');
102
-                if ($limit < 1) {
103
-                    $limit = -1;
104
-                }
105
-                $load = 'fullMapStart: true, storeLimit: ' . $limit . ', maxDistance: true,';
106
-
107
-                $listTemplatePath = $this->getListTemplate();
108
-                $infowindowTemplatePath = $this->getInfoWindowTemplate();
109
-
110
-                $kilometer = ($this->data()->Unit == 'km') ? "lengthUnit: 'km'" : "lengthUnit: 'm'";
111
-
112
-                // pass GET variables to xml action
113
-                $vars = $this->request->getVars();
114
-                unset($vars['url']);
115
-                $url = '';
116
-                if (count($vars)) {
117
-                    $url .= '?' . http_build_query($vars);
118
-                }
119
-                $link = Controller::join_links($this->Link(), 'xml.xml', $url);
120
-
121
-                // containers
122
-                $map_id = Config::inst()->get(LocatorController::class, 'map_container');
123
-                $list_class = Config::inst()->get(LocatorController::class, 'list_container');
124
-
125
-                $mapStyle = '';
126
-                if ($stylePath = $this->getMapStyleJSONPath()) {
127
-                    if ($style = file_get_contents($stylePath)) {
128
-                        $mapStyle = "styles: {$style},";
129
-                    }
130
-                };
131
-
132
-                $markerImage = '';
133
-                if ($imagePath = $this->getMarkerIcon()) {
134
-                    $markerImage = "markerImg: '{$imagePath}',";
135
-                }
136
-
137
-                // init map
138
-                Requirements::customScript("
21
+	/**
22
+	 * @var array
23
+	 */
24
+	private static $allowed_actions = [
25
+		'xml',
26
+		'json',
27
+	];
28
+
29
+	/**
30
+	 * @var array
31
+	 */
32
+	private static $base_filter = [];
33
+
34
+	/**
35
+	 * @var array
36
+	 */
37
+	private static $base_exclude = [
38
+		'Lat' => 0,
39
+		'Lng' => 0,
40
+	];
41
+
42
+	/**
43
+	 * @var array
44
+	 */
45
+	private static $base_filter_any = [];
46
+
47
+	/**
48
+	 * @var bool
49
+	 */
50
+	private static $bootstrapify = true;
51
+
52
+	/**
53
+	 * ID of map container
54
+	 *
55
+	 * @var string
56
+	 */
57
+	private static $map_container = 'map';
58
+
59
+	/**
60
+	 * class of location list container
61
+	 *
62
+	 * @var string
63
+	 */
64
+	private static $list_container = 'loc-list';
65
+
66
+	/**
67
+	 * @var DataList|ArrayList
68
+	 */
69
+	protected $locations;
70
+
71
+	/**
72
+	 * Set Requirements based on input from CMS
73
+	 */
74
+	public function init()
75
+	{
76
+		parent::init();
77
+		// google maps api key
78
+		$key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key');
79
+		Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key);
80
+
81
+		// prevent init of map if no query
82
+		$request = Controller::curr()->getRequest();
83
+
84
+		if ($this->getTrigger($request)) {
85
+			$locations = $this->getLocations();
86
+
87
+			if ($locations) {
88
+				$featuredInList = ($locations->filter('Featured', true)->count() > 0);
89
+				$defaultCoords = $this->getAddressSearchCoords() ?
90
+					$this->getAddressSearchCoords() :
91
+					new ArrayData([
92
+						"Lat" => 0,
93
+						"Lng" => 0,
94
+					]);
95
+
96
+				$featured = $featuredInList
97
+					? 'featuredLocations: true'
98
+					: 'featuredLocations: false';
99
+
100
+				// map config based on user input in Settings tab
101
+				$limit = Config::inst()->get(LocatorController::class, 'limit');
102
+				if ($limit < 1) {
103
+					$limit = -1;
104
+				}
105
+				$load = 'fullMapStart: true, storeLimit: ' . $limit . ', maxDistance: true,';
106
+
107
+				$listTemplatePath = $this->getListTemplate();
108
+				$infowindowTemplatePath = $this->getInfoWindowTemplate();
109
+
110
+				$kilometer = ($this->data()->Unit == 'km') ? "lengthUnit: 'km'" : "lengthUnit: 'm'";
111
+
112
+				// pass GET variables to xml action
113
+				$vars = $this->request->getVars();
114
+				unset($vars['url']);
115
+				$url = '';
116
+				if (count($vars)) {
117
+					$url .= '?' . http_build_query($vars);
118
+				}
119
+				$link = Controller::join_links($this->Link(), 'xml.xml', $url);
120
+
121
+				// containers
122
+				$map_id = Config::inst()->get(LocatorController::class, 'map_container');
123
+				$list_class = Config::inst()->get(LocatorController::class, 'list_container');
124
+
125
+				$mapStyle = '';
126
+				if ($stylePath = $this->getMapStyleJSONPath()) {
127
+					if ($style = file_get_contents($stylePath)) {
128
+						$mapStyle = "styles: {$style},";
129
+					}
130
+				};
131
+
132
+				$markerImage = '';
133
+				if ($imagePath = $this->getMarkerIcon()) {
134
+					$markerImage = "markerImg: '{$imagePath}',";
135
+				}
136
+
137
+				// init map
138
+				Requirements::customScript("
139 139
                 $(function(){
140 140
                     $('#map-container').storeLocator({
141 141
                         {$load}
@@ -164,192 +164,192 @@  discard block
 block discarded – undo
164 164
                     });
165 165
                 });
166 166
             ", 'jquery-locator');
167
-            }
168
-        }
169
-    }
170
-
171
-    /**
172
-     * @param HTTPRequest $request
173
-     *
174
-     * @return bool
175
-     */
176
-    public function getTrigger(HTTPRequest $request = null)
177
-    {
178
-        if ($request === null) {
179
-            $request = $this->getRequest();
180
-        }
181
-        return !empty($this->getRequest()->getVars()) || $this->data()->ResultsOnLoad;
182
-    }
183
-
184
-    /**
185
-     * @return ArrayList|DataList
186
-     */
187
-    public function getLocations()
188
-    {
189
-        if (!$this->locations) {
190
-            $this->setLocations($this->request);
191
-        }
192
-
193
-        return $this->locations;
194
-    }
195
-
196
-    /**
197
-     * @param HTTPRequest|null $request
198
-     *
199
-     * @return $this
200
-     */
201
-    public function setLocations(HTTPRequest $request = null)
202
-    {
203
-
204
-        if ($request === null) {
205
-            $request = $this->request;
206
-        }
207
-        $filter = $this->config()->get('base_filter');
208
-
209
-        $categoryVar = Config::inst()->get(Locator::class, 'category_var');
210
-        if ($request->getVar($categoryVar)) {
211
-            $filter['Categories.ID'] = $request->getVar($categoryVar);
212
-        } else {
213
-            if ($this->getPageCategories()->exists()) {
214
-                foreach ($this->getPageCategories() as $category) {
215
-                    $filter['Categories.ID'][] = $category->ID;
216
-                }
217
-            }
218
-        }
219
-
220
-        $this->extend('updateLocatorFilter', $filter, $request);
221
-
222
-        $filterAny = $this->config()->get('base_filter_any');
223
-        $this->extend('updateLocatorFilterAny', $filterAny, $request);
224
-
225
-        $exclude = $this->config()->get('base_exclude');
226
-        $this->extend('updateLocatorExclude', $exclude, $request);
227
-
228
-        $locations = Locator::get_locations($filter, $filterAny, $exclude);
229
-        $locations = DataToArrayListHelper::to_array_list($locations);
230
-
231
-        //allow for adjusting list post possible distance calculation
232
-        $this->extend('updateLocationList', $locations);
233
-
234
-        if ($locations->canSortBy('Distance')) {
235
-            $locations = $locations->sort('Distance');
236
-        }
237
-
238
-        if ($this->getShowRadius()) {
239
-            $radiusVar = Config::inst()->get(Locator::class, 'radius_var');
240
-
241
-            if ($radius = (int)$request->getVar($radiusVar)) {
242
-                $locations = $locations->filterByCallback(function ($location) use (&$radius) {
243
-                    return $location->Distance <= $radius;
244
-                });
245
-            }
246
-        }
247
-
248
-        //allow for returning list to be set as
249
-        $this->extend('updateListType', $locations);
250
-
251
-        $limit = $this->getLimit();
252
-        if ($limit > 0) {
253
-            $locations = $locations->limit($limit);
254
-        }
255
-
256
-        $this->locations = $locations;
257
-
258
-        return $this;
259
-    }
260
-
261
-    /**
262
-     * @return ArrayData|boolean
263
-     */
264
-    public function getAddressSearchCoords()
265
-    {
266
-        $addressVar = Config::inst()->get(DistanceDataExtension::class, 'address_var');
267
-        if (!$this->request->getVar($addressVar)) {
268
-            return false;
269
-        }
270
-        if (class_exists(GoogleGeocoder::class)) {
271
-            $geocoder = new GoogleGeocoder($this->request->getVar($addressVar));
272
-            $response = $geocoder->getResult();
273
-            $lat = $response->getLatitude();
274
-            $lng = $response->getLongitude();
275
-
276
-            return new ArrayData([
277
-                "Lat" => $lat,
278
-                "Lng" => $lng,
279
-            ]);
280
-        }
281
-    }
282
-
283
-    /**
284
-     * @param HTTPRequest $request
285
-     *
286
-     * @return \SilverStripe\View\ViewableData_Customised
287
-     */
288
-    public function index(HTTPRequest $request)
289
-    {
290
-        if ($this->getTrigger($request)) {
291
-            $locations = $this->getLocations();
292
-        } else {
293
-            $locations = ArrayList::create();
294
-        }
295
-
296
-        return $this->customise(array(
297
-            'Locations' => $locations,
298
-        ));
299
-    }
300
-
301
-    /**
302
-     * Renders locations in xml format
303
-     *
304
-     * @return \SilverStripe\ORM\FieldType\DBHTMLText
305
-     */
306
-    public function xml()
307
-    {
308
-        $this->getResponse()->addHeader("Content-Type", "application/xml");
309
-        $data = new ArrayData(array(
310
-            "Locations" => $this->getLocations(),
311
-            "AddressCoords" => $this->getAddressSearchCoords(),
312
-        ));
313
-
314
-        return $data->renderWith('Dynamic/Locator/Data/XML');
315
-    }
316
-
317
-    /**
318
-     * Renders locations in json format
319
-     *
320
-     * @return \SilverStripe\ORM\FieldType\DBHTMLText
321
-     */
322
-    public function json()
323
-    {
324
-        $this->getResponse()->addHeader("Content-Type", "application/json");
325
-        $data = new ArrayData(array(
326
-            "Locations" => $this->getLocations(),
327
-            "AddressCoords" => $this->getAddressSearchCoords(),
328
-        ));
329
-
330
-        return $data->renderWith('Dynamic/Locator/Data/JSON');
331
-    }
332
-
333
-    /**
334
-     * LocationSearch form.
335
-     *
336
-     * Search form for locations, updates map and results list via AJAX
337
-     *
338
-     * @return \SilverStripe\Forms\Form
339
-     */
340
-    public function LocationSearch()
341
-    {
342
-
343
-        $form = LocatorForm::create($this, 'LocationSearch');
344
-        if (class_exists(BootstrapForm::class) && $this->config()->get('bootstrapify')) {
345
-            $form->Fields()->bootstrapify();
346
-            $form->Actions()->bootstrapify();
347
-        }
348
-
349
-        return $form
350
-            ->setFormMethod('GET')
351
-            ->setFormAction($this->Link())
352
-            ->disableSecurityToken()
353
-            ->loadDataFrom($this->request->getVars());
354
-    }
167
+			}
168
+		}
169
+	}
170
+
171
+	/**
172
+	 * @param HTTPRequest $request
173
+	 *
174
+	 * @return bool
175
+	 */
176
+	public function getTrigger(HTTPRequest $request = null)
177
+	{
178
+		if ($request === null) {
179
+			$request = $this->getRequest();
180
+		}
181
+		return !empty($this->getRequest()->getVars()) || $this->data()->ResultsOnLoad;
182
+	}
183
+
184
+	/**
185
+	 * @return ArrayList|DataList
186
+	 */
187
+	public function getLocations()
188
+	{
189
+		if (!$this->locations) {
190
+			$this->setLocations($this->request);
191
+		}
192
+
193
+		return $this->locations;
194
+	}
195
+
196
+	/**
197
+	 * @param HTTPRequest|null $request
198
+	 *
199
+	 * @return $this
200
+	 */
201
+	public function setLocations(HTTPRequest $request = null)
202
+	{
203
+
204
+		if ($request === null) {
205
+			$request = $this->request;
206
+		}
207
+		$filter = $this->config()->get('base_filter');
208
+
209
+		$categoryVar = Config::inst()->get(Locator::class, 'category_var');
210
+		if ($request->getVar($categoryVar)) {
211
+			$filter['Categories.ID'] = $request->getVar($categoryVar);
212
+		} else {
213
+			if ($this->getPageCategories()->exists()) {
214
+				foreach ($this->getPageCategories() as $category) {
215
+					$filter['Categories.ID'][] = $category->ID;
216
+				}
217
+			}
218
+		}
219
+
220
+		$this->extend('updateLocatorFilter', $filter, $request);
221
+
222
+		$filterAny = $this->config()->get('base_filter_any');
223
+		$this->extend('updateLocatorFilterAny', $filterAny, $request);
224
+
225
+		$exclude = $this->config()->get('base_exclude');
226
+		$this->extend('updateLocatorExclude', $exclude, $request);
227
+
228
+		$locations = Locator::get_locations($filter, $filterAny, $exclude);
229
+		$locations = DataToArrayListHelper::to_array_list($locations);
230
+
231
+		//allow for adjusting list post possible distance calculation
232
+		$this->extend('updateLocationList', $locations);
233
+
234
+		if ($locations->canSortBy('Distance')) {
235
+			$locations = $locations->sort('Distance');
236
+		}
237
+
238
+		if ($this->getShowRadius()) {
239
+			$radiusVar = Config::inst()->get(Locator::class, 'radius_var');
240
+
241
+			if ($radius = (int)$request->getVar($radiusVar)) {
242
+				$locations = $locations->filterByCallback(function ($location) use (&$radius) {
243
+					return $location->Distance <= $radius;
244
+				});
245
+			}
246
+		}
247
+
248
+		//allow for returning list to be set as
249
+		$this->extend('updateListType', $locations);
250
+
251
+		$limit = $this->getLimit();
252
+		if ($limit > 0) {
253
+			$locations = $locations->limit($limit);
254
+		}
255
+
256
+		$this->locations = $locations;
257
+
258
+		return $this;
259
+	}
260
+
261
+	/**
262
+	 * @return ArrayData|boolean
263
+	 */
264
+	public function getAddressSearchCoords()
265
+	{
266
+		$addressVar = Config::inst()->get(DistanceDataExtension::class, 'address_var');
267
+		if (!$this->request->getVar($addressVar)) {
268
+			return false;
269
+		}
270
+		if (class_exists(GoogleGeocoder::class)) {
271
+			$geocoder = new GoogleGeocoder($this->request->getVar($addressVar));
272
+			$response = $geocoder->getResult();
273
+			$lat = $response->getLatitude();
274
+			$lng = $response->getLongitude();
275
+
276
+			return new ArrayData([
277
+				"Lat" => $lat,
278
+				"Lng" => $lng,
279
+			]);
280
+		}
281
+	}
282
+
283
+	/**
284
+	 * @param HTTPRequest $request
285
+	 *
286
+	 * @return \SilverStripe\View\ViewableData_Customised
287
+	 */
288
+	public function index(HTTPRequest $request)
289
+	{
290
+		if ($this->getTrigger($request)) {
291
+			$locations = $this->getLocations();
292
+		} else {
293
+			$locations = ArrayList::create();
294
+		}
295
+
296
+		return $this->customise(array(
297
+			'Locations' => $locations,
298
+		));
299
+	}
300
+
301
+	/**
302
+	 * Renders locations in xml format
303
+	 *
304
+	 * @return \SilverStripe\ORM\FieldType\DBHTMLText
305
+	 */
306
+	public function xml()
307
+	{
308
+		$this->getResponse()->addHeader("Content-Type", "application/xml");
309
+		$data = new ArrayData(array(
310
+			"Locations" => $this->getLocations(),
311
+			"AddressCoords" => $this->getAddressSearchCoords(),
312
+		));
313
+
314
+		return $data->renderWith('Dynamic/Locator/Data/XML');
315
+	}
316
+
317
+	/**
318
+	 * Renders locations in json format
319
+	 *
320
+	 * @return \SilverStripe\ORM\FieldType\DBHTMLText
321
+	 */
322
+	public function json()
323
+	{
324
+		$this->getResponse()->addHeader("Content-Type", "application/json");
325
+		$data = new ArrayData(array(
326
+			"Locations" => $this->getLocations(),
327
+			"AddressCoords" => $this->getAddressSearchCoords(),
328
+		));
329
+
330
+		return $data->renderWith('Dynamic/Locator/Data/JSON');
331
+	}
332
+
333
+	/**
334
+	 * LocationSearch form.
335
+	 *
336
+	 * Search form for locations, updates map and results list via AJAX
337
+	 *
338
+	 * @return \SilverStripe\Forms\Form
339
+	 */
340
+	public function LocationSearch()
341
+	{
342
+
343
+		$form = LocatorForm::create($this, 'LocationSearch');
344
+		if (class_exists(BootstrapForm::class) && $this->config()->get('bootstrapify')) {
345
+			$form->Fields()->bootstrapify();
346
+			$form->Actions()->bootstrapify();
347
+		}
348
+
349
+		return $form
350
+			->setFormMethod('GET')
351
+			->setFormAction($this->Link())
352
+			->disableSecurityToken()
353
+			->loadDataFrom($this->request->getVars());
354
+	}
355 355
 }
Please login to merge, or discard this patch.
src/pages/Locator.php 1 patch
Indentation   +252 added lines, -252 removed lines patch added patch discarded remove patch
@@ -27,258 +27,258 @@
 block discarded – undo
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(Locator::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 Locator::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
-                Locator::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
-                Locator::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(Locator::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 Locator::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
+				Locator::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
+				Locator::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
 }
Please login to merge, or discard this patch.