[TASK] Always keep <a> instead of <link> tags in RTE transformations
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Extension / Typo3Link.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\Extension;
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\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\PathUtility;
20 use TYPO3\CMS\Rtehtmlarea\RteHtmlAreaApi;
21
22 /**
23 * TYPO3Link plugin for htmlArea RTE
24 */
25 class Typo3Link extends RteHtmlAreaApi
26 {
27 /**
28 * The name of the plugin registered by the extension
29 *
30 * @var string
31 */
32 protected $pluginName = 'TYPO3Link';
33
34 /**
35 * The comma-separated list of button names that the registered plugin is adding to the htmlArea RTE toolbar
36 *
37 * @var string
38 */
39 protected $pluginButtons = 'link, unlink';
40
41 /**
42 * The name-converting array, converting the button names used in the RTE PageTSConfing to the button id's used by the JS scripts
43 *
44 * @var array
45 */
46 protected $convertToolbarForHtmlAreaArray = [
47 'link' => 'CreateLink',
48 'unlink' => 'UnLink'
49 ];
50
51 /**
52 * Returns TRUE if the plugin is available and correctly initialized
53 *
54 * @param array $configuration Configuration array given from calling object down to the single plugins
55 * @return bool TRUE if this plugin object should be made available in the current environment and is correctly initialized
56 */
57 public function main(array $configuration)
58 {
59 // Check if this should be enabled based on Page TSConfig
60 return parent::main($configuration)
61 && !$this->configuration['thisConfig']['buttons.']['link.']['TYPO3Browser.']['disabled'];
62 }
63
64 /**
65 * Return JS configuration of the htmlArea plugins registered by the extension
66 *
67 * @return string JS configuration for registered plugins, in this case, JS configuration of block elements
68 */
69 public function buildJavascriptConfiguration()
70 {
71 $jsArray = [];
72 $button = 'link';
73 if (in_array($button, $this->toolbar)) {
74 if (!is_array($this->configuration['thisConfig']['buttons.']) || !is_array($this->configuration['thisConfig']['buttons.'][$button . '.'])) {
75 $jsArray[] = 'RTEarea[editornumber].buttons.' . $button . ' = new Object();';
76 }
77 $jsArray[] = 'RTEarea[editornumber].buttons.' . $button . '.pathLinkModule = ' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('rtehtmlarea_wizard_browse_links')) . ';';
78 if (is_array($this->configuration['RTEsetup']['properties']['classesAnchor.'])) {
79 $jsArray[] = 'RTEarea[editornumber].buttons.' . $button . '.classesAnchorUrl = "' . $this->writeTemporaryFile('classesAnchor_' . $this->configuration['contentLanguageUid'], 'js', $this->buildJSClassesAnchorArray()) . '";';
80 }
81 $jsArray[] = 'RTEarea[editornumber].buttons.' . $button . '.additionalAttributes = "' . ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extensionKey]['plugins'][$this->pluginName]['additionalAttributes'] ? ',' . $GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extensionKey]['plugins'][$this->pluginName]['additionalAttributes'] : '') . '";';
82 }
83 return implode(LF, $jsArray);
84 }
85
86 /**
87 * Return a JS array for special anchor classes
88 *
89 * @return string classesAnchor array definition
90 */
91 public function buildJSClassesAnchorArray()
92 {
93 $JSClassesAnchorArray = 'HTMLArea.classesAnchorSetup = [ ' . LF;
94 $classesAnchorIndex = 0;
95 foreach ($this->configuration['RTEsetup']['properties']['classesAnchor.'] as $label => $conf) {
96 if (is_array($conf) && $conf['class']) {
97 $JSClassesAnchorArray .= ($classesAnchorIndex++ ? ',' : '') . ' { ' . LF;
98 $index = 0;
99 $JSClassesAnchorArray .= ($index++ ? ',' : '') . 'name : "' . str_replace('"', '', str_replace('\'', '', $conf['class'])) . '"' . LF;
100 if ($conf['type']) {
101 $JSClassesAnchorArray .= ($index++ ? ',' : '') . 'type : "' . str_replace('"', '', str_replace('\'', '', $conf['type'])) . '"' . LF;
102 }
103 if (trim(str_replace('\'', '', str_replace('"', '', $conf['image'])))) {
104 $JSClassesAnchorArray .= ($index++ ? ',' : '') . 'image : "' . GeneralUtility::locationHeaderUrl(PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName(trim(str_replace('\'', '', str_replace('"', '', $conf['image'])))))) . '"' . LF;
105 }
106 $JSClassesAnchorArray .= ($index++ ? ',' : '') . 'addIconAfterLink : ' . ($conf['addIconAfterLink'] ? 'true' : 'false') . LF;
107 if (trim($conf['altText'])) {
108 $string = GeneralUtility::quoteJSvalue($this->getLanguageService()->sL(trim($conf['altText'])));
109 $JSClassesAnchorArray .= ($index++ ? ',' : '') . 'altText : ' . str_replace('"', '\\"', str_replace('\\\'', '\'', $string)) . LF;
110 }
111 if (trim($conf['titleText'])) {
112 $string = GeneralUtility::quoteJSvalue($this->getLanguageService()->sL(trim($conf['titleText'])));
113 $JSClassesAnchorArray .= ($index++ ? ',' : '') . 'titleText : ' . str_replace('"', '\\"', str_replace('\\\'', '\'', $string)) . LF;
114 }
115 if (trim($conf['target'])) {
116 $JSClassesAnchorArray .= ($index++ ? ',' : '') . 'target : "' . trim($conf['target']) . '"' . LF;
117 }
118 $JSClassesAnchorArray .= '}' . LF;
119 }
120 }
121 $JSClassesAnchorArray .= '];' . LF;
122 return $JSClassesAnchorArray;
123 }
124
125 /**
126 * Return an updated array of toolbar enabled buttons
127 *
128 * @param array $show: array of toolbar elements that will be enabled, unless modified here
129 * @return array toolbar button array, possibly updated
130 */
131 public function applyToolbarConstraints($show)
132 {
133 // We will not allow unlink if link is not enabled
134 if (!in_array('link', $show)) {
135 return array_diff($show, GeneralUtility::trimExplode(',', $this->pluginButtons));
136 } else {
137 return $show;
138 }
139 }
140 }