Xataface Translation Memory Module 0.1
Translation Memory for Xataface Applications
/Applications/XAMPP/xamppfiles/htdocs/recipedb/modules/tm/actions/translate.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Xataface Translation Memory Module
00004  * Copyright (C) 2011  Steve Hannah <steve@weblite.ca>
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
00019  * Boston, MA  02110-1301, USA.
00020  *
00021  */
00022 class actions_translate {
00023         
00024         function handle($params){
00025         
00026                 $app =& Dataface_Application::getInstance();
00027                 $query = $app->getQuery();
00028                 $table = Dataface_Table::loadTable($query['-table']);
00029                 $source = $app->_conf['lang'];
00030                 $dest = ( isset($query['-destinationLanguage']) ? $query['-destinationLanguage'] : null);
00031                 $mod = Dataface_ModuleTool::loadModule('modules_tm');
00032                 
00033                 if ( !@$app->_conf['languages'] ){
00034                         throw new Exception("This application doesn't have multilingual content enabled.");
00035                 }
00036                 
00037 
00038                 $translatableLanguages = array_keys($app->_conf['languages']);
00039                 while (!isset($dest) ){
00040                         foreach ($translatableLanguages as $l){
00041                                 if ( $l != $source ){
00042                                         $dest = $l;
00043                                 }
00044                         }
00045                 }
00046                 
00047                 if ( !isset($dest) ) throw new Exception("Cannot find applicable destination language.");
00048                 
00049                 
00050                 $records = df_get_selected_records($query);
00051                 if ( !$records ){
00052                         $records = df_get_records_array($query['-table'], $query, null, null, false);
00053                 }
00054                 
00055                 if ( !$records ){
00056                         throw new Exception("No records matches your query.");
00057                 }
00058                 
00059                 $out = array();
00060                 foreach ($records as $record){
00061                         
00062                         
00063                         
00064                         $row = array();
00065                         $tlangs = $record->table()->getTranslations();
00066                         foreach (array_keys($tlangs) as $trans){
00067                                 $record->table()->getTranslation($trans);
00068                         }
00069                         $tlangs = $record->table()->getTranslations();
00070                         
00071                         
00072                         
00073                         $row['title'] = $record->getTitle();
00074                         $row['id'] = $record->getId();
00075                         
00076                         if ( !$record->checkPermission('translate') ){
00077                                 $row['error'] = 'Cannot translate this record because permission is denied.';
00078                                 $out[] = $row;
00079                                 continue;
00080                         }
00081                         
00082                         if ( !isset($tlangs[$dest]) ){
00083                                 $row['error'] = 'Record cannot be translated into '.$app->_conf['languages'][$dest];
00084                                 
00085                                 $out[] = $row;
00086                                 continue;
00087                         }
00088                         
00089                         //print_r($tlangs);
00090                         $tfields = $tlangs[$dest];
00091                         
00092                         
00093                         $tr = $this->getTranslationRow($record, $dest);
00094                 
00095                         $row['data'] = array();
00096                         
00097                         
00098                         $keys = $record->table()->keys();
00099                         
00100                         foreach ($tfields as $f){
00101                                 if ( isset($keys[$f]) ) continue;
00102                                 $row['data'][$f]['fielddef'] = $record->table()->getField($f);
00103                                 $row['data'][$f][$source] = $record->strval($f);
00104                                 if ( $tr and isset($tr->{$f}) ){
00105                                         $row['data'][$f][$dest] = $tr->{$f};
00106                                 }
00107                         }
00108                         $out[] = $row;
00109                         
00110                         
00111                         
00112                         
00113                         
00114                 }
00115                 import('Dataface/LanguageTool.php');
00116                 $otherDests = $app->_conf['languages'];
00117                 foreach ($otherDests as $k=>$v){
00118                         if ( $k == $source or $k == $dest ){
00119                                 unset($otherDests[$k]);
00120                                 
00121                         } else {
00122                                 $otherDests[$k] = Dataface_LanguageTool::getInstance()->getLanguageLabel($k);
00123                         }
00124                 }
00125                 
00126                 
00127                 $context = array(
00128                         'otherDests'=>$otherDests,
00129                         'records' => $out,
00130                         'sourceCode'=>$source,
00131                         'destCode'=>$dest,
00132                         'sourceLabel'=>Dataface_LanguageTool::getInstance()->getLanguageLabel($source),
00133                         'destLabel'=>Dataface_LanguageTool::getInstance()->getLanguageLabel($dest)
00134                 );
00135                 
00136                 
00137                 $jt = Dataface_JavascriptTool::getInstance();
00138                 $jt->addPath(dirname(__FILE__).'/../js', $mod->getBaseURL().'/js');
00139                 $jt->import('xataface/modules/tm/translate.js');
00140                 
00141                 $ct = Dataface_CSSTool::getInstance();
00142                 $ct->addPath(dirname(__FILE__).'/../css', $mod->getBaseURL().'/css');
00143                 
00144                 df_register_skin('tm', dirname(__FILE__).'/../templates');
00145                 df_display($context, 'xataface/modules/tm/translate.html');
00146                 
00147                 
00148                 
00149         }
00150         
00151         
00152         function getTranslationRow(Dataface_Record $record, $dest){
00153                 if ( !preg_match('/^[a-z0-9]{2}$/', $dest) ){
00154                         throw new Exception("Invalid language: $dest");
00155                 }
00156                 if ( $dest != Dataface_Application::getInstance()->_conf['default_language'] ){
00157                         $table = $record->table()->tablename.'_'.$dest;
00158                 } else {
00159                         $table = $record->table()->tablename;
00160                 }
00161                 $sql = "select * from `$table` where ";
00162                 $where = array();
00163                 $serializer = new Dataface_Serializer($record->table()->tablename);
00164                 
00165                 foreach ( $record->table()->keys() as $k=>$fld){
00166                         $where[] = '`'.$k."`='".addslashes($serializer->serialize($k, $record->val($k)))."'";
00167                 }
00168                 $where = implode(' AND ', $where);
00169                 $sql .= $where;
00170                 $res = mysql_query($sql, df_db());
00171                 if ( !$res ){
00172                         throw new Exception(mysql_error(df_db()));
00173                 }
00174                 if ( mysql_num_rows($res) > 1 ){
00175                         throw new Exception("Duplicate translation rows for record ".$record->getId().".");
00176                 }
00177                 $out = mysql_fetch_object($res);
00178                 @mysql_free_result($res);
00179                 return $out;
00180                 
00181                 
00182         }
00183 }
 All Data Structures Files Functions Variables