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 = ModuleResourceLoader::singleton()->resolveURL( |
121
|
2 |
|
$this->owner->getSearchIconImage() |
|
|
|
|
122
|
|
|
); |
123
|
2 |
|
$markerIconPath = ModuleResourceLoader::singleton()->resolveURL( |
124
|
2 |
|
AddressDataExtension::getIconImage(true) |
125
|
|
|
); |
126
|
|
|
|
127
|
2 |
|
if ($this->owner->SearchMarkerImageID) { |
|
|
|
|
128
|
|
|
$searchMarkerIconPath = $this->owner->SearchMarkerImage()->URL; |
|
|
|
|
129
|
|
|
} |
130
|
2 |
|
if ($this->owner->DefaultMarkerImageID) { |
|
|
|
|
131
|
|
|
$markerIconPath = $this->owner->DefaultMarkerImage()->URL; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// force to float |
135
|
2 |
|
$defaultLat = (float)$this->owner->DefaultLat; |
|
|
|
|
136
|
2 |
|
$defaultLng = (float)$this->owner->DefaultLng; |
|
|
|
|
137
|
|
|
|
138
|
2 |
|
Requirements::customScript(" |
139
|
|
|
window.dynamic_locator = { |
140
|
2 |
|
'radii': {$radiiString}, |
141
|
2 |
|
'categories': {$categoriesString}, |
142
|
2 |
|
'unit': '{$unit}', |
143
|
2 |
|
'limit': {$this->owner->getLimit()}, |
|
|
|
|
144
|
2 |
|
'clusters': {$clusters}, |
145
|
2 |
|
'mapStylePath': '{$stylePath}', |
146
|
2 |
|
'searchMarkerImagePath': '{$searchMarkerIconPath}', |
147
|
2 |
|
'markerImagePath': '{$markerIconPath}', |
148
|
|
|
'defaultCenter': { |
149
|
2 |
|
'lat': {$defaultLat}, |
150
|
2 |
|
'lng': {$defaultLng} |
151
|
|
|
}, |
152
|
2 |
|
'autocomplete': {$autocomplete} |
153
|
|
|
}; |
154
|
2 |
|
", 'react-locator'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Gets the maker icon image |
159
|
|
|
* @return null|string |
160
|
|
|
* @var boolean $svg if svgs should be included |
161
|
|
|
*/ |
162
|
2 |
|
public static function getSearchIconImage($svg = true) |
163
|
|
|
{ |
164
|
|
|
$folders = [ |
165
|
2 |
|
'client/dist/img/', |
166
|
|
|
'client/dist/images/', |
167
|
|
|
'dist/img/', |
168
|
|
|
'dist/images/', |
169
|
|
|
'img/', |
170
|
|
|
'images/', |
171
|
|
|
]; |
172
|
|
|
|
173
|
|
|
$extensions = [ |
174
|
2 |
|
'png', |
175
|
|
|
'jpg', |
176
|
|
|
'jpeg', |
177
|
|
|
'gif', |
178
|
|
|
]; |
179
|
|
|
|
180
|
2 |
|
if ($svg === true) { |
181
|
2 |
|
array_unshift($extensions, 'svg'); |
182
|
|
|
} |
183
|
|
|
|
184
|
2 |
|
$file = 'mapSearchIcon'; |
185
|
|
|
|
186
|
2 |
|
foreach ($folders as $folder) { |
187
|
2 |
|
foreach ($extensions as $extension) { |
188
|
2 |
|
if ($icon = ThemeResourceLoader::inst()->findThemedResource( |
189
|
2 |
|
"{$folder}{$file}.{$extension}", |
190
|
2 |
|
SSViewer::get_themes() |
191
|
|
|
)) { |
192
|
2 |
|
return ModuleResourceLoader::resourceURL($icon); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
2 |
|
return false; |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param $categories |
202
|
|
|
* |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
3 |
|
public function categoriesString($categories) |
206
|
|
|
{ |
207
|
3 |
|
$string = '['; |
208
|
3 |
|
for ($i = 0; $i < $categories->count(); $i++) { |
209
|
1 |
|
$cat = $categories[$i]; |
210
|
1 |
|
$ID = $cat->ID; |
211
|
1 |
|
$Name = $cat->Name; |
212
|
|
|
$string .= "{ |
213
|
1 |
|
'ID': {$ID}, |
214
|
1 |
|
'Name': '{$Name}' |
215
|
|
|
}"; |
216
|
|
|
|
217
|
1 |
|
if ($i !== $categories->count() - 1) { |
218
|
1 |
|
$string .= ','; |
219
|
|
|
} |
220
|
|
|
} |
221
|
3 |
|
$string .= ']'; |
222
|
|
|
|
223
|
3 |
|
return $string; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
1 |
|
public function getClientConfig() |
230
|
|
|
{ |
231
|
1 |
|
$token = SecurityToken::inst(); |
232
|
|
|
|
233
|
|
|
$clientConfig = [ |
234
|
1 |
|
'name' => get_class($this->owner), |
235
|
1 |
|
'url' => trim($this->owner->Link(), '/'), |
|
|
|
|
236
|
1 |
|
'baseUrl' => Director::baseURL(), |
237
|
1 |
|
'absoluteBaseUrl' => Director::absoluteBaseURL(), |
238
|
1 |
|
$token->getName() => $token->getValue(), |
239
|
|
|
'sections' => [], |
240
|
1 |
|
'debugging' => $this->owner->config()->get('debugging'), |
|
|
|
|
241
|
|
|
]; |
242
|
|
|
|
243
|
1 |
|
$clientConfig['sections'][] = Injector::inst()->get(LeftAndMain::class)->getClientConfig(); |
244
|
|
|
|
245
|
1 |
|
$this->owner->extend('updateClientConfig', $clientConfig); |
|
|
|
|
246
|
|
|
|
247
|
1 |
|
return Convert::raw2json($clientConfig); |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Gets a JSON schema representing the search form. |
252
|
|
|
* |
253
|
|
|
* @param HTTPRequest $request |
|
|
|
|
254
|
|
|
* @return HTTPResponse |
255
|
|
|
*/ |
256
|
|
|
public function schema($request) |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
return $this->getSchemaResponse("Locator.SearchForm", $this->owner->LocationSearch()); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Check if the current request has a X-Formschema-Request header set. |
263
|
|
|
* Used by conditional logic that responds to validation results |
264
|
|
|
* |
265
|
|
|
* @return bool |
266
|
|
|
*/ |
267
|
|
|
protected function getSchemaRequested() |
268
|
|
|
{ |
269
|
|
|
$parts = $this->owner->getRequest()->getHeader(LeftAndMain::SCHEMA_HEADER); |
|
|
|
|
270
|
|
|
return !empty($parts); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Generate schema for the given form based on the X-Formschema-Request header value |
275
|
|
|
* |
276
|
|
|
* @param string $schemaID ID for this schema. Required. |
277
|
|
|
* @param Form $form Required for 'state' or 'schema' response |
|
|
|
|
278
|
|
|
* @param ValidationResult $errors Required for 'error' response |
279
|
|
|
* @param array $extraData Any extra data to be merged with the schema response |
280
|
|
|
* @return HTTPResponse |
281
|
|
|
*/ |
282
|
|
|
protected function getSchemaResponse($schemaID, $form = null, ValidationResult $errors = null, $extraData = []) |
|
|
|
|
283
|
|
|
{ |
284
|
|
|
$parts = $this->owner->getRequest()->getHeader(LeftAndMain::SCHEMA_HEADER); |
285
|
|
|
$data = $this |
286
|
|
|
->getFormSchema() |
287
|
|
|
->getMultipartSchema($parts, $schemaID, $form, $errors); |
288
|
|
|
|
289
|
|
|
if ($extraData) { |
|
|
|
|
290
|
|
|
$data = array_merge($data, $extraData); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
$response = new HTTPResponse(Convert::raw2json($data)); |
|
|
|
|
294
|
|
|
$response->addHeader('Content-Type', 'application/json'); |
295
|
|
|
return $response; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|