Adding default SVN properties svn:keywords and svn:eol-style to files added lately
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_transl8tools.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2006 Kasper Skaarhoj (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 Skaarhoj <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 Skaarhoj <kasperYYYY@typo3.com>
64 * @package TYPO3
65 * @subpackage t3lib
66 */
67 class t3lib_transl8tools {
68
69 /**
70 * Returns array of system languages
71 * @param integer page id (only used to get TSconfig configuration setting flag and label for default language)
72 * @param string Backpath for flags
73 * @return array
74 */
75 function getSystemLanguages($page_id=0,$backPath='') {
76 global $TCA,$LANG;
77
78 // Icons and language titles:
79 t3lib_div::loadTCA('sys_language');
80 $flagAbsPath = t3lib_div::getFileAbsFileName($TCA['sys_language']['columns']['flag']['config']['fileFolder']);
81 $flagIconPath = $backPath.'../'.substr($flagAbsPath, strlen(PATH_site));
82
83 $modSharedTSconfig = t3lib_BEfunc::getModTSconfig($page_id, 'mod.SHARED');
84 $languageIconTitles = array();
85
86 // Set default:
87 $languageIconTitles[0] = array(
88 'uid' => 0,
89 'title' => strlen ($modSharedTSconfig['properties']['defaultLanguageLabel']) ? $modSharedTSconfig['properties']['defaultLanguageLabel'].' ('.$LANG->getLL('defaultLanguage').')' : $LANG->getLL('defaultLanguage'),
90 'ISOcode' => 'DEF',
91 'flagIcon' => strlen($modSharedTSconfig['properties']['defaultLanguageFlag']) && @is_file($flagAbsPath.$modSharedTSconfig['properties']['defaultLanguageFlag']) ? $flagIconPath.$modSharedTSconfig['properties']['defaultLanguageFlag'] : null,
92 );
93
94 // Set "All" language:
95 $languageIconTitles[-1]=array(
96 'uid' => -1,
97 'title' => $LANG->getLL('multipleLanguages'),
98 'ISOcode' => 'DEF',
99 'flagIcon' => $flagIconPath.'multi-language.gif',
100 );
101
102 // Find all system languages:
103 $sys_languages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
104 '*',
105 'sys_language',
106 ''
107 );
108 foreach($sys_languages as $row) {
109 $languageIconTitles[$row['uid']] = $row;
110
111 if ($row['static_lang_isocode'] && t3lib_extMgm::isLoaded('static_info_tables')) {
112 $staticLangRow = t3lib_BEfunc::getRecord('static_languages',$row['static_lang_isocode'],'lg_iso_2');
113 if ($staticLangRow['lg_iso_2']) {
114 $languageIconTitles[$row['uid']]['ISOcode'] = $staticLangRow['lg_iso_2'];
115 }
116 }
117 if (strlen ($row['flag'])) {
118 $languageIconTitles[$row['uid']]['flagIcon'] = @is_file($flagAbsPath.$row['flag']) ? $flagIconPath.$row['flag'] : '';
119 }
120 }
121
122 return $languageIconTitles;
123 }
124
125 /**
126 * Information about translation for an element
127 * Will overlay workspace version of record too!
128 *
129 * @param string Table name
130 * @param integer Record uid
131 * @param integer Language uid. If zero, then all languages are selected.
132 * @return array Array with information. Errors will return string with message.
133 */
134 function translationInfo($table,$uid,$sys_language_uid=0) {
135 global $TCA;
136
137 if ($TCA[$table] && $uid) {
138 t3lib_div::loadTCA($table);
139
140 $row = t3lib_BEfunc::getRecordWSOL($table,$uid);
141 if (is_array($row)) {
142 $trTable = $this->getTranslationTable($table);
143 if ($trTable) {
144 if ($trTable!==$table || $row[$TCA[$table]['ctrl']['languageField']] <= 0) {
145 if ($trTable!==$table || $row[$TCA[$table]['ctrl']['transOrigPointerField']] == 0) {
146
147 // Look for translations of this record, index by language field value:
148 $translationsTemp = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
149 'uid,'.$TCA[$trTable]['ctrl']['languageField'],
150 $trTable,
151 '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...
152 ' AND '.$TCA[$trTable]['ctrl']['languageField'].(!$sys_language_uid ? '>0' : '='.intval($sys_language_uid)).
153 ' AND '.$TCA[$trTable]['ctrl']['transOrigPointerField'].'='.intval($uid).
154 t3lib_BEfunc::deleteClause($trTable).
155 t3lib_BEfunc::versioningPlaceholderClause($trTable)
156 );
157
158 $translations = array();
159 $translations_errors = array();
160 foreach($translationsTemp as $r) {
161 if (!isset($translations[$r[$TCA[$trTable]['ctrl']['languageField']]])) {
162 $translations[$r[$TCA[$trTable]['ctrl']['languageField']]] = $r;
163 } else {
164 $translations_errors[$r[$TCA[$trTable]['ctrl']['languageField']]][] = $r;
165 }
166 }
167
168 return array(
169 'table' => $table,
170 'uid' => $uid,
171 'sys_language_uid' => $row[$TCA[$table]['ctrl']['languageField']],
172 'translation_table' => $trTable,
173 'translations' => $translations,
174 'excessive_translations' => $translations_errors
175 );
176 } else return 'Record "'.$table.'_'.$uid.'" seems to be a translation already (has a relation to record "'.$row[$TCA[$table]['ctrl']['transOrigPointerField']].'")';
177 } 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']].'")';
178 } else return 'Translation is not supported for this table!';
179 } else return 'Record "'.$table.'_'.$uid.'" was not found';
180 } else return 'No table "'.$table.'" or no UID value';
181 }
182
183 /**
184 * Returns the table in which translations for input table is found.
185 *
186 * @param [type] $table: ...
187 * @return [type] ...
188 */
189 function getTranslationTable($table) {
190 return $this->isTranslationInOwnTable($table) ? $table : $this->foreignTranslationTable($table);
191 }
192
193 /**
194 * Returns true, if the input table has localization enabled and done so with records from the same table
195 *
196 * @param [type] $table: ...
197 * @return [type] ...
198 */
199 function isTranslationInOwnTable($table) {
200 global $TCA;
201
202 return $TCA[$table]['ctrl']['languageField'] && $TCA[$table]['ctrl']['transOrigPointerField'] && !$TCA[$table]['ctrl']['transOrigPointerTable'];
203 }
204
205 /**
206 * Returns foreign translation table, if any
207 *
208 * @param [type] $table: ...
209 * @return [type] ...
210 */
211 function foreignTranslationTable($table) {
212 global $TCA;
213
214 $trTable = $TCA[$table]['ctrl']['transForeignTable'];
215
216 if ($trTable && $TCA[$trTable] && $TCA[$trTable]['ctrl']['languageField'] && $TCA[$trTable]['ctrl']['transOrigPointerField'] && $TCA[$trTable]['ctrl']['transOrigPointerTable']===$table) {
217 return $trTable;
218 }
219 }
220 }
221
222
223 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_transl8tools.php']) {
224 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_transl8tools.php']);
225 }
226 ?>