cd850b8d66adab6ecbb61b730967afbd2367d18c
[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 {
39 $this->sessionUtility = $sessionUtility;
40 }
41
42 /**
43 * Handle the incoming form data
44 *
45 * @param Element $element The element
46 * @param ValidationElement $validationElement
47 * @param mixed $modelValue
48 * @param FormBuilder $formBuilder
49 * @return void
50 */
51 public function handleIncomingFormValues(Element $element, ValidationElement $validationElement, $modelValue, FormBuilder $formBuilder)
52 {
53 $elementName = $element->getName();
54
55 if ($element->getElementType() === 'CHECKBOX') {
56 $groupedElement = false;
57 if ($element->getParentElement()->getElementType() === 'CHECKBOXGROUP') {
58 $incomingName = $element->getParentElement()->getName();
59 $groupedElement = true;
60 } else {
61 $incomingName = $elementName;
62 }
63 $incomingData = $formBuilder->getIncomingData()->getIncomingField($incomingName);
64 $checked = false;
65 if (is_array($incomingData)) {
66 if (
67 isset($incomingData[$elementName])
68 && $incomingData[$elementName] !== ''
69 ) {
70 $this->setAttribute($element, 'checked', 'checked');
71 $checked = true;
72 } else {
73 $this->setAttribute($element, 'checked', null);
74 }
75 } else {
76 if (
77 (!empty($modelValue) && $incomingData === $modelValue)
78 || $incomingData === $incomingName . '-' . $element->getElementCounter()
79 ) {
80 $this->setAttribute($element, 'checked', 'checked');
81 $checked = true;
82 } else {
83 $this->setAttribute($element, 'checked', null);
84 }
85 }
86 if (
87 $groupedElement
88 && $checked
89 ) {
90 $element->getParentElement()->setAdditionalArgument('atLeastOneCheckedChildElement', true);
91 }
92 } elseif ($element->getElementType() === 'RADIO') {
93 $groupedElement = false;
94 if ($element->getParentElement()->getElementType() === 'RADIOGROUP') {
95 $incomingName = $element->getParentElement()->getName();
96 $groupedElement = true;
97 } else {
98 $incomingName = $elementName;
99 }
100 $checked = false;
101 $incomingData = $formBuilder->getIncomingData()->getIncomingField($incomingName);
102 if (
103 (!empty($modelValue) && $incomingData === $modelValue)
104 || $incomingData === $incomingName . '-' . $element->getElementCounter()
105 ) {
106 $this->setAttribute($element, 'checked', 'checked');
107 $checked = true;
108 } else {
109 $this->setAttribute($element, 'checked', null);
110 }
111 if (
112 $groupedElement
113 && $checked
114 ) {
115 $element->getParentElement()->setAdditionalArgument('atLeastOneCheckedChildElement', true);
116 }
117 } elseif ($element->getElementType() === 'OPTION') {
118 $modelValue = (string)($element->getAdditionalArgument('value') ?: $element->getElementCounter());
119 if ($element->getParentElement()->getElementType() === 'OPTGROUP') {
120 $parentName = $element->getParentElement()->getParentElement()->getName();
121 } else {
122 $parentName = $element->getParentElement()->getName();
123 }
124 $incomingData = $formBuilder->getIncomingData()->getIncomingField($parentName);
125 /* Multiselect */
126 if (is_array($incomingData)) {
127 if (in_array($modelValue, $incomingData, true)) {
128 $element->setHtmlAttribute('selected', 'selected');
129 } else {
130 $element->setHtmlAttribute('selected', null);
131 }
132 } else {
133 if ($modelValue === $incomingData) {
134 $element->setHtmlAttribute('selected', 'selected');
135 } else {
136 $element->setHtmlAttribute('selected', null);
137 }
138 }
139 } elseif ($element->getElementType() === 'TEXTAREA') {
140 $incomingData = $formBuilder->getIncomingData()->getIncomingField($elementName);
141 $element->setAdditionalArgument('text', $incomingData);
142 } elseif ($element->getElementType() === 'FILEUPLOAD') {
143 if (
144 $formBuilder->getValidationErrors() == null
145 || (
146 $formBuilder->getValidationErrors()
147 && $formBuilder->getValidationErrors()->forProperty($elementName)->hasErrors() !== true
148 )
149 ) {
150 $uploadedFiles = $formBuilder->getIncomingData()->getIncomingField($elementName);
151 if (is_array($uploadedFiles)) {
152 foreach ($uploadedFiles as $key => &$file) {
153 $tempFilename = $this->saveUploadedFile($file['tmp_name']);
154 if (!$tempFilename) {
155 unset($uploadedFiles[$key]);
156 continue;
157 }
158 $file['tempFilename'] = $tempFilename;
159 }
160 $element->setAdditionalArgument('uploadedFiles', $uploadedFiles);
161 $this->setAttribute($element, 'value', '');
162 $this->sessionUtility->setSessionData($elementName, $uploadedFiles);
163 }
164 }
165 }
166 }
167
168 /**
169 * Save a uploaded file
170 *
171 * @param string $uploadedFile
172 * @return NULL|string
173 */
174 public function saveUploadedFile($uploadedFile)
175 {
176 if (is_uploaded_file($uploadedFile)) {
177 $tempFilename = GeneralUtility::upload_to_tempfile($uploadedFile);
178 if (TYPO3_OS === 'WIN') {
179 $tempFilename = GeneralUtility::fixWindowsFilePath($tempFilename);
180 }
181 if ($tempFilename !== '') {
182 return $tempFilename;
183 }
184 }
185 return null;
186 }
187
188 /**
189 * Set the value Attribute to the right place
190 *
191 * @param Element $element The element
192 * @param string $key
193 * @param string $value
194 * @return void
195 */
196 public function setAttribute(Element $element, $key, $value = '')
197 {
198 if ($element->getHtmlAttribute($key) !== null) {
199 $element->setHtmlAttribute($key, $value);
200 } else {
201 $element->setAdditionalArgument($key, $value);
202 }
203 }
204 }