Location: PHPKode > scripts > MenuTree > menutree/TreeMenu.php
<?
/**
 * Author: Menno Merlijn
 * Date: 11-jul-2007
 * Version 1.0
 * PHP version > 5
 * 
 * Building a tree menu out of some tree nodes
 * 
 * Example
 * 
$t = new TreeMenu();

//adding the tree nodes
$t->node('start',false,'Media');
$t->node('movies','start','Movies');
$t->node('next','movies','Next','http://www.nextmovie.com',true,"movie.gif");
$t->node('lastlegion','movies','The Last Legion','http://www.imdb.com/title/tt0462396/',true,"movie.gif");
$t->node('audio','start','Audio',false,false);
$t->node('mtv','audio','MTV','http://mtv.nl/',true,'audio.png');
$t->node('search','start','Searching media');
$t->node('google','search','Google','http://www.google.nl/',true,'search.gif');

//deep tree example
$t->node('node1','start','Node depth 1');
$t->node('node2','node1','Node depth 2');
$t->node('node3','node2','Node depth 3');
$t->node('node4','node3','Node depth 4');
$t->node('node5','node4','Node depth 5');
//making the menu tree

$t->makeTree();

//output the menu tree
echo $t->printTree();


//printing a tree node out of the whole tree
$t->resetTree(); //reset the first tree so we can make a new one
$t->makeTree(true,'movies');
echo $t->printTree();
 */
class TreeMenu{
    
    //image directory
    const _IMAGE_DIR       = "./image/";
    
    //image names
    // on changing this names 
    const _IMAGE_DIR_OPEN  = "folder_open.gif";
    const _IMAGE_DIR_CLOSE = "folder_close.gif";
    const _IMAGE_PLUS      = "plus.gif";
    const _IMAGE_MIN       = "min.gif";
    
    private $nodes=array();
    private $tree;
    private $treeNumber;
    
    public function __construct(){
        $this->treeNumber=1;
    }
    
    public function resetTree(){
        $this->treeNumber++;
        $this->tree = "";
    }
    /**
     * Making tree nodes
     *
     * @param string $name (node name)
     * @param string $parent (parent node)
     * @param string $text (displayed text)
     * @param string $link
     * @param bool $active (visibile child nodes on start)
     * @param string $image (image name)
     */
    public function node($name,$parent,$text,$link=false,$active=true,$image="folder_open.gif"){
        $this->nodes[]=array("name"=>$name
                          ,"ref"=>$parent
                          ,"text"=>$text
                          ,"link"=>$link
                          ,"active"=>$active
                          ,"image"=>$image
                          );
    }
    
    /**
     * Recursive function
     * Making a string of all implied nodes
     *
     * @param bool $active  (state of the nodes on start collapsed/open)
     * @param string $node  (name of the starting parent node, could be any node in the tree
     * @param int $margin   (indent of the tree node 
     */
    public function makeTree($active=true,$node='start',$margin=0){
        $class = " class=\"treemenu\"";
        foreach ($this->nodes as $v){
            if($v['name'] == $node){
                if($v['link']){
                    $link = "<a href=\"{$v['link']}\">{$v['text']}</a>";
                }else{
                    $link = $v['text'];
                }
                //Walk through all nodes looking for a child
                $child=false;
                foreach ($this->nodes as $v2){
                    if($v2['ref'] == $node){
                        $child=true;
                    }
                }
                //als het een child node heeft dan...
                if($child){
                    $style = " style=\"margin-left:{$margin}px\"";
                    $id = " id=\"node_{$node}{$this->treeNumber}\"";
                    $onclick = " onclick=\"treemenu('node_{$node}{$this->treeNumber}'".
                                                  ",'node_{$node}Blok{$this->treeNumber}'".
                                                  ",'".self::_IMAGE_DIR."'".
                                                  ",'".self::_IMAGE_MIN."'".
                                                  ",'".self::_IMAGE_PLUS."'".
                                                  ",'".self::_IMAGE_DIR_OPEN."'".
                                                  ",'".self::_IMAGE_DIR_CLOSE."');\"";
                    if($active){//child nodes of this node are visible on start
                        
                        //add to treemenu string
                        $this->tree.= "<span{$style}{$id}{$onclick}>".
                                      "<img src=\"".self::_IMAGE_DIR.self::_IMAGE_MIN."\">".
                                      "<img src=\"".self::_IMAGE_DIR.self::_IMAGE_DIR_OPEN."\"{$class}></span>".
                                      $link.
                                      "<br>\n";      
                    }else{//child nodes of this nodes are not visible on start
                        
                        //add to treemenu string
                        $this->tree.= "<span{$style}{$id}{$onclick}>".
                                      "<img src=\"".self::_IMAGE_DIR.self::_IMAGE_PLUS."\">".
                                      "<img src=\"".self::_IMAGE_DIR.self::_IMAGE_DIR_CLOSE."\"{$class}></span>".
                                      $link.
                                      "<br>\n";      
                    }

                }else{//node with no child node
                    
                    //add to treemenu string
                    $this->tree.= "<span style=\"margin-left:".($margin+10)."px\">".
                                  "<img src=\"".self::_IMAGE_DIR."{$v['image']}\"{$class}></span>".
                                  $link.
                                  "<br>\n";
                }
            }
        }
        if($active){
            //visible child node block on start
            $this->tree.= "<div id=\"node_{$node}Blok{$this->treeNumber}\" style=\"display:block\">\n";
        }else{
            //hidden child node block on start
            $this->tree.= "<div id=\"node_{$node}Blok{$this->treeNumber}\" style=\"display:none;visibility:hidden\">\n";
        }
        
        
        //adding the child nodes from this the parent node
        foreach ($this->nodes as $v){
            if($v['ref'] == $node){
                //recursive function call
                $this->makeTree($v['active'],$v['name'],($margin+20));
            }
        }
        //end child node block
        $this->tree.= "</div>\n";
    }
    
    
    
    /**
     * returning the menu tree in html code
     *
     * @return string
     */
    public function printTree(){
        return $this->tree;
    }
    
}


?>
Return current item: MenuTree