[!!!][TASK] TCA: Remove wizard _HIDDENFIELD and hideParent
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / InputElement.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Element;
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\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Backend\Utility\IconUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\MathUtility;
21 use TYPO3\CMS\Backend\Form\NodeFactory;
22
23 /**
24 * Generation of TCEform elements of the type "input"
25 */
26 class InputElement extends AbstractFormElement {
27
28 /**
29 * This will render a single-line input form field, possibly with various control/validation features
30 *
31 * @return array As defined in initializeResultArray() of AbstractNode
32 */
33 public function render() {
34 $languageService = $this->getLanguageService();
35
36 $table = $this->globalOptions['table'];
37 $fieldName = $this->globalOptions['fieldName'];
38 $row = $this->globalOptions['databaseRow'];
39 $parameterArray = $this->globalOptions['parameterArray'];
40 $resultArray = $this->initializeResultArray();
41 $isDateField = FALSE;
42
43 $config = $parameterArray['fieldConf']['config'];
44 $specConf = BackendUtility::getSpecConfParts($parameterArray['fieldConf']['defaultExtras']);
45 $size = MathUtility::forceIntegerInRange($config['size'] ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
46 $evalList = GeneralUtility::trimExplode(',', $config['eval'], TRUE);
47 $classes = array();
48 $attributes = array();
49
50 if (!isset($config['checkbox'])) {
51 $config['checkbox'] = '0';
52 $checkboxIsset = FALSE;
53 } else {
54 $checkboxIsset = TRUE;
55 }
56
57 // set all date times available
58 $dateFormats = array(
59 'date' => '%d-%m-%Y',
60 'year' => '%Y',
61 'time' => '%H:%M',
62 'timesec' => '%H:%M:%S'
63 );
64 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat']) {
65 $dateFormats['date'] = '%m-%d-%Y';
66 }
67 $dateFormats['datetime'] = $dateFormats['time'] . ' ' . $dateFormats['date'];
68 $dateFormats['datetimesec'] = $dateFormats['timesec'] . ' ' . $dateFormats['date'];
69
70 // readonly
71 if ($this->isGlobalReadonly() || $config['readOnly']) {
72 $itemFormElValue = $parameterArray['itemFormElValue'];
73 if (in_array('date', $evalList)) {
74 $config['format'] = 'date';
75 } elseif (in_array('datetime', $evalList)) {
76 $config['format'] = 'datetime';
77 } elseif (in_array('time', $evalList)) {
78 $config['format'] = 'time';
79 }
80 if (in_array('password', $evalList)) {
81 $itemFormElValue = $itemFormElValue ? '*********' : '';
82 }
83 $options = $this->globalOptions;
84 $options['parameterArray'] = array(
85 'fieldConf' => array(
86 'config' => $config,
87 ),
88 'itemFormElValue' => $itemFormElValue,
89 );
90 $options['renderType'] = 'none';
91 /** @var NodeFactory $nodeFactory */
92 $nodeFactory = $this->globalOptions['nodeFactory'];
93 return $nodeFactory->create($options)->render();
94 }
95
96 if (in_array('datetime', $evalList, TRUE)
97 || in_array('date', $evalList)) {
98
99 $classes[] = 't3js-datetimepicker';
100 $isDateField = TRUE;
101 if (in_array('datetime', $evalList)) {
102 $attributes['data-date-type'] = 'datetime';
103 $dateFormat = $dateFormats['datetime'];
104 } elseif (in_array('date', $evalList)) {
105 $attributes['data-date-type'] = 'date';
106 $dateFormat = $dateFormats['date'];
107 }
108 if ($parameterArray['itemFormElValue'] > 0) {
109 $parameterArray['itemFormElValue'] += date('Z', $parameterArray['itemFormElValue']);
110 }
111 if (isset($config['range']['lower'])) {
112 $attributes['data-date-minDate'] = (int)$config['range']['lower'];
113 }
114 if (isset($config['range']['upper'])) {
115 $attributes['data-date-maxDate'] = (int)$config['range']['upper'];
116 }
117 } elseif (in_array('time', $evalList)) {
118 $dateFormat = $dateFormats['time'];
119 $isDateField = TRUE;
120 $classes[] = 't3js-datetimepicker';
121 $attributes['data-date-type'] = 'time';
122 } elseif (in_array('timesec', $evalList)) {
123 $dateFormat = $dateFormats['timesec'];
124 $isDateField = TRUE;
125 $classes[] = 't3js-datetimepicker';
126 $attributes['data-date-type'] = 'timesec';
127 } else {
128 if ($checkboxIsset === FALSE) {
129 $config['checkbox'] = '';
130 }
131 }
132
133 // @todo: The whole eval handling is a mess and needs refactoring
134 foreach ($evalList as $func) {
135 switch ($func) {
136 case 'required':
137 $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString(array('required' => TRUE));
138 break;
139 default:
140 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
141 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
142 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
143 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
144 if (class_exists($func)) {
145 $evalObj = GeneralUtility::makeInstance($func);
146 if (method_exists($evalObj, 'deevaluateFieldValue')) {
147 $_params = array(
148 'value' => $parameterArray['itemFormElValue']
149 );
150 $parameterArray['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
151 }
152 }
153 }
154 }
155 }
156 $paramsList = GeneralUtility::quoteJSvalue($parameterArray['itemFormElName']) . ',' . GeneralUtility::quoteJSvalue(implode(',', $evalList)) . ',' . GeneralUtility::quoteJSvalue(trim($config['is_in'])) . ',' . ($config['checkbox'] ? 1 : 0) . ',' . GeneralUtility::quoteJSvalue($config['checkbox']);
157 $parameterArray['fieldChangeFunc'] = array_merge(array('typo3form.fieldGet' => 'typo3form.fieldGet(' . $paramsList . ');'), $parameterArray['fieldChangeFunc']);
158
159 // set classes
160 $classes[] = 'form-control';
161 $classes[] = 't3js-clearable';
162 $classes[] = 'hasDefaultValue';
163
164 // calculate attributes
165 $attributes['id'] = str_replace('.', '', uniqid('formengine-input-', TRUE));
166 $attributes['name'] = $parameterArray['itemFormElName'] . '_hr';
167 $attributes['value'] = '';
168 $attributes['maxlength'] = $config['max'] ?: 256;
169 $attributes['onchange'] = implode('', $parameterArray['fieldChangeFunc']);
170
171 if (!empty($styles)) {
172 $attributes['style'] = implode(' ', $styles);
173 }
174 if (!empty($classes)) {
175 $attributes['class'] = implode(' ', $classes);
176 }
177 if (isset($config['max']) && (int)$config['max'] > 0) {
178 $attributes['maxlength'] = (int)$config['max'];
179 }
180
181 // Build the attribute string
182 $attributeString = '';
183 foreach ($attributes as $attributeName => $attributeValue) {
184 $attributeString .= ' ' . $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
185 }
186
187 // This is the EDITABLE form field.
188 $placeholderValue = $this->getPlaceholderValue($table, $config, $row);
189 $placeholderAttribute = '';
190 if (!empty($placeholderValue)) {
191 $placeholderAttribute = ' placeholder="' . htmlspecialchars(trim($languageService->sL($placeholderValue))) . '" ';
192 }
193
194 $html = '
195 <input type="text"'
196 . $attributeString
197 . $placeholderAttribute
198 . $parameterArray['onFocus'] . ' />';
199
200 // This is the ACTUAL form field - values from the EDITABLE field must be transferred to this field which is the one that is written to the database.
201 $html .= '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" />';
202
203 $resultArray['extJSCODE'] = 'typo3form.fieldSet(' . $paramsList . ');';
204
205 // Going through all custom evaluations configured for this field
206 // @todo: Similar to above code!
207 foreach ($evalList as $evalData) {
208 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$evalData])) {
209 if (class_exists($evalData)) {
210 $evalObj = GeneralUtility::makeInstance($evalData);
211 if (method_exists($evalObj, 'returnFieldJS')) {
212 $resultArray['extJSCODE'] .= LF . 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($evalData) . '] = function(value) {' . $evalObj->returnFieldJS() . '}';
213 }
214 }
215 }
216 }
217
218 // add HTML wrapper
219 if ($isDateField) {
220 $html = '
221 <div class="input-group">
222 ' . $html . '
223 <span class="input-group-btn">
224 <label class="btn btn-default" for="' . $attributes['id'] . '">
225 ' . IconUtility::getSpriteIcon('actions-edit-pick-date') . '
226 </label>
227 </span>
228 </div>';
229 }
230
231 // Wrap a wizard around the item?
232 $html = $this->renderWizards(
233 array($html),
234 $config['wizards'],
235 $table,
236 $row,
237 $fieldName,
238 $parameterArray,
239 $parameterArray['itemFormElName'] . '_hr', $specConf
240 );
241
242 // Add a wrapper to remain maximum width
243 $width = (int)$this->formMaxWidth($size);
244 $html = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px"' : '') . '>' . $html . '</div>';
245 $resultArray['html'] = $html;
246 return $resultArray;
247 }
248
249 }