ProjectIterator.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 include_once('TranslationProject.php');
00022 
00030 class ProjectIterator implements Iterator, RecursiveIterator
00031 {
00032         private $LocaleID;              
00033         private $ParentID;              
00035         private $CurrentProject;        
00037         public static $ProjectList;     
00038         public static $ProjectChildren; 
00046         public function __construct($parentID, $localeID)
00047         {
00048                 $DB =& GetDbConn();
00049                 $Tables =& GetTables();
00050 
00051                 $this->LocaleID = $localeID;
00052                 $this->ParentID = $parentID;
00053                 
00054                 // Cache all projects for the current language, unless done already
00055                 if(empty(self::$ProjectList[$this->LocaleID]))
00056                 {
00057                         $rs = $DB->Execute("SELECT ID, ParentID, Name, LocaleID, Skeleton, OrigDoc,  TIMESTAMP(LastSeen) as LastSeen FROM ".$Tables["Projects"]." WHERE LocaleID = ? ORDER BY Name", $this->LocaleID);
00058                         $haveMore = true;
00059                         
00060                         while($haveMore)
00061                         {
00062                                 $proj = new TranslationProject();
00063                                 
00064                                 if(($haveMore = $proj->LoadFromRecordSet($rs)))
00065                                 {
00066                                         self::$ProjectList[$this->LocaleID][$proj->GetID()] = $proj;
00067                                         self::$ProjectChildren[$this->LocaleID][$proj->GetParentID()][] = $proj->GetID();
00068                                 }
00069                         }
00070                         $rs->Close();
00071                 }
00072                 
00073                 if(empty(self::$ProjectList[$this->LocaleID]))
00074                 {
00075                         throw new Exception("There are no projects for Language ID: ".$this->LocaleID.". Please import before using.");
00076                 }
00077 
00078                 // Select the first child by default for ->valid()-ity check.
00079                 $this->rewind();
00080         }
00081 
00082         /* Iterator interface */
00083 
00087         public function rewind()
00088         {
00089                 // Take first child
00090                 if(!empty(self::$ProjectChildren[$this->LocaleID][$this->ParentID][0]))
00091                 {
00092                         $this->CurrentID = self::$ProjectChildren[$this->LocaleID][$this->ParentID][0];
00093                 }
00094                 else
00095                 {
00096                         $this->CurrentID = 0;
00097                 }
00098         }
00102         public function hasMore()
00103         {
00104                 // Get an array of our sibling' IDs
00105                 $siblings = self::$ProjectChildren[$this->LocaleID][
00106                         self::$ProjectList[$this->LocaleID][$this->CurrentID]->GetParentID()
00107                         ];
00108 
00109                 // Loop on each sibling
00110                 $takeNext = false;
00111                 $foundMore = false;
00112                 foreach($siblings as $pid)
00113                 {
00114                         // Found the next sibling?
00115                         if($takeNext)
00116                         {
00117                                 $foundMore = true;
00118                                 break;
00119                         }
00120 
00121                         // Is this the current item? If yes, take next sibling in the next round.
00122                         if($pid == $this->CurrentID)
00123                         {
00124                                 $takeNext = true;
00125                         }
00126                 }
00127                 return $foundMore;
00128         }
00132         public function key()
00133         {
00134                 return $this->CurrentID;
00135         }
00139         public function current()
00140         {
00141                 return self::$ProjectList[$this->LocaleID][$this->CurrentID];
00142         }
00146         public function next()
00147         {
00148                 // Get an array of our sibling' IDs
00149                 $siblings = self::$ProjectChildren[$this->LocaleID][
00150                         self::$ProjectList[$this->LocaleID][$this->CurrentID]->GetParentID()
00151                         ];
00152 
00153                 // Note the last ID and set like if we had no more (will change if we have)
00154                 $lastID = $this->CurrentID;
00155                 $this->CurrentID = 0;
00156                 
00157                 // Loop on each sibling
00158                 $takeNext = false;
00159                 foreach($siblings as $pid)
00160                 {
00161                         // Found the next sibling?
00162                         if($takeNext)
00163                         {
00164                                 $this->CurrentID = $pid;
00165                                 break;
00166                         }
00167                         
00168                         // Is this the current item? If yes, take next sibling in the next round.
00169                         if($pid == $lastID)
00170                         {
00171                                 $takeNext = true;
00172                         }
00173                 }
00174         }
00178         public function valid()
00179         {
00180                 return $this->CurrentID != 0;
00181         }
00182 
00183         /* Recursive Iterator Interface */
00184 
00188         public function hasChildren()
00189         {
00190                 // Check if the children array has items with the current id as parent
00191                 return !empty(self::$ProjectChildren[$this->LocaleID][$this->CurrentID]);
00192         }
00193 
00197         public function getChildren()
00198         {
00199                 // Make a new iterator below ourselves
00200                 return new ProjectIterator($this->CurrentID, $this->LocaleID);
00201         }
00202 }
00203 ?>

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