XarayaTranslationMemory  1.6
TranslationProject.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************************
3  * Copyright (C) 2005-2020 by Ferenc Veres *
4  * lion@netngine.hu *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 3 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18  ***************************************************************************/
19 
32 {
33  private $ProjectID;
34  private $ParentID;
35  private $RootID;
36  private $Name;
37  private $IsFolder;
38  private $LocaleID;
39  private $Skeleton;
40  private $Document;
41  private $LastSeen;
42  private $Brief;
44  public static $LocaleIDCache;
53  public function __construct($loadByID = 0, $brief = false)
54  {
55  if($loadByID != 0)
56  {
57  $this->LoadByID($loadByID, $brief);
58  }
59  }
60 
67  public static function InitLocaleIDCache()
68  {
69  $DB =& GetDbConn();
70  $Tables =& GetTables();
71 
72  $rs = $DB->Execute("SELECT ID, Name FROM ".$Tables["Locales"]." ORDER BY Name");
73  while($row = $rs->FetchRow())
74  {
75  TranslationProject::$LocaleIDCache[$row["Name"]] = $row["ID"];
76  }
77  }
78 
85  public static function GetRoots($localeID)
86  {
87  $DB =& GetDbConn();
88  $Tables =& GetTables();
89 
90  $roots = array();
91  $rs = $DB->Execute("SELECT ID FROM ".$Tables["Projects"]." WHERE ParentID = 0 AND LocaleID = ? ORDER BY Name", array($localeID));
92  while($row = $rs->FetchRow())
93  {
94  $proj = new TranslationProject();
95  $proj->LoadByID($row["ID"], false);
96  $roots[$row["ID"]] = $proj;
97  }
98  return $roots;
99  }
100 
105  private function Clear()
106  {
107  }
108 
116  private function CheckNameCollision()
117  {
118  $DB =& GetDbConn();
119  $Tables =& GetTables();
120 
121  $old = $DB->GetOne("SELECT ID FROM ".$Tables["Projects"]." WHERE ParentID = ? AND Name = ? AND LocaleID = ?", array($this->ParentID, $this->Name, $this->LocaleID));
122  if($old)
123  {
124  return true;
125  }
126  else
127  {
128  return false;
129  }
130 
131  }
132 
144  public function CreateRootProject($name, $localeID)
145  {
146  return $this->CreateProject(0, 0, $name, true, $localeID);
147  }
148 
165  public function CreateProject($rootID, $parentID, $name, $isFolder, $localeID, $document = '', $skeleton = '')
166  {
167  $this->RootID = $rootID;
168  $this->ParentID = $parentID;
169  $this->Name = $name;
170  $this->IsFolder = $isFolder;
171  $this->LocaleID = $localeID;
172  $this->Document = $document;
173  $this->Skeleton = $skeleton;
174  $this->LastSeen = time();
175  $this->Brief = false; // Newly created project is never brief, as we most likely want to save it
176 
177  // Project with this name already exists in this Parent?
178  if($this->CheckNameCollision())
179  {
180  $this->Clear();
181  throw new Exception("Project with this name already exists, same location.");
182  }
183  }
184 
194  public function Save()
195  {
196  $DB =& GetDbConn();
197  $Tables =& GetTables();
198 
199  if($this->IsFolder && ($this->Document != '' || $this->Skeleton != ''))
200  {
201  throw new Exception("Folder project must not contain a document or skeleton.");
202  }
203 
204  if($this->Brief)
205  {
206  throw new Exception("Brief=True projects cannot be saved or updated. They are read-only.");
207  }
208 
209  // Is this an already saved project?
210  if(isset($this->ProjectID))
211  {
212  // Already saved (or a loaded) project, UPDATE in database (except RootID)
213 
214  $ok = $DB->Execute("UPDATE ".$Tables["Projects"]." SET ParentID = ?, Name = ?, IsFolder = ?, LocaleID = ?, OrigDoc = ?, Skeleton = ?, LastSeen = now() WHERE ID = ?", array($this->ParentID, $this->Name, ($this->IsFolder ? 1 : 0), $this->LocaleID, $this->Document, $this->Skeleton, $this->ProjectID));
215  }
216  else
217  {
218  // New project, INSERT into database
219 
220  // Project with this name already exists in this Parent?
221  if($this->CheckNameCollision())
222  {
223  throw new Exception("Project with this name already exists, same location.");
224  }
225 
226  $root = ($this->RootID > 0 ? $this->RootID : null);
227  $ok = $DB->Execute("INSERT INTO ".$Tables["Projects"]." (RootID, ParentID, Name, IsFolder, LocaleID, OrigDoc, Skeleton, LastSeen) VALUES (?,?,?,?,?,?,?, now())", array($root, $this->ParentID, $this->Name, ($this->IsFolder ? 1 : 0), $this->LocaleID, $this->Document, $this->Skeleton));
228 
229  // Get new ID
230  if(!$DB->Insert_ID())
231  {
232  throw new Exception("DB Error: Insert_ID not supported.");
233  }
234  $this->ProjectID = $DB->Insert_ID();
235  }
236 
237  return $this->ProjectID;
238  }
239 
248  public function LoadRoot($name, $localeID)
249  {
250  return $this->Load(0, $name, $localeID, false);
251  }
252 
262  public function Load($parentID, $name, $localeID, $brief)
263  {
264  $DB =& GetDbConn();
265  $Tables =& GetTables();
266 
267  $this->Brief = $brief;
268  if($this->Brief)
269  {
270  $sql = "SELECT ID, RootID, ParentID, Name, IsFolder, LocaleID,".
271  "TIMESTAMP(LastSeen) as LastSeen ".
272  "FROM ".$Tables["Projects"].
273  " WHERE ParentID = ? AND Name = ? AND LocaleID = ?";
274  }
275  else
276  {
277  $sql = "SELECT ID, RootID, ParentID, Name, IsFolder, LocaleID,".
278  "Skeleton, OrigDoc, TIMESTAMP(LastSeen) as LastSeen ".
279  "FROM ".$Tables["Projects"].
280  " WHERE ParentID = ? AND Name = ? AND LocaleID = ?";
281  }
282 
283  $rs = $DB->Execute($sql, array($parentID, $name, $localeID));
284 
285  $retVal = $this->LoadRecordSetEntry($rs);
286  $rs->Close();
287 
288  return $retVal;
289  }
290 
291 
302  public function LoadFromRecordSet($recordSet, $brief)
303  {
304  $this->Brief = $brief;
305  return $this->LoadRecordSetEntry($recordSet);
306  }
307 
315  public function LoadByID($projectID, $brief)
316  {
317  $DB =& GetDbConn();
318  $Tables =& GetTables();
319 
320  $this->Brief = $brief;
321  if($this->Brief)
322  {
323  $sql ="SELECT ID, RootID, ParentID, Name, IsFolder, LocaleID,".
324  "TIMESTAMP(LastSeen) as LastSeen ".
325  "FROM ".$Tables["Projects"]." WHERE ID = ?";
326  }
327  else
328  {
329  $sql ="SELECT ID, RootID, ParentID, Name, IsFolder, LocaleID,".
330  "Skeleton, OrigDoc, TIMESTAMP(LastSeen) as LastSeen ".
331  "FROM ".$Tables["Projects"]." WHERE ID = ?";
332  }
333 
334  $rs = $DB->Execute($sql, array($projectID));
335 
336  $retVal = $this->LoadRecordSetEntry($rs);
337  $rs->Close();
338 
339  return $retVal;
340 
341  }
342 
351  private function LoadRecordSetEntry($rs)
352  {
353  if($row = $rs->FetchRow())
354  {
355  $this->ProjectID = $row["ID"];
356  if($row["RootID"] == null)
357  {
358  $this->RootID = 0;
359  }
360  else
361  {
362  $this->RootID = $row["RootID"];
363  }
364  $this->ParentID = $row["ParentID"];
365  $this->Name = $row["Name"];
366  $this->IsFolder = ($row["IsFolder"] == 1);
367  $this->LocaleID = $row["LocaleID"];
368  $this->LastSeen = $row["LastSeen"];
369 
370  if($this->Brief)
371  {
372  unset($this->Skeleton);
373  unset($this->Document);
374  }
375  else
376  {
377  $this->Skeleton = $row["Skeleton"];
378  $this->Document = $row["OrigDoc"];
379  }
380  return true;
381  }
382  else
383  {
384  unset($this->ProjectID);
385  unset($this->RootID);
386  unset($this->ParentID);
387  unset($this->Name);
388  unset($this->IsFolder);
389  unset($this->LocaleID);
390  unset($this->Skeleton);
391  unset($this->Document);
392  unset($this->lastSeen);
393  unset($this->ProjectID);
394  return false;
395  }
396  }
397 
403  public function DestroyData()
404  {
405  $DB =& GetDbConn();
406  $Tables =& GetTables();
407 
408  $children = new ProjectIterator($this->ProjectID, $this->LocaleID);
409  if($children->valid())
410  {
411  throw new Exception("Cannot destroy data of project ".$this->ProjectID.", because it has children.");
412  }
413 
414  $strings = new StringIterator($this->ProjectID);
415  if($strings->valid())
416  {
417  throw new Exception("Cannot destroy data of project ".$this->ProjectID.", because it contains strings.");
418  }
419  $DB->Execute("DELETE FROM ".$Tables["Projects"]." WHERE ID = ".$this->ProjectID);
420 
421  // Clear cached projects
422  ProjectIterator::ClearCache($this->LocaleID);
423  }
424 
425  /* SET METHODS */
426 
433  public function SetName($name)
434  {
435  if(!$name)
436  {
437  throw new Exception("Cannot set Project Name to emtpy.");
438  }
439  $this->Name = $name;
440  }
441 
449  public function SetSkeleton($skeleton)
450  {
451  $this->Skeleton = $skeleton;
452  }
453 
460  public function SetDocument($document)
461  {
462  $this->Document = $document;
463  }
464 
465 
466  /* GET METHODS */
467 
473  public function GetName()
474  {
475  if(!isset($this->Name))
476  {
477  throw new Exception("Uninitialized project, cannot get Name.");
478  }
479  return $this->Name;
480  }
481 
487  public function GetID()
488  {
489  if(!isset($this->Name))
490  {
491  throw new Exception("Uninitialized project, cannot get ID.");
492  }
493  return $this->ProjectID;
494  }
495 
501  public function GetRootID()
502  {
503  if(!isset($this->Name))
504  {
505  throw new Exception("Uninitialized project, cannot get Root ID.");
506  }
507  return $this->RootID;
508  }
509 
515  public function GetParentID()
516  {
517  if(!isset($this->Name))
518  {
519  throw new Exception("Uninitialized project, cannot get Parent ID.");
520  }
521  return $this->ParentID;
522  }
523 
529  public function GetIsFolder()
530  {
531  if(!isset($this->Name))
532  {
533  throw new Exception("Uninitialized project, cannot get IsFolder.");
534  }
535  return $this->IsFolder;
536  }
537 
543  public function GetLocaleID()
544  {
545  if(!isset($this->Name))
546  {
547  throw new Exception("Uninitialized project, cannot get Locale ID.");
548  }
549  return $this->LocaleID;
550  }
551 
557  public function GetSkeleton()
558  {
559  if(!isset($this->Name))
560  {
561  throw new Exception("Uninitialized project, cannot get Skeleton.");
562  }
563  return $this->Skeleton;
564  }
565 
571  public function GetDocument()
572  {
573  if(!isset($this->Name))
574  {
575  throw new Exception("Uninitialized project, cannot get Document.");
576  }
577  return $this->Document;
578  }
579 
587  public function GetLastSeen()
588  {
589  if(!isset($this->Name))
590  {
591  throw new Exception("Uninitialized project, cannot get LastSeen.");
592  }
593  return $this->LastSeen;
594  }
595 }
596 
597 ?>
CreateProject($rootID, $parentID, $name, $isFolder, $localeID, $document='', $skeleton='')
Load($parentID, $name, $localeID, $brief)
& GetTables()
Definition: Common.php:71
& GetDbConn()
Definition: Common.php:61
LoadByID($projectID, $brief)
LoadRoot($name, $localeID)
__construct($loadByID=0, $brief=false)
LoadFromRecordSet($recordSet, $brief)
static ClearCache($localeID)
$DB
Definition: export.php:28
CreateRootProject($name, $localeID)
static GetRoots($localeID)