TYPO3 CMS  TYPO3_7-6
HandleIncomingFormValues.php
Go to the documentation of this file.
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 
22 
27 {
31  protected $sessionUtility;
32 
37  public function injectSessionUtility(\TYPO3\CMS\Form\Utility\SessionUtility $sessionUtility)
38  {
39  $this->sessionUtility = $sessionUtility;
40  }
41 
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 
126  /* Multiselect */
127  if (is_array($incomingData)) {
128  if (in_array($modelValue, $incomingData, true)) {
129  $element->setAdditionalArgument('selected', 'selected');
130  } else {
131  $element->setAdditionalArgument('selected', null);
132  }
133  } else {
134  if ($modelValue === $incomingData) {
135  $element->setAdditionalArgument('selected', 'selected');
136  } else {
137  $element->setAdditionalArgument('selected', null);
138  }
139  }
140  } elseif ($element->getElementType() === 'TEXTAREA') {
141  $incomingData = $formBuilder->getIncomingData()->getIncomingField($elementName);
142  $element->setAdditionalArgument('text', $incomingData);
143  } elseif ($element->getElementType() === 'FILEUPLOAD') {
144  if (
145  $formBuilder->getValidationErrors() == null
146  || (
147  $formBuilder->getValidationErrors()
148  && $formBuilder->getValidationErrors()->forProperty($elementName)->hasErrors() !== true
149  )
150  ) {
151  $uploadedFiles = $formBuilder->getIncomingData()->getIncomingField($elementName);
152  if (is_array($uploadedFiles)) {
153  foreach ($uploadedFiles as $key => &$file) {
154  $tempFilename = $this->saveUploadedFile($file['tmp_name']);
155  if (!$tempFilename) {
156  unset($uploadedFiles[$key]);
157  continue;
158  }
159  $file['tempFilename'] = $tempFilename;
160  }
161  $element->setAdditionalArgument('uploadedFiles', $uploadedFiles);
162  $this->setAttribute($element, 'value', '');
163  $this->sessionUtility->setSessionData($elementName, $uploadedFiles);
164  }
165  }
166  }
167  }
168 
175  public function saveUploadedFile($uploadedFile)
176  {
177  if (is_uploaded_file($uploadedFile)) {
178  $tempFilename = GeneralUtility::upload_to_tempfile($uploadedFile);
179  if (TYPO3_OS === 'WIN') {
180  $tempFilename = GeneralUtility::fixWindowsFilePath($tempFilename);
181  }
182  if ($tempFilename !== '') {
183  return $tempFilename;
184  }
185  }
186  return null;
187  }
188 
197  public function setAttribute(Element $element, $key, $value = '')
198  {
199  if ($element->getHtmlAttribute($key) !== null) {
200  $element->setHtmlAttribute($key, $value);
201  } else {
202  $element->setAdditionalArgument($key, $value);
203  }
204  }
205 }
injectSessionUtility(\TYPO3\CMS\Form\Utility\SessionUtility $sessionUtility)
setHtmlAttribute($key='', $value=null)
Definition: Element.php:342
static upload_to_tempfile($uploadedFileName)
handleIncomingFormValues(Element $element, ValidationElement $validationElement, $modelValue, FormBuilder $formBuilder)
setAdditionalArgument($key='', $value=null)
Definition: Element.php:182