Sådan benyttes komponenten Tree klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Tree.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Tree::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Tree($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Tree klassen
Den fulde PHP kildekode for Tree klassen
<?php/** * @package tree-node * @filesource * @see HTML_TREE_NODE_PAGE_PATH.'/Tree.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_BASE_COMMON_PATH.'/Html.php');require_once(HTML_BASIC_UTIL_PATH.'/Message.php');require_once(HTML_TREE_NODE_UTIL_PATH.'/Node.php');require_once(HTML_TREE_NODE_VIEW_PATH.'/NodeView.php');require_once(HTML_FILE_UTIL_PATH.'/Dir.php');if (defined('HTML_TABLE_COMPONENT_PATH')) { require_once(HTML_TABLE_COMPONENT_PATH.'/TableHeader.php');}if (defined('HTML_UTIL_COMPONENT_PATH')) { require_once(HTML_UTIL_COMPONENT_PATH.'/Server.php');}/** * The Tree class builds a complete navigation tree * <code> * Usage: * $tree = new Tree($path, $text, $width, $class); * print $tree->getHtml(); * Or * Tree::display($path, $text, $width, $class); * </code> * @package tree-node */class Tree extends Html { /** * @var String $text The text of the table header */ protected $text = ''; /** * @var String $width The width of the table header */ protected $width = ''; /** * @var String $class The CSS class name */ protected $class = ''; /** * Constructor * @param String $path The path to use for the tree * @param String $text The text of the table header * @param String $width The width of the table header * @param String $class The CSS class to use */ function __construct($path='', $text='', $width='', $class='') { parent::__construct(); $this->text = $text != ''?$text :TREE_NODE_TEXT; $this->width = $width != ''?$width:TREE_NODE_WIDTH; $this->class = $class != ''?$class:CSS_TREE_NODE; if ($path != '') { $this->add($this->buildNode($path)); } else { $this->add($this->buildNode($_SERVER['DOCUMENT_ROOT'])); } } /** * Build the tree * @param String $path The path to build * @param int $level The level of the node * @return Node The Node to be build */ function buildNode($path, $level=0) { //print "$path, $level<br />\r\n"; $node = new Node(); $cwd = Dir::getCwd(); $thedir = new Dir($path); $thedir->cd(); // YOU MUST CHANGE TO THE DIRECTORY, in order to get it up and running if ($thedir->open()) { $level++; while (false !== ($dirname = $thedir->read())) { if (is_dir($dirname)) { $pathdirname = $path.'/'.$dirname; if (@file_exists($pathdirname.'/'.CONTENT_FILE_NAME)) { switch ($dirname) { case '.' : if ($level===1) { $node->add(new NodeView(basename(getcwd()), $thedir->getUrl($path),IMAGE_HOME, $level)); } break; case '..': if ($level===1) { $node->add(new NodeView(LINK_TEXT_UP, $dirname,IMAGE_PLUS, $level)); } break; default: $image = IMAGE_DOC; $indexFileName = $_SERVER['SCRIPT_NAME']; if (defined('HTML_UTIL_COMPONENT_PATH')) { $indexFileName = Server::getScriptName(); } if ($thedir->getUrl($pathdirname).INDEX_FILE_NAME==$indexFileName) { $image = IMAGE_MINUS; } //$msg = $thedir->getUrl($pathdirname)." ? $pathdirname<br />\r\n"; //Message::add($msg, __FILE__, __LINE__); $node->add(new NodeView($dirname, $thedir->getUrl($pathdirname), $image, $level)); $node->add($this->buildNode(Dir::getCwd().'/'.$dirname, $level)); break; } } else { // Ignore directories without a CONTENT_FILE_NAME // Message::add($msg, __FILE__, __LINE__); } } else { //print "ignore $dirname<br />"; // Message::add($msg, __FILE__, __LINE__); } } $thedir->close(); } else { $msg = "error open $path<br />"; Message::add($msg, __FILE__, __LINE__); } $thedir->cd($cwd); // Restore return $node; } /** * Get the html for the whole tree and all its nodes * @return String The html */ function getHtml() { $html = $this->html; if (TREE_NODE_SHOW & (TREE_NODE_SHOW_LEFT | TREE_NODE_SHOW_CENTER | TREE_NODE_SHOW_RIGHT) ) { if (CACHE_TREE_NODE && $this->getCacheFileName(CACHE_TREE_NODE_PATH) != '' && file_exists($this->getCacheFileName(CACHE_TREE_NODE_PATH))) { $html .= $this->content($this->getCacheFileName(CACHE_TREE_NODE_PATH)); } else { if (defined('CREATE_RUNTIME_KERNEL') && CREATE_RUNTIME_KERNEL) { $html .= '<?php$tree = new Tree();print $tree->getHtml();?>'; } else { if (defined('HTML_TABLE_COMPONENT_PATH')) { $tableheader = new TableHeader($this->text, $this->width); $html .= $tableheader->getHtml(); } $blank = new Images(IMAGE_BLANK, $this->width,'1','',"$this->class"); $html .= '<div class="'.$this->class.'">'; $html .= $this->getElements()."</div>\r\n"; // As html } if (CACHE_TREE_NODE) { $this->save($html, CACHE_TREE_NODE_PATH); } } } else { if (defined('DEBUG_LEVEL_SHOW_INFO') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $html .= "<!-- ".$this->getClassName()." is disabled -->\r\n"; } } return $html; } /** * Display html * <code> * Usage: * Tree::display($path, $text, $width, $class); * </code> * @static * @param String $path The path to use for the tree * @param String $text The text of the table header * @param String $width The width of the table header * @param String $class The CSS class to use */ public static function display($path='', $text='', $width='', $class='') { $html = new Tree($path, $text, $width, $class); $html->addHtml(); }}?>
Den fulde HTML kildekode for Tree klassen
<? <!-- DEBUG: Tree --> <!-- Tree is disabled --> ?>
Her er 'klasse metoderne' for Tree klassen:
Her er 'objekt variable' for Tree klassen: