Xataface Translation Memory Module 0.1
Translation Memory for Xataface Applications
/Applications/XAMPP/xamppfiles/htdocs/recipedb/modules/tm/actions/tm_get_strings.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  
00056 class actions_tm_get_strings {
00057 
00058         function handle($params){
00059                 $app = Dataface_Application::getInstance();
00060                 $query = $app->getQuery();
00061                 session_write_close();
00062                 header('Connection:close');
00063                 
00064                 try {
00065                         
00066                         if ( !isset($_POST['--source']) ){
00067                                 throw new Exception("No source language specified");
00068                         }
00069                         
00070                         if ( !isset($_POST['--dest']) ){
00071                                 throw new Exception("No destination language specified");
00072                         }
00073                         
00074                         if ( !isset($_POST['--strings']) or !is_array($_POST['--strings']) ){
00075                                 throw new Exception("No strings provided for translation.");
00076                         }
00077                         
00078                         
00079                         if ( !preg_match('/^[a-z0-9]{2}$/', $_POST['--source']) ){
00080                                 throw new Exception("Invalid source language.");
00081                         }
00082                         
00083                         if ( !preg_match('/^[a-z0-9]{2}$/', $_POST['--dest']) ){
00084                                 throw new Exception("Invalid destination language");
00085                         }
00086                         
00087                         
00088                         import(dirname(__FILE__).'/../lib/XFTranslationMemory.php');
00089                         
00090                         $tm = XFTranslationMemory::getDefaultTranslationMemory($_POST['--source'], $_POST['--dest']);
00091                         if ( !$tm ){
00092                                 throw new Exception("Failed to load translation memory.");
00093                         }
00094                         if ( !$tm->getRecord()->checkPermission('tm:get strings') ){
00095                                 throw new Exception("You don't have permission to get strings from this translation memory.");
00096                         }
00097                         
00098                         
00099                         $translations = $tm->getTranslations($_POST['--strings'], 3, 5);
00100                         $out = array();
00101                         foreach ($translations as $k=>$v){
00102                                 $out[$_POST['--strings'][$k]] = $v;
00103                         }
00104                         
00105                         $this->out(array(
00106                                 'strings'=>$out,
00107                                 'code'=>200,
00108                                 'message'=>'Successfully retrieved translations'
00109                         ));
00110         
00111                 
00112                 } catch (Exception $ex){
00113                         
00114                         $this->out(array(
00115                                 'code'=>$ex->getCode(),
00116                                 'message'=>$ex->getMessage()
00117                         ));
00118                 
00119                 
00120                 }
00121         }
00122         
00123         function out($params){
00124         
00125                 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
00126                 echo json_encode($params);
00127         }
00128 }
 All Data Structures Files Functions Variables