phptreemenu.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 PHPTreeMenu class.
  5. * @package PHPLayersMenu
  6. */
  7. /**
  8. * This is the PHPTreeMenu class of the PHP Layers Menu library.
  9. *
  10. * This class depends on the LayersMenuCommon class. It provides "server-side" (PHP-based) tree menus, that to do not require JavaScript to work.
  11. *
  12. * @version 3.2.0-rc
  13. * @package PHPLayersMenu
  14. */
  15. class PHPTreeMenu extends LayersMenuCommon
  16. {
  17. /**
  18. * The character used for the PHP Tree Menu in the query string to separate items ids
  19. * @access private
  20. * @var string
  21. */
  22. var $phpTreeMenuSeparator;
  23. /**
  24. * The default value of the expansion string for the PHP Tree Menu
  25. * @access private
  26. * @var string
  27. */
  28. var $phpTreeMenuDefaultExpansion;
  29. /**
  30. * Type of images used for the Tree Menu
  31. * @access private
  32. * @var string
  33. */
  34. var $phpTreeMenuImagesType;
  35. /**
  36. * Prefix for filenames of images of a theme
  37. * @access private
  38. * @var string
  39. */
  40. var $phpTreeMenuTheme;
  41. /**
  42. * An array where we store the PHP Tree Menu code for each menu
  43. * @access private
  44. * @var array
  45. */
  46. var $_phpTreeMenu;
  47. /**
  48. * The constructor method; it initializates some variables
  49. * @return void
  50. */
  51. function PHPTreeMenu()
  52. {
  53. $this->LayersMenuCommon();
  54. $this->phpTreeMenuSeparator = '|';
  55. $this->phpTreeMenuDefaultExpansion = '';
  56. $this->phpTreeMenuImagesType = 'png';
  57. $this->phpTreeMenuTheme = '';
  58. $this->_phpTreeMenu = array();
  59. }
  60. /**
  61. * The method to set the dirroot directory
  62. * @access public
  63. * @return boolean
  64. */
  65. function setDirroot($dirroot)
  66. {
  67. return $this->setDirrootCommon($dirroot);
  68. }
  69. /**
  70. * The method to set the value of separator for the Tree Menu query string
  71. * @access public
  72. * @return void
  73. */
  74. function setPHPTreeMenuSeparator($phpTreeMenuSeparator)
  75. {
  76. $this->phpTreeMenuSeparator = $phpTreeMenuSeparator;
  77. }
  78. /**
  79. * The method to set the default value of the expansion string for the PHP Tree Menu
  80. * @access public
  81. * @return void
  82. */
  83. function setPHPTreeMenuDefaultExpansion($phpTreeMenuDefaultExpansion)
  84. {
  85. $this->phpTreeMenuDefaultExpansion = $phpTreeMenuDefaultExpansion;
  86. }
  87. /**
  88. * The method to set the type of images used for the Tree Menu
  89. * @access public
  90. * @return void
  91. */
  92. function setPHPTreeMenuImagesType($phpTreeMenuImagesType)
  93. {
  94. $this->phpTreeMenuImagesType = $phpTreeMenuImagesType;
  95. }
  96. /**
  97. * The method to set the prefix for filenames of images of a theme
  98. * @access public
  99. * @return void
  100. */
  101. function setPHPTreeMenuTheme($phpTreeMenuTheme)
  102. {
  103. $this->phpTreeMenuTheme = $phpTreeMenuTheme;
  104. }
  105. /**
  106. * Method to prepare a new PHP Tree Menu.
  107. *
  108. * This method processes items of a menu and parameters submitted
  109. * through GET (i.e. nodes to be expanded) to prepare and return
  110. * the corresponding Tree Menu code.
  111. *
  112. * @access public
  113. * @param string $menu_name the name of the menu whose items have to be processed
  114. * @return string
  115. */
  116. function newPHPTreeMenu(
  117. $menu_name = '' // non consistent default...
  118. )
  119. {
  120. $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
  121. $this_host = (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
  122. if (isset($_SERVER['SCRIPT_NAME'])) {
  123. $me = $_SERVER['SCRIPT_NAME'];
  124. } elseif (isset($_SERVER['REQUEST_URI'])) {
  125. $me = $_SERVER['REQUEST_URI'];
  126. } elseif (isset($_SERVER['PHP_SELF'])) {
  127. $me = $_SERVER['PHP_SELF'];
  128. } elseif (isset($_SERVER['PATH_INFO'])) {
  129. $me = $_SERVER['PATH_INFO'];
  130. }
  131. $url = $protocol . $this_host . $me;
  132. $query = '';
  133. reset($_GET);
  134. while (list($key, $value) = each($_GET)) {
  135. if ($key != 'p' && $value != '') {
  136. $query .= '&amp;' . $key . '=' . $value;
  137. }
  138. }
  139. if ($query != '') {
  140. $query = '?' . substr($query, 5) . '&amp;p=';
  141. } else {
  142. $query = '?p=';
  143. }
  144. $p = (isset($_GET['p'])) ? $_GET['p'] : $this->phpTreeMenuDefaultExpansion;
  145. /* ********************************************************* */
  146. /* Based on TreeMenu 1.1 by Bjorge Dijkstra (bjorge@gmx.net) */
  147. /* ********************************************************* */
  148. $this->_phpTreeMenu[$menu_name] = '';
  149. $img_collapse = $this->imgwww . $this->phpTreeMenuTheme . 'tree_collapse.' . $this->phpTreeMenuImagesType;
  150. $alt_collapse = '--';
  151. $img_collapse_corner = $this->imgwww . $this->phpTreeMenuTheme . 'tree_collapse_corner.' . $this->phpTreeMenuImagesType;
  152. $alt_collapse_corner = '--';
  153. $img_collapse_corner_first = $this->imgwww . $this->phpTreeMenuTheme . 'tree_collapse_corner_first.' . $this->phpTreeMenuImagesType;
  154. $alt_collapse_corner_first = '--';
  155. $img_collapse_first = $this->imgwww . $this->phpTreeMenuTheme . 'tree_collapse_first.' . $this->phpTreeMenuImagesType;
  156. $alt_collapse_first = '--';
  157. $img_corner = $this->imgwww . $this->phpTreeMenuTheme . 'tree_corner.' . $this->phpTreeMenuImagesType;
  158. $alt_corner = '`-';
  159. $img_expand = $this->imgwww . $this->phpTreeMenuTheme . 'tree_expand.' . $this->phpTreeMenuImagesType;
  160. $alt_expand = '+-';
  161. $img_expand_corner = $this->imgwww . $this->phpTreeMenuTheme . 'tree_expand_corner.' . $this->phpTreeMenuImagesType;
  162. $alt_expand_corner = '+-';
  163. $img_expand_corner_first = $this->imgwww . $this->phpTreeMenuTheme . 'tree_expand_corner_first.' . $this->phpTreeMenuImagesType;
  164. $alt_expand_corner_first = '+-';
  165. $img_expand_first = $this->imgwww . $this->phpTreeMenuTheme . 'tree_expand_first.' . $this->phpTreeMenuImagesType;
  166. $alt_expand_first = '+-';
  167. $img_folder_closed = $this->imgwww . $this->phpTreeMenuTheme . 'tree_folder_closed.' . $this->phpTreeMenuImagesType;
  168. $alt_folder_closed = '->';
  169. $img_folder_open = $this->imgwww . $this->phpTreeMenuTheme . 'tree_folder_open.' . $this->phpTreeMenuImagesType;
  170. $alt_folder_open = '->';
  171. $img_leaf = $this->imgwww . $this->phpTreeMenuTheme . 'tree_leaf.' . $this->phpTreeMenuImagesType;
  172. $alt_leaf = '->';
  173. $img_space = $this->imgwww . $this->phpTreeMenuTheme . 'tree_space.' . $this->phpTreeMenuImagesType;
  174. $alt_space = ' ';
  175. $img_split = $this->imgwww . $this->phpTreeMenuTheme . 'tree_split.' . $this->phpTreeMenuImagesType;
  176. $alt_split = '|-';
  177. $img_split_first = $this->imgwww . $this->phpTreeMenuTheme . 'tree_split_first.' . $this->phpTreeMenuImagesType;
  178. $alt_split_first = '|-';
  179. $img_vertline = $this->imgwww . $this->phpTreeMenuTheme . 'tree_vertline.' . $this->phpTreeMenuImagesType;
  180. $alt_vertline = '| ';
  181. for ($i=$this->_firstItem[$menu_name]; $i<=$this->_lastItem[$menu_name]; $i++) {
  182. $expand[$i] = 0;
  183. $visible[$i] = 0;
  184. $this->tree[$i]['last_item'] = 0;
  185. }
  186. for ($i=0; $i<=$this->_maxLevel[$menu_name]; $i++) {
  187. $levels[$i] = 0;
  188. }
  189. // Get numbers of nodes to be expanded
  190. if ($p != '') {
  191. $explevels = explode($this->phpTreeMenuSeparator, $p);
  192. $explevels_count = count($explevels);
  193. for ($i=0; $i<$explevels_count; $i++) {
  194. $expand[$explevels[$i]] = 1;
  195. }
  196. }
  197. // Find last nodes of subtrees
  198. $last_level = $this->_maxLevel[$menu_name];
  199. for ($i=$this->_lastItem[$menu_name]; $i>=$this->_firstItem[$menu_name]; $i--) {
  200. if ($this->tree[$i]['level'] < $last_level) {
  201. for ($j=$this->tree[$i]['level']+1; $j<=$this->_maxLevel[$menu_name]; $j++) {
  202. $levels[$j] = 0;
  203. }
  204. }
  205. if ($levels[$this->tree[$i]['level']] == 0) {
  206. $levels[$this->tree[$i]['level']] = 1;
  207. $this->tree[$i]['last_item'] = 1;
  208. } else {
  209. $this->tree[$i]['last_item'] = 0;
  210. }
  211. $last_level = $this->tree[$i]['level'];
  212. }
  213. // Determine visible nodes
  214. // all root nodes are always visible
  215. for ($i=$this->_firstItem[$menu_name]; $i<=$this->_lastItem[$menu_name]; $i++) {
  216. if ($this->tree[$i]['level'] == 1) {
  217. $visible[$i] = 1;
  218. }
  219. }
  220. if (isset($explevels)) {
  221. for ($i=0; $i<$explevels_count; $i++) {
  222. $n = $explevels[$i];
  223. if ($n >= $this->_firstItem[$menu_name] && $n <= $this->_lastItem[$menu_name] && $visible[$n] == 1 && $expand[$n] == 1) {
  224. $j = $n + 1;
  225. while ($j<=$this->_lastItem[$menu_name] && $this->tree[$j]['level']>$this->tree[$n]['level']) {
  226. if ($this->tree[$j]['level'] == $this->tree[$n]['level']+1) {
  227. $visible[$j] = 1;
  228. }
  229. $j++;
  230. }
  231. }
  232. }
  233. }
  234. // Output nicely formatted tree
  235. for ($i=0; $i<$this->_maxLevel[$menu_name]; $i++) {
  236. $levels[$i] = 1;
  237. }
  238. $max_visible_level = 0;
  239. for ($cnt=$this->_firstItem[$menu_name]; $cnt<=$this->_lastItem[$menu_name]; $cnt++) {
  240. if ($visible[$cnt]) {
  241. $max_visible_level = max($max_visible_level, $this->tree[$cnt]['level']);
  242. }
  243. }
  244. for ($cnt=$this->_firstItem[$menu_name]; $cnt<=$this->_lastItem[$menu_name]; $cnt++) {
  245. if ($this->tree[$cnt]['text'] == '---') {
  246. continue; // separators are significant only for layers-based menus
  247. }
  248. if (isset($this->tree[$cnt]['selected']) && $this->tree[$cnt]['selected']) {
  249. $linkstyle = 'phplmselected';
  250. } else {
  251. $linkstyle = 'phplm';
  252. }
  253. if ($visible[$cnt]) {
  254. $this->_phpTreeMenu[$menu_name] .= '<div class="treemenudiv">' . "\n";
  255. // vertical lines from higher levels
  256. for ($i=0; $i<$this->tree[$cnt]['level']-1; $i++) {
  257. if ($levels[$i] == 1) {
  258. $img = $img_vertline;
  259. $alt = $alt_vertline;
  260. } else {
  261. $img = $img_space;
  262. $alt = $alt_space;
  263. }
  264. $this->_phpTreeMenu[$menu_name] .= '<img align="top" border="0" class="imgs" src="' . $img . '" alt="' . $alt . '" />';
  265. }
  266. $not_a_leaf = $cnt<$this->_lastItem[$menu_name] && $this->tree[$cnt+1]['level']>$this->tree[$cnt]['level'];
  267. if ($not_a_leaf) {
  268. // Create expand/collapse parameters
  269. $params = '';
  270. for ($i=$this->_firstItem[$menu_name]; $i<=$this->_lastItem[$menu_name]; $i++) {
  271. if ($expand[$i] == 1 && $cnt!= $i || ($expand[$i] == 0 && $cnt == $i)) {
  272. $params .= $this->phpTreeMenuSeparator . $i;
  273. }
  274. }
  275. if ($params != '') {
  276. $params = substr($params, 1);
  277. }
  278. }
  279. if ($this->tree[$cnt]['last_item'] == 1) {
  280. // corner at end of subtree or t-split
  281. if ($not_a_leaf) {
  282. if ($expand[$cnt] == 0) {
  283. if ($cnt == $this->_firstItem[$menu_name]) {
  284. $img = $img_expand_corner_first;
  285. $alt = $alt_expand_corner_first;
  286. } else {
  287. $img = $img_expand_corner;
  288. $alt = $alt_expand_corner;
  289. }
  290. } else {
  291. if ($cnt == $this->_firstItem[$menu_name]) {
  292. $img = $img_collapse_corner_first;
  293. $alt = $alt_collapse_corner_first;
  294. } else {
  295. $img = $img_collapse_corner;
  296. $alt = $alt_collapse_corner;
  297. }
  298. }
  299. $this->_phpTreeMenu[$menu_name] .= '<a name="' . $cnt . '" class="' . $linkstyle . '" href="' . $url . $query . $params . '#' . $cnt . '"><img align="top" border="0" class="imgs" src="' . $img . '" alt="' . $alt . '" /></a>';
  300. } else {
  301. $this->_phpTreeMenu[$menu_name] .= '<img align="top" border="0" class="imgs" src="' . $img_corner . '" alt="' . $alt_corner . '" />';
  302. }
  303. $levels[$this->tree[$cnt]['level']-1] = 0;
  304. } else {
  305. if ($not_a_leaf) {
  306. if ($expand[$cnt] == 0) {
  307. if ($cnt == $this->_firstItem[$menu_name]) {
  308. $img = $img_expand_first;
  309. $alt = $alt_expand_first;
  310. } else {
  311. $img = $img_expand;
  312. $alt = $alt_expand;
  313. }
  314. } else {
  315. if ($cnt == $this->_firstItem[$menu_name]) {
  316. $img = $img_collapse_first;
  317. $alt = $alt_collapse_first;
  318. } else {
  319. $img = $img_collapse;
  320. $alt = $alt_collapse;
  321. }
  322. }
  323. $this->_phpTreeMenu[$menu_name] .= '<a name="' . $cnt . '" class="' . $linkstyle . '" href="' . $url . $query . $params . '#' . $cnt . '"><img align="top" border="0" class="imgs" src="' . $img . '" alt="' . $alt . '" /></a>';
  324. } else {
  325. if ($cnt == $this->_firstItem[$menu_name]) {
  326. $img = $img_split_first;
  327. $alt = $alt_split_first;
  328. } else {
  329. $img = $img_split;
  330. $alt = $alt_split;
  331. }
  332. $this->_phpTreeMenu[$menu_name] .= '<img align="top" border="0" class="imgs" src="' . $img . '" alt="' . $alt . '" />';
  333. }
  334. $levels[$this->tree[$cnt]['level']-1] = 1;
  335. }
  336. if ($this->tree[$cnt]['parsed_href'] == '' || $this->tree[$cnt]['parsed_href'] == '#') {
  337. $a_href_open_img = '';
  338. $a_href_close_img = '';
  339. $a_href_open = '<a class="phplmnormal">';
  340. $a_href_close = '</a>';
  341. } else {
  342. $a_href_open_img = '<a href="' . $this->tree[$cnt]['parsed_href'] . '"' . $this->tree[$cnt]['parsed_title'] . $this->tree[$cnt]['parsed_target'] . '>';
  343. $a_href_close_img = '</a>';
  344. $a_href_open = '<a href="' . $this->tree[$cnt]['parsed_href'] . '"' . $this->tree[$cnt]['parsed_title'] . $this->tree[$cnt]['parsed_target'] . ' class="' . $linkstyle . '">';
  345. $a_href_close = '</a>';
  346. }
  347. if ($not_a_leaf) {
  348. if ($expand[$cnt] == 1) {
  349. $img = $img_folder_open;
  350. $alt = $alt_folder_open;
  351. } else {
  352. $img = $img_folder_closed;
  353. $alt = $alt_folder_closed;
  354. }
  355. $this->_phpTreeMenu[$menu_name] .= $a_href_open_img . '<img align="top" border="0" class="imgs" src="' . $img . '" alt="' . $alt . '" />' . $a_href_close_img;
  356. } else {
  357. if ($this->tree[$cnt]['parsed_icon'] != '') {
  358. $this->_phpTreeMenu[$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;
  359. } else {
  360. $this->_phpTreeMenu[$menu_name] .= $a_href_open_img . '<img align="top" border="0" class="imgs" src="' . $img_leaf . '" alt="' . $alt_leaf . '" />' . $a_href_close_img;
  361. }
  362. }
  363. // output item text
  364. $foobar = $max_visible_level - $this->tree[$cnt]['level'] + 1;
  365. if ($foobar > 1) {
  366. $colspan = ' colspan="' . $foobar . '"';
  367. } else {
  368. $colspan = '';
  369. }
  370. $this->_phpTreeMenu[$menu_name] .= '&nbsp;' . $a_href_open . $this->tree[$cnt]['parsed_text'] . $a_href_close . "\n";
  371. $this->_phpTreeMenu[$menu_name] .= '</div>' . "\n";
  372. }
  373. }
  374. /* ********************************************************* */
  375. /*
  376. $this->_phpTreeMenu[$menu_name] =
  377. '<div class="phplmnormal">' . "\n" .
  378. $this->_phpTreeMenu[$menu_name] .
  379. '</div>' . "\n";
  380. */
  381. // Some (old) browsers do not support the "white-space: nowrap;" CSS property...
  382. $this->_phpTreeMenu[$menu_name] =
  383. '<table cellspacing="0" cellpadding="0" border="0">' . "\n" .
  384. '<tr>' . "\n" .
  385. '<td class="phplmnormal" nowrap="nowrap">' . "\n" .
  386. $this->_phpTreeMenu[$menu_name] .
  387. '</td>' . "\n" .
  388. '</tr>' . "\n" .
  389. '</table>' . "\n";
  390. return $this->_phpTreeMenu[$menu_name];
  391. }
  392. /**
  393. * Method that returns the code of the requested PHP Tree Menu
  394. * @access public
  395. * @param string $menu_name the name of the menu whose PHP Tree Menu code
  396. * has to be returned
  397. * @return string
  398. */
  399. function getPHPTreeMenu($menu_name)
  400. {
  401. return $this->_phpTreeMenu[$menu_name];
  402. }
  403. /**
  404. * Method that prints the code of the requested PHP Tree Menu
  405. * @access public
  406. * @param string $menu_name the name of the menu whose PHP Tree Menu code
  407. * has to be printed
  408. * @return void
  409. */
  410. function printPHPTreeMenu($menu_name)
  411. {
  412. print $this->_phpTreeMenu[$menu_name];
  413. }
  414. } /* END OF CLASS */
  415. ?>