[TASK] RTE: Remove support for IE 8 and older
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Extension / Abbreviation.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
19 /**
20 * Abbreviation extension for htmlArea RTE
21 *
22 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
23 */
24 class Abbreviation extends \TYPO3\CMS\Rtehtmlarea\RteHtmlAreaApi {
25
26 /**
27 * The key of the extension that is extending htmlArea RTE
28 *
29 * @var string
30 */
31 protected $extensionKey = 'rtehtmlarea';
32
33 /**
34 * The name of the plugin registered by the extension
35 *
36 * @var string
37 */
38 protected $pluginName = 'Abbreviation';
39
40 /**
41 * Path to this main locallang file of the extension relative to the extension directory
42 *
43 * @var string
44 */
45 protected $relativePathToLocallangFile = '';
46
47 /**
48 * Path to the skin file relative to the extension directory
49 *
50 * @var string
51 */
52 protected $relativePathToSkin = 'Resources/Public/Css/Skin/Plugins/abbreviation.css';
53
54 /**
55 * Reference to the invoking object
56 *
57 * @var \TYPO3\CMS\Rtehtmlarea\RteHtmlAreaBase
58 */
59 protected $htmlAreaRTE;
60
61 protected $thisConfig;
62
63 // Reference to RTE PageTSConfig
64 protected $toolbar;
65
66 // Reference to RTE toolbar array
67 protected $LOCAL_LANG;
68
69 // Frontend language array
70
71 /**
72 * Comma-separated list of button names that the registered plugin is adding to the htmlArea RTE toolbar
73 *
74 * @var string
75 */
76 protected $pluginButtons = 'abbreviation';
77
78 /**
79 * Name-converting array, converting the button names used in the RTE PageTSConfing to the button id's used by the JS scripts
80 *
81 * @var array
82 */
83 protected $convertToolbarForHtmlAreaArray = array(
84 'abbreviation' => 'Abbreviation'
85 );
86
87 protected $acronymIndex = 0;
88
89 protected $abbreviationIndex = 0;
90
91 /**
92 * Returns TRUE if the plugin is available and correctly initialized
93 *
94 * @param \TYPO3\CMS\Rtehtmlarea\RteHtmlAreaBase Reference to parent object
95 * @return bool TRUE if this plugin should be made available in the current environment and is correctly initialized
96 */
97 public function main(\TYPO3\CMS\Rtehtmlarea\RteHtmlAreaBase $parentObject) {
98 // acronym button is deprecated as of TYPO3 CMS 7.0, use abbreviation instead
99 // Convert the acronym button configuration
100 if (isset($this->thisConfig['buttons.']['acronym.']) && is_array($this->thisConfig['buttons.']['acronym.'])) {
101 if (!isset($this->thisConfig['buttons.']['abbreviation.']) || !is_array($this->thisConfig['buttons.']['abbreviation.'])) {
102 $this->thisConfig['buttons.']['abbreviation.'] = $this->thisConfig['buttons.']['acronym.'];
103 }
104 unset($this->thisConfig['buttons.']['acronym.']);
105 }
106 // Convert any other reference to acronym two levels down in Page TSconfig, except in processing options
107 foreach ($parentObject->thisConfig as $key => $config) {
108 if ($key !== 'proc.') {
109 if (is_array($config)) {
110 foreach ($config as $subKey => $subConfig) {
111 $parentObject->thisConfig[$key][$subKey] = str_replace('acronym', 'abbreviation', $subConfig);
112 }
113 } else {
114 $parentObject->thisConfig[$key] = str_replace('acronym', 'abbreviation', $config);
115 }
116 }
117 }
118 // Convert any reference to acronym in special configuration options
119 if (is_array($parentObject->specConf['richtext']['parameters'])) {
120 foreach ($parentObject->specConf['richtext']['parameters'] as $key => $config) {
121 $parentObject->specConf['richtext']['parameters'][$key] = str_replace('acronym', 'abbreviation', $config);
122 }
123 }
124 // Convert any reference to acronym in user TSconfig
125 if (is_object($GLOBALS['BE_USER']) && isset($GLOBALS['BE_USER']->userTS['options.']['RTEkeyList'])) {
126 $GLOBALS['BE_USER']->userTS['options.']['RTEkeyList'] = str_replace('acronym', 'abbreviation', $GLOBALS['BE_USER']->userTS['options.']['RTEkeyList']);
127 }
128 return parent::main($parentObject) && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('static_info_tables');
129 }
130
131 /**
132 * Return JS configuration of the htmlArea plugins registered by the extension
133 *
134 * @param int Relative id of the RTE editing area in the form
135 * @return string JS configuration for registered plugins, in this case, JS configuration of block elements
136 */
137 public function buildJavascriptConfiguration($RTEcounter) {
138 $registerRTEinJavascriptString = '';
139 $button = 'abbreviation';
140 if (in_array($button, $this->toolbar)) {
141 if (!is_array($this->thisConfig['buttons.']) || !is_array($this->thisConfig['buttons.'][($button . '.')])) {
142 $registerRTEinJavascriptString .= '
143 RTEarea[' . $RTEcounter . '].buttons.' . $button . ' = new Object();';
144 }
145 $registerRTEinJavascriptString .= '
146 RTEarea[' . $RTEcounter . '].buttons.' . $button . '.abbreviationUrl = "' . $this->htmlAreaRTE->writeTemporaryFile('', ('abbreviation_' . $this->htmlAreaRTE->contentLanguageUid), 'js', $this->buildJSAbbreviationArray($this->htmlAreaRTE->contentLanguageUid)) . '";';
147 $registerRTEinJavascriptString .= '
148 RTEarea[' . $RTEcounter . '].buttons.' . $button . '.noAcronym = ' . ($this->acronymIndex ? 'false' : 'true') . ';
149 RTEarea[' . $RTEcounter . '].buttons.' . $button . '.noAbbr = ' . ($this->abbreviationIndex ? 'false' : 'true') . ';';
150 }
151 return $registerRTEinJavascriptString;
152 }
153
154 /**
155 * Return an abbreviation array for the Abbreviation plugin
156 *
157 * @return string abbreviation Javascript array
158 */
159 protected function buildJSAbbreviationArray($languageUid) {
160 $button = 'abbreviation';
161 $acronymArray = array();
162 $abbrArray = array();
163 $tableA = 'tx_rtehtmlarea_acronym';
164 $tableB = 'static_languages';
165 $fields = $tableA . '.type,' . $tableA . '.term,' . $tableA . '.acronym,' . $tableB . '.lg_iso_2,' . $tableB . '.lg_country_iso_2';
166 $tableAB = $tableA . ' LEFT JOIN ' . $tableB . ' ON ' . $tableA . '.static_lang_isocode=' . $tableB . '.uid';
167 $whereClause = '1=1';
168 $loadRecordsFromDatabase = TRUE;
169 // Get all abbreviations on pages to which the user has access
170 $lockBeUserToDBmounts = isset($this->thisConfig['buttons.'][$button . '.']['lockBeUserToDBmounts']) ? $this->thisConfig['buttons.'][$button . '.']['lockBeUserToDBmounts'] : $GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts'];
171 if (!$GLOBALS['BE_USER']->isAdmin() && $GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts'] && $lockBeUserToDBmounts) {
172 // Temporarily setting alternative web browsing mounts
173 $existingWebMounts = '';
174 $alternativeWebmountPoints = trim($GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.altElementBrowserMountPoints'));
175 $appendAlternativeWebmountPoints = trim($GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.altElementBrowserMountPoints.append'));
176 if (!empty($alternativeWebmountPoints)) {
177 $alternativeWebmountPoints = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $alternativeWebmountPoints);
178 $existingWebMounts = $GLOBALS['BE_USER']->returnWebmounts();
179 $GLOBALS['BE_USER']->setWebmounts($alternativeWebmountPoints, $appendAlternativeWebmountPoints);
180 }
181 $webMounts = $GLOBALS['BE_USER']->returnWebmounts();
182 $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
183 $recursive = isset($this->thisConfig['buttons.'][$button . '.']['recursive']) ? (int)$this->thisConfig['buttons.'][$button . '.']['recursive'] : 0;
184 if (trim($this->thisConfig['buttons.'][$button . '.']['pages'])) {
185 $pids = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->thisConfig['buttons.'][$button . '.']['pages'], TRUE);
186 foreach ($pids as $key => $val) {
187 if (!$GLOBALS['BE_USER']->isInWebMount($val, $perms_clause)) {
188 unset($pids[$key]);
189 }
190 }
191 } else {
192 $pids = $webMounts;
193 }
194 // Restoring webmounts
195 if (!empty($alternativeWebmountPoints)) {
196 $GLOBALS['BE_USER']->setWebmounts($existingWebMounts);
197 }
198 $queryGenerator = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\QueryGenerator::class);
199 $pageTree = '';
200 $pageTreePrefix = '';
201 foreach ($pids as $key => $val) {
202 if ($pageTree) {
203 $pageTreePrefix = ',';
204 }
205 $pageTree .= $pageTreePrefix . $queryGenerator->getTreeList($val, $recursive, ($begin = 0), $perms_clause);
206 }
207
208 if ($pageTree !== '') {
209 $whereClause .= ' AND ' . $tableA . '.pid IN (' . $pageTree . ')';
210 } else {
211 // If page tree is empty the user does not have access to any pages / acronyms.
212 // This is why we do not try do read any records from the database.
213 $loadRecordsFromDatabase = FALSE;
214 }
215 }
216
217 if ($loadRecordsFromDatabase) {
218 // Restrict to abbreviations applicable to the language of current content element
219 if ($this->htmlAreaRTE->contentLanguageUid > -1) {
220 $whereClause .= ' AND (' . $tableA . '.sys_language_uid=' . $this->htmlAreaRTE->contentLanguageUid . ' OR ' . $tableA . '.sys_language_uid=-1) ';
221 }
222 // Restrict to abbreviations in certain languages
223 if (is_array($this->thisConfig['buttons.']) && is_array($this->thisConfig['buttons.']['language.']) && isset($this->thisConfig['buttons.']['language.']['restrictToItems'])) {
224 $languageList = implode('\',\'', \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_DB']->fullQuoteStr(strtoupper($this->thisConfig['buttons.']['language.']['restrictToItems']), $tableB)));
225 $whereClause .= ' AND ' . $tableB . '.lg_iso_2 IN (' . $languageList . ') ';
226 }
227 $whereClause .= BackendUtility::BEenableFields($tableA);
228 $whereClause .= BackendUtility::deleteClause($tableA);
229 $whereClause .= BackendUtility::BEenableFields($tableB);
230 $whereClause .= BackendUtility::deleteClause($tableB);
231 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, $tableAB, $whereClause);
232 while ($abbreviationRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
233 $item = array('term' => $abbreviationRow['term'], 'abbr' => $abbreviationRow['acronym'], 'language' => strtolower($abbreviationRow['lg_iso_2']) . ($abbreviationRow['lg_country_iso_2'] ? '-' . $abbreviationRow['lg_country_iso_2'] : ''));
234 if ($abbreviationRow['type'] == 1) {
235 $acronymArray[] = $item;
236 } elseif ($abbreviationRow['type'] == 2) {
237 $abbrArray[] = $item;
238 }
239 }
240 }
241
242 $this->acronymIndex = count($acronymArray);
243 $this->abbreviationIndex = count($abbrArray);
244 return json_encode(array('abbr' => $abbrArray, 'acronym' => $acronymArray));
245 }
246
247 }