dolibarr  20.0.0-beta
treeview.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
3  * Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
27 // ------------------------------- Used by ajax tree view -----------------
28 
37 function tree_showpad(&$fulltree, $key, $silent = 0)
38 {
39  $pos = 1;
40 
41  // Initialise in case no items to loop on.
42  $atleastoneofthislevelafter = 0;
43  $nbofdirinsub = 0;
44  $nbofdocinsub = 0;
45 
46  // Loop on each pos, because we will output an img for each pos
47  while ($pos <= $fulltree[$key]['level'] && $fulltree[$key]['level'] > 0) {
48  // Process for column $pos
49 
50  $atleastoneofthislevelafter = 0;
51  $nbofdirinsub = 0;
52  $nbofdocinsub = 0;
53  $found = 0;
54  //print 'x'.$key;
55  foreach ($fulltree as $key2 => $val2) {
56  //print "x".$pos." ".$key2." ".$found." ".$fulltree[$key2]['level'];
57  if ($found == 1) { // We are after the entry to show
58  if ($fulltree[$key2]['level'] > $pos) {
59  $nbofdirinsub++;
60  if (isset($fulltree[$key2]['cachenbofdoc']) && $fulltree[$key2]['cachenbofdoc'] > 0) {
61  $nbofdocinsub += $fulltree[$key2]['cachenbofdoc'];
62  }
63  }
64  if ($fulltree[$key2]['level'] == $pos) {
65  $atleastoneofthislevelafter = 1;
66  }
67  if ($fulltree[$key2]['level'] <= $pos) {
68  break;
69  }
70  }
71  if ($key2 == $key) { // We found ourself, so now every lower level will be counted
72  $found = 1;
73  }
74  }
75  //print $atleastoneofthislevelafter;
76 
77  if (!$silent) {
78  if ($atleastoneofthislevelafter) {
79  if ($fulltree[$key]['level'] == $pos) {
80  print img_picto_common('', 'treemenu/branch.gif');
81  } else {
82  print img_picto_common('', 'treemenu/line.gif');
83  }
84  } else {
85  if ($fulltree[$key]['level'] == $pos) {
86  print img_picto_common('', 'treemenu/branchbottom.gif');
87  } else {
88  print img_picto_common('', 'treemenu/linebottom.gif');
89  }
90  }
91  }
92  $pos++;
93  }
94 
95  return array($atleastoneofthislevelafter, $nbofdirinsub, $nbofdocinsub);
96 }
97 
98 
99 
100 // ------------------------------- Used by menu editor, category view, ... -----------------
101 
120 function tree_recur($tab, $pere, $rang, $iddivjstree = 'iddivjstree', $donoresetalreadyloaded = 0, $showfk = 0, $moreparam = '')
121 {
122  global $tree_recur_alreadyadded, $menu_handler_to_search;
123 
124  if ($rang == 0 && empty($donoresetalreadyloaded)) {
125  $tree_recur_alreadyadded = array();
126  }
127 
128  if ($rang == 0) {
129  // Test also done with jstree and dynatree (not able to have <a> inside label)
130  print '<script nonce="'.getNonce().'" type="text/javascript">
131  $(document).ready(function(){
132  $("#'.$iddivjstree.'").treeview({
133  collapsed: true,
134  animated: "fast",
135  persist: "cookie",
136  control: "#'.$iddivjstree.'control",
137  toggle: function() {
138  /* window.console && console.log("%o was toggled", this); */
139  }
140  });
141  })
142  </script>';
143 
144  print '<ul id="'.$iddivjstree.'">';
145  }
146 
147  if ($rang > 50) {
148  return; // Protect against infinite loop. Max 50 depth
149  }
150 
151  // Loop on each element of tree
152  $ulprinted = 0;
153  foreach ($tab as $tmpkey => $tmpval) {
154  $x = $tmpkey;
155 
156  //var_dump($tab[$x]);exit;
157  // If an element has $pere for parent
158  if ($tab[$x]['fk_menu'] != -1 && ((int) $tab[$x]['fk_menu']) == $pere['rowid']) {
159  //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
160  if (empty($ulprinted) && !empty($pere['rowid'])) {
161  if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']])) {
162  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);
163  continue;
164  }
165 
166  print "\n".'<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>';
167  $ulprinted++;
168  }
169  print "\n".'<li '.(!empty($tab[$x]['statut']) ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
170  if ($showfk) {
171  print '<table class="nobordernopadding centpercent"><tr>';
172  print '<td class="tdoverflowmax300">';
173  print '<span class="paddingleft paddingright">'.$tab[$x]['title'].'</span>';
174  print '<span class="opacitymedium">(fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')</span>';
175  print '</td>';
176  print '<td class="right nowraponall">';
177  print $tab[$x]['buttons'];
178  print '</td></tr></table>';
179  } else {
180  // Show the badge with color for the category
181  print $tab[$x]['entry'];
182  }
183  //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";
184  $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
185  // And now we search all its sons of lower level
186  tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
187  print '</li>';
188  } elseif (!empty($tab[$x]['rowid']) && ((int) $tab[$x]['fk_menu']) == -1 && $tab[$x]['fk_mainmenu'] == $pere['mainmenu'] && $tab[$x]['fk_leftmenu'] == $pere['leftmenu']) {
189  //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
190  if (empty($ulprinted) && !empty($pere['rowid'])) {
191  if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']])) {
192  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);
193  //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.';
194  continue;
195  }
196 
197  print "\n".'<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>';
198  $ulprinted++;
199  }
200  print "\n".'<li '.(!empty($tab[$x]['statut']) ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
201  if ($showfk) {
202  print '<table class="nobordernopadding centpercent"><tr>';
203  print '<td class="tdoverflowmax200">';
204  print '<strong class="paddingleft paddingright">';
205  print '<a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&token='.newToken().'&menuId='.$tab[$x]['rowid'].$moreparam.'">';
206  print $tab[$x]['title'];
207  print '</a>';
208  print '</strong>';
209  print '<span class="small opacitymedium">(mainmenu='.$tab[$x]['mainmenu'].' - leftmenu='.$tab[$x]['leftmenu'].', fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')</small>';
210  print '</td>';
211  print '<td class="right nowraponall">';
212  print $tab[$x]['buttons'];
213  print '</td></tr></table>';
214  } else {
215  print $tab[$x]['entry'];
216  }
217  //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";
218  $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
219  // And now we search all its sons of lower level
220  //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";
221  tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
222  print '</li>';
223  }
224  }
225  if (!empty($ulprinted) && !empty($pere['rowid'])) {
226  print '</ul>'."\n";
227  }
228 
229  if ($rang == 0) {
230  print '</ul>';
231  }
232 }
newToken()
Return the value of token currently saved into session with name 'newtoken'.
img_picto_common($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $notitle=0)
Show picto (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
tree_showpad(&$fulltree, $key, $silent=0)
Show indent and picto of a tree line.
tree_recur($tab, $pere, $rang, $iddivjstree='iddivjstree', $donoresetalreadyloaded=0, $showfk=0, $moreparam='')
Recursive function to output a tree.