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