TYPO3  7.6
AbstractLinkBrowserController.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Recordlist\Controller;
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 
28 
33 {
37  protected $doc;
38 
42  protected $parameters;
43 
49  protected $thisScript = '';
50 
54  protected $linkHandlers = [];
55 
63  protected $currentLinkParts = [];
64 
71 
78 
85 
93  protected $displayedLinkHandlerId = '';
94 
100  protected $linkAttributeFields = [];
101 
107  protected $linkAttributeValues = [];
108 
112  protected $hookObjects = [];
113 
117  public function __construct()
118  {
119  $this->initHookObjects();
120  $this->init();
121  }
122 
128  protected function init()
129  {
130  $this->getLanguageService()->includeLLFile('EXT:lang/locallang_browse_links.xlf');
131  }
132 
139  protected function initHookObjects()
140  {
141  if (
142  isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['LinkBrowser']['hooks'])
143  && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['LinkBrowser']['hooks'])
144  ) {
145  $hooks = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies(
146  $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['LinkBrowser']['hooks']
147  );
148  foreach ($hooks as $key => $hook) {
149  $this->hookObjects[] = GeneralUtility::makeInstance($hook['handler']);
150  }
151  }
152  }
153 
163  {
164  $this->determineScriptUrl($request);
165  $this->initVariables($request);
166  $this->loadLinkHandlers();
167  $this->initCurrentUrl();
168 
169  $menuData = $this->buildMenuArray();
170  $renderLinkAttributeFields = $this->renderLinkAttributeFields();
171  $browserContent = $this->displayedLinkHandler->render($request);
172 
173  $this->initDocumentTemplate();
174  $content = $this->doc->startPage('Link Browser');
175  $content .= $this->doc->getFlashMessages();
176 
177  if (!empty($this->currentLinkParts)) {
178  $content .= $this->renderCurrentUrl();
179  }
180  $content .= $this->doc->getTabMenuRaw($menuData);
181  $content .= $renderLinkAttributeFields;
182 
183  $content .= '<div class="linkBrowser-tabContent">' . $browserContent . '</div>';
184  $content .= $this->doc->endPage();
185 
186  $response->getBody()->write($this->doc->insertStylesAndJS($content));
187  return $response;
188  }
189 
198  protected function determineScriptUrl(ServerRequestInterface $request)
199  {
200  if ($routePath = $request->getQueryParams()['route']) {
201  $router = GeneralUtility::makeInstance(Router::class);
202  $route = $router->match($routePath);
203  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
204  $this->thisScript = (string)$uriBuilder->buildUriFromRoute($route->getOption('_identifier'));
205  } elseif ($moduleName = $request->getQueryParams()['M']) {
206  $this->thisScript = BackendUtility::getModuleUrl($moduleName);
207  } else {
208  $this->thisScript = GeneralUtility::getIndpEnv('SCRIPT_NAME');
209  }
210  }
211 
215  protected function initVariables(ServerRequestInterface $request)
216  {
217  $queryParams = $request->getQueryParams();
218  $act = isset($queryParams['act']) ? $queryParams['act'] : '';
219  // @deprecated since CMS 7, remove with CMS 8
220  if (strpos($act, '|')) {
221  GeneralUtility::deprecationLog('Using multiple values for the "act" parameter in the link wizard is deprecated. Only a single value is allowed. Values were: ' . $act);
222  $act = array_shift(explode('|', $act));
223  }
224  $this->displayedLinkHandlerId = $act;
225  $this->parameters = isset($queryParams['P']) ? $queryParams['P'] : [];
226  $this->linkAttributeValues = isset($queryParams['linkAttributes']) ? $queryParams['linkAttributes'] : [];
227  }
228 
233  protected function loadLinkHandlers()
234  {
235  $linkHandlers = $this->getLinkHandlers();
236  if (empty($linkHandlers)) {
237  throw new \UnexpectedValueException('No link handlers are configured. Check page TSconfig TCEMAIN.linkHandlers.', 1442787911);
238  }
239 
240  $lang = $this->getLanguageService();
241  foreach ($linkHandlers as $identifier => $configuration) {
242  $identifier = rtrim($identifier, '.');
244  $handler = GeneralUtility::makeInstance($configuration['handler']);
245  $handler->initialize(
246  $this,
247  $identifier,
248  isset($configuration['configuration.']) ? $configuration['configuration.'] : []
249  );
250 
251  $this->linkHandlers[$identifier] = [
252  'handlerInstance' => $handler,
253  'label' => $lang->sL($configuration['label'], true),
254  'displayBefore' => isset($configuration['displayBefore']) ? GeneralUtility::trimExplode(',', $configuration['displayBefore']) : [],
255  'displayAfter' => isset($configuration['displayAfter']) ? GeneralUtility::trimExplode(',', $configuration['displayAfter']) : [],
256  'scanBefore' => isset($configuration['scanBefore']) ? GeneralUtility::trimExplode(',', $configuration['scanBefore']) : [],
257  'scanAfter' => isset($configuration['scanAfter']) ? GeneralUtility::trimExplode(',', $configuration['scanAfter']) : [],
258  'addParams' => isset($configuration['addParams']) ? $configuration['addParams'] : '',
259  ];
260  }
261  }
262 
268  protected function getLinkHandlers()
269  {
270  $pageTSconfig = BackendUtility::getPagesTSconfig($this->getCurrentPageId());
271  $pageTSconfig = $this->getBackendUser()->getTSConfig('TCEMAIN.linkHandler.', $pageTSconfig);
272  $linkHandlers = (array)$pageTSconfig['properties'];
273 
274  foreach ($this->hookObjects as $hookObject) {
275  if (method_exists($hookObject, 'modifyLinkHandlers')) {
276  $linkHandlers = $hookObject->modifyLinkHandlers($linkHandlers, $this->currentLinkParts);
277  }
278  }
279 
280  return $linkHandlers;
281  }
282 
288  protected function initCurrentUrl()
289  {
290  if (empty($this->currentLinkParts)) {
291  return;
292  }
293 
294  $orderedHandlers = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($this->linkHandlers, 'scanBefore', 'scanAfter');
295 
296  // find responsible handler for current link
297  foreach ($orderedHandlers as $key => $configuration) {
299  $handler = $configuration['handlerInstance'];
300  if ($handler->canHandleLink($this->currentLinkParts)) {
301  $this->currentLinkHandler = $handler;
302  $this->currentLinkHandlerId = $key;
303  break;
304  }
305  }
306  // reset the link if we have no handler for it
307  if (!$this->currentLinkHandler) {
308  $this->currentLinkParts = [];
309  }
310 
311  // overwrite any preexisting
312  foreach ($this->currentLinkParts as $key => $part) {
313  if ($key !== 'url') {
314  $this->linkAttributeValues[$key] = $part;
315  }
316  }
317  }
318 
324  protected function initDocumentTemplate()
325  {
326  $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
327  $this->doc->bodyTagId = 'typo3-browse-links-php';
328 
329  foreach ($this->getBodyTagAttributes() as $attributeName => $value) {
330  $this->doc->bodyTagAdditions .= ' ' . $attributeName . '="' . htmlspecialchars($value) . '"';
331  }
332 
333  // Finally, add the accumulated JavaScript to the template object:
334  // also unset the default jumpToUrl() function before
335  unset($this->doc->JScodeArray['jumpToUrl']);
336  }
337 
343  protected function renderCurrentUrl()
344  {
345  return '<!-- Print current URL -->
346  <table border="0" cellpadding="0" cellspacing="0" id="typo3-curUrl">
347  <tr>
348  <td>' . $this->getLanguageService()->getLL('currentLink', true) . ': ' . htmlspecialchars($this->currentLinkHandler->formatCurrentUrl()) . '</td>
349  </tr>
350  </table>';
351  }
352 
358  protected function buildMenuArray()
359  {
360  $allowedItems = $this->getAllowedItems();
361  if ($this->displayedLinkHandlerId && !in_array($this->displayedLinkHandlerId, $allowedItems, true)) {
362  $this->displayedLinkHandlerId = '';
363  }
364 
365  $allowedHandlers = array_flip($allowedItems);
366  $menuDef = array();
367  foreach ($this->linkHandlers as $identifier => $configuration) {
368  if (!isset($allowedHandlers[$identifier])) {
369  continue;
370  }
371 
373  $handlerInstance = $configuration['handlerInstance'];
374  $isActive = $this->displayedLinkHandlerId === $identifier || !$this->displayedLinkHandlerId && $handlerInstance === $this->currentLinkHandler;
375  if ($isActive) {
376  $this->displayedLinkHandler = $handlerInstance;
377  if (!$this->displayedLinkHandlerId) {
378  $this->displayedLinkHandlerId = $this->currentLinkHandlerId;
379  }
380  }
381 
382  if ($configuration['addParams']) {
383  $addParams = $configuration['addParams'];
384  } else {
385  $parameters = GeneralUtility::implodeArrayForUrl('', $this->getUrlParameters(['act' => $identifier]));
386  $addParams = 'onclick="jumpToUrl(' . GeneralUtility::quoteJSvalue('?' . ltrim($parameters, '&')) . ');return false;"';
387  }
388  $menuDef[$identifier] = [
389  'isActive' => $isActive,
390  'label' => $configuration['label'],
391  'url' => '#',
392  'addParams' => $addParams,
393  'before' => $configuration['displayBefore'],
394  'after' => $configuration['displayAfter']
395  ];
396  }
397 
398  $menuDef = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($menuDef);
399 
400  // if there is no active tab
401  if (!$this->displayedLinkHandler) {
402  // empty the current link
403  $this->currentLinkParts = [];
404  $this->currentLinkHandler = null;
405  $this->currentLinkHandler = '';
406  // select first tab
407  reset($menuDef);
408  $this->displayedLinkHandlerId = key($menuDef);
409  $this->displayedLinkHandler = $this->linkHandlers[$this->displayedLinkHandlerId]['handlerInstance'];
410  $menuDef[$this->displayedLinkHandlerId]['isActive'] = true;
411  }
412 
413  return $menuDef;
414  }
415 
421  protected function getAllowedItems()
422  {
423  $allowedItems = array_keys($this->linkHandlers);
424 
425  foreach ($this->hookObjects as $hookObject) {
426  if (method_exists($hookObject, 'modifyAllowedItems')) {
427  $allowedItems = $hookObject->modifyAllowedItems($allowedItems, $this->currentLinkParts);
428  }
429  }
430 
431  // Initializing the action value, possibly removing blinded values etc:
432  $blindLinkOptions = isset($this->parameters['params']['blindLinkOptions'])
433  ? GeneralUtility::trimExplode(',', $this->parameters['params']['blindLinkOptions'])
434  : [];
435  $allowedItems = array_diff($allowedItems, $blindLinkOptions);
436 
437  return $allowedItems;
438  }
439 
445  protected function getAllowedLinkAttributes()
446  {
447  $allowedLinkAttributes = $this->displayedLinkHandler->getLinkAttributes();
448 
449  // Removing link fields if configured
450  $blindLinkFields = isset($this->parameters['params']['blindLinkFields'])
451  ? GeneralUtility::trimExplode(',', $this->parameters['params']['blindLinkFields'], true)
452  : [];
453  $allowedLinkAttributes = array_diff($allowedLinkAttributes, $blindLinkFields);
454 
455  return $allowedLinkAttributes;
456  }
457 
463  public function renderLinkAttributeFields()
464  {
465  $fieldRenderingDefinitions = $this->getLinkAttributeFieldDefinitions();
466 
467  $fieldRenderingDefinitions = $this->displayedLinkHandler->modifyLinkAttributes($fieldRenderingDefinitions);
468 
469  $this->linkAttributeFields = $this->getAllowedLinkAttributes();
470 
471  $content = '';
472  foreach ($this->linkAttributeFields as $attribute) {
473  $content .= $fieldRenderingDefinitions[$attribute];
474  }
475 
476  // add update button if appropriate
477  if (!empty($this->currentLinkParts) && $this->displayedLinkHandler === $this->currentLinkHandler && $this->currentLinkHandler->isUpdateSupported()) {
478  $content .= '
479  <form action="" name="lparamsform" id="lparamsform">
480  <table border="0" cellpadding="2" cellspacing="1" id="typo3-linkParams">
481  <tr><td>
482  <input class="btn btn-default t3js-linkCurrent" type="submit" value="' . $this->getLanguageService()->getLL('update', true) . '" />
483  </td></tr>
484  </table>
485  </form><br /><br />';
486  }
487 
488  return $content;
489  }
490 
496  protected function getLinkAttributeFieldDefinitions()
497  {
498  $lang = $this->getLanguageService();
499 
500  $fieldRenderingDefinitions = [];
501  $fieldRenderingDefinitions['target'] = '
502  <!--
503  Selecting target for link:
504  -->
505  <form action="" name="ltargetform" id="ltargetform">
506  <table border="0" cellpadding="2" cellspacing="1" id="typo3-linkTarget">
507  <tr>
508  <td style="width: 96px;">' . $lang->getLL('target', true) . ':</td>
509  <td>
510  <input type="text" name="ltarget" class="t3js-linkTarget" value="' . htmlspecialchars($this->linkAttributeValues['target']) . '" />
511  <select name="ltarget_type" class="t3js-targetPreselect">
512  <option value=""></option>
513  <option value="_top">' . $lang->getLL('top', true) . '</option>
514  <option value="_blank">' . $lang->getLL('newWindow', true) . '</option>
515  </select>
516  </td>
517  </tr>
518  </table>
519  </form>';
520 
521  $fieldRenderingDefinitions['title'] = '
522  <!--
523  Selecting title for link:
524  -->
525  <form action="" name="ltitleform" id="ltitleform">
526  <table border="0" cellpadding="2" cellspacing="1" id="typo3-linkTitle">
527  <tr>
528  <td style="width: 96px;">' . $lang->getLL('title', true) . '</td>
529  <td><input type="text" name="ltitle" class="typo3-link-input" value="' . htmlspecialchars($this->linkAttributeValues['title']) . '" /></td>
530  </tr>
531  </table>
532  </form>
533  ';
534 
535  $fieldRenderingDefinitions['class'] = '
536  <!--
537  Selecting class for link:
538  -->
539  <form action="" name="lclassform" id="lclassform">
540  <table border="0" cellpadding="2" cellspacing="1" id="typo3-linkClass">
541  <tr>
542  <td style="width: 96px;">' . $lang->getLL('class', true) . '</td>
543  <td><input type="text" name="lclass" class="typo3-link-input" value="' . htmlspecialchars($this->linkAttributeValues['class']) . '" /></td>
544  </tr>
545  </table>
546  </form>
547  ';
548 
549  $fieldRenderingDefinitions['params'] = '
550  <!--
551  Selecting params for link:
552  -->
553  <form action="" name="lparamsform" id="lparamsform">
554  <table border="0" cellpadding="2" cellspacing="1" id="typo3-linkParams">
555  <tr>
556  <td style="width: 96px;">' . $lang->getLL('params', true) . '</td>
557  <td><input type="text" name="lparams" class="typo3-link-input" value="' . htmlspecialchars($this->linkAttributeValues['params']) . '" /></td>
558  </tr>
559  </table>
560  </form>
561  ';
562 
563  return $fieldRenderingDefinitions;
564  }
565 
571  public function getUrlParameters(array $overrides = null)
572  {
573  return [
574  'act' => isset($overrides['act']) ? $overrides['act'] : $this->displayedLinkHandlerId
575  ];
576  }
577 
583  protected function getBodyTagAttributes()
584  {
585  $parameters = [];
586  $parameters['uid'] = $this->parameters['uid'];
587  $parameters['pid'] = $this->parameters['pid'];
588  $parameters['itemName'] = $this->parameters['itemName'];
589  $parameters['formName'] = $this->parameters['formName'];
590  $parameters['params']['allowedExtensions'] = isset($this->parameters['params']['allowedExtensions']) ? $this->parameters['params']['allowedExtensions'] : '';
591  $parameters['params']['blindLinkOptions'] = isset($this->parameters['params']['blindLinkOptions']) ? $this->parameters['params']['blindLinkOptions'] : '';
592  $parameters['params']['blindLinkFields'] = isset($this->parameters['params']['blindLinkFields']) ? $this->parameters['params']['blindLinkFields']: '';
593  $addPassOnParams = GeneralUtility::implodeArrayForUrl('P', $parameters);
594 
595  $attributes = $this->displayedLinkHandler->getBodyTagAttributes();
596  return array_merge(
597  $attributes,
598  [
599  'data-this-script-url' => strpos($this->thisScript, '?') === false ? $this->thisScript . '?' : $this->thisScript . '&',
600  'data-url-parameters' => json_encode($this->getUrlParameters()),
601  'data-parameters' => json_encode($this->parameters),
602  'data-add-on-params' => $addPassOnParams,
603  'data-link-attribute-fields' => json_encode($this->linkAttributeFields)
604  ]
605  );
606  }
607 
613  abstract protected function getCurrentPageId();
614 
618  public function getParameters()
619  {
620  return $this->parameters;
621  }
622 
628  public function getConfiguration() {
629  return [];
630  }
631 
635  public function getDisplayedLinkHandlerId()
636  {
638  }
639 
643  public function getScriptUrl()
644  {
645  return $this->thisScript;
646  }
647 
651  protected function getLanguageService()
652  {
653  return $GLOBALS['LANG'];
654  }
655 
659  protected function getBackendUser()
660  {
661  return $GLOBALS['BE_USER'];
662  }
663 }