[TASK] Remove unneeded parenthesis on array-access
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Extension / Language.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 SJBR\StaticInfoTables\Utility\LocalizationUtility;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
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
24 /**
25 * Language plugin for htmlArea RTE
26 */
27 class Language extends RteHtmlAreaApi
28 {
29 /**
30 * The name of the plugin registered by the extension
31 *
32 * @var string
33 */
34 protected $pluginName = 'Language';
35
36 /**
37 * The comma-separated list of button names that the registered plugin is adding to the htmlArea RTE toolbar
38 *
39 * @var string
40 */
41 protected $pluginButtons = 'lefttoright,righttoleft,language,showlanguagemarks';
42
43 /**
44 * The name-converting array, converting the button names used in the RTE PageTSConfing to the button id's used by the JS scripts
45 *
46 * @var array
47 */
48 protected $convertToolbarForHtmlAreaArray = array(
49 'lefttoright' => 'LeftToRight',
50 'righttoleft' => 'RightToLeft',
51 'language' => 'Language',
52 'showlanguagemarks' => 'ShowLanguageMarks'
53 );
54
55 /**
56 * Returns TRUE if the plugin is available and correctly initialized
57 *
58 * @param array $configuration Configuration array given from calling object down to the single plugins
59 * @return bool TRUE if this plugin object should be made available in the current environment and is correctly initialized
60 */
61 public function main(array $configuration)
62 {
63 if (!ExtensionManagementUtility::isLoaded('static_info_tables')) {
64 $this->pluginButtons = GeneralUtility::rmFromList('language', $this->pluginButtons);
65 }
66 return parent::main($configuration);
67 }
68
69 /**
70 * Return JS configuration of the htmlArea plugins registered by the extension
71 *
72 * @return string JS configuration for registered plugins
73 */
74 public function buildJavascriptConfiguration()
75 {
76 $button = 'language';
77 $jsArray = array();
78 if (!is_array($this->configuration['thisConfig']['buttons.']) || !is_array($this->configuration['thisConfig']['buttons.'][$button . '.'])) {
79 $jsArray[] = 'RTEarea[editornumber].buttons.' . $button . ' = new Object();';
80 }
81 $languages = array(
82 'none' => $this->getLanguageService()->sL(
83 'LLL:EXT:rtehtmlarea/Resources/Private/Language/Plugins/Language/locallang.xlf:No language mark'
84 ),
85 );
86 $languages = array_flip(array_merge($languages, $this->getLanguages()));
87 $languagesJSArray = array();
88 foreach ($languages as $key => $value) {
89 $languagesJSArray[] = array('text' => $key, 'value' => $value);
90 }
91 $languagesJSArray = json_encode(array('options' => $languagesJSArray));
92 $jsArray[] = 'RTEarea[editornumber].buttons.' . $button . '.dataUrl = "' . $this->writeTemporaryFile($button . '_' . $this->configuration['contentLanguageUid'], 'js', $languagesJSArray) . '";';
93 return implode(LF, $jsArray);
94 }
95
96 /**
97 * Getting all languages into an array
98 * where the key is the ISO alpha-2 code of the language
99 * and where the value are the name of the language in the current language
100 * Note: we exclude sacred and constructed languages
101 *
102 * @return array An array of names of languages
103 */
104 protected function getLanguages()
105 {
106 $databaseConnection = $this->getDatabaseConnection();
107 $nameArray = array();
108 if (ExtensionManagementUtility::isLoaded('static_info_tables')) {
109 $where = '1=1';
110 $table = 'static_languages';
111 $lang = LocalizationUtility::getCurrentLanguage();
112 $titleFields = LocalizationUtility::getLabelFields($table, $lang);
113 $prefixedTitleFields = array();
114 foreach ($titleFields as $titleField) {
115 $prefixedTitleFields[] = $table . '.' . $titleField;
116 }
117 $labelFields = implode(',', $prefixedTitleFields);
118 // Restrict to certain languages
119 if (is_array($this->configuration['thisConfig']['buttons.']) && is_array($this->configuration['thisConfig']['buttons.']['language.']) && isset($this->configuration['thisConfig']['buttons.']['language.']['restrictToItems'])) {
120 $languageList = implode('\',\'', GeneralUtility::trimExplode(',', $databaseConnection->fullQuoteStr(strtoupper($this->configuration['thisConfig']['buttons.']['language.']['restrictToItems']), $table)));
121 $where .= ' AND ' . $table . '.lg_iso_2 IN (' . $languageList . ')';
122 }
123 $res = $databaseConnection->exec_SELECTquery(
124 $table . '.lg_iso_2,' . $table . '.lg_country_iso_2,' . $labelFields,
125 $table,
126 $where . ' AND lg_constructed = 0 ' . BackendUtility::BEenableFields($table) . BackendUtility::deleteClause($table)
127 );
128 $prefixLabelWithCode = (bool)$this->configuration['thisConfig']['buttons.']['language.']['prefixLabelWithCode'];
129 $postfixLabelWithCode = (bool)$this->configuration['thisConfig']['buttons.']['language.']['postfixLabelWithCode'];
130 while ($row = $databaseConnection->sql_fetch_assoc($res)) {
131 $code = strtolower($row['lg_iso_2']) . ($row['lg_country_iso_2'] ? '-' . strtoupper($row['lg_country_iso_2']) : '');
132 foreach ($titleFields as $titleField) {
133 if ($row[$titleField]) {
134 $nameArray[$code] = $prefixLabelWithCode ? $code . ' - ' . $row[$titleField] : ($postfixLabelWithCode ? $row[$titleField] . ' - ' . $code : $row[$titleField]);
135 break;
136 }
137 }
138 }
139 $databaseConnection->sql_free_result($res);
140 uasort($nameArray, 'strcoll');
141 }
142 return $nameArray;
143 }
144
145 /**
146 * Return an updated array of toolbar enabled buttons
147 *
148 * @param array $show: array of toolbar elements that will be enabled, unless modified here
149 * @return array toolbar button array, possibly updated
150 */
151 public function applyToolbarConstraints($show)
152 {
153 if (!ExtensionManagementUtility::isLoaded('static_info_tables')) {
154 return array_diff($show, array('language'));
155 } else {
156 return $show;
157 }
158 }
159
160 /**
161 * @return DatabaseConnection
162 */
163 protected function getDatabaseConnection()
164 {
165 return $GLOBALS['TYPO3_DB'];
166 }
167 }