treeview.lib.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. // Process for column $pos
  38. $atleastoneofthislevelafter = 0;
  39. $nbofdirinsub = 0;
  40. $nbofdocinsub = 0;
  41. $found = 0;
  42. //print 'x'.$key;
  43. foreach ($fulltree as $key2 => $val2) {
  44. //print "x".$pos." ".$key2." ".$found." ".$fulltree[$key2]['level'];
  45. if ($found == 1) { // We are after the entry to show
  46. if ($fulltree[$key2]['level'] > $pos) {
  47. $nbofdirinsub++;
  48. if (isset($fulltree[$key2]['cachenbofdoc']) && $fulltree[$key2]['cachenbofdoc'] > 0) {
  49. $nbofdocinsub += $fulltree[$key2]['cachenbofdoc'];
  50. }
  51. }
  52. if ($fulltree[$key2]['level'] == $pos) {
  53. $atleastoneofthislevelafter = 1;
  54. }
  55. if ($fulltree[$key2]['level'] <= $pos) {
  56. break;
  57. }
  58. }
  59. if ($key2 == $key) { // We found ourself, so now every lower level will be counted
  60. $found = 1;
  61. }
  62. }
  63. //print $atleastoneofthislevelafter;
  64. if (!$silent) {
  65. if ($atleastoneofthislevelafter) {
  66. if ($fulltree[$key]['level'] == $pos) {
  67. print img_picto_common('', 'treemenu/branch.gif');
  68. } else {
  69. print img_picto_common('', 'treemenu/line.gif');
  70. }
  71. } else {
  72. if ($fulltree[$key]['level'] == $pos) {
  73. print img_picto_common('', 'treemenu/branchbottom.gif');
  74. } else {
  75. print img_picto_common('', 'treemenu/linebottom.gif');
  76. }
  77. }
  78. }
  79. $pos++;
  80. }
  81. return array($atleastoneofthislevelafter, $nbofdirinsub, $nbofdocinsub);
  82. }
  83. // ------------------------------- Used by menu editor, category view, ... -----------------
  84. /**
  85. * Recursive function to output a tree. <ul id="iddivjstree"><li>...</li></ul>
  86. * It is also used for the tree of categories.
  87. * Note: To have this function working, check you have loaded the js and css for treeview.
  88. * $arrayofjs=array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.js',
  89. * '/includes/jquery/plugins/jquerytreeview/lib/jquery.cookie.js');
  90. * $arrayofcss=array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.css');
  91. * TODO Replace with jstree plugin instead of treeview plugin.
  92. *
  93. * @param array $tab Array of all elements
  94. * @param array $pere Array with parent ids ('rowid'=>,'mainmenu'=>,'leftmenu'=>,'fk_mainmenu=>,'fk_leftmenu=>)
  95. * @param int $rang Level of element
  96. * @param string $iddivjstree Id to use for parent ul element
  97. * @param int $donoresetalreadyloaded Do not reset global array $donoresetalreadyloaded used to avoid to go down on an aleady processed record
  98. * @param int $showfk 1=show fk_links to parent into label (used by menu editor only)
  99. * @param string $moreparam Add more param on url of elements
  100. * @return void
  101. */
  102. function tree_recur($tab, $pere, $rang, $iddivjstree = 'iddivjstree', $donoresetalreadyloaded = 0, $showfk = 0, $moreparam = '')
  103. {
  104. global $tree_recur_alreadyadded, $menu_handler_to_search;
  105. if ($rang == 0 && empty($donoresetalreadyloaded)) {
  106. $tree_recur_alreadyadded = array();
  107. }
  108. if ($rang == 0) {
  109. // Test also done with jstree and dynatree (not able to have <a> inside label)
  110. print '<script type="text/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. return; // Protect against infinite loop. Max 50 depth
  127. }
  128. //ballayage du tableau
  129. $sizeoftab = count($tab);
  130. $ulprinted = 0;
  131. for ($x = 0; $x < $sizeoftab; $x++) {
  132. //var_dump($tab[$x]);exit;
  133. // If an element has $pere for parent
  134. if ($tab[$x]['fk_menu'] != -1 && $tab[$x]['fk_menu'] == $pere['rowid']) {
  135. //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
  136. if (empty($ulprinted) && !empty($pere['rowid'])) {
  137. if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']])) {
  138. 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);
  139. continue;
  140. }
  141. print '<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>';
  142. $ulprinted++;
  143. }
  144. print "\n".'<li '.(!empty($tab[$x]['statut']) ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
  145. if ($showfk) {
  146. print '<table class="nobordernopadding centpercent"><tr><td>';
  147. print '<span class="paddingleftonly">'.$tab[$x]['title'].'</span>';
  148. print '&nbsp; <span class="opacitymedium">(fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')</span>';
  149. print '</td><td class="right nowraponall">';
  150. print $tab[$x]['buttons'];
  151. print '</td></tr></table>';
  152. } else {
  153. print $tab[$x]['entry'];
  154. }
  155. //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";
  156. $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
  157. // And now we search all its sons of lower level
  158. tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
  159. print '</li>';
  160. } elseif (!empty($tab[$x]['rowid']) && $tab[$x]['fk_menu'] == -1 && $tab[$x]['fk_mainmenu'] == $pere['mainmenu'] && $tab[$x]['fk_leftmenu'] == $pere['leftmenu']) {
  161. //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
  162. if (empty($ulprinted) && !empty($pere['rowid'])) {
  163. if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']])) {
  164. 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);
  165. //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.';
  166. continue;
  167. }
  168. print '<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>';
  169. $ulprinted++;
  170. }
  171. print "\n".'<li '.(!empty($tab[$x]['statut']) ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
  172. if ($showfk) {
  173. print '<table class="nobordernopadding centpercent"><tr><td>';
  174. print '<strong> &nbsp; <a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&token='.newToken().'&menuId='.$tab[$x]['rowid'].$moreparam.'">';
  175. print $tab[$x]['title'];
  176. print '</a></strong>';
  177. print '&nbsp; (mainmenu='.$tab[$x]['mainmenu'].' leftmenu='.$tab[$x]['leftmenu'].' - fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')';
  178. print '</td><td class="right">';
  179. print $tab[$x]['buttons'];
  180. print '</td></tr></table>';
  181. } else {
  182. print $tab[$x]['entry'];
  183. }
  184. //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";
  185. $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
  186. // And now we search all its sons of lower level
  187. //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";
  188. tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
  189. print '</li>';
  190. }
  191. }
  192. if (!empty($ulprinted) && !empty($pere['rowid'])) {
  193. print '</ul>'."\n";
  194. }
  195. if ($rang == 0) {
  196. print '</ul>';
  197. }
  198. }