Completed
Push — master ( 723452...88ade1 )
by
unknown
17:35
created

BrowseLinksController::getTargetField()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 8
nop 0
dl 0
loc 30
rs 9.4555
c 0
b 0
f 0
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
 * @internal This is a specific Backend Controller implementation and is not considered part of the Public TYPO3 API.
29
 */
30
class BrowseLinksController extends AbstractLinkBrowserController
31
{
32
    /**
33
     * @var string
34
     */
35
    protected $editorId;
36
37
    /**
38
     * TYPO3 language code of the content language
39
     *
40
     * @var string
41
     */
42
    protected $contentsLanguage;
43
44
    /**
45
     * Language service object for localization to the content language
46
     *
47
     * @var LanguageService
48
     */
49
    protected $contentLanguageService;
50
51
    /**
52
     * @var array
53
     */
54
    protected $buttonConfig = [];
55
56
    /**
57
     * @var array
58
     */
59
    protected $thisConfig = [];
60
61
    /**
62
     * @var array
63
     */
64
    protected $classesAnchorDefault = [];
65
66
    /**
67
     * @var array
68
     */
69
    protected $classesAnchorDefaultTitle = [];
70
71
    /**
72
     * @var array
73
     */
74
    protected $classesAnchorClassTitle = [];
75
76
    /**
77
     * @var array
78
     */
79
    protected $classesAnchorDefaultTarget = [];
80
81
    /**
82
     * @var array
83
     */
84
    protected $classesAnchorJSOptions = [];
85
86
    /**
87
     * @var string
88
     */
89
    protected $defaultLinkTarget = '';
90
91
    /**
92
     * @var array
93
     */
94
    protected $additionalAttributes = [];
95
96
    /**
97
     * @var string
98
     */
99
    protected $siteUrl = '';
100
101
    /**
102
     * Initialize controller
103
     */
104
    protected function init()
105
    {
106
        parent::init();
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['P']['curUrl'] ?? [];
122
        $this->editorId = $queryParameters['editorId'];
123
        $this->contentsLanguage = $queryParameters['contentsLanguage'];
124
125
        $this->contentLanguageService = LanguageService::create($this->contentsLanguage);
126
127
        $tcaFieldConf = ['enableRichtext' => true];
128
        if (!empty($queryParameters['P']['richtextConfigurationName'])) {
129
            $tcaFieldConf['richtextConfiguration'] = $queryParameters['P']['richtextConfigurationName'];
130
        }
131
132
        /** @var Richtext $richtextConfigurationProvider */
133
        $richtextConfigurationProvider = GeneralUtility::makeInstance(Richtext::class);
134
        $this->thisConfig = $richtextConfigurationProvider->getConfiguration(
135
            $this->parameters['table'],
136
            $this->parameters['fieldName'],
137
            (int)$this->parameters['pid'],
138
            $this->parameters['recordType'],
139
            $tcaFieldConf
140
        );
141
        $this->buttonConfig = $this->thisConfig['buttons']['link'] ?? [];
142
    }
143
144
    protected function initDocumentTemplate()
145
    {
146
        parent::initDocumentTemplate();
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
            if (!empty($this->currentLinkParts['url']['parameters'])) {
172
                $this->currentLinkParts['params'] = '&' . $this->currentLinkParts['url']['parameters'];
173
            }
174
        }
175
176
        parent::initCurrentUrl();
177
    }
178
179
    /**
180
     * Renders the link attributes for the selected link handler
181
     *
182
     * @return string
183
     */
184
    protected function renderLinkAttributeFields()
185
    {
186
        // Processing the classes configuration
187
        if (!empty($this->buttonConfig['properties']['class']['allowedClasses'])) {
188
            $classesAnchorArray = is_array($this->buttonConfig['properties']['class']['allowedClasses']) ? $this->buttonConfig['properties']['class']['allowedClasses'] : GeneralUtility::trimExplode(',', $this->buttonConfig['properties']['class']['allowedClasses'], true);
189
            // Collecting allowed classes and configured default values
190
            $classesAnchor = [
191
                'all' => []
192
            ];
193
194
            if (is_array($this->thisConfig['classesAnchor'])) {
195
                $readOnlyTitle = $this->isReadonlyTitle();
196
                foreach ($this->thisConfig['classesAnchor'] as $label => $conf) {
197
                    if (in_array($conf['class'], $classesAnchorArray, true)) {
198
                        $classesAnchor['all'][] = $conf['class'];
199
                        if ($conf['type'] === $this->displayedLinkHandlerId) {
200
                            $classesAnchor[$conf['type']][] = $conf['class'];
201
                            if ($this->buttonConfig[$conf['type']]['properties']['class']['default'] == $conf['class']) {
202
                                $this->classesAnchorDefault[$conf['type']] = $conf['class'];
203
                                if ($conf['titleText']) {
204
                                    $this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText']));
205
                                }
206
                                if (isset($conf['target'])) {
207
                                    $this->classesAnchorDefaultTarget[$conf['type']] = trim($conf['target']);
208
                                }
209
                            }
210
                        }
211
                        if ($readOnlyTitle && $conf['titleText']) {
212
                            $this->classesAnchorClassTitle[$conf['class']] = ($this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText'])));
213
                        }
214
                    }
215
                }
216
            }
217
            if (isset($this->linkAttributeValues['class'])
218
                && isset($classesAnchor[$this->displayedLinkHandlerId])
219
                && !in_array($this->linkAttributeValues['class'], $classesAnchor[$this->displayedLinkHandlerId], true)
220
            ) {
221
                unset($this->linkAttributeValues['class']);
222
            }
223
            // Constructing the class selector options
224
            foreach ($classesAnchorArray as $class) {
225
                if (!in_array($class, $classesAnchor['all']) || in_array($class, $classesAnchor['all']) && is_array($classesAnchor[$this->displayedLinkHandlerId]) && in_array($class, $classesAnchor[$this->displayedLinkHandlerId])) {
226
                    $selected = '';
227
                    if ($this->linkAttributeValues['class'] === $class || !$this->linkAttributeValues['class'] && $this->classesAnchorDefault[$this->displayedLinkHandlerId] == $class) {
228
                        $selected = 'selected="selected"';
229
                    }
230
                    $classLabel = !empty($this->thisConfig['classes'][$class]['name'])
231
                        ? $this->getPageConfigLabel($this->thisConfig['classes'][$class]['name'], 0)
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "?"; newline found
Loading history...
232
                        : $class;
0 ignored issues
show
Coding Style introduced by
Expected 1 space before ":"; newline found
Loading history...
233
                    $classStyle = !empty($this->thisConfig['classes'][$class]['value'])
234
                        ? $this->thisConfig['classes'][$class]['value']
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "?"; newline found
Loading history...
235
                        : '';
0 ignored issues
show
Coding Style introduced by
Expected 1 space before ":"; newline found
Loading history...
236
                    $title = $this->classesAnchorClassTitle[$class] ?? $this->classesAnchorDefaultTitle[$class] ?? '';
237
                    $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] .= '<option ' . $selected . ' value="' . htmlspecialchars($class) . '"'
238
                        . ($classStyle ? ' style="' . htmlspecialchars($classStyle) . '"' : '')
239
                        . 'data-link-title="' . htmlspecialchars($title) . '"'
240
                        . '>' . htmlspecialchars($classLabel)
241
                        . '</option>';
242
                }
243
            }
244
            if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId] && !($this->buttonConfig['properties']['class']['required'] || $this->buttonConfig[$this->displayedLinkHandlerId]['properties']['class']['required'])) {
245
                $selected = '';
246
                if (!$this->linkAttributeValues['class'] && !$this->classesAnchorDefault[$this->displayedLinkHandlerId]) {
247
                    $selected = 'selected="selected"';
248
                }
249
                $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] = '<option ' . $selected . ' value=""></option>' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId];
250
            }
251
        }
252
        // Default target
253
        $this->defaultLinkTarget = $this->classesAnchorDefault[$this->displayedLinkHandlerId] && $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
254
            ? $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "?"; newline found
Loading history...
255
            : ($this->buttonConfig[$this->displayedLinkHandlerId]['properties']['target']['default'] ?? $this->buttonConfig['properties']['target']['default'] ?? '');
0 ignored issues
show
Coding Style introduced by
Expected 1 space before ":"; newline found
Loading history...
256
257
        // todo: find new name for this option
258
        // Initializing additional attributes
259
        if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rte_ckeditor']['plugins']['TYPO3Link']['additionalAttributes']) {
260
            $addAttributes = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rte_ckeditor']['plugins']['TYPO3Link']['additionalAttributes'], true);
261
            foreach ($addAttributes as $attribute) {
262
                $this->additionalAttributes[$attribute] = $this->linkAttributeValues[$attribute] ?? '';
263
            }
264
        }
265
        return parent::renderLinkAttributeFields();
266
    }
267
268
    /**
269
     * Localize a label obtained from Page TSConfig
270
     *
271
     * @param string $string The label to be localized
272
     * @param bool $JScharCode If needs to be converted to an array of char numbers
273
     * @return string Localized string
274
     */
275
    protected function getPageConfigLabel($string, $JScharCode = true)
276
    {
277
        if (strpos($string, 'LLL:') !== 0) {
278
            $label = $string;
279
        } else {
280
            $label = $this->getLanguageService()->sL(trim($string));
281
        }
282
        $label = str_replace(['\\\'', '"'], ['\'', '\\"'], $label);
283
        return $JScharCode ? GeneralUtility::quoteJSvalue($label) : $label;
284
    }
285
286
    protected function renderCurrentUrl()
287
    {
288
        $this->moduleTemplate->getView()->assign('removeCurrentLink', true);
289
        parent::renderCurrentUrl();
290
    }
291
292
    /**
293
     * Get the allowed items or tabs
294
     *
295
     * @return string[]
296
     */
297
    protected function getAllowedItems()
298
    {
299
        $allowedItems = parent::getAllowedItems();
300
301
        $blindLinkOptions = isset($this->thisConfig['blindLinkOptions'])
302
            ? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkOptions'], true)
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "?"; newline found
Loading history...
303
            : [];
0 ignored issues
show
Coding Style introduced by
Expected 1 space before ":"; newline found
Loading history...
304
        $allowedItems = array_diff($allowedItems, $blindLinkOptions);
305
306
        if (is_array($this->buttonConfig['options']) && $this->buttonConfig['options']['removeItems']) {
307
            $allowedItems = array_diff($allowedItems, GeneralUtility::trimExplode(',', $this->buttonConfig['options']['removeItems'], true));
308
        }
309
310
        return $allowedItems;
311
    }
312
313
    /**
314
     * Get the allowed link attributes
315
     *
316
     * @return string[]
317
     */
318
    protected function getAllowedLinkAttributes()
319
    {
320
        $allowedLinkAttributes = parent::getAllowedLinkAttributes();
321
322
        $blindLinkFields = isset($this->thisConfig['blindLinkFields'])
323
            ? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkFields'], true)
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "?"; newline found
Loading history...
324
            : [];
0 ignored issues
show
Coding Style introduced by
Expected 1 space before ":"; newline found
Loading history...
325
        $allowedLinkAttributes = array_diff($allowedLinkAttributes, $blindLinkFields);
326
327
        return $allowedLinkAttributes;
328
    }
329
330
    /**
331
     * Create an array of link attribute field rendering definitions
332
     *
333
     * @return string[]
334
     */
335
    protected function getLinkAttributeFieldDefinitions()
336
    {
337
        $fieldRenderingDefinitions = parent::getLinkAttributeFieldDefinitions();
338
        $fieldRenderingDefinitions['title'] = $this->getTitleField();
339
        $fieldRenderingDefinitions['class'] = $this->getClassField();
340
        $fieldRenderingDefinitions['target'] = $this->getTargetField();
341
        $fieldRenderingDefinitions['rel'] = $this->getRelField();
342
        if (empty($this->buttonConfig['queryParametersSelector']['enabled'])) {
343
            unset($fieldRenderingDefinitions['params']);
344
        }
345
        return $fieldRenderingDefinitions;
346
    }
347
348
    /**
349
     * Add rel field
350
     *
351
     * @return string
352
     */
353
    protected function getRelField()
354
    {
355
        if (empty($this->buttonConfig['relAttribute']['enabled'])) {
356
            return '';
357
        }
358
359
        $currentRel = '';
360
        if ($this->displayedLinkHandler === $this->currentLinkHandler
361
            && !empty($this->currentLinkParts)
362
            && isset($this->linkAttributeValues['rel'])
363
            && is_string($this->linkAttributeValues['rel'])
364
        ) {
365
            $currentRel = $this->linkAttributeValues['rel'];
366
        }
367
368
        return '
369
            <form action="" name="lrelform" id="lrelform" class="t3js-dummyform form-horizontal">
370
                 <div class="form-group form-group-sm">
371
                    <label class="col-xs-4 control-label">' .
372
                        htmlspecialchars($this->getLanguageService()->getLL('linkRelationship')) .
373
                    '</label>
374
                    <div class="col-xs-8">
375
                        <input type="text" name="lrel" class="form-control" value="' . htmlspecialchars($currentRel) . '" />
376
                    </div>
377
                </div>
378
            </form>
379
            ';
380
    }
381
382
    /**
383
     * Add target selector
384
     *
385
     * @return string
386
     */
387
    protected function getTargetField()
388
    {
389
        $targetSelectorConfig = [];
390
        if (is_array($this->buttonConfig['targetSelector'])) {
391
            $targetSelectorConfig = $this->buttonConfig['targetSelector'];
392
        }
393
        $target = $this->linkAttributeValues['target'] ?: $this->defaultLinkTarget;
394
        $lang = $this->getLanguageService();
395
        $targetSelector = '';
396
397
        if (!$targetSelectorConfig['disabled']) {
398
            $targetSelector = '
399
						<select name="ltarget_type" class="t3js-targetPreselect form-control">
400
							<option value=""></option>
401
							<option value="_top">' . htmlspecialchars($lang->getLL('top')) . '</option>
402
							<option value="_blank">' . htmlspecialchars($lang->getLL('newWindow')) . '</option>
403
						</select>
404
			';
405
        }
406
407
        return '
408
				<form action="" name="ltargetform" id="ltargetform" class="t3js-dummyform form-horizontal">
409
                    <div class="form-group form-group-sm" ' . ($targetSelectorConfig['disabled'] ? ' style="display: none;"' : '') . '>
410
                        <label class="col-xs-4 control-label">' . htmlspecialchars($lang->getLL('target')) . '</label>
411
						<div class="col-xs-4">
412
							<input type="text" name="ltarget" class="t3js-linkTarget form-control"
413
							    value="' . htmlspecialchars($target) . '" />
414
						</div>
415
						<div class="col-xs-4">
416
							' . $targetSelector . '
417
						</div>
418
					</div>
419
				</form>
420
				';
421
    }
422
423
    /**
424
     * Add title selector
425
     *
426
     * @return string
427
     */
428
    protected function getTitleField()
429
    {
430
        if ($this->linkAttributeValues['title']) {
431
            $title = $this->linkAttributeValues['title'];
432
        } else {
433
            $title = $this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId] ?: '';
434
        }
435
        $readOnlyTitle = $this->isReadonlyTitle();
436
437
        if ($readOnlyTitle) {
438
            $currentClass = $this->linkAttributeFields['class'];
439
            if (!$currentClass) {
440
                $currentClass = empty($this->classesAnchorDefault[$this->displayedLinkHandlerId]) ? '' : $this->classesAnchorDefault[$this->displayedLinkHandlerId];
441
            }
442
            $title = $this->classesAnchorClassTitle[$currentClass] ?? $this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId] ?? '';
443
        }
444
        return '
445
                <form action="" name="ltitleform" id="ltitleform" class="t3js-dummyform form-horizontal">
446
                    <div class="form-group form-group-sm">
447
                        <label class="col-xs-4 control-label">
448
                            ' . htmlspecialchars($this->getLanguageService()->getLL('title')) . '
449
                         </label>
450
                         <div class="col-xs-8">
451
                                <input ' . ($readOnlyTitle ? 'disabled' : '') . ' type="text" name="ltitle" class="form-control t3js-linkTitle"
452
                                        value="' . htmlspecialchars($title) . '" />
453
                        </div>
454
                    </div>
455
                </form>
456
                ';
457
    }
458
459
    /**
460
     * Return html code for the class selector
461
     *
462
     * @return string the html code to be added to the form
463
     */
464
    protected function getClassField()
465
    {
466
        $selectClass = '';
467
        if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId]) {
468
            $selectClass = '
469
                <form action="" name="lclassform" id="lclassform" class="t3js-dummyform form-horizontal">
470
                    <div class="form-group form-group-sm">
471
                        <label class="col-xs-4 control-label">
472
                            ' . htmlspecialchars($this->getLanguageService()->getLL('class')) . '
473
                        </label>
474
                        <div class="col-xs-8">
475
                            <select name="lclass" class="t3js-class-selector form-control">
476
                                ' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] . '
477
                            </select>
478
                        </div>
479
                    </div>
480
                </form>
481
            ';
482
        }
483
        return $selectClass;
484
    }
485
486
    /**
487
     * Return the ID of current page
488
     *
489
     * @return int
490
     */
491
    protected function getCurrentPageId()
492
    {
493
        return (int)$this->parameters['pid'];
494
    }
495
496
    /**
497
     * Retrieve the configuration
498
     *
499
     * This is only used by RTE currently.
500
     *
501
     * @return array
502
     */
503
    public function getConfiguration()
504
    {
505
        return $this->buttonConfig;
506
    }
507
508
    /**
509
     * Get attributes for the body tag
510
     *
511
     * @return string[] Array of body-tag attributes
512
     */
513
    protected function getBodyTagAttributes()
514
    {
515
        $parameters = parent::getBodyTagAttributes();
516
        $parameters['data-site-url'] = $this->siteUrl;
517
        $parameters['data-default-link-target'] = $this->defaultLinkTarget;
518
        return $parameters;
519
    }
520
521
    /**
522
     * @param array $overrides
523
     *
524
     * @return array Array of parameters which have to be added to URLs
525
     */
526
    public function getUrlParameters(array $overrides = null)
527
    {
528
        return [
529
            'act' => $overrides['act'] ?? $this->displayedLinkHandlerId,
530
            'P' => $overrides['P'] ?? $this->parameters,
531
            'editorId' => $this->editorId,
532
            'contentsLanguage' => $this->contentsLanguage
533
        ];
534
    }
535
536
    protected function isReadonlyTitle(): bool
537
    {
538
        if (isset($this->buttonConfig[$this->displayedLinkHandlerId]['properties']['title']['readOnly'])) {
539
            return (bool)$this->buttonConfig[$this->displayedLinkHandlerId]['properties']['title']['readOnly'];
540
        }
541
542
        return (bool)($this->buttonConfig['properties']['title']['readOnly'] ?? false);
543
    }
544
}
545