TYPO3 CMS  TYPO3_7-6
Abbreviation.php
Go to the documentation of this file.
1 <?php
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 
24 
29 {
35  protected $pluginName = 'Abbreviation';
36 
42  protected $pluginButtons = 'abbreviation';
43 
50  'abbreviation' => 'Abbreviation'
51  ];
52 
58  protected $acronymIndex = 0;
59 
65  protected $abbreviationIndex = 0;
66 
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 
129  {
130  $button = 'abbreviation';
131  $jsArray = [];
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 
148  protected function buildJSAbbreviationArray()
149  {
150  $database = $this->getDatabaseConnection();
151  $backendUser = $this->getBackendUserAuthentication();
152  $button = 'abbreviation';
153  $acronymArray = [];
154  $abbrArray = [];
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 = ['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(['abbr' => $abbrArray, 'acronym' => $acronymArray]);
233  }
234 
238  protected function getDatabaseConnection()
239  {
240  return $GLOBALS['TYPO3_DB'];
241  }
242 
246  protected function getBackendUserAuthentication()
247  {
248  return $GLOBALS['BE_USER'];
249  }
250 }
static intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
$database
Definition: server.php:40
static BEenableFields($table, $inv=false)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
writeTemporaryFile($label, $fileExtension='js', $contents='')
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static deleteClause($table, $tableAlias='')