Location: PHPKode > projects > Content*Builder > contentbuilder/lib/CB_tree.class.php
<?php

/**
 * Klasse zum Management von CB Bäumen
 * 
 * @package CB Tree
 * @access public 
 * @author Jörg Stöber <hide@address.com> 
 * @version 0.1 ($Id: CB_tree.class.php,v 1.1 2004/03/29 22:45:31 cb_fog Exp $)
 */

class CB_Tree {
	// Felder, welche geladen werden sollen
	var $fields = array();
	
	var $nodes = array();
	
	// Array enthält für jeden Schlüssel (nodeID) als Wert die Strukturtiefe...
	var $flatStructure = array();
	// Array, dass für jede nodeID die Mutter anzeigt.
	var $parentStructure = array();
	var $limitStructure = 0;
	
	var $openThisFolder = array();
	var $nodeName, $nodeID, $noderSpace = array();
	
	function CB_Tree($table, $fieldArray = null) {
		// Konstruktur initialisiert die baumspeicherung
		$this->table = $table;
		if(is_array($fieldArray)) {
			$this->addFields($fieldArray);
		}
	}
	
	function setOrder($order) {
		if(!empty($order)) {
			$this->order = $order;
		}
	}
	
	function setRestriction($restrictID) {
		$this->restrictID = $restrictID;
		$this->restriction = true;
	}
	
	function addFields($fieldArray) {
		if(is_array($fieldArray)) {
			foreach($fieldArray as $k => $v) {
				if(!empty($v)) {
					$this->fields[] = $v;
				}
			}
		}
	}
	
	function getData() {
	    global $cbUser; // enable user-rights object $cbUser, initialized in main.php
		$table = $this->table;
		$order = $this->order;
		
		$multiple = false;
		foreach($this->fields as $k => $v) {
			if($multiple == true) {
				$fieldString .= ", ";
			}
			$fieldString .= $v;
			$multiple = true;
		}
		
		if($this->restriction) {
			$restrictID = $this->restrictID;
			$restrictSelect = mysql_fetch_object(mysql_query("SELECT $fieldString FROM $table WHERE fid = '$restrictID'"));
			$this->restrictLevel = $restrictSelect->status;
			if(empty($where)) {
				$where = "WHERE status > '".$restrictSelect->status."'";
			}
		}
		
		$select = mysql_query("SELECT $fieldString FROM $table $where $order");
		while($treeQuery = mysql_fetch_array($select)) {
			$takeElement = true;
			if($this->restriction) {
				$status	= $treeQuery['status'] - $this->restrictLevel;
				$treeQuery['status'] = $status;
				$takeElement = $this->isRootElement($status, $treeQuery['nodeID'], $treeQuery['parent']);
			} else {
				$status	= $treeQuery['status'];
			}
			// Rubrik-Permission-Check deka ....
		    if (($treeQuery["usersallowed"])||($treeQuery["groupsAllowed"])){
		        if ($cbUser->isUserAllowed($treeQuery["usersallowed"],$treeQuery["groupsAllowed"])==FALSE){
		            //trigger_error("User is not allowed ... in ".$fquery["fname"]);
                    continue;
                }
		    }
			// Rubrik Permission Check end ..
			
			if($takeElement) {
				$nodeID = $treeQuery['nodeID'];
				
				$this->flatStructure[$nodeID]   = $status;
				$this->parentStructure[$nodeID] = $treeQuery['parent'];
				
				// Speicherung der Struktur und aller Werte der aktuellen Rubrik in einem Array
				foreach($treeQuery as $k => $value) {
					$this->nodes[$status][$nodeID][$k] = $value;
				}
				if($status > 1) {
					// wenn Status > 1, dann ist es eine Unterrubrik
					// Errechnen des letzten Levels
					$lasti = ($status - 1);
					if(empty($this->nodes[$lasti][$treeQuery['parent']]['children'])) {
						// wenn das Feld children von der Mutter leer ist, 
						// dann ist dieses das erste Kind
						$cnr = 1;
						// Anzahl der Kinder wird gespeichert
						$this->nodes[$lasti][$treeQuery['parent']]['children'] = 1;
					} else {
						// wenn nicht, dann ist dieses das Kind Nummer LetztesKind + 1
						$cnr = $this->nodes[$lasti][$treeQuery['parent']]['children'] + 1;
						// Anzahl der Kinder wird gespeichert
						$this->nodes[$lasti][$treeQuery['parent']]['children'] = $cnr;
					}
					// Hier werden der Mutter die Kinder angehangen
					$this->nodes[$lasti][$treeQuery['parent']]['child'.$cnr] = $nodeID;
				}
			}
		}
	}
	
	function isRootElement($level, $nodeID, $parentID) {
		if($level > 1) {
			for($z = $level; $z != 1; --$z) {
				$parentID = $this->nodes[$z - 1][$parentID]['parent'];
			}
		}
		if($parentID == $this->restrictID) {
			$result = true;
		} else {
			$result = false;
		}
		return $result;
	}
	
	function getNodeName() {
		return $this->nodeName;
	}
	function getNodeID() {
		return $this->nodeID;
	}
	function getNodeSpace() {
		return $this->nodeSpace;
	}
	
	function printMenu($nodeID, $name, $level) {
		$this->nodeName[]  = $name;
		$this->nodeID[]    = $nodeID;
		$this->nodeSpace[] = $level * 10;
	}
	
	
	function getOpenFolders($openfolder) {
		$this->limitStructure = 1;
		$this->openThisFolder[$openfolder] = "1";
		$thisLevel  = $this->flatStructure[$openfolder];
		$thisMother = $this->parentStructure[$openfolder];
		if(($thisLevel - 1) > 0) {
			$this->getOpenFolders($thisMother);
		}
	}
	
	function getTree() {
		// Die erste Ebene der Rubriken Struktur wird abgerufen und durchlaufen
		if(is_array($this->nodes['1'])) {	
			while(list($key, $value) = each($this->nodes['1'])) {
				// Übergabe dieses Elements an printMenu()
				$this->printMenu($key, $this->nodes['1'][$key]['name'], 1);
				// Herausfinden, ob Kinder existieren			
				$children = $this->nodes['1'][$key]['children'];
				if(!empty($children)) {
					// wenn Kinder existieren, dann diese ausgeben
					if(!empty($this->openThisFolder[$key]) OR $this->limitStructure == 0) {
						$this->getChildren($children, '2', $key);
					}
				}
			}
		}
	}
	
	function getChildren($cnr, $level, $mother) {
		$lastLevel = $level - 1;
		$nextLevel = $level + 1;
		echo $this->test;
			
		// Schleifendurchläufe = Kinderzahl
		for($z = 1; $z <= $cnr; ++$z) {
			// Schlüssel (fid) des jeweiligen Kindes
			$key = $this->nodes[$lastLevel][$mother]['child'.$z];

			$this->printMenu($key, $this->nodes[$level][$key]['name'], $level);
			
			// Rekursive Abfrage der Kinder
			$children = $this->nodes[$level][$key]['children'];
			if(!empty($children)) {
				if(!empty($this->openThisFolder[$key]) OR $this->limitStructure == 0) {
					$this->getChildren($children, $nextLevel, $key);
				}
			}
		}
	}
}
?>
Return current item: Content*Builder