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\JavaScript; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\AssetHandler\AbstractAssetHandler; |
17
|
|
|
use Romm\Formz\Configuration\Form\Field\Field; |
18
|
|
|
use Romm\Formz\Error\FormzMessageInterface; |
19
|
|
|
use Romm\Formz\Service\ArrayService; |
20
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Will handle the "one time run" data needed by JavaScript: the submitted |
24
|
|
|
* values, and others. |
25
|
|
|
*/ |
26
|
|
|
class FormRequestDataJavaScriptAssetHandler extends AbstractAssetHandler |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* See class description. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getFormRequestDataJavaScriptCode() |
35
|
|
|
{ |
36
|
|
|
$submittedFormValues = []; |
37
|
|
|
$fieldsExistingMessages = []; |
38
|
|
|
$deactivatedFields = []; |
39
|
|
|
$formWasSubmitted = 'false'; |
40
|
|
|
|
41
|
|
|
if ($this->getFormObject()->formWasSubmitted()) { |
42
|
|
|
$formWasSubmitted = 'true'; |
43
|
|
|
$submittedFormValues = $this->getSubmittedFormValues(); |
44
|
|
|
$fieldsExistingMessages = $this->getFieldsExistingMessages(); |
45
|
|
|
$deactivatedFields = $this->getDeactivatedFields(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$submittedFormValues = ArrayService::get()->arrayToJavaScriptJson($submittedFormValues); |
49
|
|
|
$fieldsExistingMessages = ArrayService::get()->arrayToJavaScriptJson($fieldsExistingMessages); |
50
|
|
|
$deactivatedFields = ArrayService::get()->arrayToJavaScriptJson($deactivatedFields); |
51
|
|
|
|
52
|
|
|
$formName = GeneralUtility::quoteJSvalue($this->getFormObject()->getName()); |
53
|
|
|
|
54
|
|
|
$javaScriptCode = <<<JS |
55
|
|
|
(function() { |
56
|
|
|
Fz.Form.beforeInitialization($formName, function(form) { |
57
|
|
|
form.injectRequestData($submittedFormValues, $fieldsExistingMessages, $formWasSubmitted, $deactivatedFields) |
58
|
|
|
}); |
59
|
|
|
})(); |
60
|
|
|
JS; |
61
|
|
|
|
62
|
|
|
return $javaScriptCode; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Will fetch the values of the fields of the submitted form, if a form has |
67
|
|
|
* been submitted. |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected function getSubmittedFormValues() |
72
|
|
|
{ |
73
|
|
|
$result = []; |
74
|
|
|
$formName = $this->getFormObject()->getName(); |
75
|
|
|
$originalRequest = $this->getControllerContext() |
76
|
|
|
->getRequest() |
77
|
|
|
->getOriginalRequest(); |
78
|
|
|
|
79
|
|
|
if (null !== $originalRequest |
80
|
|
|
&& $originalRequest->hasArgument($formName) |
81
|
|
|
) { |
82
|
|
|
$result = $originalRequest->getArgument($formName); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* This function checks every message which may exist on every property of |
90
|
|
|
* the form (used to tell to JavaScript which messages already exist). |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
protected function getFieldsExistingMessages() |
95
|
|
|
{ |
96
|
|
|
$fieldsMessages = []; |
97
|
|
|
$formObject = $this->getFormObject(); |
98
|
|
|
|
99
|
|
|
if ($formObject->formWasSubmitted() |
100
|
|
|
&& $formObject->hasFormResult() |
101
|
|
|
) { |
102
|
|
|
foreach ($this->getFormObject()->getConfiguration()->getFields() as $field) { |
103
|
|
|
$fieldsMessages[$field->getFieldName()] = $this->getSingleFieldExistingMessages($field); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $fieldsMessages; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Field $field |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
protected function getSingleFieldExistingMessages(Field $field) |
115
|
|
|
{ |
116
|
|
|
$formResult = $this->getFormObject()->getFormResult(); |
117
|
|
|
$result = $formResult->forProperty($field->getFieldName()); |
118
|
|
|
$messages = []; |
119
|
|
|
|
120
|
|
|
if ($result->hasErrors()) { |
121
|
|
|
$messages['errors'] = $this->formatMessages($result->getErrors()); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($result->hasWarnings()) { |
125
|
|
|
$messages['warnings'] = $this->formatMessages($result->getWarnings()); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($result->hasNotices()) { |
129
|
|
|
$messages['notices'] = $this->formatMessages($result->getNotices()); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $messages; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param FormzMessageInterface[] $messages |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
protected function formatMessages(array $messages) |
140
|
|
|
{ |
141
|
|
|
$sortedMessages = []; |
142
|
|
|
|
143
|
|
|
foreach ($messages as $message) { |
144
|
|
|
$validationName = $message->getValidationName(); |
145
|
|
|
$messageKey = $message->getMessageKey(); |
146
|
|
|
|
147
|
|
|
if (false === isset($sortedMessages[$validationName])) { |
148
|
|
|
$sortedMessages[$validationName] = []; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$sortedMessages[$validationName][$messageKey] = $message->render(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $sortedMessages; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
protected function getDeactivatedFields() |
161
|
|
|
{ |
162
|
|
|
return ($this->getFormObject()->hasFormResult()) |
163
|
|
|
? array_keys($this->getFormObject()->getFormResult()->getDeactivatedFields()) |
164
|
|
|
: []; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: