WebBackend.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('TranslationBackend.php');
00023 include_once('export/ExporterFactory.php');
00024 
00033 class WebBackend extends TranslationBackend
00034 {
00035         public $xStringStack;   
00036         public $xBrowserHelper; 
00037         public $chkSearchSelects; 
00043         function __construct()
00044         {
00045                 parent::__construct();
00046         }
00047 
00052         public function EventHandler()
00053         {
00054                 global $_POST;
00055                 
00056                 // Handle Events
00057                 $this->Event = (empty($_POST["__Event"]) ? "" : $_POST["__Event"] );
00058 
00059                 if($this->Event != "")
00060                 {
00061                         
00062                         // Move web variables into local controls
00063 
00064                         $this->xBrowserHelper = (empty($_POST["xBrowserHelper"]) ? "" : $_POST["xBrowserHelper"]);
00065                         $this->txtEditString = (empty($_POST["txtEditString_".$this->xBrowserHelper]) ? "" : $_POST["txtEditString_".$this->xBrowserHelper] );
00066                         $this->txtEditString  = trim($this->txtEditString);
00067 
00068                         $this->txtFindText = (empty($_POST["txtFindText"]) ? "" : $_POST["txtFindText"] );
00069                         $this->ddlFindPercent = (empty($_POST["ddlFindPercent"]) ? 60 : $_POST["ddlFindPercent"]);
00070                         $this->xStringID = (empty($_POST["xStringID"]) ? 0 : $_POST["xStringID"]);
00071                         $this->xProjectID = (empty($_POST["xProjectID"]) ? 0 : $_POST["xProjectID"]);
00072                         $this->ddlLocaleID = (empty($_POST["xOldLocaleID"]) ? "" : $_POST["xOldLocaleID"]);
00073                         $this->xOldLocaleID = (empty($_POST["xOldLocaleID"]) ? "" : $_POST["xOldLocaleID"]);
00074                         $this->ddlSearchType = (empty($_POST["ddlSearchType"]) ? 0 : $_POST["ddlSearchType"]);
00075                         $this->xStringStack = (empty($_POST["xStringStack"]) ? "" : $_POST["xStringStack"]);
00076                         $this->xDelStringID = (empty($_POST["xDelStringID"]) ? "" : $_POST["xDelStringID"]);
00077                         $this->xDelProjectID = (empty($_POST["xDelProjectID"]) ? "" : $_POST["xDelProjectID"]);
00078 
00079                         // Store search results checkboxes if any checked (for "save to all checked" operation)
00080                         if(empty($_POST["chkSearchSelects"]))
00081                         {
00082                                 unset($this->chkSearchSelects);
00083                         }
00084                         else
00085                         {
00086                                 $this->chkSearchSelects = $_POST["chkSearchSelects"];
00087                         }
00088 
00089                         // If no valid root yet, try with posted ddlLocaleID too.
00090                         if($this->xProjectID == 0)
00091                         {
00092                                 $this->InitLanguage();
00093                         }
00094 
00095                         //echo "EVENT:".$this->Event;
00096                         
00097                         switch($this->Event)
00098                         {
00099                                 case "find":
00100                                         $this->Event_FindCustomString();
00101                                         break;
00102                                         
00103                                 case "editstring":
00104                                         $this->Event_EditString();
00105                                         break;
00106                                         
00107                                 case "deletestring":
00108                                         $this->Event_DeleteString();
00109                                         break;
00110                                         
00111                                 case "deleteproject":
00112                                         $this->Event_DeleteProject();
00113                                         break;
00114                                 
00115                                 case "savenextmissing":
00116                                 case "savepopstack":
00117                                 case "savenext":
00118                                 case "nextmissing":
00119                                 case "next":
00120                                 case "save":
00121                                 case "savetoallchecked":
00122                                         $this->Event_SaveNavigate();
00123                                         break;
00124                                         
00125                                 case "setproject":
00126                                         $this->Event_SetProject();
00127                                         break;
00128 
00129                                 case "setlocale":
00130                                         $this->Event_SetLocale();
00131                                         break;
00132                                         
00133                                 case "exportpack":
00134                                         $this->Event_ExportPack();
00135                                         break;
00136 
00137                         }
00138                         
00139 
00140                         // If anything to edit, set browser input field suffix (for browser form cache)
00141                         if(!empty($this->LocatedString))
00142                         {
00143                                 $this->xBrowserHelper = md5($this->LocatedString->GetOriginal());
00144                         }
00145 
00146 
00147                 }
00148         }
00149 
00154         private function Event_FindCustomString()
00155         {
00156                 // Search the string
00157                 $this->FindStrings();
00158 
00159                 if($this->xStringID != 0)
00160                 {
00161                         $this->SyncToString();
00162                 }
00163                 else
00164                 {
00165                         $this->SyncToProject();
00166                 }
00167         }
00168 
00169 
00174         private function Event_EditString()
00175         {
00176                 // Load the selected string and related project paths
00177                 $this->SyncToString();
00178                 
00179                 // Find similar strings
00180                 if(!empty($this->LocatedString))
00181                 {
00182                         $this->ddlSearchType = 0;
00183                         $this->txtFindText = $this->LocatedString->GetOriginal();
00184                         $this->FindStrings();
00185                 }
00186         }
00187 
00192         private function Event_DeleteString()
00193         {
00194                 $this->DeleteString();
00195                 
00196                 if($this->xDelStringID == $this->xStringID)
00197                 {
00198                         // If it was the current string, go back to the first in this project.
00199                         $this->xStringID = 0;
00200                         $this->SyncToProject();
00201                 }
00202                 else
00203                 {
00204                         // Not current string, so sync back to that string
00205                         $this->SyncToString();
00206                 }
00207 
00208                 // Search unchanged
00209                 $this->FindStrings();
00210         }
00211         
00216         private function Event_DeleteProject()
00217         {
00218                 // Deletes project and returns its parent.
00219                 $this->xProjectID = $this->DeleteProject($this->xDelProjectID);
00220                 $this->xStringID = 0;
00221                 $this->SyncToProject();
00222 
00223                 // Search unchanged
00224                 $this->FindStrings();
00225         }
00226 
00231         private function Event_SaveNavigate()
00232         {
00233                 // Save the string??
00234                 if($this->Event == "savenext" ||
00235                         $this->Event == "savenextmissing" ||
00236                         $this->Event == "savepopstack" ||
00237                         $this->Event == "save" ||
00238                         $this->Event == "savetoallchecked")
00239                 {
00240                         $this->SaveString();
00241                 }
00242 
00243                 // Save to all checked search result values?
00244                 if($this->Event == "savetoallchecked")
00245                 {
00246                         if(count($this->chkSearchSelects) > 0)
00247                         {
00248                                 foreach($this->chkSearchSelects as $stringID)
00249                                 {
00250                                         // Load each string from the DB, change it and save changes
00251                                         $string = new TranslationString($stringID);
00252                                         $string->SetTranslation($this->txtEditString);
00253                                         $string->Save();
00254                                 }
00255                         }
00256                 }
00257 
00258                 // How to find the next string?
00259                 $locType = LOCATE_CURR;
00260                 
00261                 if($this->Event == "savenext" || $this->Event == "next")
00262                 {
00263                         $locType = LOCATE_NEXT;
00264                 }
00265                 else if($this->Event == "savenextmissing" || $this->Event == "nextmissing")
00266                 {
00267                         $locType = LOCATE_NEXTMISSING;
00268                 }
00269                 else if($this->Event == "savepopstack")
00270                 {
00271                         // Also locate curr, but we take the last string from stack!
00272                         $locType = LOCATE_CURR;
00273                         
00274                         $stack = split(",", $this->xStringStack);
00275                         $this->xStringID = array_pop($stack);
00276                         $this->xStringStack = join($stack, ",");
00277                 }
00278                 
00279                 // Locate it on the selected way
00280                 $this->LocateString($locType);
00281 
00282                 $this->SyncToString();
00283                 
00284                 // Find similar strings
00285                 if(!empty($this->LocatedString))
00286                 {
00287                         $this->ddlSearchType = 0;
00288                         $this->txtFindText = $this->LocatedString->GetOriginal();
00289                         $this->FindStrings();
00290                 }
00291         }
00292 
00297         private function Event_SetProject()
00298         {
00299                 $this->SyncToProject(); 
00300                 
00301                 // Find similar strings
00302                 if(!empty($this->LocatedString))
00303                 {
00304                         $this->ddlSearchType = 0;
00305                         $this->txtFindText = $this->LocatedString->GetOriginal();
00306                         $this->FindStrings();
00307                 }
00308 
00309         }
00310 
00315         private function Event_SetLocale()
00316         {
00317                 $this->xOldLocaleID = (empty($_POST["ddlLocaleID"]) ? "" : $_POST["ddlLocaleID"]);
00318                 $this->ddlLocaleID = $this->xOldLocaleID;
00319                 $this->InitLanguage();
00320 
00321                 $this->SyncToProject();
00322         }
00323 
00328         private function Event_ExportPack()
00329         {
00330                 global $CONFIG;
00331 
00332                 // Check if exporting is enabled, quit if not
00333                 if(!$CONFIG['enable_web_export'])
00334                 {
00335                         throw new Exception("Web GUI export function is disabled in configuration.");
00336                 }
00337                 
00338                 // Find selected locale's name
00339                 $localeName = "";
00340                 foreach(TranslationProject::$LocaleIDCache as $name => $id)
00341                 {
00342                         if($id == $this->ddlLocaleID)
00343                         {
00344                                 $localeName = $name;
00345                                 break;
00346                         }
00347                 }
00348 
00349                 if($localeName == "")
00350                 {
00351                         throw new Exception("Cannot find locale name for current locale id: " + $this->ddlLocaleID);
00352                 }
00353 
00354                 // Create a Xaraya exporter
00355                 // TODO: (Hardcoded project type for now!)
00356                 $factory = new ExporterFactory();
00357                 $proc = $factory->NewExporter("Xaraya");
00358 
00359                 // Export to the configured output location
00360                 $proc->Export($localeName, true);
00361                 
00362                 // Compress the dir with TAR
00363                 $languagePack = $localeName."_".date('Y-m-d').".tar.gz";
00364                 system("tar -C ".$CONFIG['export_path']." --exclude $localeName/php -czf ".$CONFIG['language_pack_dir']."/$languagePack $localeName", $ret);
00365                 if($ret != 0)
00366                 {
00367                         echo("<p>Tar returned $ret, the package may be incomplete.</p>");
00368                 }
00369                 $link = $CONFIG['language_pack_url']."/$languagePack";
00370                 echo("<p>Language pack saved to: <a href=\"$link\">$link</a>.</p>");
00371         
00372                 // Sync controls
00373                 if($this->xStringID != 0)
00374                 {
00375                         $this->SyncToString();
00376                 }
00377                 else
00378                 {
00379                         $this->SyncToProject();
00380                 }
00381         }
00382 
00387         private function SyncToProject()
00388         {
00389                 $this->xStringID = 0;
00390                 
00391                 if($this->xProjectID == 0)
00392                 {
00393                         return;
00394                 }
00395 
00396                 // Load the project's strings
00397                 $this->LoadProjectStrings();
00398                 
00399                 if($this->CurrentStrings->valid())
00400                 {
00401                         // If there are strings, take the first one and Load it
00402                         $this->xStringID = $this->CurrentStrings->current()->GetStringID();
00403                         $this->LocateString();
00404 
00405                         // Find similar strings
00406                         $this->txtFindText = $this->LocatedString->GetOriginal();
00407                         $this->FindStrings();
00408                 }
00409                 else
00410                 {
00411                         // Forget last string ID if no strings for this project.
00412                         $this->xStringID = 0;
00413                 }
00414 
00415                 // Load related project tree too
00416                 $this->FindProjectPath();
00417         }
00418 
00423         private function SyncToString()
00424         {
00425                 if($this->xStringID == 0)
00426                 {
00427                         return;
00428                 }
00429                 
00430                 $this->LocateString();
00431                 
00432                 if(!empty($this->LocatedString))
00433                 {
00434                         // If found the string, load related project info
00435                         $this->xProjectID = $this->LocatedString->GetProjectID();
00436                         $this->LoadProjectStrings();
00437                 }
00438 
00439                 // Load related project tree too
00440                 $this->FindProjectPath();
00441         }
00442 
00443 }

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