[!!!][FEATURE] FormEngine element level refactoring
[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\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\MathUtility;
20 use TYPO3\CMS\Core\Utility\StringUtility;
21
22 /**
23 * Generation of form element of the type rsaInput
24 */
25 class RsaInputElement extends AbstractFormElement
26 {
27 /**
28 * Default field wizards enabled for this element.
29 *
30 * @var array
31 */
32 protected $defaultFieldWizard = [
33 'otherLanguageContent' => [
34 'renderType' => 'otherLanguageContent',
35 ],
36 'defaultLanguageDifferences' => [
37 'renderType' => 'defaultLanguageDifferences',
38 'after' => [
39 'otherLanguageContent',
40 ],
41 ],
42 ];
43
44 /**
45 * This will render a single-line input form field, possibly with various control/validation features
46 *
47 * @return array As defined in initializeResultArray() of AbstractNode
48 */
49 public function render()
50 {
51 $fieldName = $this->data['fieldName'];
52 $parameterArray = $this->data['parameterArray'];
53 $resultArray = $this->initializeResultArray();
54 $resultArray['requireJsModules'] = ['TYPO3/CMS/Rsaauth/RsaEncryptionModule'];
55
56 $itemValue = $parameterArray['itemFormElValue'];
57 $config = $parameterArray['fieldConf']['config'];
58 $size = MathUtility::forceIntegerInRange($config['size'] ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
59 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
60 $width = (int)$this->formMaxWidth($size);
61
62 if ($config['readOnly']) {
63 // Early return for read only fields
64 if (in_array('password', $evalList, true)) {
65 $itemValue = $itemValue ? '*********' : '';
66 }
67 $html = [];
68 $html[] = '<div class="t3js-formengine-field-item">';
69 $html[] = '<div class="form-wizards-wrap">';
70 $html[] = '<div class="form-wizards-element">';
71 $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
72 $html[] = '<input class="form-control" value="' . htmlspecialchars($itemValue) . '" type="text" disabled>';
73 $html[] = '</div>';
74 $html[] = '</div>';
75 $html[] = '</div>';
76 $html[] = '</div>';
77 $resultArray['html'] = implode(LF, $html);
78 return $resultArray;
79 }
80
81 // @todo: The whole eval handling is a mess and needs refactoring
82 foreach ($evalList as $func) {
83 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
84 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
85 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
86 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
87 if (class_exists($func)) {
88 $evalObj = GeneralUtility::makeInstance($func);
89 if (method_exists($evalObj, 'deevaluateFieldValue')) {
90 $_params = [
91 'value' => $itemValue
92 ];
93 $itemValue = $evalObj->deevaluateFieldValue($_params);
94 }
95 if (method_exists($evalObj, 'returnFieldJS')) {
96 $resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($func) . ']'
97 . ' = function(value) {' . $evalObj->returnFieldJS() . '};';
98 }
99 }
100 }
101 }
102 $evalList = array_filter($evalList, function ($value) {
103 return $value !== 'password';
104 });
105
106 $attributes = [
107 'type' => 'text',
108 'id' => StringUtility::getUniqueId('formengine-input-'),
109 'value' => '',
110 'class' => implode(' ', [
111 'form-control',
112 't3js-clearable',
113 'hasDefaultValue',
114 ]),
115 'data-formengine-validation-rules'=> $this->getValidationDataAsJsonString($config),
116 'data-formengine-input-params' => json_encode([
117 'field' => $parameterArray['itemFormElName'],
118 'evalList' => implode(',', $evalList),
119 'is_in' => trim($config['is_in']),
120 ]),
121 'data-formengine-input-name' => htmlspecialchars($parameterArray['itemFormElName']),
122 ];
123
124 if (isset($config['max']) && (int)$config['max'] > 0) {
125 $attributes['maxlength'] = (int)$config['max'];
126 }
127 if (!empty($config['placeholder'])) {
128 $attributes['placeholder'] = trim($config['placeholder']);
129 }
130 if (isset($config['autocomplete'])) {
131 $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
132 }
133 if (in_array('password', $evalList)) {
134 $attributes['type'] = 'password';
135 $attributes['value'] = $itemValue ? '*********' : '';
136 $attributes['autocomplete'] = 'new-' . $fieldName;
137 }
138
139 $legacyWizards = $this->renderWizards();
140 $legacyFieldControlHtml = implode(LF, $legacyWizards['fieldControl']);
141 $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
142
143 $fieldInformationResult = $this->renderFieldInformation();
144 $fieldInformationHtml = $fieldInformationResult['html'];
145 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
146
147 $fieldControlResult = $this->renderFieldControl();
148 $fieldControlHtml = $legacyFieldControlHtml . $fieldControlResult['html'];
149 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
150
151 $fieldWizardResult = $this->renderFieldWizard();
152 $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
153 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
154
155 $html = [];
156 $html[] = '<div class="t3js-formengine-field-item">';
157 $html[] = $fieldInformationHtml;
158 $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
159 $html[] = '<div class="form-wizards-wrap">';
160 $html[] = '<div class="form-wizards-element">';
161 $html[] = '<input ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
162 $html[] = '<input';
163 $html[] = ' type="hidden"';
164 $html[] = ' data-rsa-encryption=""';
165 $html[] = ' id="' . $parameterArray['itemFormElID'] . '_hidden"';
166 $html[] = ' name="' . $parameterArray['itemFormElName'] . '"';
167 $html[] = ' value="' . htmlspecialchars($itemValue) . '"';
168 $html[] = '/>';
169 $html[] = '</div>';
170 $html[] = '<div class="form-wizards-items-aside">';
171 $html[] = '<div class="btn-group">';
172 $html[] = $fieldControlHtml;
173 $html[] = '</div>';
174 $html[] = '</div>';
175 $html[] = '<div class="form-wizards-items-bottom">';
176 $html[] = $fieldWizardHtml;
177 $html[] = '</div>';
178 $html[] = '</div>';
179 $html[] = '</div>';
180 $html[] = '</div>';
181
182 $resultArray['html'] = implode(LF, $html);
183 return $resultArray;
184 }
185 }