[CLEANUP] TranslationConfigurationProvider
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Configuration / TranslationConfigurationProvider.php
1 <?php
2 namespace TYPO3\CMS\Backend\Configuration;
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\Backend\Utility\IconUtility;
19 use TYPO3\CMS\Core\Database\DatabaseConnection;
20 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
21 use TYPO3\CMS\Lang\LanguageService;
22
23 /**
24 * Contains translation tools
25 *
26 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
27 */
28 class TranslationConfigurationProvider {
29
30 /**
31 * @return DatabaseConnection
32 */
33 protected function getDatabaseConnection() {
34 return $GLOBALS['TYPO3_DB'];
35 }
36
37 /**
38 * @return LanguageService
39 */
40 protected function getLanguageService() {
41 return $GLOBALS['LANG'];
42 }
43
44 /**
45 * Returns array of system languages
46 *
47 * The property flagIcon returns a string <flags-xx>. The calling party should call
48 * \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon(<flags-xx>) to get an HTML
49 * which will represent the flag of this language.
50 *
51 * @param int $pageId Page id (used to get TSconfig configuration setting flag and label for default language)
52 * @return array Array with languages (uid, title, ISOcode, flagIcon)
53 */
54 public function getSystemLanguages($pageId = 0) {
55 $modSharedTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.SHARED');
56
57 // default language and "all languages" are always present
58 $languages = array(
59 // 0: default language
60 0 => array(
61 'uid' => 0,
62 'title' => $this->getDefaultLanguageLabel($modSharedTSconfig),
63 'ISOcode' => 'DEF',
64 'flagIcon' => $this->getDefaultLanguageFlag($modSharedTSconfig),
65 ),
66 // -1: all languages
67 -1 => array(
68 'uid' => -1,
69 'title' => $this->getLanguageService()->getLL('multipleLanguages'),
70 'ISOcode' => 'DEF',
71 'flagIcon' => 'flags-multiple',
72 ),
73 );
74
75 // add the additional languages from database records
76 $languageRecords = $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'sys_language', '');
77 foreach ($languageRecords as $languageRecord) {
78 $languages[$languageRecord['uid']] = $languageRecord;
79 if ($languageRecord['static_lang_isocode'] && ExtensionManagementUtility::isLoaded('static_info_tables')) {
80 $staticLangRow = BackendUtility::getRecord('static_languages', $languageRecord['static_lang_isocode'], 'lg_iso_2');
81 if ($staticLangRow['lg_iso_2']) {
82 $languages[$languageRecord['uid']]['ISOcode'] = $staticLangRow['lg_iso_2'];
83 }
84 }
85 if ($languageRecord['flag'] !== '') {
86 $languages[$languageRecord['uid']]['flagIcon'] = IconUtility::mapRecordTypeToSpriteIconName('sys_language', $languageRecord);
87 }
88 }
89 return $languages;
90 }
91
92 /**
93 * Information about translation for an element
94 * Will overlay workspace version of record too!
95 *
96 * @param string $table Table name
97 * @param int $uid Record uid
98 * @param int $languageUid Language uid. If 0, then all languages are selected.
99 * @param array $row The record to be translated
100 * @param string $selFieldList Select fields for the query which fetches the translations of the current record
101 * @return mixed Array with information or error message as a string.
102 */
103 public function translationInfo($table, $uid, $languageUid = 0, array $row = NULL, $selFieldList = '') {
104 if (!$GLOBALS['TCA'][$table] || !$uid) {
105 return 'No table "' . $table . '" or no UID value';
106 }
107 if ($row === NULL) {
108 $row = BackendUtility::getRecordWSOL($table, $uid);
109 }
110 if (!is_array($row)) {
111 return 'Record "' . $table . '_' . $uid . '" was not found';
112 }
113 $translationTable = $this->getTranslationTable($table);
114 if ($translationTable === '') {
115 return 'Translation is not supported for this table!';
116 }
117 if ($translationTable === $table && $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0) {
118 return 'Record "' . $table . '_' . $uid . '" seems to be a translation already (has a language value "' . $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] . '", relation to record "' . $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] . '")';
119 }
120 if ($translationTable === $table && $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] != 0) {
121 return 'Record "' . $table . '_' . $uid . '" seems to be a translation already (has a relation to record "' . $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] . '")';
122 }
123 // Look for translations of this record, index by language field value:
124 if (!$selFieldList) {
125 $selFieldList = 'uid,' . $GLOBALS['TCA'][$translationTable]['ctrl']['languageField'];
126 }
127 $where = $GLOBALS['TCA'][$translationTable]['ctrl']['transOrigPointerField'] . '=' . (int)$uid .
128 ' AND pid=' . (int)($table === 'pages' ? $row['uid'] : $row['pid']) .
129 ' AND ' . $GLOBALS['TCA'][$translationTable]['ctrl']['languageField'] . (! $languageUid ? '>0' : '=' . (int)$languageUid) .
130 BackendUtility::deleteClause($translationTable) .
131 BackendUtility::versioningPlaceholderClause($translationTable);
132 $translationRecords = $this->getDatabaseConnection()->exec_SELECTgetRows($selFieldList, $translationTable, $where);
133 $translations = array();
134 $translationsErrors = array();
135 foreach ($translationRecords as $translationRecord) {
136 if (!isset($translations[$translationRecord[$GLOBALS['TCA'][$translationTable]['ctrl']['languageField']]])) {
137 $translations[$translationRecord[$GLOBALS['TCA'][$translationTable]['ctrl']['languageField']]] = $translationRecord;
138 } else {
139 $translationsErrors[$translationRecord[$GLOBALS['TCA'][$translationTable]['ctrl']['languageField']]][] = $translationRecord;
140 }
141 }
142 return array(
143 'table' => $table,
144 'uid' => $uid,
145 'CType' => $row['CType'],
146 'sys_language_uid' => $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']],
147 'translation_table' => $translationTable,
148 'translations' => $translations,
149 'excessive_translations' => $translationsErrors
150 );
151 }
152
153 /**
154 * Returns the table in which translations for input table is found.
155 *
156 * @param string $table The table name
157 * @return string
158 */
159 public function getTranslationTable($table) {
160 return $this->isTranslationInOwnTable($table) ? $table : $this->foreignTranslationTable($table);
161 }
162
163 /**
164 * Returns TRUE, if the input table has localization enabled and done so with records from the same table
165 *
166 * @param string $table The table name
167 * @return bool
168 */
169 public function isTranslationInOwnTable($table) {
170 return $GLOBALS['TCA'][$table]['ctrl']['languageField'] && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] && !$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerTable'];
171 }
172
173 /**
174 * Returns foreign translation table, if any
175 *
176 * @param string $table The table name
177 * @return string Translation foreign table
178 */
179 public function foreignTranslationTable($table) {
180 $translationTable = $GLOBALS['TCA'][$table]['ctrl']['transForeignTable'];
181 if (
182 !$translationTable ||
183 !$GLOBALS['TCA'][$translationTable] ||
184 !$GLOBALS['TCA'][$translationTable]['ctrl']['languageField'] ||
185 !$GLOBALS['TCA'][$translationTable]['ctrl']['transOrigPointerField'] ||
186 $GLOBALS['TCA'][$translationTable]['ctrl']['transOrigPointerTable'] !== $table
187 ) {
188 $translationTable = '';
189 }
190 return $translationTable;
191 }
192
193 /**
194 * @param array $modSharedTSconfig
195 * @return string
196 */
197 protected function getDefaultLanguageFlag(array $modSharedTSconfig) {
198 if (strlen($modSharedTSconfig['properties']['defaultLanguageFlag'])) {
199 // fallback "old iconstyles"
200 if (preg_match('/\\.gif$/', $modSharedTSconfig['properties']['defaultLanguageFlag'])) {
201 $modSharedTSconfig['properties']['defaultLanguageFlag'] = str_replace('.gif', '', $modSharedTSconfig['properties']['defaultLanguageFlag']);
202 }
203 $defaultLanguageFlag = 'flags-' . $modSharedTSconfig['properties']['defaultLanguageFlag'];
204 } else {
205 $defaultLanguageFlag = 'empty-empty';
206 }
207 return $defaultLanguageFlag;
208 }
209
210 /**
211 * @param array $modSharedTSconfig
212 * @return string
213 */
214 protected function getDefaultLanguageLabel(array $modSharedTSconfig) {
215 if (strlen($modSharedTSconfig['properties']['defaultLanguageLabel'])) {
216 $defaultLanguageLabel = $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $this->getLanguageService()->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage') . ')';
217 } else {
218 $defaultLanguageLabel = $this->getLanguageService()->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage');
219 }
220 return $defaultLanguageLabel;
221 }
222
223 }