Xataface Translation Memory Module 0.1
Translation Memory for Xataface Applications
/Applications/XAMPP/xamppfiles/htdocs/recipedb/modules/tm/actions/tm_add_translations.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  
00060 class actions_tm_add_translations {
00061 
00062         function handle($params){
00063         
00064                 $app = Dataface_Application::getInstance();
00065                 $query = $app->getQuery();
00066                 session_write_close();
00067                 header('Connection:close');
00068                 
00069                 try {
00070                 
00071                         
00072                         if ( !@$_POST['--source'] ) throw new Exception("No source language provided");
00073                         if ( !@$_POST['--dest'] ) throw new Exception("No destination language provided");
00074                         if ( !@$_POST['--strings'] or !is_array($_POST['--strings']) ){
00075                                 throw new Exception("No strings provided");
00076                         }
00077                         if ( !@$_POST['--translations'] or !is_array($_POST['--translations']) ){
00078                                 throw new Exception("No translations provided");
00079                         }
00080                         
00081                         if ( count($_POST['--translations']) != count($_POST['--strings']) ){
00082                                 throw new Exception("Number of translations does not match the number of strings.");
00083                         }
00084                         
00085                         if ( !preg_match('/^[a-z0-9]{2}$/', $_POST['--source']) ){
00086                                 throw new Exception("Invalid source language.");
00087                         }
00088                         
00089                         if ( !preg_match('/^[a-z0-9]{2}$/', $_POST['--dest']) ){
00090                                 throw new Exception("Invalid destination language");
00091                         }
00092                         
00093                         
00094                         import(dirname(__FILE__).'/../lib/XFTranslationMemory.php');
00095                         
00096                         $tm = XFTranslationMemory::getDefaultTranslationMemory($_POST['--source'], $_POST['--dest']);
00097                         if ( !$tm ){
00098                                 throw new Exception("Failed to load translation memory.");
00099                         }
00100                         if ( !$tm->getRecord()->checkPermission('tm:add translation') ){
00101                                 throw new Exception("You don't have permission to add translations to this translation memory.");
00102                         }
00103                         
00104                         $username = 'Anonymous';
00105                         if ( class_exists('Dataface_AuthenticationTool') ){
00106                                 $username = Dataface_AuthenticationTool::getInstance()->getLoggedInUserName();
00107                         }
00108                         
00109                         $saved = array();
00110                         $code = 200;
00111                         $outTranslations = array();
00112                         foreach ($_POST['--strings'] as $k=>$str){
00113                                 $translation = $_POST['--translations'][$k];
00114                                 try {
00115                                 
00116                                         $tm->addTranslation($str, $translation, $username);
00117                                         $tm->setTranslationStatus($str, $translation, XFTranslationMemory::TRANSLATION_SUBMITTED);
00118                                         $saved[] = $str;
00119                                         $outTranslations[$str] = $translation;
00120                                 } catch (Exception $ex){
00121                                         error_log('Failed to add translation "$str":"$translation" : '.$ex->getMessage());
00122                                 }
00123                                 
00124                         
00125                         }
00126                         
00127                         $this->out(array(
00128                                 'code'=>200,
00129                                 'saved'=>$saved,
00130                                 'outTranslations'=>$outTranslations,
00131                                 'message'=>'Saving successful'
00132                         ));
00133                         
00134                         
00135                         
00136                 } catch (Exception $ex){
00137                 
00138                         $this->out(array(
00139                                 'code'=>$ex->getCode(),
00140                                 'message'=>$ex->getMessage()
00141                         ));
00142                 
00143                 }
00144         
00145         }
00146         
00147         function out($params){
00148         
00149                 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
00150                 echo json_encode($params);
00151         }
00152 }
 All Data Structures Files Functions Variables