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