[BUGFIX] t3lib_BEfunc::getViewDomain() returns wrong or no domain
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_transl8tools.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2006-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains translation tools
29 *
30 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
31 */
32
33
34 /**
35 * Contains translation tools
36 *
37 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
38 * @package TYPO3
39 * @subpackage t3lib
40 */
41 class t3lib_transl8tools {
42
43 /**
44 * Returns array of system languages
45 *
46 * Since TYPO3 4.5 the flagIcon is not returned as a filename in "gfx/flags/*" anymore,
47 * but as a string <flags-xx>. The calling party should call
48 * t3lib_iconWorks::getSpriteIcon(<flags-xx>) to get an HTML which will represent
49 * the flag of this language.
50 *
51 * @param integer page id (only used to get TSconfig configuration setting flag and label for default language)
52 * @param string Backpath for flags
53 * @return array Array with languages (title, uid, flagIcon)
54 */
55 function getSystemLanguages($page_id = 0, $backPath = '') {
56 $modSharedTSconfig = t3lib_BEfunc::getModTSconfig($page_id, 'mod.SHARED');
57 $languageIconTitles = array();
58
59 // fallback "old iconstyles"
60 if (preg_match('/\.gif$/', $modSharedTSconfig['properties']['defaultLanguageFlag'])) {
61 $modSharedTSconfig['properties']['defaultLanguageFlag'] = str_replace('.gif', '', $modSharedTSconfig['properties']['defaultLanguageFlag']);
62 }
63
64 $languageIconTitles[0] = array(
65 'uid' => 0,
66 'title' => strlen($modSharedTSconfig['properties']['defaultLanguageLabel']) ? $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_web_list.xml:defaultLanguage') . ')' : $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_web_list.xml:defaultLanguage'),
67 'ISOcode' => 'DEF',
68 'flagIcon' => strlen($modSharedTSconfig['properties']['defaultLanguageFlag']) ? 'flags-' . $modSharedTSconfig['properties']['defaultLanguageFlag'] : 'empty-empty',
69 );
70
71 // Set "All" language:
72 $languageIconTitles[-1] = array(
73 'uid' => -1,
74 'title' => $GLOBALS['LANG']->getLL('multipleLanguages'),
75 'ISOcode' => 'DEF',
76 'flagIcon' => 'flags-multiple',
77 );
78
79 // Find all system languages:
80 $sys_languages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
81 '*',
82 'sys_language',
83 ''
84 );
85 foreach ($sys_languages as $row) {
86 $languageIconTitles[$row['uid']] = $row;
87
88 if ($row['static_lang_isocode'] && t3lib_extMgm::isLoaded('static_info_tables')) {
89 $staticLangRow = t3lib_BEfunc::getRecord('static_languages', $row['static_lang_isocode'], 'lg_iso_2');
90 if ($staticLangRow['lg_iso_2']) {
91 $languageIconTitles[$row['uid']]['ISOcode'] = $staticLangRow['lg_iso_2'];
92 }
93 }
94 if (strlen($row['flag'])) {
95 $languageIconTitles[$row['uid']]['flagIcon'] = t3lib_iconWorks::mapRecordTypeToSpriteIconName('sys_language', $row);
96 }
97 }
98
99 return $languageIconTitles;
100 }
101
102 /**
103 * Information about translation for an element
104 * Will overlay workspace version of record too!
105 *
106 * @param string Table name
107 * @param integer Record uid
108 * @param integer Language uid. If zero, then all languages are selected.
109 * @param array The record to be translated
110 * @param array select fields for the query which fetches the translations of the current record
111 * @return array Array with information. Errors will return string with message.
112 */
113 function translationInfo($table, $uid, $sys_language_uid = 0, $row = NULL, $selFieldList = '') {
114 if ($GLOBALS['TCA'][$table] && $uid) {
115 t3lib_div::loadTCA($table);
116
117 if ($row === NULL) {
118 $row = t3lib_BEfunc::getRecordWSOL($table, $uid);
119 }
120
121 if (is_array($row)) {
122 $trTable = $this->getTranslationTable($table);
123 if ($trTable) {
124 if ($trTable !== $table || $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] <= 0) {
125 if ($trTable !== $table || $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] == 0) {
126
127 // Look for translations of this record, index by language field value:
128 $translationsTemp = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
129 ($selFieldList ? $selFieldList : 'uid,' . $GLOBALS['TCA'][$trTable]['ctrl']['languageField']),
130 $trTable,
131 $GLOBALS['TCA'][$trTable]['ctrl']['transOrigPointerField'] . '=' . intval($uid) .
132 ' AND pid=' . intval($table === 'pages' ? $row['uid'] : $row['pid']) . // Making exception for pages of course where the translations will always be ON the page, not on the level above...
133 ' AND ' . $GLOBALS['TCA'][$trTable]['ctrl']['languageField'] . (!$sys_language_uid ? '>0' : '=' . intval($sys_language_uid)) .
134 t3lib_BEfunc::deleteClause($trTable) .
135 t3lib_BEfunc::versioningPlaceholderClause($trTable)
136 );
137
138 $translations = array();
139 $translations_errors = array();
140 foreach ($translationsTemp as $r) {
141 if (!isset($translations[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]])) {
142 $translations[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]] = $r;
143 } else {
144 $translations_errors[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]][] = $r;
145 }
146 }
147
148 return array(
149 'table' => $table,
150 'uid' => $uid,
151 'CType' => $row['CType'],
152 'sys_language_uid' => $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']],
153 'translation_table' => $trTable,
154 'translations' => $translations,
155 'excessive_translations' => $translations_errors
156 );
157 } else {
158 return 'Record "' . $table . '_' . $uid . '" seems to be a translation already (has a relation to record "' . $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] . '")';
159 }
160 } else {
161 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']] . '")';
162 }
163 } else {
164 return 'Translation is not supported for this table!';
165 }
166 } else {
167 return 'Record "' . $table . '_' . $uid . '" was not found';
168 }
169 } else {
170 return 'No table "' . $table . '" or no UID value';
171 }
172 }
173
174 /**
175 * Returns the table in which translations for input table is found.
176 *
177 * @param [type] $table: ...
178 * @return [type] ...
179 */
180 function getTranslationTable($table) {
181 return $this->isTranslationInOwnTable($table) ? $table : $this->foreignTranslationTable($table);
182 }
183
184 /**
185 * Returns TRUE, if the input table has localization enabled and done so with records from the same table
186 *
187 * @param [type] $table: ...
188 * @return [type] ...
189 */
190 function isTranslationInOwnTable($table) {
191 return $GLOBALS['TCA'][$table]['ctrl']['languageField']
192 && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']
193 && !$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerTable'];
194 }
195
196 /**
197 * Returns foreign translation table, if any
198 *
199 * @param [type] $table: ...
200 * @return [type] ...
201 */
202 function foreignTranslationTable($table) {
203 $trTable = $GLOBALS['TCA'][$table]['ctrl']['transForeignTable'];
204
205 if ($trTable && $GLOBALS['TCA'][$trTable] && $GLOBALS['TCA'][$trTable]['ctrl']['languageField']
206 && $GLOBALS['TCA'][$trTable]['ctrl']['transOrigPointerField']
207 && $GLOBALS['TCA'][$trTable]['ctrl']['transOrigPointerTable'] === $table) {
208 return $trTable;
209 }
210 }
211 }
212
213
214 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_transl8tools.php'])) {
215 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_transl8tools.php']);
216 }
217 ?>