[!!!][TASK] Improve flex and TCA handling in FormEngine
[Packages/TYPO3.CMS.git] / typo3 / sysext / rsaauth / Classes / Form / Element / RsaInputElement.php
1 <?php
2 namespace TYPO3\CMS\Rsaauth\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\Form\Element\AbstractFormElement;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
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 form element of the type rsaInput
25 */
26 class RsaInputElement 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 $resultArray['requireJsModules'] = ['TYPO3/CMS/Rsaauth/RsaEncryptionModule'];
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 = [];
47 $attributes = [
48 'type' => 'text',
49 'value' => '',
50 ];
51
52 if ($config['readOnly']) {
53 $itemFormElValue = $parameterArray['itemFormElValue'];
54 $options = $this->data;
55 $options['parameterArray'] = [
56 'fieldConf' => [
57 'config' => $config,
58 ],
59 'itemFormElValue' => $itemFormElValue,
60 ];
61 $options['renderType'] = 'none';
62 return $this->nodeFactory->create($options)->render();
63 }
64
65 // @todo: The whole eval handling is a mess and needs refactoring
66 foreach ($evalList as $func) {
67 switch ($func) {
68 case 'required':
69 $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString(['required' => true]);
70 break;
71 case 'password':
72 $attributes['type'] = 'password';
73 $attributes['value'] = $parameterArray['itemFormElValue'] ? '*********' : '';
74 $attributes['autocomplete'] = 'new-' . $fieldName;
75 break;
76 default:
77 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
78 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
79 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
80 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
81 if (class_exists($func)) {
82 $evalObj = GeneralUtility::makeInstance($func);
83 if (method_exists($evalObj, 'deevaluateFieldValue')) {
84 $_params = [
85 'value' => $parameterArray['itemFormElValue']
86 ];
87 $parameterArray['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
88 }
89 }
90 }
91 }
92 }
93 $evalList = array_filter($evalList, function ($value) {
94 return $value !== 'password';
95 });
96
97 $paramsList = [
98 'field' => $parameterArray['itemFormElName'],
99 'evalList' => implode(',', $evalList),
100 'is_in' => trim($config['is_in']),
101 ];
102 // set classes
103 $classes[] = 'form-control';
104 $classes[] = 't3js-clearable';
105 $classes[] = 'hasDefaultValue';
106
107 // calculate attributes
108 $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString($config);
109 $attributes['data-formengine-input-params'] = json_encode($paramsList);
110 $attributes['data-formengine-input-name'] = htmlspecialchars($parameterArray['itemFormElName']);
111 $attributes['id'] = StringUtility::getUniqueId('formengine-input-');
112 if (isset($config['max']) && (int)$config['max'] > 0) {
113 $attributes['maxlength'] = (int)$config['max'];
114 }
115 if (!empty($classes)) {
116 $attributes['class'] = implode(' ', $classes);
117 }
118 if (isset($config['max']) && (int)$config['max'] > 0) {
119 $attributes['maxlength'] = (int)$config['max'];
120 }
121
122 // This is the EDITABLE form field.
123 if (!empty($config['placeholder'])) {
124 $attributes['placeholder'] = trim($config['placeholder']);
125 }
126
127 if (isset($config['autocomplete'])) {
128 $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
129 }
130
131 // Build the attribute string
132 $attributeString = '';
133 foreach ($attributes as $attributeName => $attributeValue) {
134 $attributeString .= ' ' . $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
135 }
136
137 $html = '<input' . $attributeString . ' />';
138
139 // 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.
140 $html .= '<input type="hidden" data-rsa-encryption="" id="' . $parameterArray['itemFormElID'] . '_hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" />';
141
142 // Going through all custom evaluations configured for this field
143 // @todo: Similar to above code!
144 foreach ($evalList as $evalData) {
145 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$evalData])) {
146 if (class_exists($evalData)) {
147 $evalObj = GeneralUtility::makeInstance($evalData);
148 if (method_exists($evalObj, 'returnFieldJS')) {
149 $resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($evalData) . '] = function(value) {' . $evalObj->returnFieldJS() . '};';
150 }
151 }
152 }
153 }
154
155 // Wrap a wizard around the item?
156 $html = $this->renderWizards(
157 [$html],
158 $config['wizards'],
159 $table,
160 $row,
161 $fieldName,
162 $parameterArray,
163 $parameterArray['itemFormElName'],
164 $specConf
165 );
166
167 // Add a wrapper to remain maximum width
168 $width = (int)$this->formMaxWidth($size);
169 $html = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px"' : '') . '>' . $html . '</div>';
170 $resultArray['html'] = $html;
171 return $resultArray;
172 }
173 }