0577f81ec03f043c443a3002e79e89cf1c1ead36
[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
20 /**
21 * Contains translation tools
22 *
23 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
24 */
25 class TranslationConfigurationProvider {
26
27 /**
28 * Returns array of system languages
29 *
30 * The property flagIcon returns a string <flags-xx>. The calling party should call
31 * \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon(<flags-xx>) to get an HTML
32 * which will represent the flag of this language.
33 *
34 * @param int $page_id Page id (only used to get TSconfig configuration setting flag and label for default language)
35 * @param string $backPath Backpath for flags
36 * @return array Array with languages (title, uid, flagIcon - used with IconUtility::getSpriteIcon)
37 */
38 public function getSystemLanguages($page_id = 0, $backPath = '') {
39 $modSharedTSconfig = BackendUtility::getModTSconfig($page_id, 'mod.SHARED');
40 $languageIconTitles = array();
41 // fallback "old iconstyles"
42 if (preg_match('/\\.gif$/', $modSharedTSconfig['properties']['defaultLanguageFlag'])) {
43 $modSharedTSconfig['properties']['defaultLanguageFlag'] = str_replace('.gif', '', $modSharedTSconfig['properties']['defaultLanguageFlag']);
44 }
45 $languageIconTitles[0] = array(
46 'uid' => 0,
47 'title' => $modSharedTSconfig['properties']['defaultLanguageLabel'] !== ''
48 ? $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage') . ')'
49 : $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage'),
50 'ISOcode' => 'DEF',
51 'flagIcon' => $modSharedTSconfig['properties']['defaultLanguageFlag'] !== ''
52 ? 'flags-' . $modSharedTSconfig['properties']['defaultLanguageFlag']
53 : 'empty-empty'
54 );
55 // Set "All" language:
56 $languageIconTitles[-1] = array(
57 'uid' => -1,
58 'title' => $GLOBALS['LANG']->getLL('multipleLanguages'),
59 'ISOcode' => 'DEF',
60 'flagIcon' => 'flags-multiple'
61 );
62 // Find all system languages:
63 $sysLanguages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_language', '1=1' . BackendUtility::BEenableFields('sys_language'));
64 foreach ($sysLanguages as $row) {
65 $languageIconTitles[$row['uid']] = $row;
66 if (!empty($row['language_isocode'])) {
67 $languageIconTitles[$row['uid']]['ISOcode'] = $row['language_isocode'];
68 } elseif ($row['static_lang_isocode'] && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('static_info_tables')) {
69 \TYPO3\CMS\Core\Utility\GeneralUtility::deprecationLog('Usage of the field "static_lang_isocode" is discouraged, and will stop working with CMS 8. Use the built-in language field "language_isocode" in your sys_language records.');
70 $staticLangRow = BackendUtility::getRecord('static_languages', $row['static_lang_isocode'], 'lg_iso_2');
71 if ($staticLangRow['lg_iso_2']) {
72 $languageIconTitles[$row['uid']]['ISOcode'] = $staticLangRow['lg_iso_2'];
73 }
74 }
75 if ($row['flag'] !== '') {
76 $languageIconTitles[$row['uid']]['flagIcon'] = IconUtility::mapRecordTypeToSpriteIconName('sys_language', $row);
77 }
78 }
79 return $languageIconTitles;
80 }
81
82 /**
83 * Information about translation for an element
84 * Will overlay workspace version of record too!
85 *
86 * @param string $table Table name
87 * @param int $uid Record uid
88 * @param int $sys_language_uid Language uid. If zero, then all languages are selected.
89 * @param array $row The record to be translated
90 * @param array $selFieldList Select fields for the query which fetches the translations of the current record
91 * @return array Array with information. Errors will return string with message.
92 */
93 public function translationInfo($table, $uid, $sys_language_uid = 0, $row = NULL, $selFieldList = '') {
94 if ($GLOBALS['TCA'][$table] && $uid) {
95 if ($row === NULL) {
96 $row = BackendUtility::getRecordWSOL($table, $uid);
97 }
98 if (is_array($row)) {
99 $trTable = $this->getTranslationTable($table);
100 if ($trTable) {
101 if ($trTable !== $table || $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] <= 0) {
102 if ($trTable !== $table || $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] == 0) {
103 // Look for translations of this record, index by language field value:
104 $translationsTemp = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($selFieldList ? $selFieldList : 'uid,' . $GLOBALS['TCA'][$trTable]['ctrl']['languageField'], $trTable, $GLOBALS['TCA'][$trTable]['ctrl']['transOrigPointerField'] . '=' . (int)$uid . ' AND pid=' . (int)($table === 'pages' ? $row['uid'] : $row['pid']) . ' AND ' . $GLOBALS['TCA'][$trTable]['ctrl']['languageField'] . (!$sys_language_uid ? '>0' : '=' . (int)$sys_language_uid) . BackendUtility::deleteClause($trTable) . BackendUtility::versioningPlaceholderClause($trTable));
105 $translations = array();
106 $translations_errors = array();
107 foreach ($translationsTemp as $r) {
108 if (!isset($translations[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]])) {
109 $translations[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]] = $r;
110 } else {
111 $translations_errors[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]][] = $r;
112 }
113 }
114 return array(
115 'table' => $table,
116 'uid' => $uid,
117 'CType' => $row['CType'],
118 'sys_language_uid' => $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']],
119 'translation_table' => $trTable,
120 'translations' => $translations,
121 'excessive_translations' => $translations_errors
122 );
123 } else {
124 return 'Record "' . $table . '_' . $uid . '" seems to be a translation already (has a relation to record "' . $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] . '")';
125 }
126 } else {
127 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']] . '")';
128 }
129 } else {
130 return 'Translation is not supported for this table!';
131 }
132 } else {
133 return 'Record "' . $table . '_' . $uid . '" was not found';
134 }
135 } else {
136 return 'No table "' . $table . '" or no UID value';
137 }
138 }
139
140 /**
141 * Returns the table in which translations for input table is found.
142 *
143 * @param string $table The table name
144 * @return bool
145 */
146 public function getTranslationTable($table) {
147 return $this->isTranslationInOwnTable($table) ? $table : $this->foreignTranslationTable($table);
148 }
149
150 /**
151 * Returns TRUE, if the input table has localization enabled and done so with records from the same table
152 *
153 * @param string $table The table name
154 * @return bool
155 */
156 public function isTranslationInOwnTable($table) {
157 return $GLOBALS['TCA'][$table]['ctrl']['languageField'] && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] && !$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerTable'];
158 }
159
160 /**
161 * Returns foreign translation table, if any
162 *
163 * @param string $table The table name
164 * @return string Translation foreign table
165 */
166 public function foreignTranslationTable($table) {
167 $trTable = $GLOBALS['TCA'][$table]['ctrl']['transForeignTable'];
168 if ($trTable && $GLOBALS['TCA'][$trTable] && $GLOBALS['TCA'][$trTable]['ctrl']['languageField'] && $GLOBALS['TCA'][$trTable]['ctrl']['transOrigPointerField'] && $GLOBALS['TCA'][$trTable]['ctrl']['transOrigPointerTable'] === $table) {
169 return $trTable;
170 }
171 }
172
173 }