languages
[ class tree: languages ] [ index: languages ] [ all elements ]

Source for file LibertyTranslations.php

Documentation is available at LibertyTranslations.php

  1. <?php
  2. /**
  3.  * @package languages
  4.  * @version $Header$
  5.  *
  6.  * @author ?
  7.  */
  8.  
  9. /**
  10.  * @package languages
  11.  */
  12.  class LibertyTranslations extends LibertyBase {
  13.     function __construct( $pContentId = NULL ) {
  14.         $this->mContentId = $pContentId;
  15.         parent::__construct();
  16.     }
  17.  
  18.     function getContentTranslations() {
  19.         global $gBitSystem, $gBitLanguage;
  20.         $ret = array();
  21.         if( @BitBase::verifyId( $this->mContentId ) ) {
  22.             $translationId = $this->mDb->getOne( "SELECT `translation_id` FROM `".BIT_DB_PREFIX."i18n_content_trans_map` WHERE `content_id`=?", array( $this->mContentId ) );
  23.             if( @BitBase::verifyId( $translationId ) ) {
  24.                 $query = "SELECT lc.`content_id`, lc.`title`, lc.`lang_code`, ictm.`translation_id`
  25.                     FROM `".BIT_DB_PREFIX."i18n_content_trans_map` ictm
  26.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lc.`content_id`=ictm.`content_id` )
  27.                     WHERE ictm.`translation_id`=?";
  28.                 $result = $this->mDb->query( $query, array( $translationId ) );
  29.                 while( $aux = $result->fetchRow() ) {
  30.                     // default to site language
  31.                     if( empty( $aux['lang_code'] )) {
  32.                         $aux['lang_code'] = $gBitLanguage->mLanguage;
  33.                     }
  34.                     $ret[$aux['lang_code']] = $aux;
  35.                 }
  36.             }
  37.         }
  38.         return $ret;
  39.     }
  40.  
  41.     function storeTranslation( $pParamHash ) {
  42.         if( $this->verify( $pParamHash ) ) {
  43.             $table = BIT_DB_PREFIX."i18n_content_trans_map";
  44.             if( !@BitBase::verifyId( $pParamHash['translation_store']['translation_id'] ) && is_array( $pParamHash['translation_store'] ) ) {
  45.                 foreach( $pParamHash['translation_store'] as $store ) {
  46.                     $result = $this->mDb->associateInsert( $table, $store );
  47.                 }
  48.             } else {
  49.                 $result = $this->mDb->associateInsert( $table, $pParamHash['translation_store'] );
  50.             }
  51.         }
  52.     }
  53.  
  54.     function verify( &$pParamHash ) {
  55.         $i = 0;
  56.  
  57.         // make sure we don't have a translation_id for this content yet
  58.         if( @BitBase::verifyId( $pParamHash['from_id'] ) ) {
  59.             $pParamHash['translation_id'] = $this->mDb->getOne( "SELECT `translation_id` FROM `".BIT_DB_PREFIX."i18n_content_trans_map` WHERE `content_id`=?", array( $pParamHash['from_id'] ) );
  60.         }
  61.  
  62.         // if we have this page in this translation, we should inform the user somehow.
  63.         // in theory, this shouldn't happen, but there might be a situation where we end up with 2 users translating the same page at the same time. (is this true?)
  64.  
  65.         // if we have a translation_id, we add this content to the same group of translations
  66.         if( @BitBase::verifyId( $pParamHash['translation_id'] ) ) {
  67.             $pParamHash['translation_store']['translation_id']     = $pParamHash['translation_id'];
  68.             $pParamHash['translation_store']['content_id']         = $pParamHash['content_id'];
  69.         } elseif( @BitBase::verifyId( $pParamHash['from_id'] ) ) {
  70.             // we have a from_id but no translation_id, this is a new entry in the translation map and we need both, the original and the new content_id entered
  71.             // we can simply use the from_id as the translation_id
  72.             $pParamHash['translation_store'][$i]['translation_id'] = $pParamHash['from_id'];
  73.             $pParamHash['translation_store'][$i]['content_id']     = $pParamHash['from_id'];
  74.             $i++;
  75.             $pParamHash['translation_store'][$i]['translation_id'] = $pParamHash['from_id'];
  76.             $pParamHash['translation_store'][$i]['content_id']     = $pParamHash['content_id'];
  77.         }
  78.         return( count( $this->mErrors ) == 0 );
  79.     }
  80.  
  81.     function expunge() {
  82.         if( @BitBase::verifyId( $this->mContentId ) ) {
  83.             $result = $this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."i18n_content_trans_map` WHERE `content_id`=?", $this->mContentId );
  84.         }
  85.     }
  86. }
  87.  
  88. // ================== service functions ==================
  89.  
  90. function translation_content_display( &$pObject ) {
  91.     global $gBitSmarty, $gBitLanguage;
  92.     $trans = new LibertyTranslations( $pObject->mContentId );
  93.     $translations = $trans->getContentTranslations();
  94.     // merge this information that we can display the appropriate flags
  95.     if( count( $translations ) > 1 ) {
  96.         foreach( $translations as $key => $trans ) {
  97.             $translations[$key] = array_merge( $gBitLanguage->mLanguageList[$trans['lang_code']], $trans );
  98.         }
  99.         $gBitSmarty->assign( 'i18nTranslations', $translations );
  100.     }
  101. }
  102.  
  103. function translation_content_edit( &$pObject, &$pParamHash ) {
  104.     global $gBitLanguage, $gBitSmarty, $gBitUser;
  105.     $trans = new LibertyTranslations( $pObject->mContentId );
  106.     $translationId = NULL;
  107.     $translations = $trans->getContentTranslations();
  108.     foreach( $gBitLanguage->mLanguageList as $lang_code => $language ) {
  109.         $translationsList[$lang_code] = $language;
  110.         if( !empty( $translations[$lang_code]['content_id'] ) ) {
  111.             $translationsList[$lang_code]['content_id'] = $translations[$lang_code]['content_id'];
  112.             $translationsList[$lang_code]['title'] = $translations[$lang_code]['title'];
  113.             $translationId = $translations[$lang_code]['translation_id'];
  114.         }
  115.     }
  116.     $gBitSmarty->assign( 'translationsList', $translationsList );
  117.     $gBitSmarty->assign( 'translationId', $translationId );
  118.  
  119.     if( @BitBase::verifyId( $_REQUEST['i18n']['from_id'] ) ) {
  120.         // load the content we're translating from
  121.         $transObject = LibertyBase::getLibertyObject( $_REQUEST['i18n']['from_id'] );
  122.         $gBitSmarty->assign_by_ref( "translateFrom", $transObject );
  123.  
  124.         // attempt google translation
  125.         if( !empty( $_REQUEST['i18n']['google'] ) && !empty( $transObject->mInfo['data'] )) {
  126.             // temporarily replace \n with a string
  127.             $nl = 'nlnlnlnlnl';
  128.             // initiate some variables
  129.             $transObject->mInfo['google_guess'] = '';
  130.             // we need to split the strings into small chunks due to url length limitations
  131.             $strings = str_split( $transObject->mInfo['data'], 1500 );
  132.             foreach( $strings as $string ) {
  133.                 $requestUrl = "http://translate.google.com/translate_t?ie=UTF-8&oe=UTF-8&text=".urlencode( preg_replace( '/[\n]/', $nl, $string ))."&langpair=en|{$_REQUEST['i18n']['lang_code']}";
  134.                 if( $handle = fopen( $requestUrl, "r" )) {
  135.                     $data = '';
  136.                     while( !feof( $handle )) {
  137.                         $data .= fread( $handle, 8192 );
  138.                     }
  139.                     fclose( $handle );
  140.                     preg_match_all( "!<div id=result_box[^>]*>([^<]*)</div>.*!", $data, $matches );
  141.                     if( isset( $matches[1][0] )) {
  142.                         $transObject->mInfo['google_guess'] .= preg_replace( "/".preg_quote( $nl, "/" )."/", "\n", $matches[1][0] );
  143.                     }
  144.                 }
  145.             }
  146. die;
  147.         }
  148.     }
  149. }
  150.  
  151. // store the content
  152. function translation_content_store( $pObject, $pParamHash ) {
  153.     // if we are creating this content and we have a from_id, we know that we're translating a page
  154.     // mInfo['content_id'] isn't set when content is created
  155.     if( empty( $pObject->mInfo['content_id'] ) && @BitBase::verifyId( $_REQUEST['i18n']['from_id'] ) ) {
  156.         $trans = new LibertyTranslations();
  157.         $storeHash = $_REQUEST['i18n'];
  158.         $storeHash['content_id'] = $pParamHash['content_id'];
  159.         if( !$trans->storeTranslation( $storeHash ) ) {
  160.             // error
  161.         }
  162.     }
  163. }
  164.  
  165. function translation_content_exunge( $pObject, $pParamHash ) {
  166.     $trans = new LibertyTranslations( $pObject->mContentId );
  167.     $trans->expunge();
  168. }
  169. ?>

Documentation generated on Wed, 29 Jul 2015 13:57:00 +0000 by phpDocumentor 1.5.0-lsces