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