dolibarr  17.0.4
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  // Process for column $pos
43 
44  $atleastoneofthislevelafter = 0;
45  $nbofdirinsub = 0;
46  $nbofdocinsub = 0;
47  $found = 0;
48  //print 'x'.$key;
49  foreach ($fulltree as $key2 => $val2) {
50  //print "x".$pos." ".$key2." ".$found." ".$fulltree[$key2]['level'];
51  if ($found == 1) { // We are after the entry to show
52  if ($fulltree[$key2]['level'] > $pos) {
53  $nbofdirinsub++;
54  if (isset($fulltree[$key2]['cachenbofdoc']) && $fulltree[$key2]['cachenbofdoc'] > 0) {
55  $nbofdocinsub += $fulltree[$key2]['cachenbofdoc'];
56  }
57  }
58  if ($fulltree[$key2]['level'] == $pos) {
59  $atleastoneofthislevelafter = 1;
60  }
61  if ($fulltree[$key2]['level'] <= $pos) {
62  break;
63  }
64  }
65  if ($key2 == $key) { // We found ourself, so now every lower level will be counted
66  $found = 1;
67  }
68  }
69  //print $atleastoneofthislevelafter;
70 
71  if (!$silent) {
72  if ($atleastoneofthislevelafter) {
73  if ($fulltree[$key]['level'] == $pos) {
74  print img_picto_common('', 'treemenu/branch.gif');
75  } else {
76  print img_picto_common('', 'treemenu/line.gif');
77  }
78  } else {
79  if ($fulltree[$key]['level'] == $pos) {
80  print img_picto_common('', 'treemenu/branchbottom.gif');
81  } else {
82  print img_picto_common('', 'treemenu/linebottom.gif');
83  }
84  }
85  }
86  $pos++;
87  }
88 
89  return array($atleastoneofthislevelafter, $nbofdirinsub, $nbofdocinsub);
90 }
91 
92 
93 
94 // ------------------------------- Used by menu editor, category view, ... -----------------
95 
114 function tree_recur($tab, $pere, $rang, $iddivjstree = 'iddivjstree', $donoresetalreadyloaded = 0, $showfk = 0, $moreparam = '')
115 {
116  global $tree_recur_alreadyadded, $menu_handler_to_search;
117 
118  if ($rang == 0 && empty($donoresetalreadyloaded)) {
119  $tree_recur_alreadyadded = array();
120  }
121 
122  if ($rang == 0) {
123  // Test also done with jstree and dynatree (not able to have <a> inside label)
124  print '<script type="text/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  return; // Protect against infinite loop. Max 50 depth
143  }
144 
145  // Loop on each element of tree
146  $ulprinted = 0;
147  foreach ($tab as $tmpkey => $tmpval) {
148  $x = $tmpkey;
149 
150  //var_dump($tab[$x]);exit;
151  // If an element has $pere for parent
152  if ($tab[$x]['fk_menu'] != -1 && ((int) $tab[$x]['fk_menu']) == $pere['rowid']) {
153  //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
154  if (empty($ulprinted) && !empty($pere['rowid'])) {
155  if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']])) {
156  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);
157  continue;
158  }
159 
160  print '<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>';
161  $ulprinted++;
162  }
163  print "\n".'<li '.(!empty($tab[$x]['statut']) ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
164  if ($showfk) {
165  print '<table class="nobordernopadding centpercent"><tr><td>';
166  print '<span class="paddingleftonly">'.$tab[$x]['title'].'</span>';
167  print '&nbsp; <span class="opacitymedium">(fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')</span>';
168  print '</td><td class="right nowraponall">';
169  print $tab[$x]['buttons'];
170  print '</td></tr></table>';
171  } else {
172  print $tab[$x]['entry'];
173  }
174  //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";
175  $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
176  // And now we search all its sons of lower level
177  tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
178  print '</li>';
179  } elseif (!empty($tab[$x]['rowid']) && ((int) $tab[$x]['fk_menu']) == -1 && $tab[$x]['fk_mainmenu'] == $pere['mainmenu'] && $tab[$x]['fk_leftmenu'] == $pere['leftmenu']) {
180  //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
181  if (empty($ulprinted) && !empty($pere['rowid'])) {
182  if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']])) {
183  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);
184  //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.';
185  continue;
186  }
187 
188  print '<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>';
189  $ulprinted++;
190  }
191  print "\n".'<li '.(!empty($tab[$x]['statut']) ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
192  if ($showfk) {
193  print '<table class="nobordernopadding centpercent"><tr><td>';
194  print '<strong> &nbsp; <a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&token='.newToken().'&menuId='.$tab[$x]['rowid'].$moreparam.'">';
195  print $tab[$x]['title'];
196  print '</a></strong>';
197  print '&nbsp; (mainmenu='.$tab[$x]['mainmenu'].' leftmenu='.$tab[$x]['leftmenu'].' - fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')';
198  print '</td><td class="right">';
199  print $tab[$x]['buttons'];
200  print '</td></tr></table>';
201  } else {
202  print $tab[$x]['entry'];
203  }
204  //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";
205  $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
206  // And now we search all its sons of lower level
207  //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";
208  tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
209  print '</li>';
210  }
211  }
212  if (!empty($ulprinted) && !empty($pere['rowid'])) {
213  print '</ul>'."\n";
214  }
215 
216  if ($rang == 0) {
217  print '</ul>';
218  }
219 }
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.