[TASK] EXT:form - remove unused url parameters
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Hooks / HandleIncomingFormValues.php
1 <?php
2 namespace TYPO3\CMS\Form\Hooks;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\SingletonInterface;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Form\Domain\Model\Element;
20 use TYPO3\CMS\Form\Domain\Model\ValidationElement;
21 use TYPO3\CMS\Form\Domain\Builder\FormBuilder;
22
23 /**
24 * Handle the incoming form data
25 */
26 class HandleIncomingFormValues implements SingletonInterface {
27
28 /**
29 * @var \TYPO3\CMS\Form\Utility\SessionUtility
30 */
31 protected $sessionUtility;
32
33 /**
34 * @param \TYPO3\CMS\Form\Utility\SessionUtility $sessionUtility
35 * @return void
36 */
37 public function injectSessionUtility(\TYPO3\CMS\Form\Utility\SessionUtility $sessionUtility) {
38 $this->sessionUtility = $sessionUtility;
39 }
40
41 /**
42 * Handle the incoming form data
43 *
44 * @param Element $element The element
45 * @param ValidationElement $validationElement
46 * @param mixed $modelValue
47 * @param FormBuilder $formBuilder
48 * @return void
49 */
50 public function handleIncomingFormValues(Element $element, ValidationElement $validationElement, $modelValue, FormBuilder $formBuilder) {
51 $elementName = $element->getName();
52
53 if ($element->getElementType() === 'CHECKBOX') {
54 $groupedElement = FALSE;
55 if ($element->getParentElement()->getElementType() === 'CHECKBOXGROUP') {
56 $incomingName = $element->getParentElement()->getName();
57 $groupedElement = TRUE;
58 } else {
59 $incomingName = $elementName;
60 }
61 $incomingData = $formBuilder->getIncomingData()->getIncomingField($incomingName);
62 $checked = FALSE;
63 if (is_array($incomingData)) {
64 if (
65 isset($incomingData[$elementName])
66 && $incomingData[$elementName] !== ''
67 ) {
68 $this->setAttribute($element, 'checked', 'checked');
69 $checked = TRUE;
70 } else {
71 $this->setAttribute($element, 'checked', NULL);
72 }
73 } else {
74 if (
75 (!empty($modelValue) && $incomingData === $modelValue)
76 || $incomingData === $incomingName . '-' . $element->getElementCounter()
77 ) {
78 $this->setAttribute($element, 'checked', 'checked');
79 $checked = TRUE;
80 } else {
81 $this->setAttribute($element, 'checked', NULL);
82 }
83 }
84 if (
85 $groupedElement
86 && $checked
87 ) {
88 $element->getParentElement()->setAdditionalArgument('atLeastOneCheckedChildElement', TRUE);
89 }
90 } elseif ($element->getElementType() === 'RADIO') {
91 $groupedElement = FALSE;
92 if ($element->getParentElement()->getElementType() === 'RADIOGROUP') {
93 $incomingName = $element->getParentElement()->getName();
94 $groupedElement = TRUE;
95 } else {
96 $incomingName = $elementName;
97 }
98 $checked = FALSE;
99 $incomingData = $formBuilder->getIncomingData()->getIncomingField($incomingName);
100 if (
101 (!empty($modelValue) && $incomingData === $modelValue)
102 || $incomingData === $incomingName . '-' . $element->getElementCounter()
103 ) {
104 $this->setAttribute($element, 'checked', 'checked');
105 $checked = TRUE;
106 } else {
107 $this->setAttribute($element, 'checked', NULL);
108 }
109 if (
110 $groupedElement
111 && $checked
112 ) {
113 $element->getParentElement()->setAdditionalArgument('atLeastOneCheckedChildElement', TRUE);
114 }
115 } elseif ($element->getElementType() === 'OPTION') {
116 $modelValue = (string)($element->getAdditionalArgument('value') ?: $element->getElementCounter());
117 if ($element->getParentElement()->getElementType() === 'OPTGROUP') {
118 $parentName = $element->getParentElement()->getParentElement()->getName();
119 } else {
120 $parentName = $element->getParentElement()->getName();
121 }
122 $incomingData = $formBuilder->getIncomingData()->getIncomingField($parentName);
123 /* Multiselect */
124 if (is_array($incomingData)) {
125 if (in_array($modelValue, $incomingData, TRUE)) {
126 $element->setHtmlAttribute('selected', 'selected');
127 } else {
128 $element->setHtmlAttribute('selected', NULL);
129 }
130 } else {
131 if ($modelValue === $incomingData) {
132 $element->setHtmlAttribute('selected', 'selected');
133 } else {
134 $element->setHtmlAttribute('selected', NULL);
135 }
136 }
137 } elseif ($element->getElementType() === 'TEXTAREA') {
138 $incomingData = $formBuilder->getIncomingData()->getIncomingField($elementName);
139 $element->setAdditionalArgument('text', $incomingData);
140 } elseif ($element->getElementType() === 'FILEUPLOAD') {
141 if (
142 $formBuilder->getValidationErrors() == NULL
143 || (
144 $formBuilder->getValidationErrors()
145 && $formBuilder->getValidationErrors()->forProperty($elementName)->hasErrors() !== TRUE
146 )
147 ) {
148 $formPrefix = $formBuilder->getFormPrefix();
149 if (
150 isset($_FILES['tx_form_form']['tmp_name'][$formPrefix])
151 && is_array($_FILES['tx_form_form']['tmp_name'][$formPrefix])
152 ) {
153 foreach ($_FILES['tx_form_form']['tmp_name'][$formPrefix] as $fieldName => $uploadedFile) {
154 $uploadedFiles = array();
155 if (is_string($uploadedFile)) {
156 $uploadedFiles[] = $this->saveUploadedFile($formPrefix, $fieldName, -1, $uploadedFile);
157 } else {
158 // multi upload
159 foreach ($uploadedFile as $key => $file) {
160 $uploadedFiles[] = $this->saveUploadedFile($formPrefix, $fieldName, $key, $file);
161 }
162 }
163 $element->setAdditionalArgument('uploadedFiles', $uploadedFiles);
164 $this->setAttribute($element, 'value', '');
165 $this->sessionUtility->setSessionData($fieldName, $uploadedFiles);
166 }
167 }
168 }
169 }
170 }
171
172 /**
173 * Save a uploaded file
174 *
175 * @param string $formPrefix
176 * @param string $fieldName
177 * @param integer $key
178 * @param string $uploadedFile
179 * @return NULL|array
180 */
181 public function saveUploadedFile($formPrefix, $fieldName, $key, $uploadedFile) {
182 if (is_uploaded_file($uploadedFile)) {
183 $tempFilename = GeneralUtility::upload_to_tempfile($uploadedFile);
184 if (TYPO3_OS === 'WIN') {
185 $tempFilename = GeneralUtility::fixWindowsFilePath($tempFilename);
186 }
187 if ($tempFilename !== '') {
188 if ($key == -1) {
189 $originalFilename = $_FILES['tx_form_form']['name'][$formPrefix][$fieldName];
190 $size = $_FILES['tx_form_form']['size'][$formPrefix][$fieldName];
191 } else {
192 $originalFilename = $_FILES['tx_form_form']['name'][$formPrefix][$fieldName][$key];
193 $size = $_FILES['tx_form_form']['size'][$formPrefix][$fieldName][$key];
194 }
195 $fileInfo = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Type\File\FileInfo::class, $tempFilename);
196 return array(
197 'tempFilename' => $tempFilename,
198 'originalFilename' => $originalFilename,
199 'type' => $fileInfo->getMimeType(),
200 'size' => (int)$size
201 );
202 }
203 }
204 return NULL;
205 }
206
207 /**
208 * Set the value Attribute to the right place
209 *
210 * @param Element $element The element
211 * @param string $key
212 * @param string $value
213 * @return void
214 */
215 public function setAttribute(Element $element, $key, $value = '') {
216 if ($element->getHtmlAttribute($key) !== NULL) {
217 $element->setHtmlAttribute($key, $value);
218 } else {
219 $element->setAdditionalArgument($key, $value);
220 }
221 }
222
223 }