6046f27f4b67f12430123c26ef4db5ff13494973
[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' => strlen($modSharedTSconfig['properties']['defaultLanguageLabel']) ? $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage') . ')' : $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage'),
48 'ISOcode' => 'DEF',
49 'flagIcon' => strlen($modSharedTSconfig['properties']['defaultLanguageFlag']) ? 'flags-' . $modSharedTSconfig['properties']['defaultLanguageFlag'] : 'empty-empty'
50 );
51 // Set "All" language:
52 $languageIconTitles[-1] = array(
53 'uid' => -1,
54 'title' => $GLOBALS['LANG']->getLL('multipleLanguages'),
55 'ISOcode' => 'DEF',
56 'flagIcon' => 'flags-multiple'
57 );
58 // Find all system languages:
59 $sysLanguages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_language', '1=1' . BackendUtility::BEenableFields('sys_language'));
60 foreach ($sysLanguages as $row) {
61 $languageIconTitles[$row['uid']] = $row;
62 if (!empty($row['language_isocode'])) {
63 $languageIconTitles[$row['uid']]['ISOcode'] = $row['language_isocode'];
64 } elseif ($row['static_lang_isocode'] && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('static_info_tables')) {
65 \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.');
66 $staticLangRow = BackendUtility::getRecord('static_languages', $row['static_lang_isocode'], 'lg_iso_2');
67 if ($staticLangRow['lg_iso_2']) {
68 $languageIconTitles[$row['uid']]['ISOcode'] = $staticLangRow['lg_iso_2'];
69 }
70 }
71 if (strlen($row['flag'])) {
72 $languageIconTitles[$row['uid']]['flagIcon'] = IconUtility::mapRecordTypeToSpriteIconName('sys_language', $row);
73 }
74 }
75 return $languageIconTitles;
76 }
77
78 /**
79 * Information about translation for an element
80 * Will overlay workspace version of record too!
81 *
82 * @param string $table Table name
83 * @param int $uid Record uid
84 * @param int $sys_language_uid Language uid. If zero, then all languages are selected.
85 * @param array $row The record to be translated
86 * @param array $selFieldList Select fields for the query which fetches the translations of the current record
87 * @return array Array with information. Errors will return string with message.
88 */
89 public function translationInfo($table, $uid, $sys_language_uid = 0, $row = NULL, $selFieldList = '') {
90 if ($GLOBALS['TCA'][$table] && $uid) {
91 if ($row === NULL) {
92 $row = BackendUtility::getRecordWSOL($table, $uid);
93 }
94 if (is_array($row)) {
95 $trTable = $this->getTranslationTable($table);
96 if ($trTable) {
97 if ($trTable !== $table || $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] <= 0) {
98 if ($trTable !== $table || $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] == 0) {
99 // Look for translations of this record, index by language field value:
100 $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));
101 $translations = array();
102 $translations_errors = array();
103 foreach ($translationsTemp as $r) {
104 if (!isset($translations[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]])) {
105 $translations[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]] = $r;
106 } else {
107 $translations_errors[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]][] = $r;
108 }
109 }
110 return array(
111 'table' => $table,
112 'uid' => $uid,
113 'CType' => $row['CType'],
114 'sys_language_uid' => $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']],
115 'translation_table' => $trTable,
116 'translations' => $translations,
117 'excessive_translations' => $translations_errors
118 );
119 } else {
120 return 'Record "' . $table . '_' . $uid . '" seems to be a translation already (has a relation to record "' . $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] . '")';
121 }
122 } else {
123 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']] . '")';
124 }
125 } else {
126 return 'Translation is not supported for this table!';
127 }
128 } else {
129 return 'Record "' . $table . '_' . $uid . '" was not found';
130 }
131 } else {
132 return 'No table "' . $table . '" or no UID value';
133 }
134 }
135
136 /**
137 * Returns the table in which translations for input table is found.
138 *
139 * @param string $table The table name
140 * @return bool
141 */
142 public function getTranslationTable($table) {
143 return $this->isTranslationInOwnTable($table) ? $table : $this->foreignTranslationTable($table);
144 }
145
146 /**
147 * Returns TRUE, if the input table has localization enabled and done so with records from the same table
148 *
149 * @param string $table The table name
150 * @return bool
151 */
152 public function isTranslationInOwnTable($table) {
153 return $GLOBALS['TCA'][$table]['ctrl']['languageField'] && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] && !$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerTable'];
154 }
155
156 /**
157 * Returns foreign translation table, if any
158 *
159 * @param string $table The table name
160 * @return string Translation foreign table
161 */
162 public function foreignTranslationTable($table) {
163 $trTable = $GLOBALS['TCA'][$table]['ctrl']['transForeignTable'];
164 if ($trTable && $GLOBALS['TCA'][$trTable] && $GLOBALS['TCA'][$trTable]['ctrl']['languageField'] && $GLOBALS['TCA'][$trTable]['ctrl']['transOrigPointerField'] && $GLOBALS['TCA'][$trTable]['ctrl']['transOrigPointerTable'] === $table) {
165 return $trTable;
166 }
167 }
168
169 }