|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Locator; |
|
4
|
|
|
|
|
5
|
|
|
use \Page; |
|
|
|
|
|
|
6
|
|
|
use SilverStripe\Control\Director; |
|
7
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader; |
|
8
|
|
|
use SilverStripe\Forms\DropdownField; |
|
9
|
|
|
use SilverStripe\Forms\FieldList; |
|
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 bool |
|
47
|
|
|
*/ |
|
48
|
|
|
private static $show_radius = true; |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
private static $limit = -1; |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
private static $locationClass = Location::class; |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
private static $db = array( |
|
58
|
|
|
'Unit' => 'Enum("m,km","m")', |
|
59
|
|
|
'Clusters' => 'Enum("false,true","false")', |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
private static $many_many = array( |
|
66
|
|
|
'Categories' => LocationCategory::class, |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
private static $table_name = 'Locator'; |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
private static $location_class = Location::class; |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return FieldList |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getCMSFields() |
|
83
|
|
|
{ |
|
84
|
|
|
// so it can easily be extended - concept taken from the blog module |
|
85
|
|
|
$this->beforeUpdateCMSFields(function ($fields) { |
|
86
|
|
|
// Settings |
|
87
|
|
|
$fields->addFieldsToTab('Root.Settings', array( |
|
88
|
|
|
HeaderField::create('DisplayOptions', 'Display Options', 3), |
|
|
|
|
|
|
89
|
|
|
OptionsetField::create('Unit', 'Unit of measure', array( |
|
90
|
|
|
'm' => 'Miles', |
|
91
|
|
|
'km' => 'Kilometers' |
|
92
|
|
|
), 'm'), |
|
93
|
|
|
OptionsetField::create('Clusters', 'Use clusters?', array( |
|
94
|
|
|
'false' => 'No', |
|
95
|
|
|
'true' => 'Yes' |
|
96
|
|
|
), 'false'), |
|
97
|
|
|
)); |
|
98
|
|
|
|
|
99
|
|
|
// Filter categories |
|
100
|
|
|
$config = GridFieldConfig_RelationEditor::create(); |
|
101
|
|
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
|
102
|
|
|
$config->addComponent(new GridFieldAddExistingSearchButton()); |
|
103
|
|
|
$categories = $this->Categories(); |
|
104
|
|
|
$categoriesField = GridField::create('Categories', 'Categories', $categories, $config) |
|
105
|
|
|
->setDescription('only show locations from the selected category'); |
|
106
|
|
|
|
|
107
|
|
|
// Filter |
|
108
|
|
|
$fields->addFieldsToTab('Root.Filter', array( |
|
109
|
|
|
HeaderField::create('CategoryOptionsHeader', 'Location Filtering', 3), |
|
110
|
|
|
$categoriesField, |
|
111
|
|
|
)); |
|
112
|
|
|
}); |
|
113
|
|
|
|
|
114
|
|
|
return parent::getCMSFields(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Gets the list of radius |
|
119
|
|
|
* |
|
120
|
|
|
* @return ArrayList |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getRadii() |
|
123
|
|
|
{ |
|
124
|
|
|
$radii = [ |
|
125
|
|
|
'0' => '25', |
|
126
|
|
|
'1' => '50', |
|
127
|
|
|
'2' => '75', |
|
128
|
|
|
'3' => '100', |
|
129
|
|
|
]; |
|
130
|
|
|
|
|
131
|
|
|
$config_radii = Config::inst()->get(Locator::class, 'radius_array'); |
|
132
|
|
|
if ($config_radii) { |
|
133
|
|
|
$radii = $config_radii; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$list = []; |
|
137
|
|
|
foreach ($radii as $radius) { |
|
138
|
|
|
$list[] = new ArrayData(array( |
|
139
|
|
|
'Radius' => $radius, |
|
140
|
|
|
)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return new ArrayList($list); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Gets the limit of locations |
|
148
|
|
|
* @return mixed |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getLimit() |
|
151
|
|
|
{ |
|
152
|
|
|
return Config::inst()->get(Locator::class, 'limit'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Gets if the radius drop down should be shown |
|
157
|
|
|
* @return mixed |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getShowRadius() |
|
160
|
|
|
{ |
|
161
|
|
|
return Config::inst()->get(Locator::class, 'show_radius'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return mixed |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getCategories() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->Categories()->filter(array( |
|
170
|
|
|
'Locations.ID:GreaterThan' => 0 |
|
171
|
|
|
)); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Gets the info window template |
|
176
|
|
|
* @return mixed |
|
177
|
|
|
*/ |
|
178
|
|
View Code Duplication |
public function getInfoWindowTemplate() |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
$contents = json_encode( |
|
181
|
|
|
file_get_contents( |
|
182
|
|
|
$this->parseModuleResourceReference( |
|
183
|
|
|
Config::inst()->get( |
|
184
|
|
|
Locator::class, |
|
185
|
|
|
'infoWindowTemplate' |
|
186
|
|
|
) |
|
187
|
|
|
) |
|
188
|
|
|
) |
|
189
|
|
|
); |
|
190
|
|
|
|
|
191
|
|
|
return str_replace('\n', '', $contents); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Gets the template for locations in the list |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
|
View Code Duplication |
public function getListTemplate() |
|
|
|
|
|
|
199
|
|
|
{ |
|
200
|
|
|
$contents = json_encode( |
|
201
|
|
|
file_get_contents( |
|
202
|
|
|
$this->parseModuleResourceReference( |
|
203
|
|
|
Config::inst()->get( |
|
204
|
|
|
Locator::class, |
|
205
|
|
|
'listTemplate' |
|
206
|
|
|
) |
|
207
|
|
|
) |
|
208
|
|
|
) |
|
209
|
|
|
); |
|
210
|
|
|
|
|
211
|
|
|
return str_replace('\n', '', $contents); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* From SilverStripe/View/Requirements_Backend |
|
216
|
|
|
* |
|
217
|
|
|
* Modified to use the base folder |
|
218
|
|
|
*/ |
|
219
|
|
|
protected function parseModuleResourceReference($file) |
|
220
|
|
|
{ |
|
221
|
|
|
// String of the form vendor/package:resource. Excludes "http://bla" as that's an absolute URL |
|
222
|
|
|
if (preg_match('#([^\/\/][^ /]*\/[^ /]*) *: *([^ ]*)#', $file, $matches)) { |
|
223
|
|
|
list(, $module, $resource) = $matches; |
|
224
|
|
|
$moduleObj = ModuleLoader::getModule($module); |
|
225
|
|
|
if (!$moduleObj) { |
|
226
|
|
|
throw new \InvalidArgumentException("Can't find module '$module'"); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return Director::baseFolder() . '/' . $moduleObj->getRelativeResourcePath($resource); |
|
|
|
|
|
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return $file; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @param array $filter |
|
237
|
|
|
* @param array $filterAny |
|
238
|
|
|
* @param array $exclude |
|
239
|
|
|
* @param null|callable $callback |
|
240
|
|
|
* |
|
241
|
|
|
* @return DataList|ArrayList |
|
242
|
|
|
*/ |
|
243
|
|
|
public static function get_locations( |
|
244
|
|
|
$filter = [], |
|
245
|
|
|
$filterAny = [], |
|
246
|
|
|
$exclude = [], |
|
247
|
|
|
$callback = null |
|
248
|
|
|
) { |
|
249
|
|
|
$locationClass = Config::inst()->get(Locator::class, 'location_class'); |
|
250
|
|
|
$locations = $locationClass::get()->filter($filter)->exclude($exclude); |
|
251
|
|
|
if (!empty($filterAny)) { |
|
252
|
|
|
$locations = $locations->filterAny($filterAny); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
if (!empty($exclude)) { |
|
256
|
|
|
$locations = $locations->exclude($exclude); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
if ($callback !== null && is_callable($callback)) { |
|
260
|
|
|
$locations->filterByCallback($callback); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
return $locations; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths