show(); ?> A global variable $e was used to pass expanded nodes from one invocation of the script to the next in Bjorge's version. Now you must specify the variable name when you create your TreeMenu object: $tree = new TreeMenu("a"); When the object is created, this variable name gets the letters "tmv" prepended to it. This allows the constructor to loop through the global variables and pick out those that it needs when writing certain URLs. This came about as a modification to some code sent to me by Enrico Stahn [enrico.stahn@gmx.net] on 11/29/2001 that added multimenu functionality to the script. Much thanks to him. I have archived his version at the following URL: http://www.bitbuddha.com/php/treemenu/stahn.112901.treemenu.inc I got rid of the overloaded constructor that would accept a file name or an array and act accordingly. This required programmers to know too much about the implementation of the class. I left the ability to pass a file name and supplied an addNode() method. You can use it like this: addNode(1, "grandpa"); $tree->addNode(2, "pa"); $tree->addNode(3, "son"); $tree->addNode(2, "uncle"); $tree->show(); ?> Look at the addNode method signature to see the two optional parameters (ok, so they are $link and $target). Your tree will look like this: + grandpa | |----+ pa | | | |---o son | |---o uncle If you have some hierarchical data in a database then it would be pretty easy to write a recursive function to build your tree dynamically using the addNode() method. The loadTreeFromFile() method allows you to add as many node files to a tree as you want. You can mix it up with other sources (database, etc) using the addNode() method, too. An example: addNode(1, "grandpa"); $tree->addNode(2, "pa"); $tree->addNode(3, "son"); $tree->addNode(2, "uncle"); $tree->loadTreeFromFile("demomenu.txt"); $tree->show(); ?> There is an additional getHTMLTable() method if you ever need that for some reason. It returns the table instead of echoing it. Denny Shimkoski 2001 dhscompguy@yahoo.com I have only fixed two bugs in this release, everything else is in its original form. You can see my comments in place - just search for the string "Changed by Chris". See more documentation on my Treemenu Block for PHPNuke at my homepage: http://www.karakas-online.de/myTreemenu/t1.html As far as I am concerned, we can release this under the GPL. Chris Karakas 2003 chris@karakas-online.de ************************************************************/ var $tree = array(); var $expand = array(); var $visible = array(); var $levels = array(); var $explevels = array(); var $urlparams = array(); var $maxlevel = 0; var $i = 0; var $urlparam; var $script; var $img_expand = "blocks/treemenu/tree_expand.png"; var $img_collapse = "blocks/treemenu/tree_collapse.png"; var $img_line = "blocks/treemenu/tree_vertline.png"; var $img_split = "blocks/treemenu/tree_split.png"; var $img_end = "blocks/treemenu/tree_end.png"; var $img_leaf = "blocks/treemenu/tree_leaf.png"; var $img_spc = "blocks/treemenu/tree_space.png"; function treemenu($urlparam, $nodefile = ""){ $this->script = isset($GLOBALS["PATH_INFO"]) ? $GLOBALS["PATH_INFO"] : $GLOBALS["SCRIPT_NAME"]; $this->urlparam = "tmv".$urlparam; reset($GLOBALS["HTTP_GET_VARS"]); while (list($key,) = each ($GLOBALS["HTTP_GET_VARS"])){ /********************************************* Changed by Chris. Commented the if statement. If you don't comment it, this "if" will "eat up" the "other parameters" you may have in your URL (these are stored in the $otherparams variable, which thus will be empty). The result will be that, instead of, for example http://yourhost/html/modules.php?name=Your_Account&tmva=1|#1 you will get http://yourhost/html/modules.php?tmva=1|#1 which is deadly for your PHPNuke. if (substr($key, 0, 3) == "tmv"){ *********************************************/ $this->urlparams[] = $key; /********************************************* } *********************************************/ } if (!empty($nodefile)) $this->loadTreeFromFile($nodefile); } function addNode($level, $text, $link = "", $target = ""){ $this->tree[$this->i][0] = $level; $this->tree[$this->i][1] = "".$text.""; $this->tree[$this->i][2] = $link; $this->tree[$this->i][3] = $target; $this->tree[$this->i][4] = 0; if ($this->tree[$this->i][0] > $this->maxlevel) $this->maxlevel = $this->tree[$this->i][0]; $this->expand[$this->i]=0; $this->visible[$this->i]=0; $this->levels[$this->i]=0; $this->i++; } function loadTreeFromFile($nodefile){ $fd = fopen($nodefile, "r"); if ($fd == 0) die($this->script." : Unable to open file ".$nodefile); while ($buffer = fgets($fd, 4096)){ $level = strspn($buffer,"."); $node = explode("|", rtrim(substr($buffer,$level))); $text = $node[0]; $link = $node[1]; $target = $node[2]; $this->addNode($level, $text, $link, $target); } fclose($fd); } function setExpandedNodesFromURL(){ if (!empty($GLOBALS[$this->urlparam])) $this->explevels = explode("|",$GLOBALS[$this->urlparam]); $this->setExpandedNodes(); } function setExpandedNodes(){ $i=0; while($iexplevels)){ $this->expand[$this->explevels[$i]]=1; $i++; } } function setEndNodes(){ $lastlevel=$this->maxlevel; for ($i=count($this->tree)-1; $i>=0; $i--){ if ( $this->tree[$i][0] < $lastlevel ){ for ($j=$this->tree[$i][0]+1; $j <= $this->maxlevel; $j++){ $this->levels[$j]=0; } } if ( $this->levels[$this->tree[$i][0]]==0 ){ $this->levels[$this->tree[$i][0]]=1; $this->tree[$i][4]=1; } else { $this->tree[$i][4]=0; } $lastlevel=$this->tree[$i][0]; } } function setVisibleNodes(){ // all root nodes are always visible for ($i=0; $i < count($this->tree); $i++){ if ($this->tree[$i][0]==1){ $this->visible[$i]=1; } } for ($i=0; $i < count($this->explevels); $i++){ $n = $this->explevels[$i]; if ( ($this->visible[$n]==1) && ($this->expand[$n]==1) ){ $j=$n+1; while ( $this->tree[$j][0] > $this->tree[$n][0] ){ if ($this->tree[$j][0]==$this->tree[$n][0]+1) $this->visible[$j]=1; $j++; } } } } function show(){ echo $this->getHTMLTable(); } function getHTMLTable(){ $this->setExpandedNodesFromURL(); $this->setEndNodes(); $this->setVisibleNodes(); /********************************************* * Output nicely formatted tree * *********************************************/ for ($i=0; $i<$this->maxlevel; $i++) $this->levels[$i]=1; $this->maxlevel++; $html = "\n"; $html .= ""; /********************************************* * Changed by Chris. * * Changed $this->maxlevel to * * ($this->maxlevel+3) in the condition. * * Just as the table created just above * * has ($this->maxlevel+3) columns, * * we have to output that many * * * * or an ugly scrollbar will appear at * * the bottom. * *********************************************/ for ($i=0; $i<($this->maxlevel+3); $i++) $html .= ""; $html .= "\n"; $cnt=0; while ($cnttree)){ if ($this->visible[$cnt]){ /**************************************** * start new row * ****************************************/ $html .= ""; /**************************************** * vertical lines from higher levels * ****************************************/ $i=0; while ($i<$this->tree[$cnt][0]-1){ if ($this->levels[$i]==1){ $html .= ""; } else { $html .= ""; } $i++; } /**************************************** * corner at end of subtree or t-split * ****************************************/ if ($this->tree[$cnt][4]==1){ $html .= ""; $this->levels[$this->tree[$cnt][0]-1]=0; } else { $html .= ""; $this->levels[$this->tree[$cnt][0]-1]=1; } /******************************************** * Node (with subtree) or Leaf (no subtree) * ********************************************/ if ($this->tree[$cnt+1][0]>$this->tree[$cnt][0]){ /**************************************** * Create expand/collapse parameters * ****************************************/ reset($this->urlparams); while (list(,$value) = each ($this->urlparams)){ if (!empty($GLOBALS[$value]) && $value <> $this->urlparam) { $otherparams .= $value."=".$GLOBALS[$value]."&"; } } $params="?".$otherparams.$this->urlparam."="; $i=0; while($iexpand)){ if ( ($this->expand[$i]==1) && ($cnt!=$i) || ($this->expand[$i]==0 && $cnt==$i)){ $params=$params.$i; $params=$params."|"; } $i++; } $otherparams = ""; if ($this->expand[$cnt]==0){ $html .= ""; } else { $html .= ""; } } else { /************************* * Tree Leaf * *************************/ $html .= ""; } /**************************************** * output item text * ****************************************/ if ($this->tree[$cnt][2]==""){ $html .= ""; } else { $html .= ""; } /**************************************** * end row * ****************************************/ $html .= "\n"; } $cnt++; } $html .= "
 
img_line."\">img_spc."\">img_end."\">img_split."\">script.$params."#$cnt\">img_expand."\" border=no>script.$params."#$cnt\">img_collapse."\" border=no>img_leaf."\">".$this->tree[$cnt][1]."tree[$cnt][2]."\" target=\"".$this->tree[$cnt][3]."\">".$this->tree[$cnt][1]."
\n"; return $html; } } ?>