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