1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Locator; |
4
|
|
|
|
5
|
|
|
use Dynamic\SilverStripeGeocoder\AddressDataExtension; |
6
|
|
|
use SilverStripe\Core\Manifest\ModuleResourceLoader; |
7
|
|
|
use SilverStripe\Forms\CheckboxField; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
10
|
|
|
use SilverStripe\Forms\HeaderField; |
11
|
|
|
use SilverStripe\Forms\OptionsetField; |
12
|
|
|
use SilverStripe\Forms\GridField\GridField; |
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; |
14
|
|
|
use SilverStripe\ORM\DataList; |
15
|
|
|
use SilverStripe\ORM\ArrayList; |
16
|
|
|
use SilverStripe\Core\Config\Config; |
17
|
|
|
use SilverStripe\View\ArrayData; |
18
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Locator |
22
|
|
|
* |
23
|
|
|
* @property bool $AutoGeocode |
24
|
|
|
* @property bool $ModalWindow |
25
|
|
|
* @property string $Unit |
26
|
|
|
* @method Categories|ManyManyList $Categories |
27
|
|
|
*/ |
28
|
|
|
class Locator extends \Page |
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 = [ |
|
|
|
|
49
|
|
|
'Unit' => 'Enum("m,km","m")', |
50
|
|
|
'ResultsOnLoad' => 'Boolean', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private static $many_many = [ |
|
|
|
|
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', [ |
85
|
|
|
HeaderField::create('DisplayOptions', 'Display Options', 3), |
86
|
|
|
OptionsetField::create('Unit', 'Unit of measure', ['m' => 'Miles', 'km' => 'Kilometers']), |
87
|
|
|
CheckboxField::create('ResultsOnLoad', 'Show results on page load') |
88
|
|
|
->setDescription('For larger collections of locations, it is |
89
|
|
|
recommended to only show a limited amount of results after a location |
90
|
|
|
search.') |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
// Filter categories |
94
|
|
|
$config = GridFieldConfig_RelationEditor::create(); |
95
|
|
|
$config->removeComponentsByType(GridFieldAddExistingAutocompleter::class); |
96
|
|
|
$config->addComponent(new GridFieldAddExistingSearchButton()); |
97
|
|
|
$categories = $this->Categories(); |
|
|
|
|
98
|
|
|
$categoriesField = GridField::create('Categories', 'Categories', $categories, $config) |
99
|
|
|
->setDescription('only show locations from the selected category'); |
100
|
|
|
|
101
|
|
|
// Filter |
102
|
|
|
$fields->addFieldsToTab('Root.Filter', [ |
103
|
|
|
HeaderField::create('CategoryOptionsHeader', 'Location Filtering', 3), |
104
|
|
|
$categoriesField, |
105
|
|
|
]); |
106
|
|
|
}); |
107
|
|
|
|
108
|
|
|
return parent::getCMSFields(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $filter |
113
|
|
|
* @param array $filterAny |
114
|
|
|
* @param array $exclude |
115
|
|
|
* @param null|callable $callback |
116
|
|
|
* |
117
|
|
|
* @return DataList|ArrayList |
118
|
|
|
*/ |
119
|
|
|
public static function get_locations( |
120
|
|
|
$filter = [], |
121
|
|
|
$filterAny = [], |
122
|
|
|
$exclude = [], |
123
|
|
|
$callback = null |
124
|
|
|
) { |
125
|
|
|
$locationClass = Config::inst()->get(static::class, 'location_class'); |
126
|
|
|
$locations = $locationClass::get()->filter($filter)->exclude($exclude); |
127
|
|
|
|
128
|
|
|
if (!empty($filterAny)) { |
129
|
|
|
$locations = $locations->filterAny($filterAny); |
130
|
|
|
} |
131
|
|
|
if (!empty($exclude)) { |
132
|
|
|
$locations = $locations->exclude($exclude); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($callback !== null && is_callable($callback)) { |
136
|
|
|
$locations->filterByCallback($callback); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $locations; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return DataList |
144
|
|
|
*/ |
145
|
|
|
public static function get_all_categories() |
146
|
|
|
{ |
147
|
|
|
return LocationCategory::get(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function getPageCategories() |
154
|
|
|
{ |
155
|
|
|
return self::locator_categories_by_locator($this->ID); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param int $id |
160
|
|
|
* |
161
|
|
|
* @return bool| |
162
|
|
|
*/ |
163
|
|
|
public static function locator_categories_by_locator($id = 0) |
164
|
|
|
{ |
165
|
|
|
if ($id == 0) { |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** @var Locator $locator */ |
170
|
|
|
if ($locator = static::get()->byID($id)) { |
171
|
|
|
return $locator->getUsedCategories(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Gets the list of radii |
179
|
|
|
* |
180
|
|
|
* @return ArrayList |
181
|
|
|
*/ |
182
|
|
|
public function getRadii() |
183
|
|
|
{ |
184
|
|
|
$radii = [ |
185
|
|
|
'0' => '25', |
186
|
|
|
'1' => '50', |
187
|
|
|
'2' => '75', |
188
|
|
|
'3' => '100', |
189
|
|
|
]; |
190
|
|
|
$config_radii = $this->config()->get('radii'); |
191
|
|
|
if ($config_radii) { |
192
|
|
|
$radii = $config_radii; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $radii; |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function getRadiiArrayList() |
199
|
|
|
{ |
200
|
|
|
$list = []; |
201
|
|
|
|
202
|
|
|
foreach ($this->getRadii() as $radius) { |
203
|
|
|
$list[] = new ArrayData([ |
204
|
|
|
'Radius' => $radius, |
205
|
|
|
]); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return new ArrayList($list); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Gets the limit of locations |
213
|
|
|
* @return mixed |
214
|
|
|
*/ |
215
|
|
|
public function getLimit() |
216
|
|
|
{ |
217
|
|
|
return $this->config()->get('limit'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Gets if the radius drop down should be shown |
222
|
|
|
* @return mixed |
223
|
|
|
*/ |
224
|
|
|
public function getShowRadius() |
225
|
|
|
{ |
226
|
|
|
return $this->config()->get('show_radius'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return mixed |
231
|
|
|
*/ |
232
|
|
|
public function getUsedCategories() |
233
|
|
|
{ |
234
|
|
|
return $this->Categories()->filter([ |
235
|
|
|
'LocationSet.ID:GreaterThan' => 0, |
236
|
|
|
]); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Gets the path of the info window template |
241
|
|
|
* |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
public function getInfoWindowTemplate() |
245
|
|
|
{ |
246
|
|
|
return ModuleResourceLoader::singleton()->resolveURL( |
247
|
|
|
Config::inst()->get( |
248
|
|
|
static::class, |
249
|
|
|
'infoWindowTemplate' |
250
|
|
|
) |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Gets the path of the list template |
256
|
|
|
* |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
public function getListTemplate() |
260
|
|
|
{ |
261
|
|
|
return ModuleResourceLoader::singleton()->resolveURL( |
262
|
|
|
Config::inst()->get( |
263
|
|
|
static::class, |
264
|
|
|
'listTemplate' |
265
|
|
|
) |
266
|
|
|
); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return null|string |
271
|
|
|
*/ |
272
|
|
|
public function getMapStyle() |
273
|
|
|
{ |
274
|
|
|
return AddressDataExtension::getMapStyleJSON(); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function getMapStyleJSONPath() |
278
|
|
|
{ |
279
|
|
|
return AddressDataExtension::getMapStyleJSONPath(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return null|string |
284
|
|
|
*/ |
285
|
|
|
public function getMarkerIcon() |
286
|
|
|
{ |
287
|
|
|
return AddressDataExtension::getIconImage(); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|