[CLEANUP] Make use of possible imports and remove unused ones
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / TextElement.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 of the type "text"
23 */
24 class TextElement extends AbstractFormElement {
25
26 /**
27 * This will render a <textarea> OR RTE area form field,
28 * possibly with various control/validation features
29 *
30 * @param string $table The table name of the record
31 * @param string $field The field name which this element is supposed to edit
32 * @param array $row The record data array where the value(s) for the field can be found
33 * @param array $additionalInformation An array with additional configuration options.
34 * @return string The HTML code for the TCEform field
35 */
36 public function render($table, $field, $row, &$additionalInformation) {
37 $config = $additionalInformation['fieldConf']['config'];
38
39 // Setting columns number
40 $cols = MathUtility::forceIntegerInRange($config['cols'] ?: 30, 5, $this->formEngine->maxTextareaWidth);
41
42 // Setting number of rows
43 $rows = MathUtility::forceIntegerInRange($config['rows'] ?: 5, 1, 20);
44 $originalRows = $rows;
45
46 $itemFormElementValueLength = strlen($additionalInformation['itemFormElValue']);
47 if ($itemFormElementValueLength > $this->formEngine->charsPerRow * 2) {
48 $cols = $this->formEngine->maxTextareaWidth;
49 $rows = MathUtility::forceIntegerInRange(
50 round($itemFormElementValueLength / $this->formEngine->charsPerRow),
51 count(explode(LF, $additionalInformation['itemFormElValue'])),
52 20
53 );
54 if ($rows < $originalRows) {
55 $rows = $originalRows;
56 }
57 }
58
59 // must be called after the cols and rows calculation, so the parameters are applied
60 // to read-only fields as well.
61 if ($this->isRenderReadonly() || $config['readOnly']) {
62 $config['cols'] = $cols;
63 $config['rows'] = $rows;
64 return $this->formEngine->getSingleField_typeNone_render($config, $additionalInformation['itemFormElValue']);
65 }
66
67 $evalList = GeneralUtility::trimExplode(',', $config['eval'], TRUE);
68 if (in_array('required', $evalList, TRUE)) {
69 $this->formEngine->requiredFields[$table . '_' . $row['uid'] . '_' . $field] = $additionalInformation['itemFormElName'];
70 }
71 // Init RTE vars
72 // Set TRUE, if the RTE is loaded; If not a normal textarea is shown.
73 $rteWasLoaded = FALSE;
74 // Set TRUE, if the RTE would have been loaded if it wasn't for the disable-RTE flag in the bottom of the page...
75 $rteWouldHaveBeenLoaded = FALSE;
76 // "Extra" configuration; Returns configuration for the field based on settings found in the "types" fieldlist. Traditionally, this is where RTE configuration has been found.
77 $specialConfiguration = BackendUtility::getSpecConfParts($additionalInformation['extra'], $additionalInformation['fieldConf']['defaultExtras']);
78 // Setting up the altItem form field, which is a hidden field containing the value
79 $altItem = '<input type="hidden" name="' . htmlspecialchars($additionalInformation['itemFormElName']) . '" value="' . htmlspecialchars($additionalInformation['itemFormElValue']) . '" />';
80 $item = '';
81 // If RTE is generally enabled (TYPO3_CONF_VARS and user settings)
82 if ($this->formEngine->RTEenabled) {
83 $parameters = BackendUtility::getSpecConfParametersFromArray($specialConfiguration['rte_transform']['parameters']);
84 // If the field is configured for RTE and if any flag-field is not set to disable it.
85 if (isset($specialConfiguration['richtext']) && (!$parameters['flag'] || !$row[$parameters['flag']])) {
86 BackendUtility::fixVersioningPid($table, $row);
87 list($recordPid, $tsConfigPid) = BackendUtility::getTSCpidCached($table, $row['uid'], $row['pid']);
88 // If the pid-value is not negative (that is, a pid could NOT be fetched)
89 if ($tsConfigPid >= 0) {
90 $rteSetup = $this->getBackendUserAuthentication()->getTSConfig('RTE', BackendUtility::getPagesTSconfig($recordPid));
91 $rteTcaTypeValue = BackendUtility::getTCAtypeValue($table, $row);
92 $rteSetupConfiguration = BackendUtility::RTEsetup($rteSetup['properties'], $table, $field, $rteTcaTypeValue);
93 if (!$rteSetupConfiguration['disabled']) {
94 if (!$this->formEngine->disableRTE) {
95 $this->formEngine->RTEcounter++;
96 // Get RTE object, draw form and set flag:
97 $rteObject = BackendUtility::RTEgetObj();
98 $item = $rteObject->drawRTE(
99 $this->formEngine,
100 $table,
101 $field,
102 $row,
103 $additionalInformation,
104 $specialConfiguration,
105 $rteSetupConfiguration,
106 $rteTcaTypeValue,
107 '',
108 $tsConfigPid
109 );
110
111 // Wizard
112 $item = $this->formEngine->renderWizards(
113 array($item, $altItem),
114 $config['wizards'],
115 $table,
116 $row,
117 $field,
118 $additionalInformation,
119 $additionalInformation['itemFormElName'],
120 $specialConfiguration,
121 TRUE
122 );
123 $rteWasLoaded = TRUE;
124 } else {
125 $rteWouldHaveBeenLoaded = TRUE;
126 $this->formEngine->commentMessages[] = $additionalInformation['itemFormElName'] . ': RTE is disabled by the on-page RTE-flag (probably you can enable it by the check-box in the bottom of this page!)';
127 }
128 } else {
129 $this->formEngine->commentMessages[] = $additionalInformation['itemFormElName'] . ': RTE is disabled by the Page TSconfig, "RTE"-key (eg. by RTE.default.disabled=0 or such)';
130 }
131 } else {
132 $this->formEngine->commentMessages[] = $additionalInformation['itemFormElName'] . ': PID value could NOT be fetched. Rare error, normally with new records.';
133 }
134 } else {
135 if (!isset($specialConfiguration['richtext'])) {
136 $this->formEngine->commentMessages[] = $additionalInformation['itemFormElName'] . ': RTE was not configured for this field in TCA-types';
137 }
138 if (!(!$parameters['flag'] || !$row[$parameters['flag']])) {
139 $this->formEngine->commentMessages[] = $additionalInformation['itemFormElName'] . ': Field-flag (' . $additionalInformation['flag'] . ') has been set to disable RTE!';
140 }
141 }
142 }
143 // Display ordinary field if RTE was not loaded.
144 if (!$rteWasLoaded) {
145 // Show message, if no RTE (field can only be edited with RTE!)
146 if ($specialConfiguration['rte_only']) {
147 $item = '<p><em>' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.noRTEfound')) . '</em></p>';
148 } else {
149 if ($specialConfiguration['nowrap']) {
150 $wrap = 'off';
151 } else {
152 $wrap = $config['wrap'] ?: 'virtual';
153 }
154 $classes = array();
155 if ($specialConfiguration['fixed-font']) {
156 $classes[] = 'fixed-font';
157 }
158 if ($specialConfiguration['enable-tab']) {
159 $classes[] = 'enable-tab';
160 }
161 $formWidthText = $this->formWidthText($cols, $wrap);
162 // add the max-height from the users' preference to it
163 $maximumHeight = (int)$this->getBackendUserAuthentication()->uc['resizeTextareas_MaxHeight'];
164 if ($maximumHeight > 0) {
165 $formWidthText = str_replace('style="', 'style="max-height: ' . $maximumHeight . 'px; ', $formWidthText);
166 }
167
168 // Extract class attributes from $formWidthText (otherwise it would be added twice to the output)
169 $res = array();
170 if (preg_match('/ class="(.+?)"/', $formWidthText, $res)) {
171 $formWidthText = str_replace(' class="' . $res[1] . '"', '', $formWidthText);
172 $classes = array_merge($classes, explode(' ', $res[1]));
173 }
174
175 if (!empty($classes)) {
176 $class = ' class="tceforms-textarea t3js-formengine-textarea ' . implode(' ', $classes) . '"';
177 } else {
178 $class = ' class="tceforms-textarea t3js-formengine-textarea"';
179 }
180
181 foreach ($evalList as $func) {
182 if ($func === 'required') {
183 $this->formEngine->registerRequiredProperty('field', $table . '_' . $row['uid'] . '_' . $field, $additionalInformation['itemFormElName']);
184 } else {
185 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
186 // and \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_text_Eval()
187 $evalObj = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func] . ':&' . $func);
188 if (is_object($evalObj) && method_exists($evalObj, 'deevaluateFieldValue')) {
189 $_params = array(
190 'value' => $additionalInformation['itemFormElValue']
191 );
192 $additionalInformation['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
193 }
194 }
195 }
196 $textOnChange = implode('', $additionalInformation['fieldChangeFunc']);
197 $additionalAttributes = '';
198 if (isset($config['max']) && (int)$config['max'] > 0) {
199 $additionalAttributes = ' maxlength="' . (int)$config['max'] . '"';
200 }
201 $item .= '
202 <textarea ' . 'id="' . str_replace('.', '', uniqid('tceforms-textarea-', TRUE)) . '" ' . 'name="' . $additionalInformation['itemFormElName']
203 . '"' . $formWidthText . $class . ' ' . 'rows="' . $rows . '" ' . 'wrap="' . $wrap . '" ' . 'onchange="'
204 . htmlspecialchars($textOnChange) . '"' . $this->formEngine->getPlaceholderAttribute($table, $field, $config, $row)
205 . $additionalInformation['onFocus'] . $additionalAttributes . '>' . GeneralUtility::formatForTextarea($additionalInformation['itemFormElValue']) . '</textarea>';
206 $item = $this->formEngine->renderWizards(
207 array($item, $altItem),
208 $config['wizards'],
209 $table,
210 $row,
211 $field,
212 $additionalInformation,
213 $additionalInformation['itemFormElName'],
214 $specialConfiguration,
215 $rteWouldHaveBeenLoaded
216 );
217 }
218 }
219 return $item;
220 }
221
222 /**
223 * Returns parameters to set with for a textarea field
224 *
225 * @param int $size The abstract width (1-48)
226 * @param string $wrap Empty or "off" (text wrapping in the field or not)
227 * @return string The "cols" attribute string (or style from formWidth())
228 * @see formWidth()
229 */
230 protected function formWidthText($size = 48, $wrap = '') {
231 $wTags = $this->formEngine->formWidth($size, TRUE);
232 // Netscape 6+ seems to have this ODD problem where there WILL ALWAYS be wrapping
233 // with the cols-attribute set and NEVER without the col-attribute...
234 if (strtolower(trim($wrap)) != 'off' && $GLOBALS['CLIENT']['BROWSER'] == 'net' && $GLOBALS['CLIENT']['VERSION'] >= 5) {
235 $wTags .= ' cols="' . $size . '"';
236 }
237 return $wTags;
238 }
239
240 }