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