TYPO3 API  SVNRelease
class.t3lib_transl8tools.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2006-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 /**
00028  * Contains translation tools
00029  *
00030  * $Id: class.t3lib_transl8tools.php 10121 2011-01-18 20:15:30Z ohader $
00031  *
00032  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00033  */
00034 /**
00035  * [CLASS/FUNCTION INDEX of SCRIPT]
00036  *
00037  *
00038  *
00039  *   67: class t3lib_transl8tools
00040  *   74:     function getSystemLanguages($page_id=0,$backPath='')
00041  *  132:     function translationInfo($table,$uid,$sys_language_uid=0)
00042  *  187:     function getTranslationTable($table)
00043  *  197:     function isTranslationInOwnTable($table)
00044  *  209:     function foreignTranslationTable($table)
00045  *
00046  * TOTAL FUNCTIONS: 5
00047  * (This index is automatically created/updated by the extension "extdeveval")
00048  *
00049  */
00050 
00051 
00052 /**
00053  * Contains translation tools
00054  *
00055  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00056  * @package TYPO3
00057  * @subpackage t3lib
00058  */
00059 class t3lib_transl8tools {
00060 
00061     /**
00062      * Returns array of system languages
00063      *
00064      * Since TYPO3 4.5 the flagIcon is not returned as a filename in "gfx/flags/*" anymore,
00065      * but as a string <flags-xx>. The calling party should call
00066      * t3lib_iconWorks::getSpriteIcon(<flags-xx>) to get an HTML which will represent
00067      * the flag of this language.
00068      *
00069      * @param   integer     page id (only used to get TSconfig configuration setting flag and label for default language)
00070      * @param   string      Backpath for flags
00071      * @return  array       Array with languages (title, uid, flagIcon)
00072      */
00073     function getSystemLanguages($page_id = 0, $backPath = '') {
00074         global $TCA, $LANG;
00075 
00076         $modSharedTSconfig = t3lib_BEfunc::getModTSconfig($page_id, 'mod.SHARED');
00077         $languageIconTitles = array();
00078 
00079             // fallback "old iconstyles"
00080         if (preg_match('/\.gif$/', $modSharedTSconfig['properties']['defaultLanguageFlag'])) {
00081             $modSharedTSconfig['properties']['defaultLanguageFlag'] = str_replace('.gif', '', $modSharedTSconfig['properties']['defaultLanguageFlag']);
00082         }
00083 
00084         $languageIconTitles[0] = array(
00085             'uid' => 0,
00086             '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'),
00087             'ISOcode' => 'DEF',
00088             'flagIcon' => strlen($modSharedTSconfig['properties']['defaultLanguageFlag']) ? 'flags-' . $modSharedTSconfig['properties']['defaultLanguageFlag'] : 'empty-empty',
00089         );
00090 
00091             // Set "All" language:
00092         $languageIconTitles[-1] = array(
00093             'uid' => -1,
00094             'title' => $LANG->getLL('multipleLanguages'),
00095             'ISOcode' => 'DEF',
00096             'flagIcon' => 'flags-multiple',
00097         );
00098 
00099             // Find all system languages:
00100         $sys_languages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00101             '*',
00102             'sys_language',
00103             ''
00104         );
00105         foreach ($sys_languages as $row) {
00106             $languageIconTitles[$row['uid']] = $row;
00107 
00108             if ($row['static_lang_isocode'] && t3lib_extMgm::isLoaded('static_info_tables')) {
00109                 $staticLangRow = t3lib_BEfunc::getRecord('static_languages', $row['static_lang_isocode'], 'lg_iso_2');
00110                 if ($staticLangRow['lg_iso_2']) {
00111                     $languageIconTitles[$row['uid']]['ISOcode'] = $staticLangRow['lg_iso_2'];
00112                 }
00113             }
00114             if (strlen($row['flag'])) {
00115                 $languageIconTitles[$row['uid']]['flagIcon'] = t3lib_iconWorks::mapRecordTypeToSpriteIconName('sys_language', $row);
00116             }
00117         }
00118 
00119         return $languageIconTitles;
00120     }
00121 
00122     /**
00123      * Information about translation for an element
00124      * Will overlay workspace version of record too!
00125      *
00126      * @param   string      Table name
00127      * @param   integer     Record uid
00128      * @param   integer     Language uid. If zero, then all languages are selected.
00129      * @param   array       The record to be translated
00130      * @param   array       select fields for the query which fetches the translations of the current record
00131      * @return  array       Array with information. Errors will return string with message.
00132      */
00133     function translationInfo($table, $uid, $sys_language_uid = 0, $row = NULL, $selFieldList = '') {
00134         global $TCA;
00135 
00136         if ($TCA[$table] && $uid) {
00137             t3lib_div::loadTCA($table);
00138 
00139             if ($row === NULL) {
00140                 $row = t3lib_BEfunc::getRecordWSOL($table, $uid);
00141             }
00142 
00143             if (is_array($row)) {
00144                 $trTable = $this->getTranslationTable($table);
00145                 if ($trTable) {
00146                     if ($trTable !== $table || $row[$TCA[$table]['ctrl']['languageField']] <= 0) {
00147                         if ($trTable !== $table || $row[$TCA[$table]['ctrl']['transOrigPointerField']] == 0) {
00148 
00149                                 // Look for translations of this record, index by language field value:
00150                             $translationsTemp = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00151                                 ($selFieldList ? $selFieldList : 'uid,' . $TCA[$trTable]['ctrl']['languageField']),
00152                                 $trTable,
00153                                 $TCA[$trTable]['ctrl']['transOrigPointerField'] . '=' . intval($uid) .
00154                                 ' 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...
00155                                 ' AND ' . $TCA[$trTable]['ctrl']['languageField'] . (!$sys_language_uid ? '>0' : '=' . intval($sys_language_uid)) .
00156                                 t3lib_BEfunc::deleteClause($trTable) .
00157                                 t3lib_BEfunc::versioningPlaceholderClause($trTable)
00158                             );
00159 
00160                             $translations = array();
00161                             $translations_errors = array();
00162                             foreach ($translationsTemp as $r) {
00163                                 if (!isset($translations[$r[$TCA[$trTable]['ctrl']['languageField']]])) {
00164                                     $translations[$r[$TCA[$trTable]['ctrl']['languageField']]] = $r;
00165                                 } else {
00166                                     $translations_errors[$r[$TCA[$trTable]['ctrl']['languageField']]][] = $r;
00167                                 }
00168                             }
00169 
00170                             return array(
00171                                 'table' => $table,
00172                                 'uid' => $uid,
00173                                 'CType' => $row['CType'],
00174                                 'sys_language_uid' => $row[$TCA[$table]['ctrl']['languageField']],
00175                                 'translation_table' => $trTable,
00176                                 'translations' => $translations,
00177                                 'excessive_translations' => $translations_errors
00178                             );
00179                         } else {
00180                             return 'Record "' . $table . '_' . $uid . '" seems to be a translation already (has a relation to record "' . $row[$TCA[$table]['ctrl']['transOrigPointerField']] . '")';
00181                         }
00182                     } else {
00183                         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']] . '")';
00184                     }
00185                 } else {
00186                     return 'Translation is not supported for this table!';
00187                 }
00188             } else {
00189                 return 'Record "' . $table . '_' . $uid . '" was not found';
00190             }
00191         } else {
00192             return 'No table "' . $table . '" or no UID value';
00193         }
00194     }
00195 
00196     /**
00197      * Returns the table in which translations for input table is found.
00198      *
00199      * @param   [type]      $table: ...
00200      * @return  [type]      ...
00201      */
00202     function getTranslationTable($table) {
00203         return $this->isTranslationInOwnTable($table) ? $table : $this->foreignTranslationTable($table);
00204     }
00205 
00206     /**
00207      * Returns true, if the input table has localization enabled and done so with records from the same table
00208      *
00209      * @param   [type]      $table: ...
00210      * @return  [type]      ...
00211      */
00212     function isTranslationInOwnTable($table) {
00213         global $TCA;
00214 
00215         return $TCA[$table]['ctrl']['languageField'] && $TCA[$table]['ctrl']['transOrigPointerField'] && !$TCA[$table]['ctrl']['transOrigPointerTable'];
00216     }
00217 
00218     /**
00219      * Returns foreign translation table, if any
00220      *
00221      * @param   [type]      $table: ...
00222      * @return  [type]      ...
00223      */
00224     function foreignTranslationTable($table) {
00225         global $TCA;
00226 
00227         $trTable = $TCA[$table]['ctrl']['transForeignTable'];
00228 
00229         if ($trTable && $TCA[$trTable] && $TCA[$trTable]['ctrl']['languageField'] && $TCA[$trTable]['ctrl']['transOrigPointerField'] && $TCA[$trTable]['ctrl']['transOrigPointerTable'] === $table) {
00230             return $trTable;
00231         }
00232     }
00233 }
00234 
00235 
00236 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_transl8tools.php'])) {
00237     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_transl8tools.php']);
00238 }
00239 ?>