dolibarr  13.0.2
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  *
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 
26 // ------------------------------- Used by ajax tree view -----------------
27 
36 function tree_showpad(&$fulltree, $key, $silent = 0)
37 {
38  $pos = 1;
39 
40  // Loop on each pos, because we will output an img for each pos
41  while ($pos <= $fulltree[$key]['level'] && $fulltree[$key]['level'] > 0)
42  {
43  // Process for column $pos
44 
45  $atleastoneofthislevelafter = 0;
46  $nbofdirinsub = 0;
47  $nbofdocinsub = 0;
48  $found = 0;
49  //print 'x'.$key;
50  foreach ($fulltree as $key2 => $val2)
51  {
52  //print "x".$pos." ".$key2." ".$found." ".$fulltree[$key2]['level'];
53  if ($found == 1) // We are after the entry to show
54  {
55  if ($fulltree[$key2]['level'] > $pos)
56  {
57  $nbofdirinsub++;
58  if (isset($fulltree[$key2]['cachenbofdoc']) && $fulltree[$key2]['cachenbofdoc'] > 0) $nbofdocinsub += $fulltree[$key2]['cachenbofdoc'];
59  }
60  if ($fulltree[$key2]['level'] == $pos)
61  {
62  $atleastoneofthislevelafter = 1;
63  }
64  if ($fulltree[$key2]['level'] <= $pos)
65  {
66  break;
67  }
68  }
69  if ($key2 == $key) // We found ourself, so now every lower level will be counted
70  {
71  $found = 1;
72  }
73  }
74  //print $atleastoneofthislevelafter;
75 
76  if (!$silent)
77  {
78  if ($atleastoneofthislevelafter)
79  {
80  if ($fulltree[$key]['level'] == $pos) print img_picto_common('', 'treemenu/branch.gif');
81  else print img_picto_common('', 'treemenu/line.gif');
82  } else {
83  if ($fulltree[$key]['level'] == $pos) print img_picto_common('', 'treemenu/branchbottom.gif');
84  else print img_picto_common('', 'treemenu/linebottom.gif');
85  }
86  }
87  $pos++;
88  }
89 
90  return array($atleastoneofthislevelafter, $nbofdirinsub, $nbofdocinsub);
91 }
92 
93 
94 
95 // ------------------------------- Used by menu editor, category view, ... -----------------
96 
115 function tree_recur($tab, $pere, $rang, $iddivjstree = 'iddivjstree', $donoresetalreadyloaded = 0, $showfk = 0, $moreparam = '')
116 {
117  global $tree_recur_alreadyadded, $menu_handler_to_search;
118 
119  if ($rang == 0 && empty($donoresetalreadyloaded)) $tree_recur_alreadyadded = array();
120 
121  if ($rang == 0)
122  {
123  // Test also done with jstree and dynatree (not able to have <a> inside label)
124  print '<script type="text/javascript" language="javascript">
125  $(document).ready(function(){
126  $("#'.$iddivjstree.'").treeview({
127  collapsed: true,
128  animated: "fast",
129  persist: "cookie",
130  control: "#'.$iddivjstree.'control",
131  toggle: function() {
132  /* window.console && console.log("%o was toggled", this); */
133  }
134  });
135  })
136  </script>';
137 
138  print '<ul id="'.$iddivjstree.'">';
139  }
140 
141  if ($rang > 50)
142  {
143  return; // Protect against infinite loop. Max 50 depth
144  }
145 
146  //ballayage du tableau
147  $sizeoftab = count($tab);
148  $ulprinted = 0;
149  for ($x = 0; $x < $sizeoftab; $x++)
150  {
151  //var_dump($tab[$x]);exit;
152  // If an element has $pere for parent
153  if ($tab[$x]['fk_menu'] != -1 && $tab[$x]['fk_menu'] == $pere['rowid'])
154  {
155  //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
156  if (empty($ulprinted) && !empty($pere['rowid']))
157  {
158  if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']]))
159  {
160  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);
161  continue;
162  }
163 
164  print '<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>'; $ulprinted++;
165  }
166  print "\n".'<li '.($tab[$x]['statut'] ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
167  if ($showfk)
168  {
169  print '<table class="nobordernopadding centpercent"><tr><td>';
170  print '<strong> &nbsp; ';
171  print $tab[$x]['title'];
172  print '&nbsp; (fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')';
173  print '</td><td class="right">';
174  print $tab[$x]['buttons'];
175  print '</td></tr></table>';
176  } else {
177  print $tab[$x]['entry'];
178  }
179  //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";
180  $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
181  // And now we search all its sons of lower level
182  tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
183  print '</li>';
184  } elseif (!empty($tab[$x]['rowid']) && $tab[$x]['fk_menu'] == -1 && $tab[$x]['fk_mainmenu'] == $pere['mainmenu'] && $tab[$x]['fk_leftmenu'] == $pere['leftmenu']) {
185  //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
186  if (empty($ulprinted) && !empty($pere['rowid']))
187  {
188  if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']]))
189  {
190  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);
191  //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.';
192  continue;
193  }
194 
195  print '<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>'; $ulprinted++;
196  }
197  print "\n".'<li '.($tab[$x]['statut'] ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
198  if ($showfk)
199  {
200  print '<table class="nobordernopadding centpercent"><tr><td>';
201  print '<strong> &nbsp; <a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&menuId='.$tab[$x]['rowid'].$moreparam.'">';
202  print $tab[$x]['title'];
203  print '</a></strong>';
204  print '&nbsp; (mainmenu='.$tab[$x]['mainmenu'].' leftmenu='.$tab[$x]['leftmenu'].' - fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')';
205  print '</td><td class="right">';
206  print $tab[$x]['buttons'];
207  print '</td></tr></table>';
208  } else {
209  print $tab[$x]['entry'];
210  }
211  //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";
212  $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
213  // And now we search all its sons of lower level
214  //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";
215  tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
216  print '</li>';
217  }
218  }
219  if (!empty($ulprinted) && !empty($pere['rowid'])) { print '</ul>'."\n"; }
220 
221  if ($rang == 0) print '</ul>';
222 }
img_picto_common($titlealt, $picto, $moreatt= '', $pictoisfullpath=0)
Show picto (generic function)
tree_showpad(&$fulltree, $key, $silent=0)
Show indent and picto of a tree line.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
print
Draft customers invoices.
Definition: index.php:89
tree_recur($tab, $pere, $rang, $iddivjstree= 'iddivjstree', $donoresetalreadyloaded=0, $showfk=0, $moreparam= '')
Recursive function to output a tree.