treeview.lib.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /* Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
  3. * Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/lib/treeview.lib.php
  20. * \ingroup core
  21. * \brief Libraries for tree views
  22. */
  23. // ------------------------------- Used by ajax tree view -----------------
  24. /**
  25. * Show indent and picto of a tree line. Return array with information of line.
  26. *
  27. * @param array &$fulltree Array of entries in correct order
  28. * @param string $key Key of entry into fulltree to show picto
  29. * @param int $silent Do not output indent and picto, returns only value
  30. * @return array array(0 or 1 if at least one of this level after, 0 or 1 if at least one of higher level after, nbofdirinsub, nbofdocinsub)
  31. */
  32. function tree_showpad(&$fulltree,$key,$silent=0)
  33. {
  34. $pos=1;
  35. // Loop on each pos, because we will output an img for each pos
  36. while ($pos <= $fulltree[$key]['level'] && $fulltree[$key]['level'] > 0)
  37. {
  38. // Process for column $pos
  39. $atleastoneofthislevelafter=0;
  40. $nbofdirinsub=0;
  41. $nbofdocinsub=0;
  42. $found=0;
  43. //print 'x'.$key;
  44. foreach($fulltree as $key2 => $val2)
  45. {
  46. //print "x".$pos." ".$key2." ".$found." ".$fulltree[$key2]['level'];
  47. if ($found == 1) // We are after the entry to show
  48. {
  49. if ($fulltree[$key2]['level'] > $pos)
  50. {
  51. $nbofdirinsub++;
  52. if (isset($fulltree[$key2]['cachenbofdoc']) && $fulltree[$key2]['cachenbofdoc'] > 0) $nbofdocinsub+=$fulltree[$key2]['cachenbofdoc'];
  53. }
  54. if ($fulltree[$key2]['level'] == $pos)
  55. {
  56. $atleastoneofthislevelafter=1;
  57. }
  58. if ($fulltree[$key2]['level'] <= $pos)
  59. {
  60. break;
  61. }
  62. }
  63. if ($key2 == $key) // We found ourself, so now every lower level will be counted
  64. {
  65. $found=1;
  66. }
  67. }
  68. //print $atleastoneofthislevelafter;
  69. if (! $silent)
  70. {
  71. if ($atleastoneofthislevelafter)
  72. {
  73. if ($fulltree[$key]['level'] == $pos) print img_picto_common('','treemenu/branch.gif');
  74. else print img_picto_common('','treemenu/line.gif');
  75. }
  76. else
  77. {
  78. if ($fulltree[$key]['level'] == $pos) print img_picto_common('','treemenu/branchbottom.gif');
  79. else print img_picto_common('','treemenu/linebottom.gif');
  80. }
  81. }
  82. $pos++;
  83. }
  84. return array($atleastoneofthislevelafter,$nbofdirinsub,$nbofdocinsub);
  85. }
  86. // ------------------------------- Used by menu editor -----------------
  87. /**
  88. * Recursive function to output menu tree. <ul id="iddivjstree"><li>...</li></ul>
  89. * Note: To have this function working, check you have loaded the js and css for treeview.
  90. * $arrayofjs=array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.js',
  91. * '/includes/jquery/plugins/jquerytreeview/lib/jquery.cookie.js');
  92. * $arrayofcss=array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.css');
  93. * TODO Replace with jstree plugin instead of treeview plugin.
  94. *
  95. * @param array $tab Array of all elements
  96. * @param int $pere Array with parent ids ('rowid'=>,'mainmenu'=>,'leftmenu'=>,'fk_mainmenu=>,'fk_leftmenu=>)
  97. * @param int $rang Level of element
  98. * @param string $iddivjstree Id to use for parent ul element
  99. * @return void
  100. */
  101. function tree_recur($tab, $pere, $rang, $iddivjstree='iddivjstree')
  102. {
  103. if (empty($pere['rowid']))
  104. {
  105. // Test also done with jstree and dynatree (not able to have <a> inside label)
  106. print '<script type="text/javascript" language="javascript">
  107. $(document).ready(function(){
  108. $("#'.$iddivjstree.'").treeview({
  109. collapsed: true,
  110. animated: "fast",
  111. persist: "cookie",
  112. control: "#'.$iddivjstree.'control",
  113. toggle: function() {
  114. /* window.console && console.log("%o was toggled", this); */
  115. }
  116. });
  117. })
  118. </script>';
  119. print '<ul id="'.$iddivjstree.'">';
  120. }
  121. if ($rang > 10) return; // Protection contre boucle infinie
  122. //ballayage du tableau
  123. $sizeoftab=count($tab);
  124. $ulprinted=0;
  125. for ($x=0; $x < $sizeoftab; $x++)
  126. {
  127. //var_dump($tab[$x]);exit;
  128. // If an element has $pere for parent
  129. if ($tab[$x]['fk_menu'] != -1 && $tab[$x]['fk_menu'] == $pere['rowid'])
  130. {
  131. if (empty($ulprinted) && ! empty($pere['rowid'])) { print '<ul'.(empty($pere['rowid'])?' id="treeData"':'').'>'; $ulprinted++; }
  132. print "\n".'<li>';
  133. print $tab[$x]['entry'];
  134. // And now we search all its sons of lower level
  135. tree_recur($tab,$tab[$x],$rang+1);
  136. print '</li>';
  137. }
  138. elseif (! empty($tab[$x]['rowid']) && $tab[$x]['fk_menu'] == -1 && $tab[$x]['fk_mainmenu'] == $pere['mainmenu'] && $tab[$x]['fk_leftmenu'] == $pere['leftmenu'])
  139. {
  140. if (empty($ulprinted) && ! empty($pere['rowid'])) { print '<ul'.(empty($pere['rowid'])?' id="treeData"':'').'>'; $ulprinted++; }
  141. print "\n".'<li>';
  142. print $tab[$x]['entry'];
  143. // And now we search all its sons of lower level
  144. tree_recur($tab,$tab[$x],$rang+1);
  145. print '</li>';
  146. }
  147. }
  148. if (! empty($ulprinted) && ! empty($pere['rowid'])) { print '</ul>'."\n"; }
  149. if (empty($pere['rowid'])) print '</ul>';
  150. }