050d8e49e24d9ee7511f45590d4a7d2c933f2b1f
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / InputLinkElement.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\Imaging\Icon;
19 use TYPO3\CMS\Core\LinkHandling\LinkService;
20 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
21 use TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException;
22 use TYPO3\CMS\Core\Resource\File;
23 use TYPO3\CMS\Core\Resource\Folder;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Core\Utility\MathUtility;
26 use TYPO3\CMS\Core\Utility\StringUtility;
27 use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
28 use TYPO3\CMS\Lang\LanguageService;
29
30 /**
31 * Link input element.
32 *
33 * Shows current link and the link popup.
34 */
35 class InputLinkElement extends AbstractFormElement
36 {
37 /**
38 * Default field controls render the link icon
39 *
40 * @var array
41 */
42 protected $defaultFieldControl = [
43 'linkPopup' => [
44 'renderType' => 'linkPopup',
45 'options' => []
46 ],
47 ];
48
49 /**
50 * Default field wizards enabled for this element.
51 *
52 * @var array
53 */
54 protected $defaultFieldWizard = [
55 'localizationStateSelector' => [
56 'renderType' => 'localizationStateSelector',
57 ],
58 'otherLanguageContent' => [
59 'renderType' => 'otherLanguageContent',
60 'after' => [
61 'localizationStateSelector'
62 ],
63 ],
64 'defaultLanguageDifferences' => [
65 'renderType' => 'defaultLanguageDifferences',
66 'after' => [
67 'otherLanguageContent',
68 ],
69 ],
70 ];
71
72 /**
73 * This will render a single-line input form field, possibly with various control/validation features
74 *
75 * @return array As defined in initializeResultArray() of AbstractNode
76 */
77 public function render()
78 {
79 $languageService = $this->getLanguageService();
80
81 $table = $this->data['tableName'];
82 $fieldName = $this->data['fieldName'];
83 $row = $this->data['databaseRow'];
84 $parameterArray = $this->data['parameterArray'];
85 $resultArray = $this->initializeResultArray();
86 $config = $parameterArray['fieldConf']['config'];
87
88 $itemValue = $parameterArray['itemFormElValue'];
89 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
90 $size = MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
91 $width = (int)$this->formMaxWidth($size);
92 $nullControlNameAttribute = ' name="' . htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']') . '"';
93
94 if ($config['readOnly']) {
95 // Early return for read only fields
96 $html = [];
97 $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
98 $html[] = '<div class="form-wizards-wrap">';
99 $html[] = '<div class="form-wizards-element">';
100 $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
101 $html[] = '<input class="form-control" value="' . htmlspecialchars($itemValue) . '" type="text" disabled>';
102 $html[] = '</div>';
103 $html[] = '</div>';
104 $html[] = '</div>';
105 $html[] = '</div>';
106 $resultArray['html'] = implode(LF, $html);
107 return $resultArray;
108 }
109
110 // @todo: The whole eval handling is a mess and needs refactoring
111 foreach ($evalList as $func) {
112 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
113 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
114 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
115 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
116 if (class_exists($func)) {
117 $evalObj = GeneralUtility::makeInstance($func);
118 if (method_exists($evalObj, 'deevaluateFieldValue')) {
119 $_params = [
120 'value' => $itemValue
121 ];
122 $itemValue = $evalObj->deevaluateFieldValue($_params);
123 }
124 if (method_exists($evalObj, 'returnFieldJS')) {
125 $resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($func) . ']'
126 . ' = function(value) {' . $evalObj->returnFieldJS() . '};';
127 }
128 }
129 }
130 }
131
132 $attributes = [
133 'value' => '',
134 'id' => StringUtility::getUniqueId('formengine-input-'),
135 'class' => implode(' ', [
136 'form-control',
137 't3js-clearable',
138 't3js-form-field-inputlink-input',
139 'hidden',
140 'hasDefaultValue',
141 ]),
142 'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
143 'data-formengine-input-params' => json_encode([
144 'field' => $parameterArray['itemFormElName'],
145 'evalList' => implode(',', $evalList)
146 ]),
147 'data-formengine-input-name' => $parameterArray['itemFormElName'],
148 ];
149
150 $maxLength = $config['max'] ?? 0;
151 if ((int)$maxLength > 0) {
152 $attributes['maxlength'] = (int)$maxLength;
153 }
154 if (!empty($config['placeholder'])) {
155 $attributes['placeholder'] = trim($config['placeholder']);
156 }
157 if (isset($config['autocomplete'])) {
158 $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
159 }
160
161 $valuePickerHtml = [];
162 if (isset($config['valuePicker']['items']) && is_array($config['valuePicker']['items'])) {
163 $mode = $config['valuePicker']['mode'] ?? '';
164 $itemName = $parameterArray['itemFormElName'];
165 $fieldChangeFunc = $parameterArray['fieldChangeFunc'];
166 if ($mode === 'append') {
167 $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
168 . '.value=\'\'+this.options[this.selectedIndex].value+document.editform[' . GeneralUtility::quoteJSvalue($itemName) . '].value';
169 } elseif ($mode === 'prepend') {
170 $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
171 . '.value+=\'\'+this.options[this.selectedIndex].value';
172 } else {
173 $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
174 . '.value=this.options[this.selectedIndex].value';
175 }
176 $valuePickerHtml[] = '<select';
177 $valuePickerHtml[] = ' class="form-control tceforms-select tceforms-wizardselect"';
178 $valuePickerHtml[] = ' onchange="' . htmlspecialchars($assignValue . ';this.blur();this.selectedIndex=0;' . implode('', $fieldChangeFunc)) . '"';
179 $valuePickerHtml[] = '>';
180 $valuePickerHtml[] = '<option></option>';
181 foreach ($config['valuePicker']['items'] as $item) {
182 $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
183 }
184 $valuePickerHtml[] = '</select>';
185 }
186
187 $legacyWizards = $this->renderWizards();
188 $legacyFieldControlHtml = implode(LF, $legacyWizards['fieldControl']);
189 $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
190
191 $fieldInformationResult = $this->renderFieldInformation();
192 $fieldInformationHtml = $fieldInformationResult['html'];
193 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
194
195 $fieldWizardResult = $this->renderFieldWizard();
196 $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
197 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
198
199 $fieldControlResult = $this->renderFieldControl();
200 $fieldControlHtml = $legacyFieldControlHtml . $fieldControlResult['html'];
201 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
202
203 $linkExplanation = $this->getLinkExplanation($itemValue ?: '');
204 $explanation = htmlspecialchars($linkExplanation['text']);
205 $toggleButtonTitle = $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:buttons.toggleLinkExplanation');
206
207 $expansionHtml = [];
208 $expansionHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
209 $expansionHtml[] = '<div class="form-wizards-wrap">';
210 $expansionHtml[] = '<div class="form-wizards-element">';
211 $expansionHtml[] = '<div class="input-group t3js-form-field-inputlink">';
212 $expansionHtml[] = '<span class="input-group-addon">' . $linkExplanation['icon'] . '</span>';
213 $expansionHtml[] = '<input class="form-control form-field-inputlink-explanation t3js-form-field-inputlink-explanation" data-toggle="tooltip" data-title="' . $explanation . '" value="' . $explanation . '" readonly>';
214 $expansionHtml[] = '<input type="text"' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
215 $expansionHtml[] = '<span class="input-group-btn">';
216 $expansionHtml[] = '<button class="btn btn-default t3js-form-field-inputlink-explanation-toggle" type="button" title="' . htmlspecialchars($toggleButtonTitle) . '">';
217 $expansionHtml[] = $this->iconFactory->getIcon('actions-version-workspaces-preview-link', Icon::SIZE_SMALL)->render();
218 $expansionHtml[] = '</button>';
219 $expansionHtml[] = '</span>';
220 $expansionHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
221 $expansionHtml[] = '</div>';
222 $expansionHtml[] = '</div>';
223 $expansionHtml[] = '<div class="form-wizards-items-aside">';
224 $expansionHtml[] = '<div class="btn-group">';
225 $expansionHtml[] = implode(LF, $valuePickerHtml);
226 $expansionHtml[] = $fieldControlHtml;
227 $expansionHtml[] = '</div>';
228 $expansionHtml[] = '</div>';
229 $expansionHtml[] = '<div class="form-wizards-items-bottom">';
230 $expansionHtml[] = $linkExplanation['additionalAttributes'];
231 $expansionHtml[] = $fieldWizardHtml;
232 $expansionHtml[] = '</div>';
233 $expansionHtml[] = '</div>';
234 $expansionHtml[] = '</div>';
235 $expansionHtml = implode(LF, $expansionHtml);
236
237 $fullElement = $expansionHtml;
238 if ($this->hasNullCheckboxButNoPlaceholder()) {
239 $checked = $itemValue !== null ? ' checked="checked"' : '';
240 $fullElement = [];
241 $fullElement[] = '<div class="t3-form-field-disable"></div>';
242 $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
243 $fullElement[] = '<label>';
244 $fullElement[] = '<input type="hidden"' . $nullControlNameAttribute . ' value="0" />';
245 $fullElement[] = '<input type="checkbox"' . $nullControlNameAttribute . ' value="1"' . $checked . ' />';
246 $fullElement[] = $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
247 $fullElement[] = '</label>';
248 $fullElement[] = '</div>';
249 $fullElement[] = $expansionHtml;
250 $fullElement = implode(LF, $fullElement);
251 } elseif ($this->hasNullCheckboxWithPlaceholder()) {
252 $checked = $itemValue !== null ? ' checked="checked"' : '';
253 $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
254 $disabled = '';
255 $fallbackValue = 0;
256 if (strlen($placeholder) > 0) {
257 $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
258 if ($placeholder !== $shortenedPlaceholder) {
259 $overrideLabel = sprintf(
260 $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
261 '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
262 );
263 } else {
264 $overrideLabel = sprintf(
265 $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
266 htmlspecialchars($placeholder)
267 );
268 }
269 } else {
270 $fallbackValue = 1;
271 $checked = ' checked="checked"';
272 $disabled = ' disabled="disabled"';
273 $overrideLabel = $languageService->sL(
274 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
275 );
276 }
277 $fullElement = [];
278 $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
279 $fullElement[] = '<label>';
280 $fullElement[] = '<input type="hidden"' . $nullControlNameAttribute . ' value="' . $fallbackValue . '" />';
281 $fullElement[] = '<input type="checkbox"' . $nullControlNameAttribute . ' value="1"' . $checked . $disabled . ' />';
282 $fullElement[] = $overrideLabel;
283 $fullElement[] = '</label>';
284 $fullElement[] = '</div>';
285 $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
286 $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
287 $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . $shortenedPlaceholder . '" />';
288 $fullElement[] = '</div>';
289 $fullElement[] = '</div>';
290 $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
291 $fullElement[] = $expansionHtml;
292 $fullElement[] = '</div>';
293 $fullElement = implode(LF, $fullElement);
294 }
295
296 $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
297 return $resultArray;
298 }
299
300 /**
301 * @param string $itemValue
302 * @return array
303 */
304 protected function getLinkExplanation(string $itemValue): array
305 {
306 if (empty($itemValue)) {
307 return [];
308 }
309 $data = ['text' => '', 'icon' => ''];
310 $typolinkService = GeneralUtility::makeInstance(TypoLinkCodecService::class);
311 $linkParts = $typolinkService->decode($itemValue);
312 $linkService = GeneralUtility::makeInstance(LinkService::class);
313
314 try {
315 $linkData = $linkService->resolve($linkParts['url']);
316 } catch (FileDoesNotExistException $e) {
317 return $data;
318 } catch (FolderDoesNotExistException $e) {
319 return $data;
320 }
321
322 // Resolving the TypoLink parts (class, title, params)
323 $additionalAttributes = [];
324 foreach ($linkParts as $key => $value) {
325 if ($key === 'url') {
326 continue;
327 }
328 if ($value) {
329 switch ($key) {
330 case 'class':
331 $label = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_browse_links.xlf:class');
332 break;
333 case 'title':
334 $label = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_browse_links.xlf:title');
335 break;
336 case 'additionalParams':
337 $label = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_browse_links.xlf:params');
338 break;
339 default:
340 $label = $key;
341 }
342
343 $additionalAttributes[] = '<span><strong>' . htmlspecialchars($label) . ': </strong> ' . htmlspecialchars($value) . '</span>';
344 }
345 }
346
347 // Resolve the actual link
348 switch ($linkData['type']) {
349 case LinkService::TYPE_PAGE:
350 $pageRecord = BackendUtility::readPageAccess($linkData['pageuid'], '1=1');
351 // Is this a real page
352 if ($pageRecord['uid']) {
353 $data = [
354 'text' => $pageRecord['_thePathFull'] . '[' . $pageRecord['uid'] . ']',
355 'icon' => $this->iconFactory->getIconForRecord('pages', $pageRecord, Icon::SIZE_SMALL)->render()
356 ];
357 }
358 break;
359 case LinkService::TYPE_EMAIL:
360 $data = [
361 'text' => $linkData['email'],
362 'icon' => $this->iconFactory->getIcon('content-elements-mailform', Icon::SIZE_SMALL)->render()
363 ];
364 break;
365 case LinkService::TYPE_URL:
366 $data = [
367 'text' => $this->getDomainByUrl($linkData['url']),
368 'icon' => $this->iconFactory->getIcon('apps-pagetree-page-shortcut-external', Icon::SIZE_SMALL)->render()
369
370 ];
371 break;
372 case LinkService::TYPE_FILE:
373 /** @var File $file */
374 $file = $linkData['file'];
375 if ($file) {
376 $data = [
377 'text' => $file->getPublicUrl(),
378 'icon' => $this->iconFactory->getIconForFileExtension($file->getExtension(), Icon::SIZE_SMALL)->render()
379 ];
380 }
381 break;
382 case LinkService::TYPE_FOLDER:
383 /** @var Folder $folder */
384 $folder = $linkData['folder'];
385 if ($folder) {
386 $data = [
387 'text' => $folder->getPublicUrl(),
388 'icon' => $this->iconFactory->getIcon('apps-filetree-folder-default', Icon::SIZE_SMALL)->render()
389 ];
390 }
391 break;
392 case LinkService::TYPE_RECORD:
393 $table = $this->data['pageTsConfig']['TCEMAIN.']['linkHandler.'][$linkData['identifier'] . '.']['configuration.']['table'];
394 $record = BackendUtility::getRecord($table, $linkData['uid']);
395 $recordTitle = BackendUtility::getRecordTitle($table, $record);
396 $tableTitle = $this->getLanguageService()->sL($GLOBALS['TCA'][$table]['ctrl']['title']);
397 $data = [
398 'text' => sprintf('%s [%s:%d]', $recordTitle, $tableTitle, $linkData['uid']),
399 'icon' => $this->iconFactory->getIconForRecord($table, $record, Icon::SIZE_SMALL)->render()
400 ];
401 break;
402 default:
403 // Please note that this hook is preliminary and might change, as this element could become its own
404 // TCA type in the future
405 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['linkHandler'][$linkData['type']])) {
406 $linkBuilder = GeneralUtility::makeInstance($GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['linkHandler'][$linkData['type']]);
407 $data = $linkBuilder->getFormData($linkData, $linkParts, $this->data, $this);
408 } else {
409 $data = [
410 'text' => 'not implemented type ' . $linkData['type'],
411 'icon' => ''
412 ];
413 }
414 }
415
416 $data['additionalAttributes'] = '<div class="help-block">' . implode(' - ', $additionalAttributes) . '</div>';
417 return $data;
418 }
419
420 /**
421 * @param string $uriString
422 *
423 * @return string
424 */
425 protected function getDomainByUrl(string $uriString): string
426 {
427 $data = parse_url($uriString);
428 return $data['host'] ?? '';
429 }
430
431 /**
432 * @return LanguageService
433 */
434 protected function getLanguageService()
435 {
436 return $GLOBALS['LANG'];
437 }
438 }