indent = 0;
$this->first = true;
$this->treeCount = 0;
$this->imgCount = 0;
$this->output = "";
}
// addItem, adds a child to this menu
// Takes a XNode object reference as argument
function &addItem(&$node)
{
$this->items[] = &$node;
return $this->items[count($this->items) - 1];
}
// generateTree, generates the HTML code (UL list) for the dynamic tree-view
function generateTree($root = false)
{
if(!$root) $root = $this;
if($this->first){
$this->output .= $this->codeIndent()."
\n";
$this->first = false;
}else{
if (array_search($this->treeCount, $this->visibleNodes) !== false)
{
$this->output .= $this->codeIndent()."treeCount."\" class=\"Xtree\" style=\"display:block;\">\n";
}
else
{
$this->output .= $this->codeIndent()."\n";
$this->indent --;
return $this->output;
}
// saveTree and restoreTree - thanks to Niels Fanoe (niels.f@noee.dk) for giving me the idea
// saveTree, save the generated HTML code to a file for future use without generating again
function saveTree($filename = "xMenuCache.html")
{
$file = new File();
$file->write($this->output,$filename);
$file->printError();
return $filename;
}
// restoreTree, returns the previously generated HTML code stored in a file
// Call this method STATICALLY for easier use: XMenu::restoreTree("xPandMenuCode.html");
function restoreTree($filename = "xMenuCache.html")
{
$file = new File();
$menu = $file->read($filename);
$error = $file->getError();
if(!empty($error)) return false;
else return $menu;
}
// codeIndent, only used to create a nice and readable HTML code (indents the UL and LI tags)
function codeIndent()
{
$str = "";
for($i=0;$i<$this->indent;$i++){
$str .= " ";
}
return $str;
}
}
// XNode class: A node item in the menu
class XNode
{
// Name assigned to this node (Text shown on the item)
var $name;
// Link for the item (if any)
var $link;
// Sub-items of this node
var $items = array();
// Absolute URL of this node's icon
var $img;
// Absolute URL of this node's icon (alternate, used for expanded and collapsed states)
var $alt_img;
// constructor
// $name: text shown for this item
// $link: where does this item links to when clicked (optional)
// $img and $alt_img: images displayed next to this item (absolute paths to images must be used)
function XNode($name,$link = false,$img = LEAF_DEFAULT_IMG,$alt_img = LEAF_DEFAULT_ALT_IMG)
{
$this->name = $name;
$this->link = $link;
$this->img = $img;
$this->alt_img = $alt_img;
}
// addItem, adds a subnode under this node
// Takes a XNode object reference as argument
function &addItem(&$node)
{
if($this->img == LEAF_DEFAULT_IMG){$this->img = NODE_DEFAULT_IMG;}
if($this->alt_img == LEAF_DEFAULT_ALT_IMG){$this->alt_img = NODE_DEFAULT_ALT_IMG;}
$this->items[] = &$node;
return $this->items[count($this->items) - 1];
}
}
?>