Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

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 CacheIndexes();
00028 
00029 define('LOCATE_CURR', 0);
00030 define('LOCATE_NEXT', 1);
00031 define('LOCATE_NEXTMISSING', 2);
00032 define('LOCATE_PREV', 3);
00033 define('LOCATE_PREVMISSING', 4);
00034 
00043 class TranslationBackend
00044 {
00045         public $Event;  
00047         // CONTROLS
00048         
00049         public $txtFindText;    
00050         public $ddlSearchType;  
00051         public $ddlFindPercent; 
00052         public $ddlLocaleID;    
00053         public $xOldLocaleID;   
00054         public $xStringID;      
00055         public $xDelStringID;   
00056         public $txtEditString;  
00058         // OUTPUT
00059 
00060         public $Translations;   
00061         public $LocatedString;  
00062         public $LocatedProjects;
00063         public $ChildProjects;  
00064         public $CurrentStrings; 
00065         public $RootLastSeen;   
00067         // PRIVATE 
00068 
00069         private $String;        
00078         function __construct()
00079         {
00080                 // Create a new translation string handler class
00081                 $this->String = new TranslationString();
00082                 $this->DB = GetDbConn();
00083                 $this->Tables = GetTables();
00084 
00085                 $this->txtFindText = "";
00086                 $this->ddlSearchType = 0;
00087                 $this->ddlFindPercent = 60;
00088                 $this->ddlLocaleID = 1;
00089                 $this->xOldLocaleID = 1;
00090                 $this->xStringID = 0;
00091                 $this->xProjectID = 0;
00092 
00093                 $this->InitLanguage();
00094         }
00095 
00100         public function InitLanguage()
00101         {
00102                 // Find a legal Xaraya root project
00103                 $proj = new TranslationProject();
00104                 if($proj->LoadRoot("Xaraya", $this->ddlLocaleID))
00105                 {
00106                         $this->xProjectID = $proj->GetID();
00107                         $this->FindProjectPath();
00108                 }
00109         }
00110         
00126         function FindStrings()
00127         {
00128                 switch($this->ddlSearchType)
00129                 {
00130 
00131                         case 1:
00132                                 // Find in Original as part of the string
00133                                 $this->Translations = $this->String->FindText($this->txtFindText, $this->ddlLocaleID, true);
00134                                 break;
00135                         
00136                         case 2:
00137                                 // Find in Translation as part of the string
00138                                 $this->Translations = $this->String->FindText($this->txtFindText, $this->ddlLocaleID, false);
00139                                 break;
00140 
00141                         default:
00142                                 // Similarity matching
00143                                 $this->Translations = $this->String->FindSimilar($this->txtFindText, $this->ddlLocaleID, $this->ddlFindPercent);
00144                                 break;
00145                 }
00146         }
00147 
00160         function LocateString($locate = LOCATE_CURR)
00161         {
00162                 // Load string details
00163                 switch($locate)
00164                 {
00165                         case LOCATE_CURR:
00166                                 $this->LocatedString = new TranslationString($this->xStringID);
00167                                 break;
00168 
00169                         case LOCATE_NEXT:
00170                         case LOCATE_NEXTMISSING:
00171                                 $this->LocateStringNext( ($locate == LOCATE_NEXTMISSING) );
00172                                 break;
00173                 }
00174         }
00175 
00180         private function LocateStringNext($missingOnly)
00181         {
00182                 unset($this->LocatedString);
00183 
00184                 // Skip string-in-project search if xStringID is empty:
00185                 // which means there were no strings in this project
00186 
00187                 if($this->xStringID != 0)
00188                 {
00189                         // Take current string and an iterator for it's siblings
00190                         $currentString = new TranslationString($this->xStringID);
00191                         $stringIter = new StringIterator($currentString->GetProjectID());
00192         
00193                         // FLAT SEARCH - STRINGS OF THE CURRENT PROJECT
00194                         
00195                         // Find current and then take next. If we are last, this will end with false.
00196                         $takeString = false;
00197                         foreach($stringIter as $str)
00198                         {
00199                                 if($takeString)
00200                                 {
00201                                         $this->LocatedString = new TranslationString($str->GetStringID());
00202         
00203                                         // If we find Missing Strings, ignore non-empty results!
00204                                         if($missingOnly && $this->LocatedString->GetTranslation() != "")
00205                                         {
00206                                                 // Need to search a missing one, drop that result
00207                                                 unset($this->LocatedString);
00208                                         }
00209                                         else
00210                                         {
00211                                                 // Found, break!
00212                                                 $this->xStringID = $str->GetStringID();
00213                                                 break;
00214                                         }
00215                                 }
00216                                 
00217                                 // If reached the current string, note to take the next one
00218                                 if($str->GetStringID() == $currentString->GetStringID())
00219                                 {
00220                                         $takeString = true;
00221                                 }
00222                         }
00223                 }
00224                 
00225                 // RECURSIVE SEARCH - UPWARDS AND DOWNWARDS
00226                 
00227                 // If this was a "last string" in the project, we will search across projects!
00228                 $currentProject = new TranslationProject($this->xProjectID);
00229 
00230                 // Stop variable for reaching root
00231                 $rootHit = false;
00232 
00233                 // Search next string across projects!
00234                 while(empty($this->LocatedString) && !$rootHit)
00235                 {
00236                         $takeProject = false;
00237 
00238                         // Start an iterator on sibling and subprojects
00239                         $parentProjects = new RecursiveIteratorIterator(
00240                                 new ProjectIterator($currentProject->GetParentID(), $this->ddlLocaleID),
00241                                 RecursiveIteratorIterator::SELF_FIRST);
00242 
00243                         // Loop on the projects and find the current one (which hit last string above!)
00244                         foreach($parentProjects as $proj)
00245                         {
00246                                 // This happens after the "last-string-hit" project, take next project:
00247                                 if($takeProject)
00248                                 {
00249                                         // Get strings of all next projects and see if there are any strings.
00250                                         $stringIter = new StringIterator($proj->GetID());
00251 
00252                                         // There are strings, take the first and we are done! (in not missing ok)
00253                                         if(!$missingOnly)
00254                                         {
00255                                                 // Any strings? If yes, found, break!
00256                                                 if($stringIter->valid())
00257                                                 {
00258                                                         $this->xStringID = $stringIter->current()->GetStringID();
00259                                                         $this->LocatedString = new TranslationString($this->xStringID);
00260                                                         break;
00261                                                 }
00262                                         }
00263                                         else
00264                                         {
00265                                                 $foundEmpty = false;
00266                                                 // Missing only, so loop on all strings
00267                                                 // If we find Missing Strings, ignore non-empty results!
00268                                                 foreach($stringIter as $str)
00269                                                 {
00270                                                         if($str->GetTranslation() == "")
00271                                                         {
00272                                                                 $this->xStringID = $str->GetStringID();
00273                                                                 $this->LocatedString = new TranslationString($this->xStringID);
00274                                                                 $foundEmpty = true;
00275                                                                 break;
00276                                                         }
00277                                                 }
00278 
00279                                                 // If iterator not finished, we found the result
00280                                                 if($foundEmpty)
00281                                                 {
00282                                                         break;
00283                                                 }
00284                                         }
00285                                 }
00286 
00287                                 // Found the "last-string-hit" project?
00288                                 if($proj->GetID() == $this->xProjectID)
00289                                 {
00290                                         $takeProject = true;
00291                                 }
00292                         }
00293 
00294                         // Stop working if taking parent would result in looping on root!
00295                         $rootHit = ($currentProject->GetParentID() == 0);
00296 
00297                         // If not root and not found a string, recurse UPWARDS
00298                         if($currentProject->GetParentID() != 0 && empty($this->LocatedString))
00299                         {
00300                                 $currentProject = new TranslationProject($currentProject->GetParentID());
00301                         }
00302 
00303                 }
00304         }
00318         function FindProjectPath()
00319         {
00320                 unset ($this->LocatedProjects);
00321                 unset ($this->ChildProjects);
00322 
00323                 $pid = $this->xProjectID;
00324                 
00325                 // Loop and collect projects until reaching the end
00326                 while($pid != 0)
00327                 {
00328                         $proj = new TranslationProject($pid);
00329                         $this->LocatedProjects[] = $proj;
00330                         $pid = $proj->GetParentID();
00331 
00332                         // Store the root project's date for dead strings/projects timestamp
00333                         if($pid == 0)
00334                         {
00335                                 $this->RootLastSeen = $proj->GetLastSeen();
00336                         }
00337                 }
00338                 
00339                 // Make a new project Iterator with current parent to see if there are children
00340                 $this->ChildProjects = new ProjectIterator($this->xProjectID, $this->ddlLocaleID);
00341         }
00342 
00351         function SaveString()
00352         {
00353                 // Load our string from the DB.
00354                 $string = new TranslationString($this->xStringID);
00355                 $string->SetTranslation($this->txtEditString);
00356                 $string->Save();
00357         }
00358 
00363         function DeleteString()
00364         {
00365                 // Load our string
00366                 $string = new TranslationString($this->xDelStringID);
00367                 $string->DestroyData();
00368         }
00373         function LoadProjectStrings()
00374         {
00375                 $this->CurrentStrings = new StringIterator($this->xProjectID);
00376         }
00377 }
00378 
00379 ?>

Generated on Sat Apr 22 16:49:54 2006 for XarayaTranslationMemory by  doxygen 1.4.4