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