TYPO3 CMS  TYPO3_8-7
BrowseLinksController.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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 
25 
30 {
34  protected $editorId;
35 
41  protected $contentsLanguage;
42 
49 
53  protected $buttonConfig = [];
54 
58  protected $thisConfig = [];
59 
63  protected $classesAnchorDefault = [];
64 
69 
73  protected $classesAnchorClassTitle = [];
74 
79 
83  protected $classesAnchorJSOptions = [];
84 
88  protected $defaultLinkTarget = '';
89 
93  protected $additionalAttributes = [];
94 
98  protected $siteUrl = '';
99 
103  protected function init()
104  {
105  parent::init();
106 
107  $this->contentLanguageService = GeneralUtility::makeInstance(LanguageService::class);
108  }
109 
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  $this->RTEtsConfigParams = $queryParameters['RTEtsConfigParams'] ?? null;
125 
126  $this->contentLanguageService->init($this->contentsLanguage);
127 
128  $tcaFieldConf = ['enableRichtext' => true];
129  if (!empty($queryParameters['P']['richtextConfigurationName'])) {
130  $tcaFieldConf['richtextConfiguration'] = $queryParameters['P']['richtextConfigurationName'];
131  }
132 
134  $richtextConfigurationProvider = GeneralUtility::makeInstance(Richtext::class);
135  $this->thisConfig = $richtextConfigurationProvider->getConfiguration(
136  $this->parameters['table'],
137  $this->parameters['fieldName'],
138  (int)$this->parameters['pid'],
139  $this->parameters['recordType'],
140  $tcaFieldConf
141  );
142  $this->buttonConfig = $this->thisConfig['buttons']['link'] ?? [];
143  }
144 
148  protected function initDocumentTemplate()
149  {
150  parent::initDocumentTemplate();
151 
152  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
153  $pageRenderer->loadRequireJsModule(
154  'TYPO3/CMS/RteCkeditor/RteLinkBrowser',
155  'function(RteLinkBrowser) {
156  RteLinkBrowser.initialize(' . GeneralUtility::quoteJSvalue($this->editorId) . ');
157  }'
158  );
159  }
160 
164  protected function initCurrentUrl()
165  {
166  if (empty($this->currentLinkParts)) {
167  return;
168  }
169 
170  if (!empty($this->currentLinkParts['url'])) {
171  $linkService = GeneralUtility::makeInstance(LinkService::class);
172  $data = $linkService->resolve($this->currentLinkParts['url']);
173  $this->currentLinkParts['type'] = $data['type'];
174  unset($data['type']);
175  $this->currentLinkParts['url'] = $data;
176  if (!empty($this->currentLinkParts['url']['parameters'])) {
177  $this->currentLinkParts['params'] = '&' . $this->currentLinkParts['url']['parameters'];
178  }
179  }
180 
181  parent::initCurrentUrl();
182  }
183 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
516  protected function getCurrentPageId()
517  {
518  return (int)$this->parameters['pid'];
519  }
520 
528  public function getConfiguration()
529  {
530  return $this->buttonConfig;
531  }
532 
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 
551  public function getUrlParameters(array $overrides = null)
552  {
553  return [
554  'act' => isset($overrides['act']) ? $overrides['act'] : $this->displayedLinkHandlerId,
555  'P' => $overrides['P'] ?? $this->parameters,
556  'editorId' => $this->editorId,
557  'contentsLanguage' => $this->contentsLanguage
558  ];
559  }
560 }
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']