[BUGFIX] CKEditor: Make link editable
[Packages/TYPO3.CMS.git] / typo3 / sysext / rte_ckeditor / Classes / Controller / BrowseLinksController.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\RteCKEditor\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Configuration\Richtext;
20 use TYPO3\CMS\Core\LinkHandling\LinkService;
21 use TYPO3\CMS\Core\Localization\LanguageService;
22 use TYPO3\CMS\Core\Page\PageRenderer;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Recordlist\Controller\AbstractLinkBrowserController;
25
26 /**
27 * Extended controller for link browser
28 */
29 class BrowseLinksController extends AbstractLinkBrowserController
30 {
31 /**
32 * @var string
33 */
34 protected $editorId;
35
36 /**
37 * TYPO3 language code of the content language
38 *
39 * @var int
40 */
41 protected $contentsLanguage;
42
43 /**
44 * Language service object for localization to the content language
45 *
46 * @var LanguageService
47 */
48 protected $contentLanguageService;
49
50 /**
51 * @var array
52 */
53 protected $buttonConfig = [];
54
55 /**
56 * @var array
57 */
58 protected $thisConfig = [];
59
60 /**
61 * @var array
62 */
63 protected $classesAnchorDefault = [];
64
65 /**
66 * @var array
67 */
68 protected $classesAnchorDefaultTitle = [];
69
70 /**
71 * @var array
72 */
73 protected $classesAnchorClassTitle = [];
74
75 /**
76 * @var array
77 */
78 protected $classesAnchorDefaultTarget = [];
79
80 /**
81 * @var array
82 */
83 protected $classesAnchorJSOptions = [];
84
85 /**
86 * @var string
87 */
88 protected $defaultLinkTarget = '';
89
90 /**
91 * @var array
92 */
93 protected $additionalAttributes = [];
94
95 /**
96 * @var string
97 */
98 protected $siteUrl = '';
99
100 /**
101 * Initialize controller
102 */
103 protected function init()
104 {
105 parent::init();
106
107 $this->contentLanguageService = GeneralUtility::makeInstance(LanguageService::class);
108 }
109
110 /**
111 * @param ServerRequestInterface $request
112 */
113 protected function initVariables(ServerRequestInterface $request)
114 {
115 parent::initVariables($request);
116
117 $queryParameters = $request->getQueryParams();
118
119 $this->siteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
120
121 $this->currentLinkParts = $queryParameters['curUrl'] ?? [];
122 $this->editorId = $queryParameters['editorId'];
123 $this->contentsLanguage = $queryParameters['contentsLanguage'];
124 $this->RTEtsConfigParams = $queryParameters['RTEtsConfigParams'] ?? null;
125
126 $this->contentLanguageService->init($this->contentsLanguage);
127
128 /** @var Richtext $richtextConfigurationProvider */
129 $richtextConfigurationProvider = GeneralUtility::makeInstance(Richtext::class);
130 $this->thisConfig = $richtextConfigurationProvider->getConfiguration(
131 $this->parameters['table'],
132 $this->parameters['fieldName'],
133 (int)$this->parameters['pid'],
134 $this->parameters['recordType'],
135 ['richtext' => true]
136 );
137 $this->buttonConfig = $this->thisConfig['buttons']['link'] ?? [];
138 }
139
140 /**
141 * Initialize document template object
142 */
143 protected function initDocumentTemplate()
144 {
145 parent::initDocumentTemplate();
146
147 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
148 $pageRenderer->loadRequireJsModule(
149 'TYPO3/CMS/RteCkeditor/RteLinkBrowser',
150 'function(RteLinkBrowser) {
151 RteLinkBrowser.initialize(' . GeneralUtility::quoteJSvalue($this->editorId) . ');
152 }'
153 );
154 }
155
156 /**
157 * Initialize $this->currentLink and $this->currentLinkHandler
158 */
159 protected function initCurrentUrl()
160 {
161 if (empty($this->currentLinkParts)) {
162 return;
163 }
164
165 if (!empty($this->currentLinkParts['url'])) {
166 $linkService = GeneralUtility::makeInstance(LinkService::class);
167 $data = $linkService->resolve($this->currentLinkParts['url']);
168 $this->currentLinkParts['type'] = $data['type'];
169 unset($data['type']);
170 $this->currentLinkParts['url'] = $data;
171 }
172
173 if (!empty($this->currentLinkParts['class'])) {
174 // Only keep last class value (others are automatically added again by required option)
175 // https://review.typo3.org/#/c/29643
176 $currentClasses = GeneralUtility::trimExplode(' ', $this->currentLinkParts['class'], true);
177 if (count($currentClasses) > 1) {
178 $this->currentLinkParts['class'] = end($currentClasses);
179 }
180 }
181 parent::initCurrentUrl();
182 }
183
184 /**
185 * Renders the link attributes for the selected link handler
186 *
187 * @return string
188 */
189 public function renderLinkAttributeFields()
190 {
191 // Processing the classes configuration
192 if (!empty($this->buttonConfig['properties']['class']['allowedClasses'])) {
193 $classesAnchorArray = is_array($this->buttonConfig['properties']['class']['allowedClasses']) ? $this->buttonConfig['properties']['class']['allowedClasses'] : GeneralUtility::trimExplode(',', $this->buttonConfig['properties']['class']['allowedClasses'], true);
194 // Collecting allowed classes and configured default values
195 $classesAnchor = [
196 'all' => []
197 ];
198
199 if (is_array($this->thisConfig['classesAnchor'])) {
200 foreach ($this->thisConfig['classesAnchor'] as $label => $conf) {
201 if (in_array($conf['class'], $classesAnchorArray, true)) {
202 $classesAnchor['all'][] = $conf['class'];
203 if ($conf['type'] === $this->displayedLinkHandlerId) {
204 $classesAnchor[$conf['type']][] = $conf['class'];
205 if ($this->buttonConfig[$conf['type']]['properties']['class']['default'] == $conf['class']) {
206 $this->classesAnchorDefault[$conf['type']] = $conf['class'];
207 if ($conf['titleText']) {
208 $this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText']));
209 }
210 if (isset($conf['target'])) {
211 $this->classesAnchorDefaultTarget[$conf['type']] = trim($conf['target']);
212 }
213 }
214 }
215 if ($conf['titleText']) {
216 $this->classesAnchorClassTitle[$conf['class']] = ($this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText'])));
217 }
218 }
219 }
220 }
221 if (isset($this->linkAttributeValues['class'])
222 && isset($classesAnchor[$this->displayedLinkHandlerId])
223 && !in_array($this->linkAttributeValues['class'], $classesAnchor[$this->displayedLinkHandlerId], true)
224 ) {
225 unset($this->linkAttributeValues['class']);
226 }
227 // Constructing the class selector options
228 foreach ($classesAnchorArray as $class) {
229 if (!in_array($class, $classesAnchor['all']) || in_array($class, $classesAnchor['all']) && is_array($classesAnchor[$this->displayedLinkHandlerId]) && in_array($class, $classesAnchor[$this->displayedLinkHandlerId])) {
230 $selected = '';
231 if ($this->linkAttributeValues['class'] === $class || !$this->linkAttributeValues['class'] && $this->classesAnchorDefault[$this->displayedLinkHandlerId] == $class) {
232 $selected = 'selected="selected"';
233 }
234 $classLabel = !empty($this->thisConfig['classes'][$class]['name'])
235 ? $this->getPageConfigLabel($this->thisConfig['classes'][$class]['name'], 0)
236 : $class;
237 $classStyle = !empty($this->thisConfig['classes'][$class]['value'])
238 ? $this->thisConfig['classes'][$class]['value']
239 : '';
240 $title = $this->classesAnchorClassTitle[$class] ?? $this->classesAnchorDefaultTitle[$class] ?? '';
241 $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] .= '<option ' . $selected . ' value="' . htmlspecialchars($class) . '"'
242 . ($classStyle ? ' style="' . htmlspecialchars($classStyle) . '"' : '')
243 . 'data-link-title="' . htmlspecialchars($title) . '"'
244 . '>' . htmlspecialchars($classLabel)
245 . '</option>';
246 }
247 }
248 if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId] && !($this->buttonConfig['properties']['class']['required'] || $this->buttonConfig[$this->displayedLinkHandlerId]['properties']['class']['required'])) {
249 $selected = '';
250 if (!$this->linkAttributeValues['class'] && !$this->classesAnchorDefault[$this->displayedLinkHandlerId]) {
251 $selected = 'selected="selected"';
252 }
253 $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] = '<option ' . $selected . ' value=""></option>' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId];
254 }
255 }
256 // Default target
257 $this->defaultLinkTarget = $this->classesAnchorDefault[$this->displayedLinkHandlerId] && $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
258 ? $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
259 : (isset($this->buttonConfig[$this->displayedLinkHandlerId]['properties']['target']['default'])
260 ? $this->buttonConfig[$this->displayedLinkHandlerId]['properties']['target']['default']
261 : (isset($this->buttonConfig['properties']['target']['default'])
262 ? $this->buttonConfig['properties']['target']['default']
263 : ''));
264
265 // todo: find new name for this option
266 // Initializing additional attributes
267 if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rte_ckeditor']['plugins']['TYPO3Link']['additionalAttributes']) {
268 $addAttributes = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rte_ckeditor']['plugins']['TYPO3Link']['additionalAttributes'], true);
269 foreach ($addAttributes as $attribute) {
270 $this->additionalAttributes[$attribute] = $this->linkAttributeValues[$attribute] ?? '';
271 }
272 }
273 return parent::renderLinkAttributeFields();
274 }
275
276 /**
277 * Localize a label obtained from Page TSConfig
278 *
279 * @param string $string The label to be localized
280 * @param bool $JScharCode If needs to be converted to an array of char numbers
281 * @return string Localized string
282 */
283 public function getPageConfigLabel($string, $JScharCode = true)
284 {
285 if (strpos($string, 'LLL:') !== 0) {
286 $label = $string;
287 } else {
288 $label = $this->getLanguageService()->sL(trim($string));
289 }
290 $label = str_replace('"', '\\"', str_replace('\\\'', '\'', $label));
291 return $JScharCode ? GeneralUtility::quoteJSvalue($label) : $label;
292 }
293
294 /**
295 * @return string
296 */
297 protected function renderCurrentUrl()
298 {
299 $removeLink = ' <a href="#" class="t3js-removeCurrentLink">' . htmlspecialchars($this->getLanguageService()->getLL('removeLink')) . '</a>';
300 return '
301 <div class="element-browser-panel element-browser-title">' .
302 htmlspecialchars($this->getLanguageService()->getLL('currentLink')) .
303 ': ' .
304 htmlspecialchars($this->currentLinkHandler->formatCurrentUrl()) .
305 '<span class="pull-right">' . $removeLink . '</span>' .
306 '</div>';
307 }
308
309 /**
310 * Get the allowed items or tabs
311 *
312 * @return string[]
313 */
314 protected function getAllowedItems()
315 {
316 $allowedItems = parent::getAllowedItems();
317
318 $blindLinkOptions = isset($this->thisConfig['blindLinkOptions'])
319 ? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkOptions'], true)
320 : [];
321 $allowedItems = array_diff($allowedItems, $blindLinkOptions);
322
323 if (is_array($this->buttonConfig['options']) && $this->buttonConfig['options']['removeItems']) {
324 $allowedItems = array_diff($allowedItems, GeneralUtility::trimExplode(',', $this->buttonConfig['options']['removeItems'], true));
325 }
326
327 return $allowedItems;
328 }
329
330 /**
331 * Get the allowed link attributes
332 *
333 * @return string[]
334 */
335 protected function getAllowedLinkAttributes()
336 {
337 $allowedLinkAttributes = parent::getAllowedLinkAttributes();
338
339 $blindLinkFields = isset($this->thisConfig['blindLinkFields'])
340 ? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkFields'], true)
341 : [];
342 $allowedLinkAttributes = array_diff($allowedLinkAttributes, $blindLinkFields);
343
344 return $allowedLinkAttributes;
345 }
346
347 /**
348 * Create an array of link attribute field rendering definitions
349 *
350 * @return string[]
351 */
352 protected function getLinkAttributeFieldDefinitions()
353 {
354 $fieldRenderingDefinitions = parent::getLinkAttributeFieldDefinitions();
355 $fieldRenderingDefinitions['title'] = $this->getTitleField();
356 $fieldRenderingDefinitions['class'] = $this->getClassField();
357 $fieldRenderingDefinitions['target'] = $this->getTargetField();
358 $fieldRenderingDefinitions['rel'] = $this->getRelField();
359 if (empty($this->buttonConfig['queryParametersSelector']['enabled'])) {
360 unset($fieldRenderingDefinitions['params']);
361 }
362 return $fieldRenderingDefinitions;
363 }
364
365 /**
366 * Add rel field
367 *
368 * @return string
369 */
370 protected function getRelField()
371 {
372 if (empty($this->buttonConfig['relAttribute']['enabled'])) {
373 return '';
374 }
375
376 $currentRel = '';
377 if ($this->displayedLinkHandler === $this->currentLinkHandler
378 && !empty($this->currentLinkParts)
379 && isset($this->linkAttributeValues['rel'])
380 && is_string($this->linkAttributeValues['rel'])
381 ) {
382 $currentRel = $this->linkAttributeValues['rel'];
383 }
384
385 return '
386 <form action="" name="lrelform" id="lrelform" class="t3js-dummyform form-horizontal">
387 <div class="form-group form-group-sm">
388 <label class="col-xs-4 control-label">' .
389 htmlspecialchars($this->getLanguageService()->getLL('linkRelationship')) .
390 '</label>
391 <div class="col-xs-8">
392 <input type="text" name="lrel" class="form-control" value="' . htmlspecialchars($currentRel) . '" />
393 </div>
394 </div>
395 </form>
396 ';
397 }
398
399 /**
400 * Add target selector
401 *
402 * @return string
403 */
404 protected function getTargetField()
405 {
406 $targetSelectorConfig = [];
407 if (is_array($this->buttonConfig['targetSelector'])) {
408 $targetSelectorConfig = $this->buttonConfig['targetSelector'];
409 }
410 $target = $this->linkAttributeValues['target'] ?: $this->defaultLinkTarget;
411 $lang = $this->getLanguageService();
412 $targetSelector = '';
413
414 if (!$targetSelectorConfig['disabled']) {
415 $targetSelector = '
416 <select name="ltarget_type" class="t3js-targetPreselect form-control">
417 <option value=""></option>
418 <option value="_top">' . htmlspecialchars($lang->getLL('top')) . '</option>
419 <option value="_blank">' . htmlspecialchars($lang->getLL('newWindow')) . '</option>
420 </select>
421 ';
422 }
423
424 return '
425 <form action="" name="ltargetform" id="ltargetform" class="t3js-dummyform form-horizontal">
426 <div class="form-group form-group-sm" ' . ($targetSelectorConfig['disabled'] ? ' style="display: none;"' : '') . '>
427 <label class="col-xs-4 control-label">' . htmlspecialchars($lang->getLL('target')) . '</label>
428 <div class="col-xs-4">
429 <input type="text" name="ltarget" class="t3js-linkTarget form-control"
430 value="' . htmlspecialchars($target) . '" />
431 </div>
432 <div class="col-xs-4">
433 ' . $targetSelector . '
434 </div>
435 </div>
436 </form>
437 ';
438 }
439
440 /**
441 * Add title selector
442 *
443 * @return string
444 */
445 protected function getTitleField()
446 {
447 if ($this->linkAttributeValues['title']) {
448 $title = $this->linkAttributeValues['title'];
449 } else {
450 $title = $this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId] ?: '';
451 }
452 if (isset($this->buttonConfig[$this->displayedLinkHandlerId]['properties']['title']['readOnly'])) {
453 $readOnly = (bool)$this->buttonConfig[$this->displayedLinkHandlerId]['properties']['title']['readOnly'];
454 } else {
455 $readOnly = isset($this->buttonConfig['properties']['title']['readOnly'])
456 ? (bool)$this->buttonConfig['properties']['title']['readOnly']
457 : false;
458 }
459
460 if ($readOnly) {
461 $currentClass = $this->linkAttributeFields['class'];
462 if (!$currentClass) {
463 $currentClass = empty($this->classesAnchorDefault[$this->displayedLinkHandlerId]) ? '' : $this->classesAnchorDefault[$this->displayedLinkHandlerId];
464 }
465 $title = $currentClass
466 ? $this->classesAnchorClassTitle[$currentClass]
467 : ($this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId] ?? '');
468 }
469 return '
470 <form action="" name="ltitleform" id="ltitleform" class="t3js-dummyform form-horizontal">
471 <div class="form-group form-group-sm">
472 <label class="col-xs-4 control-label">
473 ' . htmlspecialchars($this->getLanguageService()->getLL('title')) . '
474 </label>
475 <div class="col-xs-8">
476 <input ' . ($readOnly ? 'disabled' : '') . ' type="text" name="ltitle" class="form-control t3js-linkTitle"
477 value="' . htmlspecialchars($title) . '" />
478 </div>
479 </div>
480 </form>
481 ';
482 }
483
484 /**
485 * Return html code for the class selector
486 *
487 * @return string the html code to be added to the form
488 */
489 protected function getClassField()
490 {
491 $selectClass = '';
492 if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId]) {
493 $selectClass = '
494 <form action="" name="lclassform" id="lclassform" class="t3js-dummyform form-horizontal">
495 <div class="form-group form-group-sm">
496 <label class="col-xs-4 control-label">
497 ' . htmlspecialchars($this->getLanguageService()->getLL('class')) . '
498 </label>
499 <div class="col-xs-8">
500 <select name="lclass" class="t3js-class-selector form-control">
501 ' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] . '
502 </select>
503 </div>
504 </div>
505 </form>
506 ';
507 }
508 return $selectClass;
509 }
510
511 /**
512 * Return the ID of current page
513 *
514 * @return int
515 */
516 protected function getCurrentPageId()
517 {
518 return (int)$this->parameters['pid'];
519 }
520
521 /**
522 * Retrieve the configuration
523 *
524 * This is only used by RTE currently.
525 *
526 * @return array
527 */
528 public function getConfiguration()
529 {
530 return $this->buttonConfig;
531 }
532
533 /**
534 * Get attributes for the body tag
535 *
536 * @return string[] Array of body-tag attributes
537 */
538 protected function getBodyTagAttributes()
539 {
540 $parameters = parent::getBodyTagAttributes();
541 $parameters['data-site-url'] = $this->siteUrl;
542 $parameters['data-default-link-target'] = $this->defaultLinkTarget;
543 return $parameters;
544 }
545
546 /**
547 * @param array $overrides
548 *
549 * @return array Array of parameters which have to be added to URLs
550 */
551 public function getUrlParameters(array $overrides = null)
552 {
553 return [
554 'act' => isset($overrides['act']) ? $overrides['act'] : $this->displayedLinkHandlerId,
555 'editorId' => $this->editorId,
556 'contentsLanguage' => $this->contentsLanguage,
557 'P' => $this->parameters
558 ];
559 }
560 }