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