1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 Formz project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\AssetHandler\Connector; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\AssetHandler\AbstractAssetHandler; |
17
|
|
|
use Romm\Formz\AssetHandler\JavaScript\FieldsActivationJavaScriptAssetHandler; |
18
|
|
|
use Romm\Formz\AssetHandler\JavaScript\FieldsValidationActivationJavaScriptAssetHandler; |
19
|
|
|
use Romm\Formz\AssetHandler\JavaScript\FieldsValidationJavaScriptAssetHandler; |
20
|
|
|
use Romm\Formz\AssetHandler\JavaScript\FormInitializationJavaScriptAssetHandler; |
21
|
|
|
use Romm\Formz\AssetHandler\JavaScript\FormRequestDataJavaScriptAssetHandler; |
22
|
|
|
use Romm\Formz\AssetHandler\JavaScript\FormzConfigurationJavaScriptAssetHandler; |
23
|
|
|
use Romm\Formz\AssetHandler\JavaScript\FormzLocalizationJavaScriptAssetHandler; |
24
|
|
|
use Romm\Formz\Condition\Processor\ConditionProcessor; |
25
|
|
|
use Romm\Formz\Condition\Processor\ConditionProcessorFactory; |
26
|
|
|
use Romm\Formz\Core\Core; |
27
|
|
|
use Romm\Formz\Form\FormObject; |
28
|
|
|
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder; |
29
|
|
|
|
30
|
|
|
class JavaScriptAssetHandlerConnector |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* List of JavaScript files which will be included whenever this view helper |
34
|
|
|
* is used. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $javaScriptFiles = [ |
39
|
|
|
'Formz.Main.js', |
40
|
|
|
'Formz.Misc.js', |
41
|
|
|
'Formz.EventsManager.js', |
42
|
|
|
'Formz.Result.js', |
43
|
|
|
'Formz.Localization.js', |
44
|
|
|
'Form/Formz.Form.js', |
45
|
|
|
'Form/Formz.Form.SubmissionService.js', |
46
|
|
|
'Field/Formz.Field.js', |
47
|
|
|
'Field/Formz.Field.DataAttributesService.js', |
48
|
|
|
'Field/Formz.Field.ValidationService.js', |
49
|
|
|
'Conditions/Formz.Condition.js', |
50
|
|
|
'Validators/Formz.Validation.js', |
51
|
|
|
'Validators/Formz.Validator.Ajax.js' |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var AssetHandlerConnectorManager |
56
|
|
|
*/ |
57
|
|
|
private $assetHandlerConnectorManager; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param AssetHandlerConnectorManager $assetHandlerConnectorManager |
61
|
|
|
*/ |
62
|
|
|
public function __construct(AssetHandlerConnectorManager $assetHandlerConnectorManager) |
63
|
|
|
{ |
64
|
|
|
$this->assetHandlerConnectorManager = $assetHandlerConnectorManager; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Will include all default JavaScript files declared in the property |
69
|
|
|
* `$javaScriptFiles` of this class, as well as the main Formz |
70
|
|
|
* configuration. |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function includeDefaultJavaScriptFiles() |
75
|
|
|
{ |
76
|
|
|
if (Core::get()->isInDebugMode()) { |
77
|
|
|
$this->javaScriptFiles[] = 'Formz.Debug.js'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
foreach ($this->javaScriptFiles as $file) { |
81
|
|
|
$filePath = Core::get()->getExtensionRelativePath('Resources/Public/JavaScript/' . $file); |
82
|
|
|
|
83
|
|
|
$this->includeJsFile($filePath); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* This function will handle the JavaScript language files. |
91
|
|
|
* |
92
|
|
|
* A file will be created for the current language (there can be as many |
93
|
|
|
* files as languages), containing the translations handling for JavaScript. |
94
|
|
|
* If the file already exists, it is directly included. |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function includeLanguageJavaScriptFiles() |
99
|
|
|
{ |
100
|
|
|
$filePath = $this->assetHandlerConnectorManager->getFormzGeneratedFilePath('local-' . Core::get()->getLanguageKey()) . '.js'; |
101
|
|
|
|
102
|
|
|
$this->assetHandlerConnectorManager->createFileInTemporaryDirectory( |
103
|
|
|
$filePath, |
104
|
|
|
function () { |
105
|
|
|
return $this->getFormzLocalizationJavaScriptAssetHandler() |
|
|
|
|
106
|
|
|
->injectTranslationsForFormFieldsValidation() |
107
|
|
|
->getJavaScriptCode(); |
108
|
|
|
} |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->includeJsFile(Core::get()->getResourceRelativePath($filePath)); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Includes Formz configuration JavaScript declaration. If the file exists, |
118
|
|
|
* it is directly included, otherwise the JavaScript code is calculated, |
119
|
|
|
* then put in the cache file. |
120
|
|
|
* |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function generateAndIncludeFormzConfigurationJavaScript() |
124
|
|
|
{ |
125
|
|
|
$formzConfigurationJavaScriptAssetHandler = $this->getFormzConfigurationJavaScriptAssetHandler(); |
126
|
|
|
$fileName = $formzConfigurationJavaScriptAssetHandler->getJavaScriptFileName(); |
127
|
|
|
|
128
|
|
|
$this->assetHandlerConnectorManager->createFileInTemporaryDirectory( |
129
|
|
|
$fileName, |
130
|
|
|
function () use ($formzConfigurationJavaScriptAssetHandler) { |
131
|
|
|
return $formzConfigurationJavaScriptAssetHandler->getJavaScriptCode(); |
132
|
|
|
} |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$this->includeJsFile(Core::get()->getResourceRelativePath($fileName)); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Will include the generated JavaScript, from multiple asset handlers |
142
|
|
|
* sources. |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function generateAndIncludeJavaScript() |
147
|
|
|
{ |
148
|
|
|
$filePath = $this->assetHandlerConnectorManager->getFormzGeneratedFilePath() . '.js'; |
149
|
|
|
|
150
|
|
|
$this->assetHandlerConnectorManager->createFileInTemporaryDirectory( |
151
|
|
|
$filePath, |
152
|
|
|
function () { |
153
|
|
|
return |
154
|
|
|
// Form initialization code. |
155
|
|
|
$this->getFormInitializationJavaScriptAssetHandler() |
|
|
|
|
156
|
|
|
->getFormInitializationJavaScriptCode() . |
157
|
|
|
LF . |
158
|
|
|
// Fields validation code. |
159
|
|
|
$this->getFieldsValidationJavaScriptAssetHandler() |
|
|
|
|
160
|
|
|
->getJavaScriptCode() . |
161
|
|
|
LF . |
162
|
|
|
// Fields activation conditions code. |
163
|
|
|
$this->getFieldsActivationJavaScriptAssetHandler() |
|
|
|
|
164
|
|
|
->getFieldsActivationJavaScriptCode() . |
165
|
|
|
LF . |
166
|
|
|
// Fields validation activation conditions code. |
167
|
|
|
$this->getFieldsValidationActivationJavaScriptAssetHandler() |
|
|
|
|
168
|
|
|
->getFieldsValidationActivationJavaScriptCode(); |
169
|
|
|
} |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$this->includeJsFile(Core::get()->getResourceRelativePath($filePath)); |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Here we generate the JavaScript code containing the submitted values, and |
179
|
|
|
* the existing errors, which is dynamically created at each request. |
180
|
|
|
* |
181
|
|
|
* The code is then injected as inline code in the DOM. |
182
|
|
|
* |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
|
|
public function generateAndIncludeInlineJavaScript() |
186
|
|
|
{ |
187
|
|
|
$formName = $this->assetHandlerConnectorManager |
188
|
|
|
->getAssetHandlerFactory() |
189
|
|
|
->getFormObject() |
190
|
|
|
->getName(); |
191
|
|
|
|
192
|
|
|
$javaScriptCode = $this->getFormRequestDataJavaScriptAssetHandler() |
|
|
|
|
193
|
|
|
->getFormRequestDataJavaScriptCode(); |
194
|
|
|
|
195
|
|
|
if (Core::get()->isInDebugMode()) { |
196
|
|
|
$javaScriptCode .= LF . $this->getDebugActivationCode(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$uri = $this->getAjaxUrl(); |
200
|
|
|
|
201
|
|
|
$javaScriptCode .= LF; |
202
|
|
|
$javaScriptCode .= "Formz.setAjaxUrl('$uri');"; |
203
|
|
|
|
204
|
|
|
$this->addInlineJs('Formz - Initialization ' . $formName, $javaScriptCode); |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Will include all new JavaScript files given, by checking that every given |
211
|
|
|
* file was not already included. |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
|
|
public function includeJavaScriptValidationAndConditionFiles() |
216
|
|
|
{ |
217
|
|
|
$javaScriptValidationFiles = $this->getJavaScriptFiles(); |
218
|
|
|
$assetHandlerConnectorStates = $this->assetHandlerConnectorManager |
219
|
|
|
->getAssetHandlerConnectorStates(); |
220
|
|
|
|
221
|
|
|
foreach ($javaScriptValidationFiles as $file) { |
222
|
|
|
if (false === in_array($file, $assetHandlerConnectorStates->getAlreadyIncludedValidationJavaScriptFiles())) { |
223
|
|
|
$assetHandlerConnectorStates->registerIncludedValidationJavaScriptFiles($file); |
224
|
|
|
$this->includeJsFile(Core::get()->getResourceRelativePath($file)); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns the list of JavaScript files which are used for the current form |
233
|
|
|
* object. |
234
|
|
|
* |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
|
|
protected function getJavaScriptFiles() |
238
|
|
|
{ |
239
|
|
|
$formObject = $this->assetHandlerConnectorManager |
240
|
|
|
->getAssetHandlerFactory() |
241
|
|
|
->getFormObject(); |
242
|
|
|
|
243
|
|
|
$javaScriptFiles = $this->getFieldsValidationJavaScriptAssetHandler() |
|
|
|
|
244
|
|
|
->getJavaScriptValidationFiles(); |
245
|
|
|
|
246
|
|
|
$conditionProcessor = $this->getConditionProcessor($formObject); |
247
|
|
|
|
248
|
|
|
$javaScriptFiles = array_merge($javaScriptFiles, $conditionProcessor->getJavaScriptFiles()); |
249
|
|
|
|
250
|
|
|
return $javaScriptFiles; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* We need an abstraction function because the footer inclusion for assets |
255
|
|
|
* does not work in backend. It means we include every JavaScript asset in |
256
|
|
|
* the header when the request is in a backend context. |
257
|
|
|
* |
258
|
|
|
* @see https://forge.typo3.org/issues/60213 |
259
|
|
|
* |
260
|
|
|
* @param string $path |
261
|
|
|
*/ |
262
|
|
|
protected function includeJsFile($path) |
263
|
|
|
{ |
264
|
|
|
$pageRenderer = $this->assetHandlerConnectorManager->getPageRenderer(); |
265
|
|
|
|
266
|
|
|
if (Core::get()->getEnvironmentService()->isEnvironmentInFrontendMode()) { |
267
|
|
|
$pageRenderer->addJsFooterFile($path); |
268
|
|
|
} else { |
269
|
|
|
$pageRenderer->addJsFile($path); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @see includeJsFile() |
275
|
|
|
* |
276
|
|
|
* @param string $name |
277
|
|
|
* @param string $javaScriptCode |
278
|
|
|
*/ |
279
|
|
|
protected function addInlineJs($name, $javaScriptCode) |
280
|
|
|
{ |
281
|
|
|
$pageRenderer = $this->assetHandlerConnectorManager->getPageRenderer(); |
282
|
|
|
|
283
|
|
|
if (Core::get()->getEnvironmentService()->isEnvironmentInFrontendMode()) { |
284
|
|
|
$pageRenderer->addJsFooterInlineCode($name, $javaScriptCode); |
285
|
|
|
} else { |
286
|
|
|
$pageRenderer->addJsInlineCode($name, $javaScriptCode); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
protected function getAjaxUrl() |
294
|
|
|
{ |
295
|
|
|
/** @var UriBuilder $uriBuilder */ |
296
|
|
|
$uriBuilder = Core::get()->getObjectManager()->get(UriBuilder::class); |
297
|
|
|
|
298
|
|
|
return $uriBuilder->reset() |
299
|
|
|
->setTargetPageType(1473682545) |
300
|
|
|
->setNoCache(true) |
301
|
|
|
->setUseCacheHash(false) |
302
|
|
|
->setCreateAbsoluteUri(true) |
303
|
|
|
->build(); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
|
|
protected function getDebugActivationCode() |
310
|
|
|
{ |
311
|
|
|
return 'Formz.Debug.activate();'; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param FormObject $formObject |
316
|
|
|
* @return ConditionProcessor |
317
|
|
|
*/ |
318
|
|
|
protected function getConditionProcessor(FormObject $formObject) |
319
|
|
|
{ |
320
|
|
|
return ConditionProcessorFactory::getInstance() |
321
|
|
|
->get($formObject); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return FormzConfigurationJavaScriptAssetHandler|AbstractAssetHandler |
326
|
|
|
*/ |
327
|
|
|
protected function getFormzConfigurationJavaScriptAssetHandler() |
328
|
|
|
{ |
329
|
|
|
return $this->assetHandlerConnectorManager |
330
|
|
|
->getAssetHandlerFactory() |
331
|
|
|
->getAssetHandler(FormzConfigurationJavaScriptAssetHandler::class); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @return FormInitializationJavaScriptAssetHandler|AbstractAssetHandler |
336
|
|
|
*/ |
337
|
|
|
protected function getFormInitializationJavaScriptAssetHandler() |
338
|
|
|
{ |
339
|
|
|
return $this->assetHandlerConnectorManager |
340
|
|
|
->getAssetHandlerFactory() |
341
|
|
|
->getAssetHandler(FormInitializationJavaScriptAssetHandler::class); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @return FieldsValidationJavaScriptAssetHandler|AbstractAssetHandler |
346
|
|
|
*/ |
347
|
|
|
protected function getFieldsValidationJavaScriptAssetHandler() |
348
|
|
|
{ |
349
|
|
|
return $this->assetHandlerConnectorManager |
350
|
|
|
->getAssetHandlerFactory() |
351
|
|
|
->getAssetHandler(FieldsValidationJavaScriptAssetHandler::class); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @return FieldsActivationJavaScriptAssetHandler|AbstractAssetHandler |
356
|
|
|
*/ |
357
|
|
|
protected function getFieldsActivationJavaScriptAssetHandler() |
358
|
|
|
{ |
359
|
|
|
return $this->assetHandlerConnectorManager |
360
|
|
|
->getAssetHandlerFactory() |
361
|
|
|
->getAssetHandler(FieldsActivationJavaScriptAssetHandler::class); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @return FieldsValidationActivationJavaScriptAssetHandler|AbstractAssetHandler |
366
|
|
|
*/ |
367
|
|
|
protected function getFieldsValidationActivationJavaScriptAssetHandler() |
368
|
|
|
{ |
369
|
|
|
return $this->assetHandlerConnectorManager |
370
|
|
|
->getAssetHandlerFactory() |
371
|
|
|
->getAssetHandler(FieldsValidationActivationJavaScriptAssetHandler::class); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @return FormRequestDataJavaScriptAssetHandler|AbstractAssetHandler |
376
|
|
|
*/ |
377
|
|
|
protected function getFormRequestDataJavaScriptAssetHandler() |
378
|
|
|
{ |
379
|
|
|
return $this->assetHandlerConnectorManager |
380
|
|
|
->getAssetHandlerFactory() |
381
|
|
|
->getAssetHandler(FormRequestDataJavaScriptAssetHandler::class); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @return FormzLocalizationJavaScriptAssetHandler|AbstractAssetHandler |
386
|
|
|
*/ |
387
|
|
|
protected function getFormzLocalizationJavaScriptAssetHandler() |
388
|
|
|
{ |
389
|
|
|
return $this->assetHandlerConnectorManager |
390
|
|
|
->getAssetHandlerFactory() |
391
|
|
|
->getAssetHandler(FormzLocalizationJavaScriptAssetHandler::class); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: