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