[BUGFIX] FormEngine: Do not add <br> to readonly textarea content
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / NoneElement.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\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\MathUtility;
20
21 /**
22 * Generation of TCEform elements where no rendering could be found
23 */
24 class NoneElement extends AbstractFormElement
25 {
26 /**
27 * This will render a non-editable display of the content of the field.
28 *
29 * @return string The HTML code for the TCEform field
30 */
31 public function render()
32 {
33 $parameterArray = $this->data['parameterArray'];
34 $config = $parameterArray['fieldConf']['config'];
35 $itemValue = $parameterArray['itemFormElValue'];
36
37 if ($config['format']) {
38 $itemValue = $this->formatValue($config, $itemValue);
39 }
40 if (!$config['pass_content']) {
41 $itemValue = htmlspecialchars($itemValue);
42 }
43
44 $resultArray = $this->initializeResultArray();
45 $rows = (int)$config['rows'];
46 // Render as textarea
47 if ($rows > 1 || $config['type'] === 'text') {
48 $cols = MathUtility::forceIntegerInRange($config['cols'] ?: $this->defaultInputWidth, 5, $this->maxInputWidth);
49 $width = $this->formMaxWidth($cols);
50 $html = '
51 <div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px"' : '') . '>
52 <textarea class="form-control" rows="' . $rows . '" disabled>' . $itemValue . '</textarea>
53 </div>';
54 } else {
55 $cols = $config['cols'] ?: ($config['size'] ?: $this->defaultInputWidth);
56 $size = MathUtility::forceIntegerInRange($cols ?: $this->defaultInputWidth, 5, $this->maxInputWidth);
57 $width = $this->formMaxWidth($size);
58 $html = '
59 <div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px"' : '') . '>
60 <input class="form-control" value="' . $itemValue . '" type="text" disabled>
61 </div>';
62 }
63 $resultArray['html'] = $html;
64 return $resultArray;
65 }
66
67 /**
68 * Format field content if $config['format'] is set to date, filesize, ..., user
69 *
70 * @param array $config Configuration for the display
71 * @param string $itemValue The value to display
72 * @return string Formatted field value
73 */
74 protected function formatValue($config, $itemValue)
75 {
76 $format = trim($config['format']);
77 switch ($format) {
78 case 'date':
79 if ($itemValue) {
80 $option = isset($config['format.']['option']) ? trim($config['format.']['option']) : '';
81 if ($option) {
82 if (isset($config['format.']['strftime']) && $config['format.']['strftime']) {
83 $value = strftime($option, $itemValue);
84 } else {
85 $value = date($option, $itemValue);
86 }
87 } else {
88 $value = date('d-m-Y', $itemValue);
89 }
90 } else {
91 $value = '';
92 }
93 if (isset($config['format.']['appendAge']) && $config['format.']['appendAge']) {
94 $age = BackendUtility::calcAge(
95 $GLOBALS['EXEC_TIME'] - $itemValue,
96 $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.minutesHoursDaysYears')
97 );
98 $value .= ' (' . $age . ')';
99 }
100 $itemValue = $value;
101 break;
102 case 'datetime':
103 // compatibility with "eval" (type "input")
104 if ($itemValue !== '' && !is_null($itemValue)) {
105 $itemValue = date('H:i d-m-Y', (int)$itemValue);
106 }
107 break;
108 case 'time':
109 // compatibility with "eval" (type "input")
110 if ($itemValue !== '' && !is_null($itemValue)) {
111 $itemValue = date('H:i', (int)$itemValue);
112 }
113 break;
114 case 'timesec':
115 // compatibility with "eval" (type "input")
116 if ($itemValue !== '' && !is_null($itemValue)) {
117 $itemValue = date('H:i:s', (int)$itemValue);
118 }
119 break;
120 case 'year':
121 // compatibility with "eval" (type "input")
122 if ($itemValue !== '' && !is_null($itemValue)) {
123 $itemValue = date('Y', (int)$itemValue);
124 }
125 break;
126 case 'int':
127 $baseArr = ['dec' => 'd', 'hex' => 'x', 'HEX' => 'X', 'oct' => 'o', 'bin' => 'b'];
128 $base = isset($config['format.']['base']) ? trim($config['format.']['base']) : '';
129 $format = isset($baseArr[$base]) ? $baseArr[$base] : 'd';
130 $itemValue = sprintf('%' . $format, $itemValue);
131 break;
132 case 'float':
133 // default precision
134 $precision = 2;
135 if (isset($config['format.']['precision'])) {
136 $precision = MathUtility::forceIntegerInRange($config['format.']['precision'], 1, 10, $precision);
137 }
138 $itemValue = sprintf('%.' . $precision . 'f', $itemValue);
139 break;
140 case 'number':
141 $format = isset($config['format.']['option']) ? trim($config['format.']['option']) : '';
142 $itemValue = sprintf('%' . $format, $itemValue);
143 break;
144 case 'md5':
145 $itemValue = md5($itemValue);
146 break;
147 case 'filesize':
148 // We need to cast to int here, otherwise empty values result in empty output,
149 // but we expect zero.
150 $value = GeneralUtility::formatSize((int)$itemValue);
151 if (!empty($config['format.']['appendByteSize'])) {
152 $value .= ' (' . $itemValue . ')';
153 }
154 $itemValue = $value;
155 break;
156 case 'user':
157 $func = trim($config['format.']['userFunc']);
158 if ($func) {
159 $params = [
160 'value' => $itemValue,
161 'args' => $config['format.']['userFunc'],
162 'config' => $config,
163 ];
164 $itemValue = GeneralUtility::callUserFunction($func, $params, $this);
165 }
166 break;
167 default:
168 // Do nothing e.g. when $format === ''
169 }
170 return $itemValue;
171 }
172 }