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