treeview.lib.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <https://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 integer[] 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. } else {
  76. if ($fulltree[$key]['level'] == $pos) print img_picto_common('', 'treemenu/branchbottom.gif');
  77. else print img_picto_common('', 'treemenu/linebottom.gif');
  78. }
  79. }
  80. $pos++;
  81. }
  82. return array($atleastoneofthislevelafter, $nbofdirinsub, $nbofdocinsub);
  83. }
  84. // ------------------------------- Used by menu editor, category view, ... -----------------
  85. /**
  86. * Recursive function to output a tree. <ul id="iddivjstree"><li>...</li></ul>
  87. * It is also used for the tree of categories.
  88. * Note: To have this function working, check you have loaded the js and css for treeview.
  89. * $arrayofjs=array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.js',
  90. * '/includes/jquery/plugins/jquerytreeview/lib/jquery.cookie.js');
  91. * $arrayofcss=array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.css');
  92. * TODO Replace with jstree plugin instead of treeview plugin.
  93. *
  94. * @param array $tab Array of all elements
  95. * @param array $pere Array with parent ids ('rowid'=>,'mainmenu'=>,'leftmenu'=>,'fk_mainmenu=>,'fk_leftmenu=>)
  96. * @param int $rang Level of element
  97. * @param string $iddivjstree Id to use for parent ul element
  98. * @param int $donoresetalreadyloaded Do not reset global array $donoresetalreadyloaded used to avoid to go down on an aleady processed record
  99. * @param int $showfk 1=show fk_links to parent into label (used by menu editor only)
  100. * @param string $moreparam Add more param on url of elements
  101. * @return void
  102. */
  103. function tree_recur($tab, $pere, $rang, $iddivjstree = 'iddivjstree', $donoresetalreadyloaded = 0, $showfk = 0, $moreparam = '')
  104. {
  105. global $tree_recur_alreadyadded, $menu_handler_to_search;
  106. if ($rang == 0 && empty($donoresetalreadyloaded)) $tree_recur_alreadyadded = array();
  107. if ($rang == 0)
  108. {
  109. // Test also done with jstree and dynatree (not able to have <a> inside label)
  110. print '<script type="text/javascript" language="javascript">
  111. $(document).ready(function(){
  112. $("#'.$iddivjstree.'").treeview({
  113. collapsed: true,
  114. animated: "fast",
  115. persist: "cookie",
  116. control: "#'.$iddivjstree.'control",
  117. toggle: function() {
  118. /* window.console && console.log("%o was toggled", this); */
  119. }
  120. });
  121. })
  122. </script>';
  123. print '<ul id="'.$iddivjstree.'">';
  124. }
  125. if ($rang > 50)
  126. {
  127. return; // Protect against infinite loop. Max 50 depth
  128. }
  129. //ballayage du tableau
  130. $sizeoftab = count($tab);
  131. $ulprinted = 0;
  132. for ($x = 0; $x < $sizeoftab; $x++)
  133. {
  134. //var_dump($tab[$x]);exit;
  135. // If an element has $pere for parent
  136. if ($tab[$x]['fk_menu'] != -1 && $tab[$x]['fk_menu'] == $pere['rowid'])
  137. {
  138. //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
  139. if (empty($ulprinted) && !empty($pere['rowid']))
  140. {
  141. if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']]))
  142. {
  143. dol_syslog('Error, record with id '.$tab[$x]['rowid'].' seems to be a child of record with id '.$pere['rowid'].' but it was already output. Complete field "leftmenu" and "mainmenu" on ALL records to avoid ambiguity.', LOG_WARNING);
  144. continue;
  145. }
  146. print '<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>'; $ulprinted++;
  147. }
  148. print "\n".'<li '.($tab[$x]['statut'] ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
  149. if ($showfk)
  150. {
  151. print '<table class="nobordernopadding centpercent"><tr><td>';
  152. print '<strong> &nbsp; ';
  153. print $tab[$x]['title'];
  154. print '&nbsp; (fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')';
  155. print '</td><td class="right">';
  156. print $tab[$x]['buttons'];
  157. print '</td></tr></table>';
  158. } else {
  159. print $tab[$x]['entry'];
  160. }
  161. //print ' -> A '.$tab[$x]['rowid'].' mainmenu='.$tab[$x]['mainmenu'].' leftmenu='.$tab[$x]['leftmenu'].' fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].'<br>'."\n";
  162. $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
  163. // And now we search all its sons of lower level
  164. tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
  165. print '</li>';
  166. } elseif (!empty($tab[$x]['rowid']) && $tab[$x]['fk_menu'] == -1 && $tab[$x]['fk_mainmenu'] == $pere['mainmenu'] && $tab[$x]['fk_leftmenu'] == $pere['leftmenu']) {
  167. //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
  168. if (empty($ulprinted) && !empty($pere['rowid']))
  169. {
  170. if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']]))
  171. {
  172. dol_syslog('Error, record with id '.$tab[$x]['rowid'].' seems to be a child of record with id '.$pere['rowid'].' but it was already output. Complete field "leftmenu" and "mainmenu" on ALL records to avoid ambiguity.', LOG_WARNING);
  173. //print 'Error, record with id '.$tab[$x]['rowid'].' seems to be a child of record with id '.$pere['rowid'].' but it was already output. Complete field "leftmenu" and "mainmenu" on ALL records to avoid ambiguity.';
  174. continue;
  175. }
  176. print '<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>'; $ulprinted++;
  177. }
  178. print "\n".'<li '.($tab[$x]['statut'] ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
  179. if ($showfk)
  180. {
  181. print '<table class="nobordernopadding centpercent"><tr><td>';
  182. print '<strong> &nbsp; <a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&menuId='.$tab[$x]['rowid'].$moreparam.'">';
  183. print $tab[$x]['title'];
  184. print '</a></strong>';
  185. print '&nbsp; (mainmenu='.$tab[$x]['mainmenu'].' leftmenu='.$tab[$x]['leftmenu'].' - fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')';
  186. print '</td><td class="right">';
  187. print $tab[$x]['buttons'];
  188. print '</td></tr></table>';
  189. } else {
  190. print $tab[$x]['entry'];
  191. }
  192. //print ' -> B '.$tab[$x]['rowid'].' mainmenu='.$tab[$x]['mainmenu'].' leftmenu='.$tab[$x]['leftmenu'].' fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].'<br>'."\n";
  193. $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
  194. // And now we search all its sons of lower level
  195. //print 'Call tree_recur for x='.$x.' rowid='.$tab[$x]['rowid']." fk_mainmenu pere = ".$tab[$x]['fk_mainmenu']." fk_leftmenu pere = ".$tab[$x]['fk_leftmenu']."<br>\n";
  196. tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
  197. print '</li>';
  198. }
  199. }
  200. if (!empty($ulprinted) && !empty($pere['rowid'])) { print '</ul>'."\n"; }
  201. if ($rang == 0) print '</ul>';
  202. }