Fixed bug #14050: CleanUp - CGL format of t3lib files - t3lib_tceforms_inline
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_transl8tools.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2006-2010 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 * $Id$
31 *
32 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 67: class t3lib_transl8tools
40 * 74: function getSystemLanguages($page_id=0,$backPath='')
41 * 132: function translationInfo($table,$uid,$sys_language_uid=0)
42 * 187: function getTranslationTable($table)
43 * 197: function isTranslationInOwnTable($table)
44 * 209: function foreignTranslationTable($table)
45 *
46 * TOTAL FUNCTIONS: 5
47 * (This index is automatically created/updated by the extension "extdeveval")
48 *
49 */
50
51
52
53
54
55
56
57
58
59
60 /**
61 * Contains translation tools
62 *
63 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
64 * @package TYPO3
65 * @subpackage t3lib
66 */
67 class t3lib_transl8tools {
68
69 /**
70 * Returns array of system languages
71 *
72 * Since TYPO3 4.5 the flagIcon is not returned as a filename in "gfx/flags/*" anymore,
73 * but as a string <flags-xx>. The calling party should call
74 * t3lib_iconWorks::getSpriteIcon(<flags-xx>) to get an HTML which will represent
75 * the flag of this language.
76 *
77 * @param integer page id (only used to get TSconfig configuration setting flag and label for default language)
78 * @param string Backpath for flags
79 * @return array Array with languages (title, uid, flagIcon)
80 */
81 function getSystemLanguages($page_id=0,$backPath='') {
82 global $TCA,$LANG;
83
84 $modSharedTSconfig = t3lib_BEfunc::getModTSconfig($page_id, 'mod.SHARED');
85 $languageIconTitles = array();
86
87 // fallback "old iconstyles"
88 if (preg_match('/\.gif$/', $modSharedTSconfig['properties']['defaultLanguageFlag'])) {
89 $modSharedTSconfig['properties']['defaultLanguageFlag'] = str_replace('.gif', '', $modSharedTSconfig['properties']['defaultLanguageFlag']);
90 }
91
92 $languageIconTitles[0] = array(
93 'uid' => 0,
94 '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'),
95 'ISOcode' => 'DEF',
96 'flagIcon' => strlen($modSharedTSconfig['properties']['defaultLanguageFlag']) ? 'flags-' . $modSharedTSconfig['properties']['defaultLanguageFlag'] : 'empty-empty',
97 );
98
99 // Set "All" language:
100 $languageIconTitles[-1]=array(
101 'uid' => -1,
102 'title' => $LANG->getLL('multipleLanguages'),
103 'ISOcode' => 'DEF',
104 'flagIcon' => 'flags-multiple',
105 );
106
107 // Find all system languages:
108 $sys_languages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
109 '*',
110 'sys_language',
111 ''
112 );
113 foreach($sys_languages as $row) {
114 $languageIconTitles[$row['uid']] = $row;
115
116 if ($row['static_lang_isocode'] && t3lib_extMgm::isLoaded('static_info_tables')) {
117 $staticLangRow = t3lib_BEfunc::getRecord('static_languages',$row['static_lang_isocode'],'lg_iso_2');
118 if ($staticLangRow['lg_iso_2']) {
119 $languageIconTitles[$row['uid']]['ISOcode'] = $staticLangRow['lg_iso_2'];
120 }
121 }
122 if (strlen ($row['flag'])) {
123 $languageIconTitles[$row['uid']]['flagIcon'] = t3lib_iconWorks::mapRecordTypeToSpriteIconName('sys_language', $row);
124 }
125 }
126
127 return $languageIconTitles;
128 }
129
130 /**
131 * Information about translation for an element
132 * Will overlay workspace version of record too!
133 *
134 * @param string Table name
135 * @param integer Record uid
136 * @param integer Language uid. If zero, then all languages are selected.
137 * @param array The record to be translated
138 * @param array select fields for the query which fetches the translations of the current record
139 * @return array Array with information. Errors will return string with message.
140 */
141 function translationInfo($table, $uid, $sys_language_uid = 0, $row = NULL, $selFieldList = '') {
142 global $TCA;
143
144 if ($TCA[$table] && $uid) {
145 t3lib_div::loadTCA($table);
146
147 if ($row === NULL) {
148 $row = t3lib_BEfunc::getRecordWSOL($table, $uid);
149 }
150
151 if (is_array($row)) {
152 $trTable = $this->getTranslationTable($table);
153 if ($trTable) {
154 if ($trTable!==$table || $row[$TCA[$table]['ctrl']['languageField']] <= 0) {
155 if ($trTable!==$table || $row[$TCA[$table]['ctrl']['transOrigPointerField']] == 0) {
156
157 // Look for translations of this record, index by language field value:
158 $translationsTemp = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
159 ($selFieldList ? $selFieldList : 'uid,'.$TCA[$trTable]['ctrl']['languageField']),
160 $trTable,
161 $TCA[$trTable]['ctrl']['transOrigPointerField'] . '=' . intval($uid) .
162 ' 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...
163 ' AND '.$TCA[$trTable]['ctrl']['languageField'].(!$sys_language_uid ? '>0' : '='.intval($sys_language_uid)).
164 t3lib_BEfunc::deleteClause($trTable).
165 t3lib_BEfunc::versioningPlaceholderClause($trTable)
166 );
167
168 $translations = array();
169 $translations_errors = array();
170 foreach($translationsTemp as $r) {
171 if (!isset($translations[$r[$TCA[$trTable]['ctrl']['languageField']]])) {
172 $translations[$r[$TCA[$trTable]['ctrl']['languageField']]] = $r;
173 } else {
174 $translations_errors[$r[$TCA[$trTable]['ctrl']['languageField']]][] = $r;
175 }
176 }
177
178 return array(
179 'table' => $table,
180 'uid' => $uid,
181 'CType' => $row['CType'],
182 'sys_language_uid' => $row[$TCA[$table]['ctrl']['languageField']],
183 'translation_table' => $trTable,
184 'translations' => $translations,
185 'excessive_translations' => $translations_errors
186 );
187 } else return 'Record "'.$table.'_'.$uid.'" seems to be a translation already (has a relation to record "'.$row[$TCA[$table]['ctrl']['transOrigPointerField']].'")';
188 } else return 'Record "'.$table.'_'.$uid.'" seems to be a translation already (has a language value "'.$row[$TCA[$table]['ctrl']['languageField']].'", relation to record "'.$row[$TCA[$table]['ctrl']['transOrigPointerField']].'")';
189 } else return 'Translation is not supported for this table!';
190 } else return 'Record "'.$table.'_'.$uid.'" was not found';
191 } else return 'No table "'.$table.'" or no UID value';
192 }
193
194 /**
195 * Returns the table in which translations for input table is found.
196 *
197 * @param [type] $table: ...
198 * @return [type] ...
199 */
200 function getTranslationTable($table) {
201 return $this->isTranslationInOwnTable($table) ? $table : $this->foreignTranslationTable($table);
202 }
203
204 /**
205 * Returns true, if the input table has localization enabled and done so with records from the same table
206 *
207 * @param [type] $table: ...
208 * @return [type] ...
209 */
210 function isTranslationInOwnTable($table) {
211 global $TCA;
212
213 return $TCA[$table]['ctrl']['languageField'] && $TCA[$table]['ctrl']['transOrigPointerField'] && !$TCA[$table]['ctrl']['transOrigPointerTable'];
214 }
215
216 /**
217 * Returns foreign translation table, if any
218 *
219 * @param [type] $table: ...
220 * @return [type] ...
221 */
222 function foreignTranslationTable($table) {
223 global $TCA;
224
225 $trTable = $TCA[$table]['ctrl']['transForeignTable'];
226
227 if ($trTable && $TCA[$trTable] && $TCA[$trTable]['ctrl']['languageField'] && $TCA[$trTable]['ctrl']['transOrigPointerField'] && $TCA[$trTable]['ctrl']['transOrigPointerTable']===$table) {
228 return $trTable;
229 }
230 }
231 }
232
233
234 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_transl8tools.php']) {
235 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_transl8tools.php']);
236 }
237 ?>