treemenu.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. // PHP Layers Menu 3.2.0-rc (C) 2001-2004 Marco Pratesi - http://www.marcopratesi.it/
  3. /**
  4. * This file contains the code of the TreeMenu class.
  5. * @package PHPLayersMenu
  6. */
  7. /**
  8. * This is the TreeMenu class of the PHP Layers Menu library.
  9. *
  10. * This class depends on the LayersMenuCommon class and on the PEAR conforming version of the PHPLib Template class, i.e. on HTML_Template_PHPLIB
  11. *
  12. * @version 3.2.0-rc
  13. * @package PHPLayersMenu
  14. */
  15. class TreeMenu extends LayersMenuCommon
  16. {
  17. /**
  18. * Type of images used for the Tree Menu
  19. * @access private
  20. * @var string
  21. */
  22. var $treeMenuImagesType;
  23. /**
  24. * Prefix for filenames of images of a theme
  25. * @access private
  26. * @var string
  27. */
  28. var $treeMenuTheme;
  29. /**
  30. * An array where we store the Tree Menu code for each menu
  31. * @access private
  32. * @var array
  33. */
  34. var $_treeMenu;
  35. /**
  36. * The constructor method; it initializates the menu system
  37. * @return void
  38. */
  39. function TreeMenu()
  40. {
  41. $this->LayersMenuCommon();
  42. $this->treeMenuImagesType = 'png';
  43. $this->treeMenuTheme = '';
  44. $this->_treeMenu = array();
  45. $this->_nodesCount = 0;
  46. $this->tree = array();
  47. $this->_maxLevel = array();
  48. $this->_firstLevelCnt = array();
  49. $this->_firstItem = array();
  50. $this->_lastItem = array();
  51. }
  52. /**
  53. * The method to set the dirroot directory
  54. * @access public
  55. * @return boolean
  56. */
  57. function setDirroot($dirroot)
  58. {
  59. return $this->setDirrootCommon($dirroot);
  60. }
  61. /**
  62. * The method to set the type of images used for the Tree Menu
  63. * @access public
  64. * @return void
  65. */
  66. function setTreeMenuImagesType($treeMenuImagesType)
  67. {
  68. $this->treeMenuImagesType = $treeMenuImagesType;
  69. }
  70. /**
  71. * The method to set the prefix for filenames of images of a theme
  72. * @access public
  73. * @return void
  74. */
  75. function setTreeMenuTheme($treeMenuTheme)
  76. {
  77. $this->treeMenuTheme = $treeMenuTheme;
  78. }
  79. /**
  80. * Method to prepare a new Tree Menu.
  81. *
  82. * This method processes items of a menu to prepare and return
  83. * the corresponding Tree Menu code.
  84. *
  85. * @access public
  86. * @param string $menu_name the name of the menu whose items have to be processed
  87. * @return string
  88. */
  89. function newTreeMenu(
  90. $menu_name = '' // non consistent default...
  91. )
  92. {
  93. if (!isset($this->_firstItem[$menu_name]) || !isset($this->_lastItem[$menu_name])) {
  94. $this->error("newTreeMenu: the first/last item of the menu '$menu_name' is not defined; please check if you have parsed its menu data.");
  95. return 0;
  96. }
  97. $this->_treeMenu[$menu_name] = '';
  98. $img_collapse = $this->imgwww . $this->treeMenuTheme . 'tree_collapse.' . $this->treeMenuImagesType;
  99. $alt_collapse = '--';
  100. $img_collapse_corner = $this->imgwww . $this->treeMenuTheme . 'tree_collapse_corner.' . $this->treeMenuImagesType;
  101. $alt_collapse_corner = '--';
  102. $img_collapse_corner_first = $this->imgwww . $this->treeMenuTheme . 'tree_collapse_corner_first.' . $this->treeMenuImagesType;
  103. $alt_collapse_corner_first = '--';
  104. $img_collapse_first = $this->imgwww . $this->treeMenuTheme . 'tree_collapse_first.' . $this->treeMenuImagesType;
  105. $alt_collapse_first = '--';
  106. $img_corner = $this->imgwww . $this->treeMenuTheme . 'tree_corner.' . $this->treeMenuImagesType;
  107. $alt_corner = '`-';
  108. $img_expand = $this->imgwww . $this->treeMenuTheme . 'tree_expand.' . $this->treeMenuImagesType;
  109. $alt_expand = '+-';
  110. $img_expand_corner = $this->imgwww . $this->treeMenuTheme . 'tree_expand_corner.' . $this->treeMenuImagesType;
  111. $alt_expand_corner = '+-';
  112. $img_expand_corner_first = $this->imgwww . $this->treeMenuTheme . 'tree_expand_corner_first.' . $this->treeMenuImagesType;
  113. $alt_expand_corner_first = '+-';
  114. $img_expand_first = $this->imgwww . $this->treeMenuTheme . 'tree_expand_first.' . $this->treeMenuImagesType;
  115. $alt_expand_first = '+-';
  116. $img_folder_closed = $this->imgwww . $this->treeMenuTheme . 'tree_folder_closed.' . $this->treeMenuImagesType;
  117. $alt_folder_closed = '->';
  118. $img_folder_open = $this->imgwww . $this->treeMenuTheme . 'tree_folder_open.' . $this->treeMenuImagesType;
  119. $alt_folder_open = '->';
  120. $img_leaf = $this->imgwww . $this->treeMenuTheme . 'tree_leaf.' . $this->treeMenuImagesType;
  121. $alt_leaf = '->';
  122. $img_space = $this->imgwww . $this->treeMenuTheme . 'tree_space.' . $this->treeMenuImagesType;
  123. $alt_space = ' ';
  124. $img_split = $this->imgwww . $this->treeMenuTheme . 'tree_split.' . $this->treeMenuImagesType;
  125. $alt_split = '|-';
  126. $img_split_first = $this->imgwww . $this->treeMenuTheme . 'tree_split_first.' . $this->treeMenuImagesType;
  127. $alt_split_first = '|-';
  128. $img_vertline = $this->imgwww . $this->treeMenuTheme . 'tree_vertline.' . $this->treeMenuImagesType;
  129. $alt_vertline = '| ';
  130. for ($i=0; $i<=$this->_maxLevel[$menu_name]; $i++) {
  131. $levels[$i] = 0;
  132. }
  133. // Find last nodes of subtrees
  134. $last_level = $this->_maxLevel[$menu_name];
  135. for ($i=$this->_lastItem[$menu_name]; $i>=$this->_firstItem[$menu_name]; $i--) {
  136. if ($this->tree[$i]['level'] < $last_level) {
  137. for ($j=$this->tree[$i]['level']+1; $j<=$this->_maxLevel[$menu_name]; $j++) {
  138. $levels[$j] = 0;
  139. }
  140. }
  141. if ($levels[$this->tree[$i]['level']] == 0) {
  142. $levels[$this->tree[$i]['level']] = 1;
  143. $this->tree[$i]['last_item'] = 1;
  144. } else {
  145. $this->tree[$i]['last_item'] = 0;
  146. }
  147. $last_level = $this->tree[$i]['level'];
  148. }
  149. $toggle = '';
  150. $toggle_function_name = 'toggle' . $menu_name;
  151. for ($cnt=$this->_firstItem[$menu_name]; $cnt<=$this->_lastItem[$menu_name]; $cnt++) {
  152. if ($this->tree[$cnt]['text'] == '---') {
  153. continue; // separators are significant only for layers-based menus
  154. }
  155. if (isset($this->tree[$cnt]['selected']) && $this->tree[$cnt]['selected']) {
  156. $linkstyle = 'phplmselected';
  157. } else {
  158. $linkstyle = 'phplm';
  159. }
  160. $this->_treeMenu[$menu_name] .= '<div id="jt' . $cnt . '" class="treemenudiv">' . "\n";
  161. // vertical lines from higher levels
  162. for ($i=0; $i<$this->tree[$cnt]['level']-1; $i++) {
  163. if ($levels[$i] == 1) {
  164. $img = $img_vertline;
  165. $alt = $alt_vertline;
  166. } else {
  167. $img = $img_space;
  168. $alt = $alt_space;
  169. }
  170. $this->_treeMenu[$menu_name] .= '<img align="top" border="0" class="imgs" src="' . $img . '" alt="' . $alt . '" />';
  171. }
  172. $not_a_leaf = $cnt<$this->_lastItem[$menu_name] && $this->tree[$cnt+1]['level']>$this->tree[$cnt]['level'];
  173. if ($this->tree[$cnt]['last_item'] == 1) {
  174. // corner at end of subtree or t-split
  175. if ($not_a_leaf) {
  176. if ($cnt == $this->_firstItem[$menu_name]) {
  177. $img = $img_collapse_corner_first;
  178. $alt = $alt_collapse_corner_first;
  179. } else {
  180. $img = $img_collapse_corner;
  181. $alt = $alt_collapse_corner;
  182. }
  183. $this->_treeMenu[$menu_name] .= '<a onmousedown="' . $toggle_function_name . "('" . $cnt . "')" . '"><img align="top" border="0" class="imgs" id="jt' . $cnt . 'node" src="' . $img . '" alt="' . $alt . '" /></a>';
  184. } else {
  185. $this->_treeMenu[$menu_name] .= '<img align="top" border="0" class="imgs" src="' . $img_corner . '" alt="' . $alt_corner . '" />';
  186. }
  187. $levels[$this->tree[$cnt]['level']-1] = 0;
  188. } else {
  189. if ($not_a_leaf) {
  190. if ($cnt == $this->_firstItem[$menu_name]) {
  191. $img = $img_collapse_first;
  192. $alt = $alt_collapse_first;
  193. } else {
  194. $img = $img_collapse;
  195. $alt = $alt_collapse;
  196. }
  197. $this->_treeMenu[$menu_name] .= '<a onmousedown="' . $toggle_function_name . "('" . $cnt . "');" . '"><img align="top" border="0" class="imgs" id="jt' . $cnt . 'node" src="' . $img . '" alt="' . $alt . '" /></a>';
  198. } else {
  199. if ($cnt == $this->_firstItem[$menu_name]) {
  200. $img = $img_split_first;
  201. $alt = $alt_split_first;
  202. } else {
  203. $img = $img_split;
  204. $alt = $alt_split;
  205. }
  206. $this->_treeMenu[$menu_name] .= '<img align="top" border="0" class="imgs" id="jt' . $cnt . 'node" src="' . $img . '" alt="' . $alt . '" />';
  207. }
  208. $levels[$this->tree[$cnt]['level']-1] = 1;
  209. }
  210. if ($this->tree[$cnt]['parsed_href'] == '' || $this->tree[$cnt]['parsed_href'] == '#') {
  211. $a_href_open_img = '';
  212. $a_href_close_img = '';
  213. $a_href_open = '<a class="phplmnormal" onmousedown="' . $toggle_function_name . "('" . $cnt . "');".'">';
  214. $a_href_close = '</a>';
  215. } else {
  216. $a_href_open_img = '<a href="' . $this->tree[$cnt]['parsed_href'] . '"' . $this->tree[$cnt]['parsed_title'] . $this->tree[$cnt]['parsed_target'] . '>';
  217. $a_href_close_img = '</a>';
  218. $a_href_open = '<a href="' . $this->tree[$cnt]['parsed_href'] . '"' . $this->tree[$cnt]['parsed_title'] . $this->tree[$cnt]['parsed_target'] . ' class="' . $linkstyle . '">';
  219. $a_href_close = '</a>';
  220. }
  221. if ($not_a_leaf) {
  222. $this->_treeMenu[$menu_name] .= $a_href_open_img . '<img align="top" border="0" class="imgs" id="jt' . $cnt . 'folder" src="' . $img_folder_open . '" alt="' . $alt_folder_open . '" />' . $a_href_close_img;
  223. } else {
  224. if ($this->tree[$cnt]['parsed_icon'] != '') {
  225. $this->_treeMenu[$menu_name] .= $a_href_open_img . '<img align="top" border="0" src="' . $this->tree[$cnt]['parsed_icon'] . '" width="' . $this->tree[$cnt]['iconwidth'] . '" height="' . $this->tree[$cnt]['iconheight'] . '" alt="' . $alt_leaf . '" />' . $a_href_close_img;
  226. } else {
  227. $this->_treeMenu[$menu_name] .= $a_href_open_img . '<img align="top" border="0" class="imgs" src="' . $img_leaf . '" alt="' . $alt_leaf . '" />' . $a_href_close_img;
  228. }
  229. }
  230. $this->_treeMenu[$menu_name] .= '&nbsp;' . $a_href_open . $this->tree[$cnt]['text'] . $a_href_close . "\n";
  231. $this->_treeMenu[$menu_name] .= '</div>' . "\n";
  232. if ($cnt<$this->_lastItem[$menu_name] && $this->tree[$cnt]['level']<$this->tree[$cnt+1]['level']) {
  233. $this->_treeMenu[$menu_name] .= '<div id="jt' . $cnt . 'son" class="treemenudiv">' . "\n";
  234. if ($this->tree[$cnt]['expanded'] != 1) {
  235. $toggle .= 'if (phplm_expand[' . $cnt . '] != 1) ' . $toggle_function_name . "('" . $cnt . "');\n";
  236. } else {
  237. $toggle .= 'if (phplm_collapse[' . $cnt . '] == 1) ' . $toggle_function_name . "('" . $cnt . "');\n";
  238. }
  239. }
  240. if ($cnt>$this->_firstItem[$menu_name] && $this->tree[$cnt]['level']>$this->tree[$cnt+1]['level']) {
  241. for ($i=max(1, $this->tree[$cnt+1]['level']); $i<$this->tree[$cnt]['level']; $i++) {
  242. $this->_treeMenu[$menu_name] .= '</div>' . "\n";
  243. }
  244. }
  245. }
  246. /*
  247. $this->_treeMenu[$menu_name] =
  248. '<div class="phplmnormal">' . "\n" .
  249. $this->_treeMenu[$menu_name] .
  250. '</div>' . "\n";
  251. */
  252. // Some (old) browsers do not support the "white-space: nowrap;" CSS property...
  253. $this->_treeMenu[$menu_name] =
  254. '<table cellspacing="0" cellpadding="0" border="0">' . "\n" .
  255. '<tr>' . "\n" .
  256. '<td class="phplmnormal" nowrap="nowrap">' . "\n" .
  257. $this->_treeMenu[$menu_name] .
  258. '</td>' . "\n" .
  259. '</tr>' . "\n" .
  260. '</table>' . "\n";
  261. $t = new Template_PHPLIB();
  262. $t->setFile('tplfile', $this->libjsdir . 'layerstreemenu.ijs');
  263. $t->setVar(array(
  264. 'toggle_function_name' => $toggle_function_name,
  265. 'img_collapse' => $img_collapse,
  266. 'img_collapse_corner' => $img_collapse_corner,
  267. 'img_collapse_corner_first' => $img_collapse_corner_first,
  268. 'img_collapse_first' => $img_collapse_first,
  269. 'img_expand' => $img_expand,
  270. 'img_expand_corner' => $img_expand_corner,
  271. 'img_expand_corner_first' => $img_expand_corner_first,
  272. 'img_expand_first' => $img_expand_first,
  273. 'img_folder_closed' => $img_folder_closed,
  274. 'img_folder_open' => $img_folder_open
  275. ));
  276. $toggle_function = $t->parse('out', 'tplfile');
  277. $toggle_function =
  278. '<script language="JavaScript" type="text/javascript">' . "\n" .
  279. '<!--' . "\n" .
  280. $toggle_function .
  281. '// -->' . "\n" .
  282. '</script>' . "\n";
  283. $toggle =
  284. '<script language="JavaScript" type="text/javascript">' . "\n" .
  285. '<!--' . "\n" .
  286. 'if ((DOM && !Opera56 && !Konqueror22) || IE4) {' . "\n" .
  287. $toggle .
  288. '}' . "\n" .
  289. '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" .
  290. '// -->' . "\n" .
  291. '</script>' . "\n";
  292. $this->_treeMenu[$menu_name] = $toggle_function . "\n" . $this->_treeMenu[$menu_name] . "\n" . $toggle;
  293. return $this->_treeMenu[$menu_name];
  294. }
  295. /**
  296. * Method that returns the code of the requested Tree Menu
  297. * @access public
  298. * @param string $menu_name the name of the menu whose Tree Menu code
  299. * has to be returned
  300. * @return string
  301. */
  302. function getTreeMenu($menu_name)
  303. {
  304. return $this->_treeMenu[$menu_name];
  305. }
  306. /**
  307. * Method that prints the code of the requested Tree Menu
  308. * @access public
  309. * @param string $menu_name the name of the menu whose Tree Menu code
  310. * has to be printed
  311. * @return void
  312. */
  313. function printTreeMenu($menu_name)
  314. {
  315. print $this->_treeMenu[$menu_name];
  316. }
  317. } /* END OF CLASS */
  318. ?>