@@ -26,246 +26,246 @@ |
||
| 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(static::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 static::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 | - static::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 | - static::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 | - { |
|
| 261 | - return AddressDataExtension::getMapStyleJSONPath(); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * @return null|string |
|
| 266 | - */ |
|
| 267 | - public function getMarkerIcon() |
|
| 268 | - { |
|
| 269 | - return AddressDataExtension::getIconImage(); |
|
| 270 | - } |
|
| 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(static::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 static::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 | + static::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 | + static::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 | + { |
|
| 261 | + return AddressDataExtension::getMapStyleJSONPath(); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * @return null|string |
|
| 266 | + */ |
|
| 267 | + public function getMarkerIcon() |
|
| 268 | + { |
|
| 269 | + return AddressDataExtension::getIconImage(); |
|
| 270 | + } |
|
| 271 | 271 | } |
@@ -18,131 +18,131 @@ 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 | - * 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, 'map_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 | - $featuredInList = ($locations->filter('Featured', true)->count() > 0); |
|
| 96 | - $defaultCoords = $this->getAddressSearchCoords() ? |
|
| 97 | - $this->getAddressSearchCoords() : |
|
| 98 | - new ArrayData([ |
|
| 99 | - "Lat" => 0, |
|
| 100 | - "Lng" => 0, |
|
| 101 | - ]); |
|
| 102 | - |
|
| 103 | - $featured = $featuredInList |
|
| 104 | - ? 'featuredLocations: true' |
|
| 105 | - : 'featuredLocations: false'; |
|
| 106 | - |
|
| 107 | - // map config based on user input in Settings tab |
|
| 108 | - $limit = Config::inst()->get(LocatorController::class, 'limit'); |
|
| 109 | - if ($limit < 1) { |
|
| 110 | - $limit = -1; |
|
| 111 | - } |
|
| 112 | - $load = 'fullMapStart: true, storeLimit: ' . $limit . ', maxDistance: true,'; |
|
| 113 | - |
|
| 114 | - $listTemplatePath = $this->getListTemplate(); |
|
| 115 | - $infowindowTemplatePath = $this->getInfoWindowTemplate(); |
|
| 116 | - |
|
| 117 | - $kilometer = ($this->data()->Unit == 'km') ? "lengthUnit: 'km'" : "lengthUnit: 'm'"; |
|
| 118 | - |
|
| 119 | - // pass GET variables to xml action |
|
| 120 | - $vars = $this->request->getVars(); |
|
| 121 | - unset($vars['url']); |
|
| 122 | - $url = ''; |
|
| 123 | - if (count($vars)) { |
|
| 124 | - $url .= '?' . http_build_query($vars); |
|
| 125 | - } |
|
| 126 | - $link = Controller::join_links($this->Link(), 'xml.xml', $url); |
|
| 127 | - |
|
| 128 | - // containers |
|
| 129 | - $map_id = Config::inst()->get(LocatorController::class, 'map_container'); |
|
| 130 | - $list_class = Config::inst()->get(LocatorController::class, 'list_container'); |
|
| 131 | - |
|
| 132 | - $mapStyle = ''; |
|
| 133 | - if ($stylePath = $this->getMapStyleJSONPath()) { |
|
| 134 | - if ($style = file_get_contents($stylePath)) { |
|
| 135 | - $mapStyle = "styles: {$style},"; |
|
| 136 | - } |
|
| 137 | - }; |
|
| 138 | - |
|
| 139 | - $markerImage = ''; |
|
| 140 | - if ($imagePath = $this->getMarkerIcon()) { |
|
| 141 | - $markerImage = "markerImg: '{$imagePath}',"; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // init map |
|
| 145 | - 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, 'map_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 | + $featuredInList = ($locations->filter('Featured', true)->count() > 0); |
|
| 96 | + $defaultCoords = $this->getAddressSearchCoords() ? |
|
| 97 | + $this->getAddressSearchCoords() : |
|
| 98 | + new ArrayData([ |
|
| 99 | + "Lat" => 0, |
|
| 100 | + "Lng" => 0, |
|
| 101 | + ]); |
|
| 102 | + |
|
| 103 | + $featured = $featuredInList |
|
| 104 | + ? 'featuredLocations: true' |
|
| 105 | + : 'featuredLocations: false'; |
|
| 106 | + |
|
| 107 | + // map config based on user input in Settings tab |
|
| 108 | + $limit = Config::inst()->get(LocatorController::class, 'limit'); |
|
| 109 | + if ($limit < 1) { |
|
| 110 | + $limit = -1; |
|
| 111 | + } |
|
| 112 | + $load = 'fullMapStart: true, storeLimit: ' . $limit . ', maxDistance: true,'; |
|
| 113 | + |
|
| 114 | + $listTemplatePath = $this->getListTemplate(); |
|
| 115 | + $infowindowTemplatePath = $this->getInfoWindowTemplate(); |
|
| 116 | + |
|
| 117 | + $kilometer = ($this->data()->Unit == 'km') ? "lengthUnit: 'km'" : "lengthUnit: 'm'"; |
|
| 118 | + |
|
| 119 | + // pass GET variables to xml action |
|
| 120 | + $vars = $this->request->getVars(); |
|
| 121 | + unset($vars['url']); |
|
| 122 | + $url = ''; |
|
| 123 | + if (count($vars)) { |
|
| 124 | + $url .= '?' . http_build_query($vars); |
|
| 125 | + } |
|
| 126 | + $link = Controller::join_links($this->Link(), 'xml.xml', $url); |
|
| 127 | + |
|
| 128 | + // containers |
|
| 129 | + $map_id = Config::inst()->get(LocatorController::class, 'map_container'); |
|
| 130 | + $list_class = Config::inst()->get(LocatorController::class, 'list_container'); |
|
| 131 | + |
|
| 132 | + $mapStyle = ''; |
|
| 133 | + if ($stylePath = $this->getMapStyleJSONPath()) { |
|
| 134 | + if ($style = file_get_contents($stylePath)) { |
|
| 135 | + $mapStyle = "styles: {$style},"; |
|
| 136 | + } |
|
| 137 | + }; |
|
| 138 | + |
|
| 139 | + $markerImage = ''; |
|
| 140 | + if ($imagePath = $this->getMarkerIcon()) { |
|
| 141 | + $markerImage = "markerImg: '{$imagePath}',"; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // init map |
|
| 145 | + Requirements::customScript(" |
|
| 146 | 146 | $(function(){ |
| 147 | 147 | $('#map-container').storeLocator({ |
| 148 | 148 | {$load} |
@@ -171,195 +171,195 @@ discard block |
||
| 171 | 171 | }); |
| 172 | 172 | }); |
| 173 | 173 | ", 'jquery-locator'); |
| 174 | - } |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @param HTTPRequest $request |
|
| 180 | - * |
|
| 181 | - * @return bool |
|
| 182 | - */ |
|
| 183 | - public function getTrigger(HTTPRequest $request = null) |
|
| 184 | - { |
|
| 185 | - if ($request === null) { |
|
| 186 | - $request = $this->getRequest(); |
|
| 187 | - } |
|
| 188 | - $trigger = $request->getVar(Config::inst()->get(LocatorController::class, 'query_trigger')); |
|
| 189 | - |
|
| 190 | - return isset($trigger); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @return ArrayList|DataList |
|
| 195 | - */ |
|
| 196 | - public function getLocations() |
|
| 197 | - { |
|
| 198 | - if (!$this->locations) { |
|
| 199 | - $this->setLocations($this->request); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - return $this->locations; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @param HTTPRequest|null $request |
|
| 207 | - * |
|
| 208 | - * @return $this |
|
| 209 | - */ |
|
| 210 | - public function setLocations(HTTPRequest $request = null) |
|
| 211 | - { |
|
| 212 | - |
|
| 213 | - if ($request === null) { |
|
| 214 | - $request = $this->request; |
|
| 215 | - } |
|
| 216 | - $filter = $this->config()->get('base_filter'); |
|
| 217 | - |
|
| 218 | - $categoryVar = $this->data()->config()->get('category_var'); |
|
| 219 | - if ($request->getVar($categoryVar)) { |
|
| 220 | - $filter['Categories.ID'] = $request->getVar($categoryVar); |
|
| 221 | - } else { |
|
| 222 | - if ($this->getPageCategories()->exists()) { |
|
| 223 | - foreach ($this->getPageCategories() as $category) { |
|
| 224 | - $filter['Categories.ID'][] = $category->ID; |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - $this->extend('updateLocatorFilter', $filter, $request); |
|
| 230 | - |
|
| 231 | - $filterAny = $this->config()->get('base_filter_any'); |
|
| 232 | - $this->extend('updateLocatorFilterAny', $filterAny, $request); |
|
| 233 | - |
|
| 234 | - $exclude = $this->config()->get('base_exclude'); |
|
| 235 | - $this->extend('updateLocatorExclude', $exclude, $request); |
|
| 236 | - |
|
| 237 | - $class = $this->data()->ClassName; |
|
| 238 | - $locations = $class::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 = $this->data()->config()->get('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 | - * @return ArrayData|boolean |
|
| 273 | - */ |
|
| 274 | - public function getAddressSearchCoords() |
|
| 275 | - { |
|
| 276 | - $addressVar = Config::inst()->get(DistanceDataExtension::class, 'address_var'); |
|
| 277 | - if (!$this->request->getVar($addressVar)) { |
|
| 278 | - return false; |
|
| 279 | - } |
|
| 280 | - if (class_exists(GoogleGeocoder::class)) { |
|
| 281 | - $geocoder = new GoogleGeocoder($this->request->getVar($addressVar)); |
|
| 282 | - $response = $geocoder->getResult(); |
|
| 283 | - $lat = $response->getLatitude(); |
|
| 284 | - $lng = $response->getLongitude(); |
|
| 285 | - |
|
| 286 | - return new ArrayData([ |
|
| 287 | - "Lat" => $lat, |
|
| 288 | - "Lng" => $lng, |
|
| 289 | - ]); |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @param HTTPRequest $request |
|
| 295 | - * |
|
| 296 | - * @return \SilverStripe\View\ViewableData_Customised |
|
| 297 | - */ |
|
| 298 | - public function index(HTTPRequest $request) |
|
| 299 | - { |
|
| 300 | - if ($this->getTrigger($request)) { |
|
| 301 | - $locations = $this->getLocations(); |
|
| 302 | - } else { |
|
| 303 | - $locations = ArrayList::create(); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - return $this->customise(array( |
|
| 307 | - 'Locations' => $locations, |
|
| 308 | - )); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Renders locations in xml format |
|
| 313 | - * |
|
| 314 | - * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 315 | - */ |
|
| 316 | - public function xml() |
|
| 317 | - { |
|
| 318 | - $this->getResponse()->addHeader("Content-Type", "application/xml"); |
|
| 319 | - $data = new ArrayData(array( |
|
| 320 | - "Locations" => $this->getLocations(), |
|
| 321 | - "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 322 | - )); |
|
| 323 | - |
|
| 324 | - return $data->renderWith('Dynamic/Locator/Data/XML'); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Renders locations in json format |
|
| 329 | - * |
|
| 330 | - * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 331 | - */ |
|
| 332 | - public function json() |
|
| 333 | - { |
|
| 334 | - $this->getResponse()->addHeader("Content-Type", "application/json"); |
|
| 335 | - $data = new ArrayData(array( |
|
| 336 | - "Locations" => $this->getLocations(), |
|
| 337 | - "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 338 | - )); |
|
| 339 | - |
|
| 340 | - return $data->renderWith('Dynamic/Locator/Data/JSON'); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * LocationSearch form. |
|
| 345 | - * |
|
| 346 | - * Search form for locations, updates map and results list via AJAX |
|
| 347 | - * |
|
| 348 | - * @return \SilverStripe\Forms\Form |
|
| 349 | - */ |
|
| 350 | - public function LocationSearch() |
|
| 351 | - { |
|
| 352 | - |
|
| 353 | - $form = LocatorForm::create($this, 'LocationSearch'); |
|
| 354 | - if (class_exists(BootstrapForm::class) && $this->config()->get('bootstrapify')) { |
|
| 355 | - $form->Fields()->bootstrapify(); |
|
| 356 | - $form->Actions()->bootstrapify(); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - return $form |
|
| 360 | - ->setFormMethod('GET') |
|
| 361 | - ->setFormAction($this->Link()) |
|
| 362 | - ->disableSecurityToken() |
|
| 363 | - ->loadDataFrom($this->request->getVars()); |
|
| 364 | - } |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @param HTTPRequest $request |
|
| 180 | + * |
|
| 181 | + * @return bool |
|
| 182 | + */ |
|
| 183 | + public function getTrigger(HTTPRequest $request = null) |
|
| 184 | + { |
|
| 185 | + if ($request === null) { |
|
| 186 | + $request = $this->getRequest(); |
|
| 187 | + } |
|
| 188 | + $trigger = $request->getVar(Config::inst()->get(LocatorController::class, 'query_trigger')); |
|
| 189 | + |
|
| 190 | + return isset($trigger); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @return ArrayList|DataList |
|
| 195 | + */ |
|
| 196 | + public function getLocations() |
|
| 197 | + { |
|
| 198 | + if (!$this->locations) { |
|
| 199 | + $this->setLocations($this->request); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + return $this->locations; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @param HTTPRequest|null $request |
|
| 207 | + * |
|
| 208 | + * @return $this |
|
| 209 | + */ |
|
| 210 | + public function setLocations(HTTPRequest $request = null) |
|
| 211 | + { |
|
| 212 | + |
|
| 213 | + if ($request === null) { |
|
| 214 | + $request = $this->request; |
|
| 215 | + } |
|
| 216 | + $filter = $this->config()->get('base_filter'); |
|
| 217 | + |
|
| 218 | + $categoryVar = $this->data()->config()->get('category_var'); |
|
| 219 | + if ($request->getVar($categoryVar)) { |
|
| 220 | + $filter['Categories.ID'] = $request->getVar($categoryVar); |
|
| 221 | + } else { |
|
| 222 | + if ($this->getPageCategories()->exists()) { |
|
| 223 | + foreach ($this->getPageCategories() as $category) { |
|
| 224 | + $filter['Categories.ID'][] = $category->ID; |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + $this->extend('updateLocatorFilter', $filter, $request); |
|
| 230 | + |
|
| 231 | + $filterAny = $this->config()->get('base_filter_any'); |
|
| 232 | + $this->extend('updateLocatorFilterAny', $filterAny, $request); |
|
| 233 | + |
|
| 234 | + $exclude = $this->config()->get('base_exclude'); |
|
| 235 | + $this->extend('updateLocatorExclude', $exclude, $request); |
|
| 236 | + |
|
| 237 | + $class = $this->data()->ClassName; |
|
| 238 | + $locations = $class::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 = $this->data()->config()->get('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 | + * @return ArrayData|boolean |
|
| 273 | + */ |
|
| 274 | + public function getAddressSearchCoords() |
|
| 275 | + { |
|
| 276 | + $addressVar = Config::inst()->get(DistanceDataExtension::class, 'address_var'); |
|
| 277 | + if (!$this->request->getVar($addressVar)) { |
|
| 278 | + return false; |
|
| 279 | + } |
|
| 280 | + if (class_exists(GoogleGeocoder::class)) { |
|
| 281 | + $geocoder = new GoogleGeocoder($this->request->getVar($addressVar)); |
|
| 282 | + $response = $geocoder->getResult(); |
|
| 283 | + $lat = $response->getLatitude(); |
|
| 284 | + $lng = $response->getLongitude(); |
|
| 285 | + |
|
| 286 | + return new ArrayData([ |
|
| 287 | + "Lat" => $lat, |
|
| 288 | + "Lng" => $lng, |
|
| 289 | + ]); |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @param HTTPRequest $request |
|
| 295 | + * |
|
| 296 | + * @return \SilverStripe\View\ViewableData_Customised |
|
| 297 | + */ |
|
| 298 | + public function index(HTTPRequest $request) |
|
| 299 | + { |
|
| 300 | + if ($this->getTrigger($request)) { |
|
| 301 | + $locations = $this->getLocations(); |
|
| 302 | + } else { |
|
| 303 | + $locations = ArrayList::create(); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + return $this->customise(array( |
|
| 307 | + 'Locations' => $locations, |
|
| 308 | + )); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Renders locations in xml format |
|
| 313 | + * |
|
| 314 | + * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 315 | + */ |
|
| 316 | + public function xml() |
|
| 317 | + { |
|
| 318 | + $this->getResponse()->addHeader("Content-Type", "application/xml"); |
|
| 319 | + $data = new ArrayData(array( |
|
| 320 | + "Locations" => $this->getLocations(), |
|
| 321 | + "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 322 | + )); |
|
| 323 | + |
|
| 324 | + return $data->renderWith('Dynamic/Locator/Data/XML'); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Renders locations in json format |
|
| 329 | + * |
|
| 330 | + * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 331 | + */ |
|
| 332 | + public function json() |
|
| 333 | + { |
|
| 334 | + $this->getResponse()->addHeader("Content-Type", "application/json"); |
|
| 335 | + $data = new ArrayData(array( |
|
| 336 | + "Locations" => $this->getLocations(), |
|
| 337 | + "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 338 | + )); |
|
| 339 | + |
|
| 340 | + return $data->renderWith('Dynamic/Locator/Data/JSON'); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * LocationSearch form. |
|
| 345 | + * |
|
| 346 | + * Search form for locations, updates map and results list via AJAX |
|
| 347 | + * |
|
| 348 | + * @return \SilverStripe\Forms\Form |
|
| 349 | + */ |
|
| 350 | + public function LocationSearch() |
|
| 351 | + { |
|
| 352 | + |
|
| 353 | + $form = LocatorForm::create($this, 'LocationSearch'); |
|
| 354 | + if (class_exists(BootstrapForm::class) && $this->config()->get('bootstrapify')) { |
|
| 355 | + $form->Fields()->bootstrapify(); |
|
| 356 | + $form->Actions()->bootstrapify(); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + return $form |
|
| 360 | + ->setFormMethod('GET') |
|
| 361 | + ->setFormAction($this->Link()) |
|
| 362 | + ->disableSecurityToken() |
|
| 363 | + ->loadDataFrom($this->request->getVars()); |
|
| 364 | + } |
|
| 365 | 365 | } |