55b21c96dcaa4cd34a77df3b17259355fc2016f9
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / InputTextElement.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\Core\Imaging\Icon;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\MathUtility;
21 use TYPO3\CMS\Core\Utility\StringUtility;
22
23 /**
24 * Generation of TCEform elements of the type "input type=text"
25 */
26 class InputTextElement 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 {
35 $table = $this->data['tableName'];
36 $fieldName = $this->data['fieldName'];
37 $row = $this->data['databaseRow'];
38 $parameterArray = $this->data['parameterArray'];
39 $resultArray = $this->initializeResultArray();
40 $isDateField = false;
41
42 $config = $parameterArray['fieldConf']['config'];
43 $specConf = BackendUtility::getSpecConfParts($parameterArray['fieldConf']['defaultExtras']);
44 $size = MathUtility::forceIntegerInRange($config['size'] ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
45 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
46 $classes = array();
47 $attributes = array();
48
49 // readonly
50 if ($config['readOnly']) {
51 $itemFormElValue = $parameterArray['itemFormElValue'];
52 if (in_array('date', $evalList)) {
53 $config['format'] = 'date';
54 } elseif (in_array('datetime', $evalList)) {
55 $config['format'] = 'datetime';
56 } elseif (in_array('time', $evalList)) {
57 $config['format'] = 'time';
58 }
59 if (in_array('password', $evalList)) {
60 $itemFormElValue = $itemFormElValue ? '*********' : '';
61 }
62 $options = $this->data;
63 $options['parameterArray'] = array(
64 'fieldConf' => array(
65 'config' => $config,
66 ),
67 'itemFormElValue' => $itemFormElValue,
68 );
69 $options['renderType'] = 'none';
70 return $this->nodeFactory->create($options)->render();
71 }
72
73 if (in_array('datetime', $evalList, true)
74 || in_array('date', $evalList)) {
75 $classes[] = 't3js-datetimepicker';
76 $isDateField = true;
77 if (in_array('datetime', $evalList)) {
78 $attributes['data-date-type'] = 'datetime';
79 } elseif (in_array('date', $evalList)) {
80 $attributes['data-date-type'] = 'date';
81 }
82 if (((int)$parameterArray['itemFormElValue']) !== 0) {
83 $parameterArray['itemFormElValue'] += date('Z', $parameterArray['itemFormElValue']);
84 }
85 if (isset($config['range']['lower'])) {
86 $attributes['data-date-minDate'] = (int)$config['range']['lower'];
87 }
88 if (isset($config['range']['upper'])) {
89 $attributes['data-date-maxDate'] = (int)$config['range']['upper'];
90 }
91 } elseif (in_array('time', $evalList)) {
92 $isDateField = true;
93 $classes[] = 't3js-datetimepicker';
94 $attributes['data-date-type'] = 'time';
95 } elseif (in_array('timesec', $evalList)) {
96 $isDateField = true;
97 $classes[] = 't3js-datetimepicker';
98 $attributes['data-date-type'] = 'timesec';
99 }
100
101 // @todo: The whole eval handling is a mess and needs refactoring
102 foreach ($evalList as $func) {
103 switch ($func) {
104 case 'required':
105 $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString(array('required' => true));
106 break;
107 default:
108 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
109 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
110 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
111 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
112 if (class_exists($func)) {
113 $evalObj = GeneralUtility::makeInstance($func);
114 if (method_exists($evalObj, 'deevaluateFieldValue')) {
115 $_params = array(
116 'value' => $parameterArray['itemFormElValue']
117 );
118 $parameterArray['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
119 }
120 }
121 }
122 }
123 }
124 $paramsList = array(
125 'field' => $parameterArray['itemFormElName'],
126 'evalList' => implode(',', $evalList),
127 'is_in' => trim($config['is_in']),
128 );
129 // set classes
130 $classes[] = 'form-control';
131 $classes[] = 't3js-clearable';
132 $classes[] = 'hasDefaultValue';
133
134 // calculate attributes
135 $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString($config);
136 $attributes['data-formengine-input-params'] = json_encode($paramsList);
137 $attributes['data-formengine-input-name'] = htmlspecialchars($parameterArray['itemFormElName']);
138 $attributes['id'] = StringUtility::getUniqueId('formengine-input-');
139 $attributes['value'] = '';
140 if (isset($config['max']) && (int)$config['max'] > 0) {
141 $attributes['maxlength'] = (int)$config['max'];
142 }
143 if (!empty($classes)) {
144 $attributes['class'] = implode(' ', $classes);
145 }
146
147 // This is the EDITABLE form field.
148 if (!empty($config['placeholder'])) {
149 $attributes['placeholder'] = trim($config['placeholder']);
150 }
151
152 if (isset($config['autocomplete'])) {
153 $attributes['autocomplete'] = empty($config['autocomplete']) ? 'off' : 'on';
154 }
155
156 // Build the attribute string
157 $attributeString = '';
158 foreach ($attributes as $attributeName => $attributeValue) {
159 $attributeString .= ' ' . $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
160 }
161
162 $html = '<input type="text"' . $attributeString . ' />';
163
164 // 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.
165 $html .= '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" />';
166
167 // Going through all custom evaluations configured for this field
168 // @todo: Similar to above code!
169 foreach ($evalList as $evalData) {
170 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$evalData])) {
171 if (class_exists($evalData)) {
172 $evalObj = GeneralUtility::makeInstance($evalData);
173 if (method_exists($evalObj, 'returnFieldJS')) {
174 $resultArray['extJSCODE'] .= LF . 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($evalData) . '] = function(value) {' . $evalObj->returnFieldJS() . '}';
175 }
176 }
177 }
178 }
179
180 // add HTML wrapper
181 if ($isDateField) {
182 $html = '
183 <div class="input-group">
184 ' . $html . '
185 <span class="input-group-btn">
186 <label class="btn btn-default" for="' . $attributes['id'] . '">
187 ' . $this->iconFactory->getIcon('actions-edit-pick-date', Icon::SIZE_SMALL)->render() . '
188 </label>
189 </span>
190 </div>';
191 }
192
193 // Wrap a wizard around the item?
194 $html = $this->renderWizards(
195 array($html),
196 $config['wizards'],
197 $table,
198 $row,
199 $fieldName,
200 $parameterArray,
201 $parameterArray['itemFormElName'],
202 $specConf
203 );
204
205 // Add a wrapper to remain maximum width
206 $width = (int)$this->formMaxWidth($size);
207 $html = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px"' : '') . '>' . $html . '</div>';
208 $resultArray['html'] = $html;
209 return $resultArray;
210 }
211 }