fe08548cabe5a87565aad8c86354d76f3db1a714
[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 foreach ($evalList as $func) {
134 switch ($func) {
135 case 'required':
136 $resultArray['requiredFields'][$table . '_' . $row['uid'] . '_' . $fieldName] = $parameterArray['itemFormElName'];
137 $tabAndInlineStack = $this->globalOptions['tabAndInlineStack'];
138 if (!empty($tabAndInlineStack) && preg_match('/^(.+\\])\\[(\\w+)\\]$/', $parameterArray['itemFormElName'], $match)) {
139 array_shift($match);
140 $resultArray['requiredNested'][$parameterArray['itemFormElName']] = array(
141 'parts' => $match,
142 'level' => $tabAndInlineStack,
143 );
144 }
145 // Mark this field for date/time disposal:
146 if (array_intersect($evalList, array('date', 'datetime', 'time', 'timesec'))) {
147 $resultArray['requiredAdditional'][$parameterArray['itemFormElName']] = array(
148 'isPositiveNumber' => TRUE,
149 );
150 }
151 break;
152 default:
153 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
154 $evalObj = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func] . ':&' . $func);
155 if (is_object($evalObj) && method_exists($evalObj, 'deevaluateFieldValue')) {
156 $_params = array(
157 'value' => $parameterArray['itemFormElValue']
158 );
159 $parameterArray['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
160 }
161 }
162 }
163 $paramsList = GeneralUtility::quoteJSvalue($parameterArray['itemFormElName']) . ',' . GeneralUtility::quoteJSvalue(implode(',', $evalList)) . ',' . GeneralUtility::quoteJSvalue(trim($config['is_in'])) . ',' . ($config['checkbox'] ? 1 : 0) . ',' . GeneralUtility::quoteJSvalue($config['checkbox']);
164 $parameterArray['fieldChangeFunc'] = array_merge(array('typo3form.fieldGet' => 'typo3form.fieldGet(' . $paramsList . ');'), $parameterArray['fieldChangeFunc']);
165
166 // set classes
167 $classes[] = 'form-control';
168 $classes[] = 't3js-clearable';
169 $classes[] = 'hasDefaultValue';
170
171 // calculate attributes
172 $attributes['id'] = str_replace('.', '', uniqid('formengine-input-', TRUE));
173 $attributes['name'] = $parameterArray['itemFormElName'] . '_hr';
174 $attributes['value'] = '';
175 $attributes['maxlength'] = $config['max'] ?: 256;
176 $attributes['onchange'] = implode('', $parameterArray['fieldChangeFunc']);
177
178 if (!empty($styles)) {
179 $attributes['style'] = implode(' ', $styles);
180 }
181 if (!empty($classes)) {
182 $attributes['class'] = implode(' ', $classes);
183 }
184 if (isset($config['max']) && (int)$config['max'] > 0) {
185 $attributes['maxlength'] = (int)$config['max'];
186 }
187
188 // Build the attribute string
189 $attributeString = '';
190 foreach ($attributes as $attributeName => $attributeValue) {
191 $attributeString .= ' ' . $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
192 }
193
194 // This is the EDITABLE form field.
195 $placeholderValue = $this->getPlaceholderValue($table, $config, $row);
196 $placeholderAttribute = '';
197 if (!empty($placeholderValue)) {
198 $placeholderAttribute = ' placeholder="' . htmlspecialchars(trim($languageService->sL($placeholderValue))) . '" ';
199 }
200
201 $html = '
202 <input type="text"'
203 . $attributeString
204 . $placeholderAttribute
205 . $parameterArray['onFocus'] . ' />';
206
207 // 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.
208 $html .= '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" />';
209
210 $resultArray['extJSCODE'] = 'typo3form.fieldSet(' . $paramsList . ');';
211 // Going through all custom evaluations configured for this field
212 foreach ($evalList as $evalData) {
213 $evalObj = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$evalData] . ':&' . $evalData);
214 if (is_object($evalObj) && method_exists($evalObj, 'returnFieldJS')) {
215 $resultArray['extJSCODE'] .= LF . 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($evalData) . '] = function(value) {' . $evalObj->returnFieldJS() . '}';
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 // Creating an alternative item without the JavaScript handlers.
233 $altItem = '
234 <input type="hidden" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '_hr" value="" />
235 <input type="hidden" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" />';
236
237 // Wrap a wizard around the item?
238 $html = $this->renderWizards(
239 array($html, $altItem),
240 $config['wizards'],
241 $table,
242 $row,
243 $fieldName,
244 $parameterArray,
245 $parameterArray['itemFormElName'] . '_hr', $specConf
246 );
247
248 // Add a wrapper to remain maximum width
249 $width = (int)$this->formMaxWidth($size);
250 $html = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px"' : '') . '>' . $html . '</div>';
251 $resultArray['html'] = $html;
252 return $resultArray;
253 }
254
255 }