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. /**
  26. * Prefix for filenames of images of a theme
  27. @access private
  28. @var string 
  29. */
  30. /**
  31. * An array where we store the Tree Menu code for each menu
  32. @access private
  33. @var array 
  34. */
  35. public $_treeMenu;
  36.  
  37. /**
  38. * The constructor method; it initializates the menu system
  39. @return void 
  40. */
  41. function TreeMenu()
  42. {
  43.     $this->LayersMenuCommon();
  44.  
  45.     $this->treeMenuImagesType = 'png';
  46.     $this->treeMenuTheme = '';
  47.     $this->_treeMenu = array();
  48.  
  49.     $this->_nodesCount = 0;
  50.     $this->tree = array();
  51.     $this->_maxLevel = array();
  52.     $this->_firstLevelCnt = array();
  53.     $this->_firstItem = array();
  54.     $this->_lastItem = array();
  55. }
  56.  
  57. /**
  58. * The method to set the dirroot directory
  59. @access public
  60. @return boolean 
  61. */
  62. function setDirroot($dirroot)
  63. {
  64.     return $this->setDirrootCommon($dirroot);
  65. }
  66.  
  67. /**
  68. * The method to set the type of images used for the Tree Menu
  69. @access public
  70. @return void 
  71. */
  72. function setTreeMenuImagesType($treeMenuImagesType)
  73. {
  74.     $this->treeMenuImagesType = $treeMenuImagesType;
  75. }
  76.  
  77. /**
  78. * The method to set the prefix for filenames of images of a theme
  79. @access public
  80. @return void 
  81. */
  82. function setTreeMenuTheme($treeMenuTheme)
  83. {
  84.     $this->treeMenuTheme = $treeMenuTheme;
  85. }
  86.  
  87. /**
  88. * Method to prepare a new Tree Menu.
  89. *
  90. * This method processes items of a menu to prepare and return
  91. * the corresponding Tree Menu code.
  92. *
  93. @access public
  94. @param string $menu_name the name of the menu whose items have to be processed
  95. @return string 
  96. */
  97. function newTreeMenu(
  98.     $menu_name ''    // non consistent default...
  99.     )
  100. {
  101.  
  102.     /* If cookies were disabled, build the url parameter for the session id.
  103.        It will be append to the url to be redirect */
  104.     $id_session_param '';
  105.     if (SID != '')
  106.             $id_session_param sprintf('&%s=%s',session_name(),session_id());
  107.     global $ldapserver;
  108.  
  109.     if (!isset($this->_firstItem[$menu_name]|| !isset($this->_lastItem[$menu_name])) {
  110.         $this->error("newTreeMenuthe first/last item of the menu '$menu_nameis not definedplease check if you have parsed its menu data.");
  111.         return 0;
  112.     }
  113.  
  114.     $this->_treeMenu[$menu_name'';
  115.  
  116.     $img_collapse            $this->imgwww $this->treeMenuTheme 'tree_collapse.' $this->treeMenuImagesType;
  117.     $alt_collapse            '--';
  118.     $img_collapse_corner        $this->imgwww $this->treeMenuTheme 'tree_collapse_corner.' $this->treeMenuImagesType;
  119.     $alt_collapse_corner        '--';
  120.     $img_collapse_corner_first    $this->imgwww $this->treeMenuTheme 'tree_collapse_corner_first.' $this->treeMenuImagesType;
  121.     $alt_collapse_corner_first    '--';
  122.     $img_collapse_first        $this->imgwww $this->treeMenuTheme 'tree_collapse_first.' $this->treeMenuImagesType;
  123.     $alt_collapse_first        '--';
  124.     $img_corner            $this->imgwww $this->treeMenuTheme 'tree_corner.' $this->treeMenuImagesType;
  125.     $alt_corner            '`-';
  126.     $img_expand            $this->imgwww $this->treeMenuTheme 'tree_expand.' $this->treeMenuImagesType;
  127.     $alt_expand            '+-';
  128.     $img_expand_corner        $this->imgwww $this->treeMenuTheme 'tree_expand_corner.' $this->treeMenuImagesType;
  129.     $alt_expand_corner        '+-';
  130.     $img_expand_corner_first    $this->imgwww $this->treeMenuTheme 'tree_expand_corner_first.' $this->treeMenuImagesType;
  131.     $alt_expand_corner_first    '+-';
  132.     $img_expand_first        $this->imgwww $this->treeMenuTheme 'tree_expand_first.' $this->treeMenuImagesType;
  133.     $alt_expand_first        '+-';
  134.     $img_folder_closed        $this->imgwww $this->treeMenuTheme 'tree_folder_closed.' $this->treeMenuImagesType;
  135.     $alt_folder_closed        '->';
  136.     $img_folder_open        $this->imgwww $this->treeMenuTheme 'tree_folder_open.' $this->treeMenuImagesType;
  137.     $alt_folder_open        '->';
  138.     $img_leaf            $this->imgwww $this->treeMenuTheme 'tree_leaf.' $this->treeMenuImagesType;
  139.     $alt_leaf            '->';
  140.     $img_space            $this->imgwww $this->treeMenuTheme 'tree_space.' $this->treeMenuImagesType;
  141.     $alt_space            '  ';
  142.     $img_split            $this->imgwww $this->treeMenuTheme 'tree_split.' $this->treeMenuImagesType;
  143.     $alt_split            '|-';
  144.     $img_split_first        $this->imgwww $this->treeMenuTheme 'tree_split_first.' $this->treeMenuImagesType;
  145.     $alt_split_first        '|-';
  146.     $img_vertline            $this->imgwww $this->treeMenuTheme 'tree_vertline.' $this->treeMenuImagesType;
  147.     $alt_vertline            '| ';
  148.  
  149.     for ($i=0$i<=$this->_maxLevel[$menu_name]$i++{
  150.         $levels[$i0;
  151.     }
  152.  
  153.     // Find last nodes of subtrees
  154.     $last_level $this->_maxLevel[$menu_name];
  155.     for ($i=$this->_lastItem[$menu_name]$i>=$this->_firstItem[$menu_name]$i--{
  156.         if ($this->tree[$i]['level'$last_level{
  157.             for ($j=$this->tree[$i]['level']+1$j<=$this->_maxLevel[$menu_name]$j++{
  158.                 $levels[$j0;
  159.             }
  160.         }
  161.         if ($levels[$this->tree[$i]['level']] == 0{
  162.             $levels[$this->tree[$i]['level']] 1;
  163.             $this->tree[$i]['last_item'1;
  164.         else {
  165.             $this->tree[$i]['last_item'0;
  166.         }
  167.         $last_level $this->tree[$i]['level'];
  168.     }
  169.  
  170.     $toggle '';
  171.     $toggle_function_name 'toggle' $menu_name;
  172.  
  173.     for ($cnt=$this->_firstItem[$menu_name]$cnt<=$this->_lastItem[$menu_name]$cnt++{
  174.         if ($this->tree[$cnt]['text'== '---'{
  175.             continue;    // separators are significant only for layers-based menus
  176.         }
  177.  
  178.         if (isset($this->tree[$cnt]['selected']&& $this->tree[$cnt]['selected']{
  179.             $linkstyle 'phplmselected';
  180.         else {
  181.             $linkstyle 'phplm';
  182.         }
  183.  
  184.         $this->_treeMenu[$menu_name.= '<div id="jt' $cnt '" class="treemenudiv">' "\n";
  185.  
  186.         // vertical lines from higher levels
  187.         for ($i=0$i<$this->tree[$cnt]['level']-1$i++{
  188.             if ($levels[$i== 1{
  189.                 $img $img_vertline;
  190.                 $alt $alt_vertline;
  191.             else {
  192.                 $img $img_space;
  193.                 $alt $alt_space;
  194.             }
  195.             $this->_treeMenu[$menu_name.= '<img align="top" border="0" class="imgs" src="' $img '" alt="' $alt '" />';
  196.         }
  197.  
  198.         $not_a_leaf ($cnt<$this->_lastItem[$menu_name&& $this->tree[$cnt+1]['level']>$this->tree[$cnt]['level']|| (isset($this->tree[$cnt]['children']&& $this->tree[$cnt]['children']);
  199.  
  200.         if ($this->tree[$cnt]['last_item'== 1{
  201.         // corner at end of subtree or t-split
  202.             if ($not_a_leaf{
  203.                 if ($cnt == $this->_firstItem[$menu_name]{
  204.                     if ($this->tree[$cnt]['last_item'&& $this->tree[$cnt]['children']{
  205.                         // Xavier Bruyet : 2006.09.28
  206.                         // Display for the first time the tree without the root node expanded
  207.                         $img $img_expand_corner_first;
  208.                         $alt $alt_expand_corner_first;
  209.                         // @todo: nasty hack, should really do this better.
  210.                         $parsed_href preg_replace('/template_engine/','expand',$this->tree[$cnt]['parsed_href']);
  211.                         $this->_treeMenu[$menu_name.= '<a href="' $parsed_href '"><img align="top" border="0" class="imgs" id="jt' $cnt 'node" src="' $img '" alt="' $alt '" /></a>';
  212.                     else {
  213.                         $img $img_collapse_corner_first;
  214.                         $alt $alt_collapse_corner_first;
  215.                         $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>';
  216.                     }
  217.                 else {
  218.                     if ($this->tree[$cnt]['last_item'&& $this->tree[$cnt]['children']{
  219.                         $img $img_expand_corner;
  220.                         $alt $alt_expand_corner;
  221.                         // @todo: nasty hack, should really do this better.
  222.                         $parsed_href preg_replace('/template_engine/','expand',$this->tree[$cnt]['parsed_href']);
  223.                         $this->_treeMenu[$menu_name.= '<a href="' $parsed_href '"><img align="top" border="0" class="imgs" id="jt' $cnt 'node" src="' $img '" alt="' $alt '" /></a>';
  224.                     else {
  225.                         $img $img_collapse_corner;
  226.                         $alt $alt_collapse_corner;
  227.                         $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>';
  228.                     }
  229.                 }
  230.             else {
  231.                 $this->_treeMenu[$menu_name.= '<img align="top" border="0" class="imgs" src="' $img_corner '" alt="' $alt_corner '" />';
  232.             }
  233.             $levels[$this->tree[$cnt]['level']-10;
  234.         else {
  235.             if ($not_a_leaf{
  236.                 if ($cnt == $this->_firstItem[$menu_name]{
  237.                     $img $img_collapse_first;
  238.                     $alt $alt_collapse_first;
  239.  
  240.                     $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>';
  241.                 else {
  242.                     if ($this->tree[$cnt]['last_item'&& $this->tree[$cnt]['children']{
  243.                         $img $img_expand;
  244.                         $alt $alt_expand;
  245.                         // @todo: nasty hack, should really do this better.
  246.                         $parsed_href preg_replace('/template_engine/','expand',$this->tree[$cnt]['parsed_href']);
  247.  
  248.                         $this->_treeMenu[$menu_name.= '<a href="' $parsed_href '"><img align="top" border="0" class="imgs" id="jt' $cnt 'node" src="' $img '" alt="' $alt '" /></a>';
  249.                     else {
  250.                         $img $img_collapse;
  251.                         $alt $alt_collapse;
  252.  
  253.                         $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>';
  254.                     }
  255.                 }
  256.             else {
  257.                 if ($cnt == $this->_firstItem[$menu_name]{
  258.                     $img $img_split_first;
  259.                     $alt $alt_split_first;
  260.                 else {
  261.                     $img $img_split;
  262.                     $alt $alt_split;
  263.                 }
  264.                 $this->_treeMenu[$menu_name.= '<img align="top" border="0" class="imgs" id="jt' $cnt 'node" src="' $img '" alt="' $alt '" />';
  265.             }
  266.             $levels[$this->tree[$cnt]['level']-11;
  267.         }
  268.  
  269.         if ($this->tree[$cnt]['parsed_href'== '' || $this->tree[$cnt]['parsed_href'== '#'{
  270.             $a_href_open_img '';
  271.             $a_href_close_img '';
  272.             $a_href_open '<a class="phplmnormal">';
  273.             $a_href_close '</a>';
  274.         else {
  275.             $a_href_open_img '<a href="' $this->tree[$cnt]['parsed_href''"' $this->tree[$cnt]['parsed_title'$this->tree[$cnt]['parsed_target''>';
  276.             $a_href_close_img '</a>';
  277.             $a_href_open '<a name="' sprintf('%s_%s%s',$ldapserver->server_id,rawurlencode($this->tree[$cnt]['title']),$id_session_param'" href="' $this->tree[$cnt]['parsed_href''"' $this->tree[$cnt]['parsed_title'$this->tree[$cnt]['parsed_target'' class="' $linkstyle '">';
  278.             $a_href_close '</a>';
  279.         }
  280.  
  281.         if ($not_a_leaf{
  282. //            $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;
  283.             $this->_treeMenu[$menu_name.= $a_href_open_img '<img align="top" border="0" class="imgs" id="jt' $cnt 'folder" src="' ($this->tree[$cnt]['parsed_icon'$this->tree[$cnt]['parsed_icon'$img_folder_open'" alt="' $alt_folder_open '" />' $a_href_close_img;
  284.         else {
  285.             if ($this->tree[$cnt]['parsed_icon'!= ''{
  286. //                $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;
  287.                 $this->_treeMenu[$menu_name.= $a_href_open_img '<img align="top" border="0" src="' $this->tree[$cnt]['parsed_icon''" alt="' $alt_leaf '" />' $a_href_close_img;
  288.             else {
  289.                 $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;
  290.             }
  291.         }
  292.         $this->_treeMenu[$menu_name.= '&nbsp;' $a_href_open $this->tree[$cnt]['text'$a_href_close "\n";
  293.         $this->_treeMenu[$menu_name.= '</div>' "\n";
  294.  
  295.         if ($cnt<$this->_lastItem[$menu_name&& $this->tree[$cnt]['level']<$this->tree[$cnt+1]['level']{
  296.             $this->_treeMenu[$menu_name.= '<div id="jt' $cnt 'son" class="treemenudiv">' "\n";
  297.             if ($this->tree[$cnt]['expanded'!= 1{
  298.                 $toggle .= 'if (phplm_expand[' $cnt '] != 1) ' $toggle_function_name "('" $cnt "');\n";
  299.             else {
  300.                 $toggle .= 'if (phplm_collapse[' $cnt '] == 1) ' $toggle_function_name "('" $cnt "');\n";
  301.             }
  302.         }
  303.  
  304.         if ($cnt>$this->_firstItem[$menu_name&& $this->tree[$cnt]['level']>$this->tree[$cnt+1]['level']{
  305.             for ($i=max(1$this->tree[$cnt+1]['level'])$i<$this->tree[$cnt]['level']$i++{
  306.                 $this->_treeMenu[$menu_name.= '</div>' "\n";
  307.             }
  308.         }
  309.     }
  310.  
  311. /*
  312.     $this->_treeMenu[$menu_name] =
  313.     '<div class="phplmnormal">' . "\n" .
  314.     $this->_treeMenu[$menu_name] .
  315.     '</div>' . "\n";
  316. */
  317.     // Some (old) browsers do not support the "white-space: nowrap;" CSS property...
  318.     $this->_treeMenu[$menu_name=
  319.     '<table cellspacing="0" cellpadding="0" border="0">' "\n" .
  320.     '<tr>' "\n" .
  321.     '<td class="phplmnormal" nowrap="nowrap">' "\n" .
  322.     $this->_treeMenu[$menu_name.
  323.     '</td>' "\n" .
  324.     '</tr>' "\n" .
  325.     '</table>' "\n";
  326.  
  327.     $t new Template_PHPLIB();
  328.     $t->setFile('tplfile'$this->libjsdir 'layerstreemenu.ijs');
  329.     $t->setVar(array(
  330.         'toggle_function_name'        => $toggle_function_name,
  331.         'img_collapse'            => $img_collapse,
  332.         'img_collapse_corner'        => $img_collapse_corner,
  333.         'img_collapse_corner_first'    => $img_collapse_corner_first,
  334.         'img_collapse_first'        => $img_collapse_first,
  335.         'img_expand'            => $img_expand,
  336.         'img_expand_corner'        => $img_expand_corner,
  337.         'img_expand_corner_first'    => $img_expand_corner_first,
  338.         'img_expand_first'        => $img_expand_first,
  339.         'img_folder_closed'        => $img_folder_closed,
  340.         'img_folder_open'        => $img_folder_open
  341.     ));
  342.  
  343.     $inc '<!-- # PHP layers menu. -->
  344.             <script type="text/javascript" language="javascript" src="js/phplayersmenu/libjs/layersmenu-browser_detection.js"></script>
  345.             <script type="text/javascript" language="javascript" src="js/phplayersmenu/libjs/layerstreemenu-cookies.js"></script>';
  346.  
  347.     $toggle_function $t->parse('out''tplfile');
  348.     $toggle_function =
  349.     '<script language="JavaScript" type="text/javascript">' "\n" .
  350.     '<!--' "\n" .
  351.     $toggle_function .
  352.     '// -->' "\n" .
  353.     '</script>' "\n";
  354.  
  355.     $toggle =
  356.     '<script language="JavaScript" type="text/javascript">' "\n" .
  357.     '<!--' "\n" .
  358.     'if ((DOM && !Opera56 && !Konqueror22) || IE4) {' "\n" .
  359.     $toggle .
  360.     '}' "\n" .
  361.     '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" .
  362.     '// -->' "\n" .
  363.     '</script>' "\n";
  364.  
  365.     $this->_treeMenu[$menu_name$inc "\n" $toggle_function "\n" $this->_treeMenu[$menu_name"\n" $toggle;
  366.  
  367.     return $this->_treeMenu[$menu_name];
  368. }
  369.  
  370. /**
  371. * Method that returns the code of the requested Tree Menu
  372. @access public
  373. @param string $menu_name the name of the menu whose Tree Menu code
  374. *    has to be returned
  375. @return string 
  376. */
  377. function getTreeMenu($menu_name)
  378. {
  379.     return $this->_treeMenu[$menu_name];
  380. }
  381.  
  382. /**
  383. * Method that prints the code of the requested Tree Menu
  384. @access public
  385. @param string $menu_name the name of the menu whose Tree Menu code
  386. *    has to be printed
  387. @return void 
  388. */
  389. function printTreeMenu($menu_name)
  390. {
  391.     print $this->_treeMenu[$menu_name];
  392. }
  393.  
  394. /* END OF CLASS */
  395.  
  396. ?>

Documentation generated on Fri, 22 May 2009 19:11:12 +1000 by phpDocumentor 1.4.1