1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Locator\React\Extensions; |
4
|
|
|
|
5
|
|
|
use Dynamic\SilverStripeGeocoder\AddressDataExtension; |
6
|
|
|
use Dynamic\SilverStripeGeocoder\GoogleGeocoder; |
7
|
|
|
use SilverStripe\Admin\LeftAndMain; |
8
|
|
|
use SilverStripe\Control\Director; |
9
|
|
|
use SilverStripe\Control\HTTPResponse; |
10
|
|
|
use SilverStripe\Core\Config\Config; |
11
|
|
|
use SilverStripe\Core\Convert; |
12
|
|
|
use SilverStripe\Core\Extension; |
13
|
|
|
use SilverStripe\Core\Injector\Injector; |
14
|
|
|
use SilverStripe\Core\Manifest\ModuleResourceLoader; |
15
|
|
|
use SilverStripe\Forms\Schema\FormSchema; |
16
|
|
|
use SilverStripe\Security\SecurityToken; |
17
|
|
|
use SilverStripe\View\Requirements; |
18
|
|
|
use SilverStripe\View\SSViewer; |
19
|
|
|
use SilverStripe\View\ThemeResourceLoader; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class LocatorControllerExtension |
23
|
|
|
* @package Dynamic\Locator\React\Extensions |
24
|
|
|
* |
25
|
|
|
* @property \Dynamic\Locator\LocatorController|LocatorControllerExtension|LocatorExtension $owner |
26
|
|
|
*/ |
27
|
|
|
class LocatorControllerExtension extends Extension |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private static $allowed_actions = [ |
|
|
|
|
34
|
|
|
'schema', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private static $dependencies = [ |
|
|
|
|
41
|
|
|
'FormSchema' => '%$' . FormSchema::class, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Current form schema helper |
46
|
|
|
* |
47
|
|
|
* @var FormSchema |
48
|
|
|
*/ |
49
|
|
|
protected $schema = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get form schema helper |
53
|
|
|
* |
54
|
|
|
* @return FormSchema |
55
|
|
|
*/ |
56
|
3 |
|
public function getFormSchema() |
57
|
|
|
{ |
58
|
3 |
|
return $this->schema; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set form schema helper for this controller |
63
|
|
|
* |
64
|
|
|
* @param FormSchema $schema |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
3 |
|
public function setFormSchema(FormSchema $schema) |
68
|
|
|
{ |
69
|
3 |
|
$this->schema = $schema; |
70
|
3 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* |
75
|
|
|
*/ |
76
|
1 |
|
public function onBeforeInit() |
77
|
|
|
{ |
78
|
|
|
// stops script from loading |
79
|
1 |
|
Requirements::block('jquery-locator'); |
80
|
|
|
|
81
|
|
|
// require i18n translation stuff |
82
|
1 |
|
Requirements::javascript('silverstripe/admin: client/dist/js/i18n.js'); |
83
|
1 |
|
Requirements::add_i18n_javascript('dynamic/silverstripe-locator-react: client/lang'); |
|
|
|
|
84
|
|
|
|
85
|
|
|
// because we need another library when using autocomplete |
86
|
1 |
|
if ($this->owner->Autocomplete) { |
|
|
|
|
87
|
|
|
// google maps api key |
88
|
1 |
|
$key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key'); |
89
|
1 |
|
Requirements::block("https://maps.google.com/maps/api/js?key={$key}"); |
90
|
1 |
|
Requirements::javascript("https://maps.google.com/maps/api/js?key={$key}&libraries=places"); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
Requirements::customScript(" |
94
|
|
|
window.ss = window.ss || {}; |
95
|
1 |
|
window.ss.config = " . $this->owner->getClientConfig() . "; |
|
|
|
|
96
|
|
|
"); |
97
|
|
|
|
98
|
1 |
|
$this->owner->customScript(); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Generates the custom script for settings |
103
|
|
|
*/ |
104
|
2 |
|
public function customScript() |
105
|
|
|
{ |
106
|
2 |
|
$radii = $this->owner->getShowRadius() ? $this->owner->getRadii() : []; |
|
|
|
|
107
|
2 |
|
$radiiString = json_encode($radii); |
108
|
|
|
|
109
|
2 |
|
$categories = $this->owner->getUsedCategories(); |
|
|
|
|
110
|
2 |
|
$categoriesString = $this->owner->categoriesString($categories); |
|
|
|
|
111
|
|
|
|
112
|
2 |
|
$unit = $this->owner->Unit ? $this->owner->Unit : 'm'; |
|
|
|
|
113
|
|
|
// otherwise this is 0 or 1 |
114
|
2 |
|
$clusters = $this->owner->Clusters ? 'true' : 'false'; |
|
|
|
|
115
|
2 |
|
$autocomplete = $this->owner->Autocomplete ? 'true' : 'false'; |
|
|
|
|
116
|
|
|
|
117
|
2 |
|
$stylePath = ModuleResourceLoader::singleton()->resolveURL( |
118
|
2 |
|
$this->owner->getMapStyle() |
|
|
|
|
119
|
|
|
); |
120
|
2 |
|
$searchMarkerIconPath = $this->owner->getSearchIconImage(); |
|
|
|
|
121
|
2 |
|
$markerIconPath = AddressDataExtension::getIconImage(true); |
122
|
|
|
|
123
|
2 |
|
if ($this->owner->SearchMarkerImageID) { |
|
|
|
|
124
|
|
|
$searchMarkerIconPath = $this->owner->SearchMarkerImage()->URL; |
|
|
|
|
125
|
|
|
} |
126
|
2 |
|
if ($this->owner->DefaultMarkerImageID) { |
|
|
|
|
127
|
|
|
$markerIconPath = $this->owner->DefaultMarkerImage()->URL; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
$clusterImages = json_encode($this->getClusterImages()); |
131
|
|
|
|
132
|
|
|
// force to float |
133
|
2 |
|
$defaultLat = (float)$this->owner->DefaultLat; |
|
|
|
|
134
|
2 |
|
$defaultLng = (float)$this->owner->DefaultLng; |
|
|
|
|
135
|
|
|
|
136
|
2 |
|
Requirements::customScript(" |
137
|
|
|
window.dynamic_locator = { |
138
|
2 |
|
'radii': {$radiiString}, |
139
|
2 |
|
'categories': {$categoriesString}, |
140
|
2 |
|
'unit': '{$unit}', |
141
|
2 |
|
'limit': {$this->owner->getLimit()}, |
|
|
|
|
142
|
2 |
|
'clusters': {$clusters}, |
143
|
2 |
|
'mapStylePath': '{$stylePath}', |
144
|
2 |
|
'searchMarkerImagePath': '{$searchMarkerIconPath}', |
145
|
2 |
|
'markerImagePath': '{$markerIconPath}', |
146
|
2 |
|
'clusterImages': {$clusterImages}, |
147
|
|
|
'defaultCenter': { |
148
|
2 |
|
'lat': {$defaultLat}, |
149
|
2 |
|
'lng': {$defaultLng} |
150
|
|
|
}, |
151
|
2 |
|
'autocomplete': {$autocomplete} |
152
|
|
|
}; |
153
|
2 |
|
", 'react-locator'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Gets the maker icon image |
158
|
|
|
* @return null|string |
159
|
|
|
* @var boolean $svg if svgs should be included |
160
|
|
|
*/ |
161
|
2 |
|
public static function getSearchIconImage($svg = true) |
162
|
|
|
{ |
163
|
|
|
$folders = [ |
164
|
2 |
|
'client/dist/img/', |
165
|
|
|
'client/dist/images/', |
166
|
|
|
'dist/img/', |
167
|
|
|
'dist/images/', |
168
|
|
|
'img/', |
169
|
|
|
'images/', |
170
|
|
|
]; |
171
|
|
|
|
172
|
|
|
$extensions = [ |
173
|
2 |
|
'png', |
174
|
|
|
'jpg', |
175
|
|
|
'jpeg', |
176
|
|
|
'gif', |
177
|
|
|
]; |
178
|
|
|
|
179
|
2 |
|
if ($svg === true) { |
180
|
2 |
|
array_unshift($extensions, 'svg'); |
181
|
|
|
} |
182
|
|
|
|
183
|
2 |
|
$file = 'mapSearchIcon'; |
184
|
|
|
|
185
|
2 |
|
foreach ($folders as $folder) { |
186
|
2 |
|
foreach ($extensions as $extension) { |
187
|
2 |
|
if ($icon = ThemeResourceLoader::inst()->findThemedResource( |
188
|
2 |
|
"{$folder}{$file}.{$extension}", |
189
|
2 |
|
SSViewer::get_themes() |
190
|
|
|
)) { |
191
|
2 |
|
return ModuleResourceLoader::resourceURL($icon); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
2 |
|
return false; |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return array|bool |
201
|
|
|
*/ |
202
|
2 |
|
public function getClusterImages() |
203
|
|
|
{ |
204
|
2 |
|
$iconPaths = $this->owner->getFailover()->config()->get('ClusterImages'); |
|
|
|
|
205
|
2 |
|
if (!$iconPaths) { |
206
|
2 |
|
return false; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$icons = []; |
210
|
|
|
foreach ($iconPaths as $path) { |
211
|
|
|
$icon = ThemeResourceLoader::inst()->findThemedResource($path, SSViewer::get_themes()); |
212
|
|
|
if ($icon) { |
213
|
|
|
$icons[] = ModuleResourceLoader::resourceURL($icon); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if (empty($icons)) { |
218
|
|
|
return false; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $icons; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param $categories |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
3 |
|
public function categoriesString($categories) |
230
|
|
|
{ |
231
|
3 |
|
$string = '['; |
232
|
3 |
|
for ($i = 0; $i < $categories->count(); $i++) { |
233
|
1 |
|
$cat = $categories[$i]; |
234
|
1 |
|
$ID = $cat->ID; |
235
|
1 |
|
$Name = $cat->Name; |
236
|
|
|
$string .= "{ |
237
|
1 |
|
'ID': {$ID}, |
238
|
1 |
|
'Name': '{$Name}' |
239
|
|
|
}"; |
240
|
|
|
|
241
|
1 |
|
if ($i !== $categories->count() - 1) { |
242
|
1 |
|
$string .= ','; |
243
|
|
|
} |
244
|
|
|
} |
245
|
3 |
|
$string .= ']'; |
246
|
|
|
|
247
|
3 |
|
return $string; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
1 |
|
public function getClientConfig() |
254
|
|
|
{ |
255
|
1 |
|
$token = SecurityToken::inst(); |
256
|
|
|
|
257
|
|
|
$clientConfig = [ |
258
|
1 |
|
'name' => get_class($this->owner), |
259
|
1 |
|
'url' => trim($this->owner->Link(), '/'), |
|
|
|
|
260
|
1 |
|
'baseUrl' => Director::baseURL(), |
261
|
1 |
|
'absoluteBaseUrl' => Director::absoluteBaseURL(), |
262
|
1 |
|
$token->getName() => $token->getValue(), |
263
|
|
|
'sections' => [], |
264
|
1 |
|
'debugging' => $this->owner->config()->get('debugging'), |
|
|
|
|
265
|
|
|
]; |
266
|
|
|
|
267
|
1 |
|
$clientConfig['sections'][] = Injector::inst()->get(LeftAndMain::class)->getClientConfig(); |
268
|
|
|
|
269
|
1 |
|
$this->owner->extend('updateClientConfig', $clientConfig); |
|
|
|
|
270
|
|
|
|
271
|
1 |
|
return Convert::raw2json($clientConfig); |
|
|
|
|
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Gets a JSON schema representing the search form. |
276
|
|
|
* |
277
|
|
|
* @param HTTPRequest $request |
|
|
|
|
278
|
|
|
* @return HTTPResponse |
279
|
|
|
*/ |
280
|
|
|
public function schema($request) |
|
|
|
|
281
|
|
|
{ |
282
|
|
|
return $this->getSchemaResponse("Locator.SearchForm", $this->owner->LocationSearch()); |
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Check if the current request has a X-Formschema-Request header set. |
287
|
|
|
* Used by conditional logic that responds to validation results |
288
|
|
|
* |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
|
|
protected function getSchemaRequested() |
292
|
|
|
{ |
293
|
|
|
$parts = $this->owner->getRequest()->getHeader(LeftAndMain::SCHEMA_HEADER); |
|
|
|
|
294
|
|
|
return !empty($parts); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Generate schema for the given form based on the X-Formschema-Request header value |
299
|
|
|
* |
300
|
|
|
* @param string $schemaID ID for this schema. Required. |
301
|
|
|
* @param Form $form Required for 'state' or 'schema' response |
|
|
|
|
302
|
|
|
* @param ValidationResult $errors Required for 'error' response |
303
|
|
|
* @param array $extraData Any extra data to be merged with the schema response |
304
|
|
|
* @return HTTPResponse |
305
|
|
|
*/ |
306
|
|
|
protected function getSchemaResponse($schemaID, $form = null, ValidationResult $errors = null, $extraData = []) |
|
|
|
|
307
|
|
|
{ |
308
|
|
|
$parts = $this->owner->getRequest()->getHeader(LeftAndMain::SCHEMA_HEADER); |
309
|
|
|
$data = $this |
310
|
|
|
->getFormSchema() |
311
|
|
|
->getMultipartSchema($parts, $schemaID, $form, $errors); |
312
|
|
|
|
313
|
|
|
if ($extraData) { |
|
|
|
|
314
|
|
|
$data = array_merge($data, $extraData); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
$response = new HTTPResponse(Convert::raw2json($data)); |
|
|
|
|
318
|
|
|
$response->addHeader('Content-Type', 'application/json'); |
319
|
|
|
return $response; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|