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