PHP > a menu library

My favorite Menu that I adapted for the new ESO home page and some of its adaptations:

A function that finally gives me the complete path to a menu item. Not just the path to the parent as getPath… here we go:

 # builds total path (including the item itself...)
 # call $m->buildTotalPath($m->menu, array())
 # use $m->pathtohere

 function buildTotalPath($menu, $path, $level=0) {
    // loop through the (sub)menu
    foreach ($menu as $node_id => $node) {
        // save the path of the current node in the urlmap
	##!!! include current node
	$path[$level] = $node_id;
	$this->pathtohere[$node["url"]] = $path;
        // if current node has a submenu
        if ($node["sub"]) {
           // continue search recursivly -
	   $this->buildTotalPath($node["sub"], $path, $level+1);
        }
    }
    return true;
 } 

 # get path including item itself.....
 function getTotalPath() {
    $this->buildTotalPath($this->menu, array());
    $this->current_url = $this->getCurrentURL();
    while ($this->current_url &&
           !isset($this->pathtohere[$this->current_url]) &&
           !isset($this->pathtohere[$this->current_url.".html"])) {
	$this->current_url = substr($this->current_url, 0, -1);
    }
    if (isset($this->pathtohere[$this->current_url])) {
       $myvar = $this->current_url;
    } else {
       $myvar = $this->current_url.".html";
    }
    return $this->pathtohere[$myvar];
 }

thanks to Ulf for providing the idea and the source code of the menu functions!

Leave a Reply