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