Passed
Push — 3 ( 4e09b0...f8f63a )
by Nic
10:00
created
tests/Extension/LocationExtension.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -11,12 +11,12 @@
 block discarded – undo
11 11
  */
12 12
 class LocationExtension extends DataExtension implements TestOnly
13 13
 {
14
-    /**
15
-     * @param $url
16
-     * @return string
17
-     */
18
-    public function updateWebsiteURL(&$url)
19
-    {
20
-        $url = 'foo';
21
-    }
14
+	/**
15
+	 * @param $url
16
+	 * @return string
17
+	 */
18
+	public function updateWebsiteURL(&$url)
19
+	{
20
+		$url = 'foo';
21
+	}
22 22
 }
Please login to merge, or discard this patch.
tests/Model/ExtendedLocation.php 1 patch
Indentation   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -12,10 +12,10 @@
 block discarded – undo
12 12
  */
13 13
 class ExtendedLocation extends Location implements TestOnly
14 14
 {
15
-    /**
16
-     * @var array
17
-     */
18
-    private static $extensions = [
19
-        LocationExtension::class,
20
-    ];
15
+	/**
16
+	 * @var array
17
+	 */
18
+	private static $extensions = [
19
+		LocationExtension::class,
20
+	];
21 21
 }
Please login to merge, or discard this patch.
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 2 patches
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.
Indentation   +330 added lines, -330 removed lines patch added patch discarded remove patch
@@ -20,338 +20,338 @@
 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
-     * @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
 }
Please login to merge, or discard this patch.