XarayaTranslationMemory  1.6
ProjectIterator.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 include_once('TranslationProject.php');
20 
29 class ProjectIterator implements Iterator, RecursiveIterator
30 {
32  private $LocaleID;
34  private $ParentID;
35 
37  private $CurrentProject;
38 
40  private $Brief;
41 
43  public static $ProjectList;
45  public static $ProjectChildren;
46 
54  public function __construct($parentID, $localeID, $brief = false)
55  {
56  global $CONFIG;
57 
58  $this->Brief = $brief;
59 
60  $DB =& GetDbConn();
61  $Tables =& GetTables();
62 
63  $this->LocaleID = $localeID;
64  $this->ParentID = $parentID;
65 
66  // Create cache file names
67  if($CONFIG['disk_cache'])
68  {
69  // These names are duplicated in ClearCache!!!
70  $cacheFile = $CONFIG['disk_cache']."/ProjectList_".$this->LocaleID.".cache";
71  $cacheFile2 = $CONFIG['disk_cache']."/ProjectChildren_".$this->LocaleID.".cache";
72  if($this->Brief)
73  {
74  $cacheFile .= ".brief";
75  $cacheFile2 .= ".brief";
76  }
77  }
78 
79  // Cache all projects for the current language, unless done already
80  if(empty(self::$ProjectList[$this->LocaleID]))
81  {
82  // Load from file cache
83  if($CONFIG['disk_cache'] && file_exists($cacheFile) && file_exists($cacheFile2))
84  {
85  self::$ProjectList[$this->LocaleID] = unserialize(file_get_contents($cacheFile));
86  self::$ProjectChildren[$this->LocaleID] = unserialize(file_get_contents($cacheFile2));
87  }
88 
89  // No file cache, reload from DB.
90  if(empty(self::$ProjectList[$this->LocaleID]))
91  {
92  if($this->Brief)
93  {
94  $sql = "SELECT ID, RootID, ParentID, Name, IsFolder, LocaleID,".
95  "TIMESTAMP(LastSeen) as LastSeen ".
96  "FROM ".$Tables["Projects"]." WHERE LocaleID = ? ORDER BY Name";
97  }
98  else
99  {
100  $sql = "SELECT ID, RootID, ParentID, Name, IsFolder, LocaleID,".
101  "Skeleton, OrigDoc, TIMESTAMP(LastSeen) as LastSeen ".
102  "FROM ".$Tables["Projects"]." WHERE LocaleID = ? ORDER BY Name";
103  }
104  $rs = $DB->Execute($sql, $this->LocaleID);
105  $haveMore = true;
106 
107  while($haveMore)
108  {
109  $proj = new TranslationProject();
110 
111  if(($haveMore = $proj->LoadFromRecordSet($rs, $this->Brief)))
112  {
113  self::$ProjectList[$this->LocaleID][$proj->GetID()] = $proj;
114  self::$ProjectChildren[$this->LocaleID][$proj->GetParentID()][] = $proj->GetID();
115  }
116  }
117  $rs->Close();
118 
119  // Store in file cache
120  if($CONFIG['disk_cache'])
121  {
122  file_put_contents($cacheFile, serialize(self::$ProjectList[$this->LocaleID]));
123  file_put_contents($cacheFile2, serialize(self::$ProjectChildren[$this->LocaleID]));
124  }
125  }
126  }
127 
128  if(empty(self::$ProjectList[$this->LocaleID]))
129  {
130  throw new Exception("There are no projects for Language ID: ".$this->LocaleID.". Please import before using.");
131  }
132 
133  // Select the first child by default for ->valid()-ity check.
134  $this->rewind();
135  }
136 
141  public static function ClearCache($localeID)
142  {
143  global $CONFIG;
144 
147 
148  if($CONFIG['disk_cache'])
149  {
150  $cacheFile = $CONFIG['disk_cache']."/ProjectList_".$localeID.".cache";
151  $cacheFile2 = $CONFIG['disk_cache']."/ProjectChildren_".$localeID.".cache";
152 
153  if(file_exists($cacheFile))
154  {
155  unlink($cacheFile);
156  }
157  if(file_exists($cacheFile2))
158  {
159  unlink($cacheFile2);
160  }
161  if(file_exists($cacheFile.".brief"))
162  {
163  unlink($cacheFile.".brief");
164  }
165  if(file_exists($cacheFile2.".brief"))
166  {
167  unlink($cacheFile2.".brief");
168  }
169  }
170  }
171  /* Iterator interface */
172 
176  public function rewind()
177  {
178  // Take first child
179  if(!empty(self::$ProjectChildren[$this->LocaleID][$this->ParentID][0]))
180  {
181  $this->CurrentID = self::$ProjectChildren[$this->LocaleID][$this->ParentID][0];
182  }
183  else
184  {
185  $this->CurrentID = 0;
186  }
187  }
191  public function hasMore()
192  {
193  // Get an array of our sibling' IDs
194  $siblings = self::$ProjectChildren[$this->LocaleID][
195  self::$ProjectList[$this->LocaleID][$this->CurrentID]->GetParentID()
196  ];
197 
198  // Loop on each sibling
199  $takeNext = false;
200  $foundMore = false;
201  foreach($siblings as $pid)
202  {
203  // Found the next sibling?
204  if($takeNext)
205  {
206  $foundMore = true;
207  break;
208  }
209 
210  // Is this the current item? If yes, take next sibling in the next round.
211  if($pid == $this->CurrentID)
212  {
213  $takeNext = true;
214  }
215  }
216  return $foundMore;
217  }
221  public function key()
222  {
223  return $this->CurrentID;
224  }
228  public function current()
229  {
230  return self::$ProjectList[$this->LocaleID][$this->CurrentID];
231  }
235  public function next()
236  {
237  // Get an array of our sibling' IDs
238  $siblings = self::$ProjectChildren[$this->LocaleID][
239  self::$ProjectList[$this->LocaleID][$this->CurrentID]->GetParentID()
240  ];
241 
242  // Note the last ID and set like if we had no more (will change if we have)
243  $lastID = $this->CurrentID;
244  $this->CurrentID = 0;
245 
246  // Loop on each sibling
247  $takeNext = false;
248  foreach($siblings as $pid)
249  {
250  // Found the next sibling?
251  if($takeNext)
252  {
253  $this->CurrentID = $pid;
254  break;
255  }
256 
257  // Is this the current item? If yes, take next sibling in the next round.
258  if($pid == $lastID)
259  {
260  $takeNext = true;
261  }
262  }
263  }
267  public function valid()
268  {
269  return $this->CurrentID != 0;
270  }
271 
272  /* Recursive Iterator Interface */
273 
277  public function hasChildren()
278  {
279  // Check if the children array has items with the current id as parent
280  return !empty(self::$ProjectChildren[$this->LocaleID][$this->CurrentID]);
281  }
282 
286  public function getChildren()
287  {
288  // Make a new iterator below ourselves
289  return new ProjectIterator($this->CurrentID, $this->LocaleID, $this->Brief);
290  }
291 }
292 ?>
& GetTables()
Definition: Common.php:71
& GetDbConn()
Definition: Common.php:61
$CONFIG['db_software']
Definition: config.inc.php:24
static ClearCache($localeID)
$DB
Definition: export.php:28
__construct($parentID, $localeID, $brief=false)