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