2 // PHP Layers Menu 3.1.1 (C) 2001-2003 Marco Pratesi (marco at telug dot it)
5 * This file contains the code of the TreeMenu class.
6 * @package PHPLayersMenu
10 * This is the TreeMenu class of the PHP Layers Menu library.
12 * This class depends on the LayersMenuCommon class and on the PEAR conforming version of the PHPLib Template class, i.e. on HTML_Template_PHPLIB
15 * @package PHPLayersMenu
17 class TreeMenu
extends LayersMenuCommon
{
20 * Type of images used for the Tree Menu
24 var $treeMenuImagesType;
26 * An array where we store the Tree Menu code for each menu
33 * The constructor method; it initializates the menu system
37 $this->LayersMenuCommon();
39 $this->treeMenuImagesType
= "png";
40 $this->_treeMenu
= array();
42 $this->_nodesCount
= 0;
43 $this->tree
= array();
44 $this->_maxLevel
= array();
45 $this->_firstLevelCnt
= array();
46 $this->_firstItem
= array();
47 $this->_lastItem
= array();
51 * The method to set the dirroot directory
55 function setDirroot($dirroot) {
56 return $this->setDirrootCommon($dirroot);
60 * The method to set the type of images used for the Tree Menu
64 function setTreeMenuImagesType($treeMenuImagesType) {
65 $this->treeMenuImagesType
= $treeMenuImagesType;
69 * Method to prepare a new Tree Menu.
71 * This method processes items of a menu to prepare and return
72 * the corresponding Tree Menu code.
75 * @param string $menu_name the name of the menu whose items have to be processed
79 $menu_name = "" // non consistent default...
81 if (!isset($this->_firstItem
[$menu_name]) ||
!isset($this->_lastItem
[$menu_name])) {
82 $this->error("newTreeMenu: the first/last item of the menu '$menu_name' is not defined; please check if you have parsed its menu data.");
86 $this->_treeMenu
[$menu_name] = "";
88 $img_space = $this->imgwww
. "tree_space." . $this->treeMenuImagesType
;
90 $img_vertline = $this->imgwww
. "tree_vertline." . $this->treeMenuImagesType
;
92 $img_expand = $this->imgwww
. "tree_expand." . $this->treeMenuImagesType
;
94 $img_expand_first = $this->imgwww
. "tree_expand_first." . $this->treeMenuImagesType
;
95 $alt_expand_first = "+-";
96 $img_expand_corner = $this->imgwww
. "tree_expand_corner." . $this->treeMenuImagesType
;
97 $alt_expand_corner = "+-";
98 $img_collapse = $this->imgwww
. "tree_collapse." . $this->treeMenuImagesType
;
100 $img_collapse_first = $this->imgwww
. "tree_collapse_first." . $this->treeMenuImagesType
;
101 $alt_collapse_first = "--";
102 $img_collapse_corner = $this->imgwww
. "tree_collapse_corner." . $this->treeMenuImagesType
;
103 $alt_collapse_corner = "--";
104 $img_split = $this->imgwww
. "tree_split." . $this->treeMenuImagesType
;
106 $img_split_first = $this->imgwww
. "tree_split_first." . $this->treeMenuImagesType
;
107 $alt_split_first = "|-";
108 $img_corner = $this->imgwww
. "tree_corner." . $this->treeMenuImagesType
;
110 $img_folder_closed = $this->imgwww
. "tree_folder_closed." . $this->treeMenuImagesType
;
111 $alt_folder_closed = "->";
112 $img_folder_open = $this->imgwww
. "tree_folder_open." . $this->treeMenuImagesType
;
113 $alt_folder_open = "->";
114 $img_leaf = $this->imgwww
. "tree_leaf." . $this->treeMenuImagesType
;
117 for ($i=0; $i<=$this->_maxLevel
[$menu_name]; $i++
) {
121 // Find last nodes of subtrees
122 $last_level = $this->_maxLevel
[$menu_name];
123 for ($i=$this->_lastItem
[$menu_name]; $i>=$this->_firstItem
[$menu_name]; $i--) {
124 if ($this->tree
[$i]["level"] < $last_level) {
125 for ($j=$this->tree
[$i]["level"]+
1; $j<=$this->_maxLevel
[$menu_name]; $j++
) {
129 if ($levels[$this->tree
[$i]["level"]] == 0) {
130 $levels[$this->tree
[$i]["level"]] = 1;
131 $this->tree
[$i]["last_item"] = 1;
133 $this->tree
[$i]["last_item"] = 0;
135 $last_level = $this->tree
[$i]["level"];
139 $toggle_function_name = "toggle" . $menu_name;
141 for ($cnt=$this->_firstItem
[$menu_name]; $cnt<=$this->_lastItem
[$menu_name]; $cnt++
) {
142 $this->_treeMenu
[$menu_name] .= "<div id=\"jt" . $cnt . "\" class=\"treemenudiv\">\n";
144 // vertical lines from higher levels
145 for ($i=0; $i<$this->tree
[$cnt]["level"]-1; $i++
) {
146 if ($levels[$i] == 1) {
147 $img = $img_vertline;
148 $alt = $alt_vertline;
153 $this->_treeMenu
[$menu_name] .= "<img class=\"imgs\" src=\"" . $img . "\" alt=\"" . $alt . "\" />";
156 $not_a_leaf = $cnt<$this->_lastItem
[$menu_name] && $this->tree
[$cnt+
1]["level"]>$this->tree
[$cnt]["level"];
158 if ($this->tree
[$cnt]["last_item"] == 1) {
159 // corner at end of subtree or t-split
161 $this->_treeMenu
[$menu_name] .= "<a onmousedown=\"". $toggle_function_name . "('" . $cnt . "')\"><img class=\"imgs\" id=\"jt" . $cnt . "node\" src=\"" . $img_collapse_corner . "\" alt=\"" . $alt_collapse_corner . "\" /></a>";
163 $this->_treeMenu
[$menu_name] .= "<img class=\"imgs\" src=\"" . $img_corner . "\" alt=\"" . $alt_corner . "\" />";
165 $levels[$this->tree
[$cnt]["level"]-1] = 0;
168 if ($cnt == $this->_firstItem
[$menu_name]) {
169 $img = $img_collapse_first;
170 $alt = $alt_collapse_first;
172 $img = $img_collapse;
173 $alt = $alt_collapse;
175 $this->_treeMenu
[$menu_name] .= "<a onmousedown=\"". $toggle_function_name . "('" . $cnt . "');\"><img class=\"imgs\" id=\"jt" . $cnt . "node\" src=\"" . $img . "\" alt=\"" . $alt . "\" /></a>";
177 if ($cnt == $this->_firstItem
[$menu_name]) {
178 $img = $img_split_first;
179 $alt = $alt_split_first;
184 $this->_treeMenu
[$menu_name] .= "<a onmousedown=\"". $toggle_function_name . "('" . $cnt . "');\"><img class=\"imgs\" id=\"jt" . $cnt . "node\" src=\"" . $img . "\" alt=\"" . $alt . "\" /></a>";
186 $levels[$this->tree
[$cnt]["level"]-1] = 1;
189 if ($this->tree
[$cnt]["parsed_href"] == "" ||
$this->tree
[$cnt]["parsed_href"] == "#") {
190 $a_href_open_img = "";
191 $a_href_close_img = "";
192 $a_href_open = "<a class=\"phplmnormal\">";
193 $a_href_close = "</a>";
195 $a_href_open_img = "<a href=\"" . $this->tree
[$cnt]["parsed_href"] . "\"" . $this->tree
[$cnt]["parsed_title"] . $this->tree
[$cnt]["parsed_target"] . ">";
196 $a_href_close_img = "</a>";
197 $a_href_open = "<a href=\"" . $this->tree
[$cnt]["parsed_href"] . "\"" . $this->tree
[$cnt]["parsed_title"] . $this->tree
[$cnt]["parsed_target"] . " class=\"phplm\">";
198 $a_href_close = "</a>";
202 $this->_treeMenu
[$menu_name] .= $a_href_open_img . "<img class=\"imgs\" id=\"jt" . $cnt . "folder\" src=\"" . $img_folder_open . "\" alt=\"" . $alt_folder_open . "\" />" . $a_href_close_img;
204 if ($this->tree
[$cnt]["parsed_icon"] != "") {
205 $this->_treeMenu
[$menu_name] .= $a_href_open_img . "<img class=\"imgs\" src=\"" . $this->imgwww
. $this->tree
[$cnt]["parsed_icon"] . "\" width=\"" . $this->tree
[$cnt]["iconwidth"] . "\" height=\"" . $this->tree
[$cnt]["iconheight"] . "\" alt=\"" . $alt_leaf . "\" />" . $a_href_close_img;
207 $this->_treeMenu
[$menu_name] .= $a_href_open_img . "<img class=\"imgs\" src=\"" . $img_leaf . "\" alt=\"" . $alt_leaf . "\" />" . $a_href_close_img;
210 $this->_treeMenu
[$menu_name] .= " " . $a_href_open . $this->tree
[$cnt]["text"] . $a_href_close . "\n";
211 $this->_treeMenu
[$menu_name] .= "</div>\n";
213 if ($cnt<$this->_lastItem
[$menu_name] && $this->tree
[$cnt]["level"]<$this->tree
[$cnt+
1]["level"]) {
214 $this->_treeMenu
[$menu_name] .= "<div id=\"jt" . $cnt . "son\" class=\"treemenudiv\">\n";
215 if ($this->tree
[$cnt]["expanded"] != 1) {
216 $toggle .= "if (expand[" . $cnt . "] != 1) " . $toggle_function_name . "('" . $cnt . "');\n";
218 $toggle .= "if (collapse[" . $cnt . "] == 1) " . $toggle_function_name . "('" . $cnt . "');\n";
222 if ($cnt>$this->_firstItem
[$menu_name] && $this->tree
[$cnt]["level"]>$this->tree
[$cnt+
1]["level"]) {
223 for ($i=max(1, $this->tree
[$cnt+
1]["level"]); $i<$this->tree
[$cnt]["level"]; $i++
) {
224 $this->_treeMenu
[$menu_name] .= "</div>\n";
230 $this->_treeMenu[$menu_name] =
231 "<div class=\"phplmnormal\">\n" .
232 $this->_treeMenu[$menu_name] .
235 // Some (old) browsers do not support the "white-space: nowrap;" CSS property...
236 $this->_treeMenu
[$menu_name] =
237 "<table cellspacing=\"0\" cellpadding=\"0\" style=\"border: 0;\">\n" .
239 "<td class=\"phplmnormal\" style=\"white-space: nowrap;\">\n" .
240 $this->_treeMenu
[$menu_name] .
245 $t = new Template_PHPLIB();
246 $t->setFile("tplfile", $this->libjsdir
. "layerstreemenu.ijs");
248 "toggle_function_name" => $toggle_function_name,
249 "img_expand" => $img_expand,
250 "img_expand_first" => $img_expand_first,
251 "img_collapse" => $img_collapse,
252 "img_collapse_first" => $img_collapse_first,
253 "img_collapse_corner" => $img_collapse_corner,
254 "img_folder_open" => $img_folder_open,
255 "img_expand_corner" => $img_expand_corner,
256 "img_folder_closed" => $img_folder_closed
258 $toggle_function = $t->parse("out", "tplfile");
260 "<script type=\"text/javascript\">\n" .
267 "<script type=\"text/javascript\">\n" .
269 "if ((DOM && !Opera56 && !Konqueror22) || IE4) {\n" .
272 "if (NS4) alert('Only the accessibility is provided to Netscape 4 on the JavaScript Tree Menu.\\nWe *strongly* suggest you to upgrade your browser.');\n" .
276 $this->_treeMenu
[$menu_name] = $toggle_function . "\n" . $this->_treeMenu
[$menu_name] . "\n" . $toggle;
278 return $this->_treeMenu
[$menu_name];
282 * Method that returns the code of the requested Tree Menu
284 * @param string $menu_name the name of the menu whose Tree Menu code
288 function getTreeMenu($menu_name) {
289 return $this->_treeMenu
[$menu_name];
293 * Method that prints the code of the requested Tree Menu
295 * @param string $menu_name the name of the menu whose Tree Menu code
299 function printTreeMenu($menu_name) {
300 print $this->_treeMenu
[$menu_name];