[TASK] FormEngine: Replace placeholder resolving with a data provider
[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 $languageService = $this->getLanguageService();
35
36 $table = $this->data['tableName'];
37 $fieldName = $this->data['fieldName'];
38 $row = $this->data['databaseRow'];
39 $parameterArray = $this->data['parameterArray'];
40 $resultArray = $this->initializeResultArray();
41 $resultArray['requireJsModules'] = array('TYPO3/CMS/Rsaauth/RsaEncryptionModule');
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 'type' => 'text',
50 'value' => '',
51 );
52
53 if ($config['readOnly']) {
54 $itemFormElValue = $parameterArray['itemFormElValue'];
55 $options = $this->data;
56 $options['parameterArray'] = array(
57 'fieldConf' => array(
58 'config' => $config,
59 ),
60 'itemFormElValue' => $itemFormElValue,
61 );
62 $options['renderType'] = 'none';
63 return $this->nodeFactory->create($options)->render();
64 }
65
66 // @todo: The whole eval handling is a mess and needs refactoring
67 foreach ($evalList as $func) {
68 switch ($func) {
69 case 'required':
70 $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString(array('required' => TRUE));
71 break;
72 case 'password':
73 $attributes['type'] = 'password';
74 $attributes['value'] = '********';
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 = array(
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 = array(
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 // Build the attribute string
128 $attributeString = '';
129 foreach ($attributes as $attributeName => $attributeValue) {
130 $attributeString .= ' ' . $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
131 }
132
133 $html = '
134 <input'
135 . $attributeString
136 . $parameterArray['onFocus'] . ' />';
137
138 // 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.
139 $html .= '<input type="hidden" data-rsa-encryption="" id="' . $parameterArray['itemFormElID'] . '_hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" />';
140
141 // Going through all custom evaluations configured for this field
142 // @todo: Similar to above code!
143 foreach ($evalList as $evalData) {
144 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$evalData])) {
145 if (class_exists($evalData)) {
146 $evalObj = GeneralUtility::makeInstance($evalData);
147 if (method_exists($evalObj, 'returnFieldJS')) {
148 $resultArray['extJSCODE'] .= LF . 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($evalData) . '] = function(value) {' . $evalObj->returnFieldJS() . '}';
149 }
150 }
151 }
152 }
153
154 // Wrap a wizard around the item?
155 $html = $this->renderWizards(
156 array($html),
157 $config['wizards'],
158 $table,
159 $row,
160 $fieldName,
161 $parameterArray,
162 $parameterArray['itemFormElName'],
163 $specConf
164 );
165
166 // Add a wrapper to remain maximum width
167 $width = (int)$this->formMaxWidth($size);
168 $html = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px"' : '') . '>' . $html . '</div>';
169 $resultArray['html'] = $html;
170 return $resultArray;
171 }
172
173 }