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->assetHandlerConnectorManager |
84
|
|
|
->getPageRenderer() |
85
|
|
|
->addJsFile($filePath); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* This function will handle the JavaScript language files. |
93
|
|
|
* |
94
|
|
|
* A file will be created for the current language (there can be as many |
95
|
|
|
* files as languages), containing the translations handling for JavaScript. |
96
|
|
|
* If the file already exists, it is directly included. |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function includeLanguageJavaScriptFiles() |
101
|
|
|
{ |
102
|
|
|
$filePath = $this->assetHandlerConnectorManager->getFormzGeneratedFilePath('local-' . Core::get()->getLanguageKey()) . '.js'; |
103
|
|
|
|
104
|
|
|
$this->assetHandlerConnectorManager->createFileInTemporaryDirectory( |
105
|
|
|
$filePath, |
106
|
|
|
function () { |
107
|
|
|
return $this->getFormzLocalizationJavaScriptAssetHandler() |
|
|
|
|
108
|
|
|
->injectTranslationsForFormFieldsValidation() |
109
|
|
|
->getJavaScriptCode(); |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$this->assetHandlerConnectorManager |
114
|
|
|
->getPageRenderer() |
115
|
|
|
->addJsFooterFile($filePath); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Includes Formz configuration JavaScript declaration. If the file exists, |
122
|
|
|
* it is directly included, otherwise the JavaScript code is calculated, |
123
|
|
|
* then put in the cache file. |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
|
|
public function generateAndIncludeFormzConfigurationJavaScript() |
128
|
|
|
{ |
129
|
|
|
$formzConfigurationJavaScriptAssetHandler = $this->getFormzConfigurationJavaScriptAssetHandler(); |
130
|
|
|
$fileName = $formzConfigurationJavaScriptAssetHandler->getJavaScriptFileName(); |
131
|
|
|
|
132
|
|
|
$this->assetHandlerConnectorManager->createFileInTemporaryDirectory( |
133
|
|
|
$fileName, |
134
|
|
|
function () use ($formzConfigurationJavaScriptAssetHandler) { |
135
|
|
|
return $formzConfigurationJavaScriptAssetHandler->getJavaScriptCode(); |
136
|
|
|
} |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$this->assetHandlerConnectorManager |
140
|
|
|
->getPageRenderer() |
141
|
|
|
->addJsFooterFile($fileName); |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Will include the generated JavaScript, from multiple asset handlers |
148
|
|
|
* sources. |
149
|
|
|
* |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
public function generateAndIncludeJavaScript() |
153
|
|
|
{ |
154
|
|
|
$filePath = $this->assetHandlerConnectorManager->getFormzGeneratedFilePath() . '.js'; |
155
|
|
|
|
156
|
|
|
$this->assetHandlerConnectorManager->createFileInTemporaryDirectory( |
157
|
|
|
$filePath, |
158
|
|
|
function () { |
159
|
|
|
return |
160
|
|
|
// Form initialization code. |
161
|
|
|
$this->getFormInitializationJavaScriptAssetHandler() |
|
|
|
|
162
|
|
|
->getFormInitializationJavaScriptCode() . |
163
|
|
|
LF . |
164
|
|
|
// Fields validation code. |
165
|
|
|
$this->getFieldsValidationJavaScriptAssetHandler() |
|
|
|
|
166
|
|
|
->getJavaScriptCode() . |
167
|
|
|
LF . |
168
|
|
|
// Fields activation conditions code. |
169
|
|
|
$this->getFieldsActivationJavaScriptAssetHandler() |
|
|
|
|
170
|
|
|
->getFieldsActivationJavaScriptCode() . |
171
|
|
|
LF . |
172
|
|
|
// Fields validation activation conditions code. |
173
|
|
|
$this->getFieldsValidationActivationJavaScriptAssetHandler() |
|
|
|
|
174
|
|
|
->getFieldsValidationActivationJavaScriptCode(); |
175
|
|
|
} |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
$this->assetHandlerConnectorManager |
179
|
|
|
->getPageRenderer() |
180
|
|
|
->addJsFooterFile($filePath); |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Here we generate the JavaScript code containing the submitted values, and |
187
|
|
|
* the existing errors, which is dynamically created at each request. |
188
|
|
|
* |
189
|
|
|
* The code is then injected as inline code in the DOM. |
190
|
|
|
* |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
|
|
public function generateAndIncludeInlineJavaScript() |
194
|
|
|
{ |
195
|
|
|
$formName = $this->assetHandlerConnectorManager |
196
|
|
|
->getAssetHandlerFactory() |
197
|
|
|
->getFormObject() |
198
|
|
|
->getName(); |
199
|
|
|
|
200
|
|
|
$javaScriptCode = $this->getFormRequestDataJavaScriptAssetHandler() |
|
|
|
|
201
|
|
|
->getFormRequestDataJavaScriptCode(); |
202
|
|
|
|
203
|
|
|
if (Core::get()->isInDebugMode()) { |
204
|
|
|
$javaScriptCode .= LF . $this->getDebugActivationCode(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$uri = $this->getAjaxUrl(); |
208
|
|
|
|
209
|
|
|
$javaScriptCode .= LF; |
210
|
|
|
$javaScriptCode .= "Formz.setAjaxUrl('$uri');"; |
211
|
|
|
|
212
|
|
|
$this->assetHandlerConnectorManager |
213
|
|
|
->getPageRenderer() |
214
|
|
|
->addJsFooterInlineCode('Formz - Initialization ' . $formName, $javaScriptCode); |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Will include all new JavaScript files given, by checking that every given |
221
|
|
|
* file was not already included. |
222
|
|
|
* |
223
|
|
|
* @return $this |
224
|
|
|
*/ |
225
|
|
|
public function includeJavaScriptValidationAndConditionFiles() |
226
|
|
|
{ |
227
|
|
|
$javaScriptValidationFiles = $this->getJavaScriptFiles(); |
228
|
|
|
$assetHandlerConnectorStates = $this->assetHandlerConnectorManager |
229
|
|
|
->getAssetHandlerConnectorStates(); |
230
|
|
|
|
231
|
|
|
foreach ($javaScriptValidationFiles as $file) { |
232
|
|
|
if (false === in_array($file, $assetHandlerConnectorStates->getAlreadyIncludedValidationJavaScriptFiles())) { |
233
|
|
|
$path = Core::get()->getResourceRelativePath($file); |
234
|
|
|
$assetHandlerConnectorStates->registerIncludedValidationJavaScriptFiles($file); |
235
|
|
|
|
236
|
|
|
$this->assetHandlerConnectorManager |
237
|
|
|
->getPageRenderer() |
238
|
|
|
->addJsFooterFile($path); |
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->assetHandlerConnectorManager |
254
|
|
|
->getAssetHandlerFactory() |
255
|
|
|
->getFormObject(); |
256
|
|
|
|
257
|
|
|
$javaScriptFiles = $this->getFieldsValidationJavaScriptAssetHandler() |
|
|
|
|
258
|
|
|
->getJavaScriptValidationFiles(); |
259
|
|
|
|
260
|
|
|
$conditionProcessor = $this->getConditionProcessor($formObject); |
261
|
|
|
|
262
|
|
|
$javaScriptFiles = array_merge($javaScriptFiles, $conditionProcessor->getJavaScriptFiles()); |
263
|
|
|
|
264
|
|
|
return $javaScriptFiles; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
|
|
protected function getAjaxUrl() |
271
|
|
|
{ |
272
|
|
|
/** @var UriBuilder $uriBuilder */ |
273
|
|
|
$uriBuilder = Core::get()->getObjectManager()->get(UriBuilder::class); |
274
|
|
|
|
275
|
|
|
return $uriBuilder->reset() |
276
|
|
|
->setTargetPageType(1473682545) |
277
|
|
|
->setNoCache(true) |
278
|
|
|
->setUseCacheHash(false) |
279
|
|
|
->setCreateAbsoluteUri(true) |
280
|
|
|
->build(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
protected function getDebugActivationCode() |
287
|
|
|
{ |
288
|
|
|
return 'Formz.Debug.activate();'; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param FormObject $formObject |
293
|
|
|
* @return ConditionProcessor |
294
|
|
|
*/ |
295
|
|
|
protected function getConditionProcessor(FormObject $formObject) |
296
|
|
|
{ |
297
|
|
|
return ConditionProcessorFactory::getInstance() |
298
|
|
|
->get($formObject); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @return FormzConfigurationJavaScriptAssetHandler|AbstractAssetHandler |
303
|
|
|
*/ |
304
|
|
|
protected function getFormzConfigurationJavaScriptAssetHandler() |
305
|
|
|
{ |
306
|
|
|
return $this->assetHandlerConnectorManager |
307
|
|
|
->getAssetHandlerFactory() |
308
|
|
|
->getAssetHandler(FormzConfigurationJavaScriptAssetHandler::class); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return FormInitializationJavaScriptAssetHandler|AbstractAssetHandler |
313
|
|
|
*/ |
314
|
|
|
protected function getFormInitializationJavaScriptAssetHandler() |
315
|
|
|
{ |
316
|
|
|
return $this->assetHandlerConnectorManager |
317
|
|
|
->getAssetHandlerFactory() |
318
|
|
|
->getAssetHandler(FormInitializationJavaScriptAssetHandler::class); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @return FieldsValidationJavaScriptAssetHandler|AbstractAssetHandler |
323
|
|
|
*/ |
324
|
|
|
protected function getFieldsValidationJavaScriptAssetHandler() |
325
|
|
|
{ |
326
|
|
|
return $this->assetHandlerConnectorManager |
327
|
|
|
->getAssetHandlerFactory() |
328
|
|
|
->getAssetHandler(FieldsValidationJavaScriptAssetHandler::class); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @return FieldsActivationJavaScriptAssetHandler|AbstractAssetHandler |
333
|
|
|
*/ |
334
|
|
|
protected function getFieldsActivationJavaScriptAssetHandler() |
335
|
|
|
{ |
336
|
|
|
return $this->assetHandlerConnectorManager |
337
|
|
|
->getAssetHandlerFactory() |
338
|
|
|
->getAssetHandler(FieldsActivationJavaScriptAssetHandler::class); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @return FieldsValidationActivationJavaScriptAssetHandler|AbstractAssetHandler |
343
|
|
|
*/ |
344
|
|
|
protected function getFieldsValidationActivationJavaScriptAssetHandler() |
345
|
|
|
{ |
346
|
|
|
return $this->assetHandlerConnectorManager |
347
|
|
|
->getAssetHandlerFactory() |
348
|
|
|
->getAssetHandler(FieldsValidationActivationJavaScriptAssetHandler::class); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @return FormRequestDataJavaScriptAssetHandler|AbstractAssetHandler |
353
|
|
|
*/ |
354
|
|
|
protected function getFormRequestDataJavaScriptAssetHandler() |
355
|
|
|
{ |
356
|
|
|
return $this->assetHandlerConnectorManager |
357
|
|
|
->getAssetHandlerFactory() |
358
|
|
|
->getAssetHandler(FormRequestDataJavaScriptAssetHandler::class); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* @return FormzLocalizationJavaScriptAssetHandler|AbstractAssetHandler |
363
|
|
|
*/ |
364
|
|
|
protected function getFormzLocalizationJavaScriptAssetHandler() |
365
|
|
|
{ |
366
|
|
|
return $this->assetHandlerConnectorManager |
367
|
|
|
->getAssetHandlerFactory() |
368
|
|
|
->getAssetHandler(FormzLocalizationJavaScriptAssetHandler::class); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
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: