TranslationBackend.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /***************************************************************************
00004  *   Copyright (C) 2005 by Ferenc Veres   *
00005  *   lion@netngine.hu   *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 include_once('config.inc.php');
00023 include_once('lib/Common.php');
00024 
00025 $DB = NewADOConnection(GetConfigVar('db_software'));
00026 $DB->Connect(GetConfigVar('db_host'), GetConfigVar('db_username') , GetConfigVar('db_password'), GetConfigVar('db_database'));
00027 $DB->Execute("SET NAMES utf8");
00028 CacheIndexes();
00029 
00030 define('LOCATE_CURR', 0);
00031 define('LOCATE_NEXT', 1);
00032 define('LOCATE_NEXTMISSING', 2);
00033 define('LOCATE_PREV', 3);
00034 define('LOCATE_PREVMISSING', 4);
00035 
00044 class TranslationBackend
00045 {
00046         public $Event;  
00048         // CONTROLS
00049         
00050         public $txtFindText;    
00051         public $ddlSearchType;  
00052         public $ddlFindPercent; 
00053         public $ddlLocaleID;    
00054         public $xOldLocaleID;   
00055         public $xStringID;      
00056         public $xDelStringID;   
00057         public $txtEditString;  
00059         // OUTPUT
00060 
00061         public $Translations;   
00062         public $LocatedString;  
00063         public $LocatedProjects;
00064         public $ChildProjects;  
00065         public $CurrentStrings; 
00066         public $RootLastSeen;   
00068         // PRIVATE 
00069 
00070         private $String;        
00079         function __construct()
00080         {
00081                 // Get and use default locale
00082                 $localeName = GetConfigVar('default_locale');
00083                 $localeID = TranslationProject::$LocaleIDCache[$localeName];
00084                 if(empty($localeID))
00085                 {
00086                         throw new Exception("Invalid default locale in the configuration: $localeName");
00087                 }
00088                 
00089                 // Create a new translation string handler class
00090                 $this->String = new TranslationString();
00091                 $this->DB = GetDbConn();
00092                 $this->Tables = GetTables();
00093 
00094                 $this->txtFindText = "";
00095                 $this->ddlSearchType = 0;
00096                 $this->ddlFindPercent = 60;
00097                 $this->ddlLocaleID = $localeID;
00098                 $this->xOldLocaleID = $localeID;
00099                 $this->xStringID = 0;
00100                 $this->xProjectID = 0;
00101 
00102                 $this->InitLanguage();
00103         }
00104 
00109         public function InitLanguage()
00110         {
00111                 // Find a legal Xaraya root project
00112                 $proj = new TranslationProject();
00113                 if($proj->LoadRoot("Xaraya", $this->ddlLocaleID))
00114                 {
00115                         $this->xProjectID = $proj->GetID();
00116                         $this->FindProjectPath();
00117                 }
00118         }
00119         
00135         function FindStrings()
00136         {
00137                 switch($this->ddlSearchType)
00138                 {
00139 
00140                         case 1:
00141                                 // Find in Original as part of the string
00142                                 $this->Translations = $this->String->FindText($this->txtFindText, $this->ddlLocaleID, true);
00143                                 break;
00144                         
00145                         case 2:
00146                                 // Find in Translation as part of the string
00147                                 $this->Translations = $this->String->FindText($this->txtFindText, $this->ddlLocaleID, false);
00148                                 break;
00149 
00150                         default:
00151                                 // Similarity matching
00152                                 $this->Translations = $this->String->FindSimilar($this->txtFindText, $this->ddlLocaleID, $this->ddlFindPercent);
00153                                 break;
00154                 }
00155         }
00156 
00169         function LocateString($locate = LOCATE_CURR)
00170         {
00171                 // Load string details
00172                 switch($locate)
00173                 {
00174                         case LOCATE_CURR:
00175                                 $this->LocatedString = new TranslationString($this->xStringID);
00176                                 break;
00177 
00178                         case LOCATE_NEXT:
00179                         case LOCATE_NEXTMISSING:
00180                                 $this->LocateStringNext( ($locate == LOCATE_NEXTMISSING) );
00181                                 break;
00182                 }
00183         }
00184 
00189         private function LocateStringNext($missingOnly)
00190         {
00191                 unset($this->LocatedString);
00192 
00193                 // Skip string-in-project search if xStringID is empty:
00194                 // which means there were no strings in this project
00195 
00196                 if($this->xStringID != 0)
00197                 {
00198                         // Take current string and an iterator for it's siblings
00199                         $currentString = new TranslationString($this->xStringID);
00200                         $stringIter = new StringIterator($currentString->GetProjectID());
00201         
00202                         // FLAT SEARCH - STRINGS OF THE CURRENT PROJECT
00203                         
00204                         // Find current and then take next. If we are last, this will end with false.
00205                         $takeString = false;
00206                         foreach($stringIter as $str)
00207                         {
00208                                 if($takeString)
00209                                 {
00210                                         $this->LocatedString = new TranslationString($str->GetStringID());
00211         
00212                                         // If we find Missing Strings, ignore non-empty results!
00213                                         if($missingOnly && $this->LocatedString->GetTranslation() != "")
00214                                         {
00215                                                 // Need to search a missing one, drop that result
00216                                                 unset($this->LocatedString);
00217                                         }
00218                                         else
00219                                         {
00220                                                 // Found, break!
00221                                                 $this->xStringID = $str->GetStringID();
00222                                                 break;
00223                                         }
00224                                 }
00225                                 
00226                                 // If reached the current string, note to take the next one
00227                                 if($str->GetStringID() == $currentString->GetStringID())
00228                                 {
00229                                         $takeString = true;
00230                                 }
00231                         }
00232                 }
00233                 
00234                 // RECURSIVE SEARCH - UPWARDS AND DOWNWARDS
00235                 
00236                 // If this was a "last string" in the project, we will search across projects!
00237                 $currentProject = new TranslationProject($this->xProjectID);
00238 
00239                 // Stop variable for reaching root
00240                 $rootHit = false;
00241 
00242                 // Search next string across projects!
00243                 while(empty($this->LocatedString) && !$rootHit)
00244                 {
00245                         $takeProject = false;
00246 
00247                         // Start an iterator on sibling and subprojects
00248                         $parentProjects = new RecursiveIteratorIterator(
00249                                 new ProjectIterator($currentProject->GetParentID(), $this->ddlLocaleID),
00250                                 RecursiveIteratorIterator::SELF_FIRST);
00251 
00252                         // Loop on the projects and find the current one (which hit last string above!)
00253                         foreach($parentProjects as $proj)
00254                         {
00255                                 // This happens after the "last-string-hit" project, take next project:
00256                                 if($takeProject)
00257                                 {
00258                                         // Get strings of all next projects and see if there are any strings.
00259                                         $stringIter = new StringIterator($proj->GetID());
00260 
00261                                         // There are strings, take the first and we are done! (in not missing ok)
00262                                         if(!$missingOnly)
00263                                         {
00264                                                 // Any strings? If yes, found, break!
00265                                                 if($stringIter->valid())
00266                                                 {
00267                                                         $this->xStringID = $stringIter->current()->GetStringID();
00268                                                         $this->LocatedString = new TranslationString($this->xStringID);
00269                                                         break;
00270                                                 }
00271                                         }
00272                                         else
00273                                         {
00274                                                 $foundEmpty = false;
00275                                                 // Missing only, so loop on all strings
00276                                                 // If we find Missing Strings, ignore non-empty results!
00277                                                 foreach($stringIter as $str)
00278                                                 {
00279                                                         if($str->GetTranslation() == "")
00280                                                         {
00281                                                                 $this->xStringID = $str->GetStringID();
00282                                                                 $this->LocatedString = new TranslationString($this->xStringID);
00283                                                                 $foundEmpty = true;
00284                                                                 break;
00285                                                         }
00286                                                 }
00287 
00288                                                 // If iterator not finished, we found the result
00289                                                 if($foundEmpty)
00290                                                 {
00291                                                         break;
00292                                                 }
00293                                         }
00294                                 }
00295 
00296                                 // Found the "last-string-hit" project?
00297                                 if($proj->GetID() == $this->xProjectID)
00298                                 {
00299                                         $takeProject = true;
00300                                 }
00301                         }
00302 
00303                         // Stop working if taking parent would result in looping on root!
00304                         $rootHit = ($currentProject->GetParentID() == 0);
00305 
00306                         // If not root and not found a string, recurse UPWARDS
00307                         if($currentProject->GetParentID() != 0 && empty($this->LocatedString))
00308                         {
00309                                 $currentProject = new TranslationProject($currentProject->GetParentID());
00310                         }
00311 
00312                 }
00313         }
00327         function FindProjectPath()
00328         {
00329                 unset ($this->LocatedProjects);
00330                 unset ($this->ChildProjects);
00331 
00332                 $pid = $this->xProjectID;
00333                 
00334                 // Loop and collect projects until reaching the end
00335                 while($pid != 0)
00336                 {
00337                         $proj = new TranslationProject($pid);
00338                         $this->LocatedProjects[] = $proj;
00339                         $pid = $proj->GetParentID();
00340 
00341                         // Store the root project's date for dead strings/projects timestamp
00342                         if($pid == 0)
00343                         {
00344                                 $this->RootLastSeen = $proj->GetLastSeen();
00345                         }
00346                 }
00347                 
00348                 // Make a new project Iterator with current parent to see if there are children
00349                 $this->ChildProjects = new ProjectIterator($this->xProjectID, $this->ddlLocaleID);
00350         }
00351 
00360         function SaveString()
00361         {
00362                 // Load our string from the DB, change it and save changes
00363                 $string = new TranslationString($this->xStringID);
00364                 $string->SetTranslation($this->txtEditString);
00365                 $string->Save();
00366         }
00367 
00372         function DeleteString()
00373         {
00374                 // Load our string
00375                 $string = new TranslationString($this->xDelStringID);
00376                 $string->DestroyData();
00377         }
00378 
00385         function DeleteProject($projectID)
00386         {
00387                 $project = new TranslationProject($projectID);
00388                 $parent = $project->GetParentID();
00389                 $project->DestroyData();
00390                 return $parent;
00391         }
00392 
00397         function LoadProjectStrings()
00398         {
00399                 $this->CurrentStrings = new StringIterator($this->xProjectID);
00400         }
00401 }
00402 
00403 ?>

Generated on Tue Mar 20 00:42:40 2007 for XarayaTranslationMemory by  doxygen 1.4.7