phpDocumentor tree-node
[ class tree: tree-node ] [ index: tree-node ] [ all elements ]

Source for file Tree.php

Documentation is available at Tree.php

  1. <?
  2. /**
  3. * @package tree-node
  4. * @filesource
  5. * @see HTML_TREE_NODE_PAGE_PATH.'/Tree.php'
  6. * @copyright (c) http://Finn-Rasmussen.com
  7. * @license http://Finn-Rasmussen.com/license/ myPHP License conditions
  8. * @author http://Finn-Rasmussen.com
  9. * @version 1.9
  10. * @since 21-oct-2005
  11. */
  12.  
  13. /**
  14. * The required files
  15. */
  16. require_once(HTML_PATH.'/Html.php');
  17. require_once(HTML_TREE_NODE_UTIL_PATH.'/Node.php');
  18. require_once(HTML_TREE_NODE_VIEW_PATH.'/NodeView.php');
  19. require_once(HTML_FILE_UTIL_PATH.'/Dir.php');
  20. if (defined('HTML_TABLE_COMPONENT_PATH')) {
  21. require_once(HTML_TABLE_COMPONENT_PATH.'/TableHeader.php');
  22. }
  23. if (defined('HTML_UTIL_COMPONENT_PATH')) {
  24. require_once(HTML_UTIL_COMPONENT_PATH.'/Server.php');
  25. }
  26.  
  27. /**
  28. * The Tree class builds a complete navigation tree
  29. * <code>
  30. * Usage:
  31. * $tree = new Tree($path,$text,$width,$class);
  32. * print $tree->getHtml();
  33. * Or
  34. * Tree::display($path,$text,$width,$class);
  35. * </code>
  36. * @package tree-node
  37. */
  38.  
  39. class Tree extends Html {
  40. /**
  41. * @var String $text The text of the table header
  42. */
  43. var $text = '';
  44.  
  45. /**
  46. * @var String $width The width of the table header
  47. */
  48. var $width = '';
  49.  
  50. /**
  51. * @var String $class The CSS class name
  52. */
  53. var $class = '';
  54.  
  55. /**
  56. * Constructor
  57. * @param String $path The path to use for the tree
  58. * @param String $text The text of the table header
  59. * @param String $width The width of the table header
  60. * @param String $class The CSS class to use
  61. */
  62. function Tree($path='',$text='',$width='',$class='') {
  63. $this->Html();
  64. $this->text = $text !=''?$text :TREE_NODE_TEXT;
  65. $this->width = $width!=''?$width:TREE_NODE_WIDTH;
  66. $this->class = $class!=''?$class:TREE_NODE_CSS;
  67. if ($path!='') {
  68. $this->add($this->buildNode($path));
  69. } else {
  70. $this->add($this->buildNode($_SERVER['DOCUMENT_ROOT']));
  71. }
  72. }
  73.  
  74. /**
  75. * Build the tree
  76. * @param String $path The path to build
  77. * @param int $level The level of the node
  78. * @return Node The Node to be build
  79. */
  80. function buildNode($path,$level=0) {
  81. //print "$path,$level<br />";
  82. $node = new Node();
  83. $cwd = Dir::getCwd();
  84. $thedir = new Dir($path);
  85. $thedir->cd(); // YOU MUST CHANGE TO THE DIRECTORY, in order to get it up and running
  86. if ($thedir->open()) {
  87. $level++;
  88. while (false !== ($dirname = $thedir->read())) {
  89. if (is_dir($dirname)) {
  90. $pathdirname = $path.'/'.$dirname;
  91. if (@file_exists($pathdirname.'/'.CONTENT_FILE_NAME)) {
  92. switch ($dirname) {
  93. case '.' :
  94. if ($level===1) {
  95. $node->add(new NodeView(basename(getcwd()),$thedir->getUrl($path),IMAGE_HOME,$level));
  96. }
  97. break;
  98. case '..':
  99. if ($level===1) {
  100. $node->add(new NodeView(LINK_TEXT_UP,$dirname,IMAGE_PLUS,$level));
  101. }
  102. break;
  103. default:
  104. $image = IMAGE_DOC;
  105. $indexFileName = $_SERVER['SCRIPT_NAME'];
  106. if (defined('HTML_UTIL_COMPONENT_PATH')) {
  107. $indexFileName = Server::getScriptName();
  108. }
  109. if ($thedir->getUrl($pathdirname).INDEX_FILE_NAME==$indexFileName) {
  110. $image = IMAGE_MINUS;
  111. }
  112. $node->add(new NodeView($dirname,$thedir->getUrl($pathdirname),$image,$level));
  113. $node->add($this->buildNode(Dir::getCwd().'/'.$dirname,$level));
  114. break;
  115. }
  116. } else {
  117. // Ignore directories without a CONTENT_FILE_NAME
  118. }
  119. } else {
  120. //print "ignore $dirname<br />";
  121. }
  122. }
  123. $thedir->close();
  124. } else {
  125. print "error open $path<br />";
  126. }
  127. $thedir->cd($cwd); // Restore
  128. return $node;
  129. }
  130.  
  131. /**
  132. * Get the html for the whole tree and all its nodes
  133. * @return String The html
  134. */
  135. function getHtml() {
  136. $html = $this->html;
  137. if (TREE_NODE_SHOW & TREE_NODE_SHOW_LEFT || TREE_NODE_SHOW & TREE_NODE_SHOW_CENTER || TREE_NODE_SHOW & TREE_NODE_SHOW_RIGHT) {
  138. if (defined('CREATE_RUNTIME_KERNEL') && CREATE_RUNTIME_KERNEL) {
  139. $html .= '<?$tree = new Tree();print $tree->getHtml();?>';
  140. } else {
  141. if (defined('HTML_TABLE_COMPONENT_PATH')) {
  142. $tableheader = new TableHeader($this->text,$this->width);
  143. $html .= $tableheader->getHtml();
  144. }
  145. $blank = new Images(IMAGE_BLANK,$this->width,'1','',"$this->class");
  146. $html .= $this->getElements()."<br />\r\n"; // As html
  147. }
  148. } else {
  149. if (defined('LOG_LEVEL') && LOG_LEVEL & LOG_LEVEL_DEBUG) {
  150. print "<!-- ".$this->getClassName()." is disabled -->\r\n";
  151. }
  152. }
  153. return $html;
  154. }
  155.  
  156. /**
  157. * Display html
  158. * <code>
  159. * Usage:
  160. * Tree::display($path,$text,$width,$class);
  161. * </code>
  162. * @static
  163. * @param String $path The path to use for the tree
  164. * @param String $text The text of the table header
  165. * @param String $width The width of the table header
  166. * @param String $class The CSS class to use
  167. */
  168. function display($path='',$text='',$width='',$class='') {
  169. $html = new Tree($path,$text,$width,$class);
  170. $html->addHtml();
  171. }
  172. }
  173. ?>

Documentation generated on Thu, 22 Dec 2005 17:21:13 +0100 by phpDocumentor 1.3.0RC3