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