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