TranslationProject.php

Go to the documentation of this file.
00001 <?
00002 /***************************************************************************
00003  *   Copyright (C) 2005 by Ferenc Veres   *
00004  *   lion@netngine.hu   *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  *   This program 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         *
00014  *   GNU General Public License for more details.                          *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU General Public License     *
00017  *   along with this program; if not, write to the                         *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00020  ***************************************************************************/
00021 
00032 class TranslationProject
00033 {
00034         private $ProjectID;     
00035         private $ParentID;      
00036         private $Name;          
00037         private $LocaleID;      
00038         private $Skeleton;      
00039         private $Document;      
00040         private $LastSeen;      
00042         public static $LocaleIDCache; 
00050         public function __construct($loadByID = 0)
00051         {
00052                 if($loadByID != 0)
00053                 {
00054                         $this->LoadByID($loadByID);
00055                 }
00056         }
00057 
00064         public static function InitLocaleIDCache()
00065         {
00066                 $DB =& GetDbConn();
00067                 $Tables =& GetTables();
00068                 
00069                 $rs = $DB->Execute("SELECT ID, Name FROM ".$Tables["Locales"]." ORDER BY Name");
00070                 while($row = $rs->FetchRow()) 
00071                 {
00072                         TranslationProject::$LocaleIDCache[$row["Name"]] = $row["ID"];
00073                 }
00074         }
00075 
00080         private function Clear()
00081         {
00082                 unset($this->ProjectID);
00083                 unset($this->ParentID);
00084                 unset($this->Name);
00085                 unset($this->LocaleID);
00086                 unset($this->Skeleton);
00087                 unset($this->Document);
00088                 unset($this->lastSeen);
00089         }
00090 
00098         private function NameCollision()
00099         {
00100                 $DB =& GetDbConn();
00101                 $Tables =& GetTables();
00102 
00103                 $old = $DB->GetOne("SELECT ID FROM ".$Tables["Projects"]." WHERE ParentID = ? AND Name = ? AND LocaleID = ?", array($this->ParentID, $this->Name, $this->LocaleID));
00104                 if($old)
00105                 {
00106                         return true;
00107                 }
00108                 else
00109                 {
00110                         return false;
00111                 }
00112 
00113         }
00114 
00126         public function CreateRootProject($name, $localeID)
00127         {
00128                 return $this->CreateProject(0, $name, $localeID);
00129         }
00130 
00146         public function CreateProject($parentID, $name, $localeID, $document = '', $skeleton = '')
00147         {
00148                 $this->Clear();
00149                 
00150                 $this->ParentID = $parentID;
00151                 $this->Name = $name;
00152                 $this->LocaleID = $localeID;
00153                 $this->Document = $document;
00154                 $this->Skeleton = $skeleton;
00155                 $this->LastSeen = time();
00156                 
00157                 // Project with this name already exists in this Parent?
00158                 if($this->NameCollision())
00159                 {
00160                         $this->Clear();
00161                         throw new Exception("Project with this name already exists, same location.");
00162                 }
00163         }
00164 
00174         public function Save()
00175         {
00176                 $DB =& GetDbConn();
00177                 $Tables =& GetTables();
00178                 
00179                 // Is this an already saved project?
00180                 if(isset($this->ProjectID))
00181                 {
00182                         // Already saved (or a loaded) project, UPDATE in database
00183                         
00184                         $ok = $DB->Execute("UPDATE ".$Tables["Projects"]." SET ParentID = ?, Name = ?, LocaleID = ?, OrigDoc = ?, Skeleton = ?, LastSeen = now() WHERE ID = ?", array($this->ParentID, $this->Name, $this->LocaleID, $this->Document, $this->Skeleton, $this->ProjectID));
00185                 }
00186                 else
00187                 {
00188                         // New project, INSERT into database
00189                         
00190                         // Project with this name already exists in this Parent?
00191                         if($this->NameCollision())
00192                         {
00193                                 throw new Exception("Project with this name already exists, same location.");
00194                         }
00195                         
00196                         $ok = $DB->Execute("INSERT INTO ".$Tables["Projects"]." (ParentID, Name, LocaleID, OrigDoc, Skeleton, LastSeen) VALUES (?,?,?,?,?, now())", array($this->ParentID, $this->Name, $this->LocaleID, $this->Document, $this->Skeleton));
00197 
00198                         // Get new ID
00199                         if(!$DB->Insert_ID())
00200                         {
00201                                 throw new Exception("DB Error: Insert_ID not supported.");
00202                         }
00203                         $this->ProjectID = $DB->Insert_ID();
00204                 }
00205 
00206                 return $this->ProjectID;
00207         }
00208 
00217         public function LoadRoot($name, $localeID)
00218         {
00219                 return $this->Load(0, $name, $localeID);
00220         }
00221 
00231         public function Load($parentID, $name, $localeID)
00232         {
00233                 $DB =& GetDbConn();
00234                 $Tables =& GetTables();
00235                 
00236                 $rs = $DB->Execute("SELECT ID, ParentID, Name, LocaleID, Skeleton, OrigDoc, TIMESTAMP(LastSeen) as LastSeen FROM ".$Tables["Projects"]." WHERE ParentID = ? AND Name = ? AND LocaleID = ?", array($parentID, $name, $localeID));
00237 
00238                 $retVal = $this->LoadRecordSetEntry($rs);
00239                 $rs->Close();
00240 
00241                 return $retVal;
00242         }
00243 
00244 
00255         public function LoadFromRecordset($recordSet)
00256         {
00257                 return $this->LoadRecordSetEntry($recordSet);
00258         }
00259 
00267         public function LoadByID($projectID)
00268         {
00269                 $DB =& GetDbConn();
00270                 $Tables =& GetTables();
00271 
00272                 $rs = $DB->Execute("SELECT ID, ParentID, Name, LocaleID, Skeleton, OrigDoc, TIMESTAMP(LastSeen) as LastSeen FROM ".$Tables["Projects"]." WHERE ID = ?", array($projectID));
00273                 
00274                 $retVal = $this->LoadRecordSetEntry($rs);
00275                 $rs->Close();
00276                 
00277                 return $retVal;
00278 
00279         }
00280 
00289         private function LoadRecordSetEntry($rs)
00290         {
00291                 $this->Clear();
00292                 
00293                 if($row = $rs->FetchRow())
00294                 {
00295                         $this->ProjectID = $row["ID"];
00296                         $this->ParentID = $row["ParentID"];
00297                         $this->Name = $row["Name"];
00298                         $this->LocaleID = $row["LocaleID"];
00299                         $this->Skeleton = $row["Skeleton"];
00300                         $this->Document = $row["OrigDoc"];
00301                         $this->LastSeen = $row["LastSeen"];
00302                         return true;
00303                 }
00304                 else
00305                 {
00306                         return false;
00307                 }
00308         }
00309 
00315         public function DestroyData()
00316         {
00317                 $DB =& GetDbConn();
00318                 $Tables =& GetTables();
00319 
00320                 $children = new ProjectIterator($this->ProjectID, $this->LocaleID);
00321                 if($children->valid())
00322                 {
00323                         throw new Exception("Cannot destroy data of project ".$this->ProjectID.", because it has children.");
00324                 }
00325 
00326                 $strings = new StringIterator($this->ProjectID);
00327                 if($strings->valid())
00328                 {
00329                         throw new Exception("Cannot destroy data of project ".$this->ProjectID.", because it contains strings.");
00330                 }
00331                 $DB->Execute("DELETE FROM ".$Tables["Projects"]." WHERE ID = ".$this->ProjectID);
00332 
00333                 // Clear cached projects
00334                 ProjectIterator::$ProjectList = null;
00335                 ProjectIterator::$ProjectChildren = null;
00336         }
00337 
00338         /* SET METHODS */
00339 
00346         public function SetName($name)
00347         {
00348                 if(!$name)
00349                 {
00350                         throw new Exception("Cannot set Project Name to emtpy.");
00351                 }
00352                 $this->Name = $name;
00353         }
00354         
00362         public function SetSkeleton($skeleton)
00363         {
00364                 $this->Skeleton = $skeleton;
00365         }
00366 
00373         public function SetDocument($document)
00374         {
00375                 $this->Document = $document;
00376         }
00377 
00378 
00379         /* GET METHODS */
00380         
00386         public function GetName()
00387         {
00388                 if(!isset($this->Name))
00389                 {
00390                         throw new Exception("Uninitialized project, cannot get Name.");
00391                 }
00392                 return $this->Name;
00393         }
00394         
00400         public function GetID()
00401         {
00402                 if(!isset($this->Name))
00403                 {
00404                         throw new Exception("Uninitialized project, cannot get ID.");
00405                 }
00406                 return $this->ProjectID;
00407         }
00408 
00414         public function GetParentID()
00415         {
00416                 if(!isset($this->Name))
00417                 {
00418                         throw new Exception("Uninitialized project, cannot get Parent ID.");
00419                 }
00420                 return $this->ParentID;
00421         }
00422 
00428         public function GetLocaleID()
00429         {
00430                 if(!isset($this->Name))
00431                 {
00432                         throw new Exception("Uninitialized project, cannot get Locale ID.");
00433                 }
00434                 return $this->LocaleID;
00435         }
00436 
00442         public function GetSkeleton()
00443         {
00444                 if(!isset($this->Name))
00445                 {
00446                         throw new Exception("Uninitialized project, cannot get Skeleton.");
00447                 }
00448                 return $this->Skeleton;
00449         }
00450         
00456         public function GetDocument()
00457         {
00458                 if(!isset($this->Name))
00459                 {
00460                         throw new Exception("Uninitialized project, cannot get Document.");
00461                 }
00462                 return $this->Document;
00463         }
00464 
00472         public function GetLastSeen()
00473         {
00474                 if(!isset($this->Name))
00475                 {
00476                         throw new Exception("Uninitialized project, cannot get LastSeen.");
00477                 }
00478                 return $this->LastSeen;
00479         }
00480 
00481 }
00482 
00483 ?>

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