[BUGFIX] Followup to deprecate FormEngine methods
[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\IconUtility;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\MathUtility;
21
22 /**
23 * Generation of TCEform elements of the type "input"
24 */
25 class InputElement extends AbstractFormElement {
26
27 /**
28 * This will render a single-line input form field, possibly with various control/validation features
29 *
30 * @param string $table The table name of the record
31 * @param string $field The field name which this element is supposed to edit
32 * @param array $row The record data array where the value(s) for the field can be found
33 * @param array $additionalInformation An array with additional configuration options.
34 * @return string The HTML code for the TCEform field
35 */
36 public function render($table, $field, $row, &$additionalInformation) {
37 $config = $additionalInformation['fieldConf']['config'];
38 $specConf = BackendUtility::getSpecConfParts($additionalInformation['extra'], $additionalInformation['fieldConf']['defaultExtras']);
39 $size = MathUtility::forceIntegerInRange($config['size'] ? $config['size'] : 30, 5, $this->formEngine->maxInputWidth);
40 $evalList = GeneralUtility::trimExplode(',', $config['eval'], TRUE);
41 $classAndStyleAttributes = $this->formEngine->formWidthAsArray($size);
42 $fieldAppendix = '';
43 $item = '';
44 $cssClasses = array($classAndStyleAttributes['class']);
45 $cssStyle = $classAndStyleAttributes['style'];
46 if (!isset($config['checkbox'])) {
47 $config['checkbox'] = '0';
48 $checkboxIsset = FALSE;
49 } else {
50 $checkboxIsset = TRUE;
51 }
52 if (in_array('date', $evalList) || in_array('datetime', $evalList)) {
53 if (in_array('datetime', $evalList)) {
54 $class = 'datetime';
55 } else {
56 $class = 'date';
57 }
58 $dateRange = '';
59 if (isset($config['range']['lower'])) {
60 $dateRange .= ' lower-' . (int)$config['range']['lower'];
61 }
62 if (isset($config['range']['upper'])) {
63 $dateRange .= ' upper-' . (int)$config['range']['upper'];
64 }
65 $inputId = uniqid('tceforms-' . $class . 'field-', TRUE);
66 $cssClasses[] = 'tceforms-textfield tceforms-' . $class . 'field' . $dateRange;
67 $fieldAppendix = IconUtility::getSpriteIcon('actions-edit-pick-date', array(
68 'style' => 'cursor:pointer;',
69 'id' => 'picker-' . $inputId
70 ));
71 } elseif (in_array('timesec', $evalList)) {
72 $inputId = uniqid('tceforms-timesecfield-', TRUE);
73 $cssClasses[] = 'tceforms-textfield tceforms-timesecfield';
74 } elseif (in_array('year', $evalList)) {
75 $inputId = uniqid('tceforms-yearfield-', TRUE);
76 $cssClasses[] = 'tceforms-textfield tceforms-yearfield';
77 } elseif (in_array('time', $evalList)) {
78 $inputId = uniqid('tceforms-timefield-', TRUE);
79 $cssClasses[] = 'tceforms-textfield tceforms-timefield';
80 } elseif (in_array('int', $evalList)) {
81 $inputId = uniqid('tceforms-intfield-', TRUE);
82 $cssClasses[] = 'tceforms-textfield tceforms-intfield';
83 } elseif (in_array('double2', $evalList)) {
84 $inputId = uniqid('tceforms-double2field-', TRUE);
85 $cssClasses[] = 'tceforms-textfield tceforms-double2field';
86 } else {
87 $inputId = uniqid('tceforms-textfield-', TRUE);
88 $cssClasses[] = 'tceforms-textfield';
89 if ($checkboxIsset === FALSE) {
90 $config['checkbox'] = '';
91 }
92 }
93 if (isset($config['wizards']['link'])) {
94 $inputId = uniqid('tceforms-linkfield-', TRUE);
95 $cssClasses[] = 'tceforms-textfield tceforms-linkfield';
96 } elseif (isset($config['wizards']['color'])) {
97 $inputId = uniqid('tceforms-colorfield-', TRUE);
98 $cssClasses[] = 'tceforms-textfield tceforms-colorfield';
99 }
100 $inputId = str_replace('.', '', $inputId);
101 if ($this->formEngine->renderReadonly || $config['readOnly']) {
102 $itemFormElValue = $additionalInformation['itemFormElValue'];
103 if (in_array('date', $evalList)) {
104 $config['format'] = 'date';
105 } elseif (in_array('datetime', $evalList)) {
106 $config['format'] = 'datetime';
107 } elseif (in_array('time', $evalList)) {
108 $config['format'] = 'time';
109 }
110 if (in_array('password', $evalList)) {
111 $itemFormElValue = $itemFormElValue ? '*********' : '';
112 }
113 return $this->formEngine->getSingleField_typeNone_render($config, $itemFormElValue);
114 }
115 foreach ($evalList as $func) {
116 switch ($func) {
117 case 'required':
118 $this->formEngine->registerRequiredProperty('field', $table . '_' . $row['uid'] . '_' . $field, $additionalInformation['itemFormElName']);
119 // Mark this field for date/time disposal:
120 if (array_intersect($evalList, array('date', 'datetime', 'time'))) {
121 $this->formEngine->requiredAdditional[$additionalInformation['itemFormElName']]['isPositiveNumber'] = TRUE;
122 }
123 break;
124 default:
125 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
126 $evalObj = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func] . ':&' . $func);
127 if (is_object($evalObj) && method_exists($evalObj, 'deevaluateFieldValue')) {
128 $_params = array(
129 'value' => $additionalInformation['itemFormElValue']
130 );
131 $additionalInformation['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
132 }
133 }
134 }
135 $paramsList = '\'' . $additionalInformation['itemFormElName'] . '\',\'' . implode(',', $evalList) . '\',\'' . trim($config['is_in']) . '\',' . (isset($config['checkbox']) ? 1 : 0) . ',\'' . $config['checkbox'] . '\'';
136 if (in_array('date', $evalList) || in_array('datetime', $evalList)) {
137 $item .= '<span class="t3-tceforms-input-wrapper-datetime">';
138 // Add server timezone offset to UTC to our stored date
139 if ($additionalInformation['itemFormElValue'] > 0) {
140 $additionalInformation['itemFormElValue'] += date('Z', $additionalInformation['itemFormElValue']);
141 }
142 } else {
143 $item .= '<span class="t3-tceforms-input-wrapper">';
144 }
145 $additionalInformation['fieldChangeFunc'] = array_merge(array('typo3form.fieldGet' => 'typo3form.fieldGet(' . $paramsList . ');'), $additionalInformation['fieldChangeFunc']);
146
147 $mLgd = $config['max'] ?: 256;
148 $iOnChange = implode('', $additionalInformation['fieldChangeFunc']);
149 $cssClasses[] = 'hasDefaultValue';
150 $item .= '<input type="text" ' . $this->formEngine->getPlaceholderAttribute($table, $field, $config, $row) . 'id="' . $inputId . '" ' . 'class="' . implode(' ', $cssClasses) . '" ' . 'name="' . $additionalInformation['itemFormElName'] . '_hr" ' . 'value=""' . 'style="' . $cssStyle . '" ' . 'maxlength="' . $mLgd . '" ' . 'onchange="' . htmlspecialchars($iOnChange) . '"' . $additionalInformation['onFocus'] . ' />';
151 // This is the EDITABLE form field.
152 $item .= '<input type="hidden" name="' . $additionalInformation['itemFormElName'] . '" value="' . htmlspecialchars($additionalInformation['itemFormElValue']) . '" />';
153 // 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.
154 $item .= $fieldAppendix . '</span><div style="clear:both;"></div>';
155 $this->formEngine->extJSCODE .= 'typo3form.fieldSet(' . $paramsList . ');';
156 // Going through all custom evaluations configured for this field
157 foreach ($evalList as $evalData) {
158 $evalObj = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$evalData] . ':&' . $evalData);
159 if (is_object($evalObj) && method_exists($evalObj, 'returnFieldJS')) {
160 $this->formEngine->extJSCODE .= '
161 TBE_EDITOR.customEvalFunctions[\'' . $evalData . '\'] = function(value) {
162 ' . $evalObj->returnFieldJS() . '
163 }
164 ';
165 }
166 }
167 // Creating an alternative item without the JavaScript handlers.
168 $altItem = '<input type="hidden" name="' . $additionalInformation['itemFormElName'] . '_hr" value="" />';
169 $altItem .= '<input type="hidden" name="' . $additionalInformation['itemFormElName'] . '" value="' . htmlspecialchars($additionalInformation['itemFormElValue']) . '" />';
170 // Wrap a wizard around the item?
171 $item = $this->formEngine->renderWizards(array($item, $altItem), $config['wizards'], $table, $row, $field, $additionalInformation, $additionalInformation['itemFormElName'] . '_hr', $specConf);
172
173 return $item;
174 }
175 }