PHPLayersMenu
[ class tree: PHPLayersMenu ] [ index: PHPLayersMenu ] [ all elements ]

Source for file treemenu.inc.php

Documentation is available at treemenu.inc.php

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

Documentation generated on Fri, 18 Jul 2008 21:58:34 +0200 by phpDocumentor 1.4.1