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