[TASK] Resident Eval - The Removal from FormEngine
[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 = array(
157 'field' => $parameterArray['itemFormElName'],
158 'evalList' => implode(',', $evalList),
159 'is_in' => trim($config['is_in']),
160 'checkbox' => ($config['checkbox'] ? 1 : 0),
161 'checkboxValue' => $config['checkbox'],
162 );
163 // set classes
164 $classes[] = 'form-control';
165 $classes[] = 't3js-clearable';
166 $classes[] = 'hasDefaultValue';
167
168 // calculate attributes
169 $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString($config);
170 $attributes['data-formengine-input-params'] = json_encode($paramsList);
171 $attributes['id'] = str_replace('.', '', uniqid('formengine-input-', TRUE));
172 $attributes['name'] = $parameterArray['itemFormElName'] . '_hr';
173 $attributes['value'] = '';
174 $attributes['maxlength'] = $config['max'] ?: 256;
175
176 if (!empty($styles)) {
177 $attributes['style'] = implode(' ', $styles);
178 }
179 if (!empty($classes)) {
180 $attributes['class'] = implode(' ', $classes);
181 }
182 if (isset($config['max']) && (int)$config['max'] > 0) {
183 $attributes['maxlength'] = (int)$config['max'];
184 }
185
186 // This is the EDITABLE form field.
187 $placeholderValue = $this->getPlaceholderValue($table, $config, $row);
188 if (!empty($placeholderValue)) {
189 $attributes['placeholder'] = trim($languageService->sL($placeholderValue));
190 }
191
192 // Build the attribute string
193 $attributeString = '';
194 foreach ($attributes as $attributeName => $attributeValue) {
195 $attributeString .= ' ' . $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
196 }
197
198 $html = '
199 <input type="text"'
200 . $attributeString
201 . $parameterArray['onFocus'] . ' />';
202
203 // 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.
204 $html .= '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" />';
205
206 // Going through all custom evaluations configured for this field
207 // @todo: Similar to above code!
208 foreach ($evalList as $evalData) {
209 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$evalData])) {
210 if (class_exists($evalData)) {
211 $evalObj = GeneralUtility::makeInstance($evalData);
212 if (method_exists($evalObj, 'returnFieldJS')) {
213 $resultArray['extJSCODE'] .= LF . 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($evalData) . '] = function(value) {' . $evalObj->returnFieldJS() . '}';
214 }
215 }
216 }
217 }
218
219 // add HTML wrapper
220 if ($isDateField) {
221 $html = '
222 <div class="input-group">
223 ' . $html . '
224 <span class="input-group-btn">
225 <label class="btn btn-default" for="' . $attributes['id'] . '">
226 ' . IconUtility::getSpriteIcon('actions-edit-pick-date') . '
227 </label>
228 </span>
229 </div>';
230 }
231
232 // Wrap a wizard around the item?
233 $html = $this->renderWizards(
234 array($html),
235 $config['wizards'],
236 $table,
237 $row,
238 $fieldName,
239 $parameterArray,
240 $parameterArray['itemFormElName'] . '_hr', $specConf
241 );
242
243 // Add a wrapper to remain maximum width
244 $width = (int)$this->formMaxWidth($size);
245 $html = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px"' : '') . '>' . $html . '</div>';
246 $resultArray['html'] = $html;
247 return $resultArray;
248 }
249
250 }