dolibarr  19.0.0-dev
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 nonce="'.getNonce().'" 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  //ballayage du tableau
146  $sizeoftab = count($tab);
147  $ulprinted = 0;
148  for ($x = 0; $x < $sizeoftab; $x++) {
149  //var_dump($tab[$x]);exit;
150  // If an element has $pere for parent
151  if ($tab[$x]['fk_menu'] != -1 && $tab[$x]['fk_menu'] == $pere['rowid']) {
152  //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
153  if (empty($ulprinted) && !empty($pere['rowid'])) {
154  if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']])) {
155  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);
156  continue;
157  }
158 
159  print '<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>';
160  $ulprinted++;
161  }
162  print "\n".'<li '.(!empty($tab[$x]['statut']) ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
163  if ($showfk) {
164  print '<table class="nobordernopadding centpercent"><tr><td>';
165  print '<span class="paddingleftonly">'.$tab[$x]['title'].'</span>';
166  print '&nbsp; <span class="opacitymedium">(fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')</span>';
167  print '</td><td class="right nowraponall">';
168  print $tab[$x]['buttons'];
169  print '</td></tr></table>';
170  } else {
171  print $tab[$x]['entry'];
172  }
173  //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";
174  $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
175  // And now we search all its sons of lower level
176  tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
177  print '</li>';
178  } elseif (!empty($tab[$x]['rowid']) && $tab[$x]['fk_menu'] == -1 && $tab[$x]['fk_mainmenu'] == $pere['mainmenu'] && $tab[$x]['fk_leftmenu'] == $pere['leftmenu']) {
179  //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
180  if (empty($ulprinted) && !empty($pere['rowid'])) {
181  if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']])) {
182  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);
183  //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.';
184  continue;
185  }
186 
187  print '<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>';
188  $ulprinted++;
189  }
190  print "\n".'<li '.(!empty($tab[$x]['statut']) ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
191  if ($showfk) {
192  print '<table class="nobordernopadding centpercent"><tr><td>';
193  print '<strong> &nbsp; <a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&token='.newToken().'&menuId='.$tab[$x]['rowid'].$moreparam.'">';
194  print $tab[$x]['title'];
195  print '</a></strong>';
196  print '&nbsp; (mainmenu='.$tab[$x]['mainmenu'].' leftmenu='.$tab[$x]['leftmenu'].' - fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')';
197  print '</td><td class="right">';
198  print $tab[$x]['buttons'];
199  print '</td></tr></table>';
200  } else {
201  print $tab[$x]['entry'];
202  }
203  //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";
204  $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
205  // And now we search all its sons of lower level
206  //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";
207  tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
208  print '</li>';
209  }
210  }
211  if (!empty($ulprinted) && !empty($pere['rowid'])) {
212  print '</ul>'."\n";
213  }
214 
215  if ($rang == 0) {
216  print '</ul>';
217  }
218 }
img_picto_common
img_picto_common($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $notitle=0)
Show picto (generic function)
Definition: functions.lib.php:4521
tree_recur
tree_recur($tab, $pere, $rang, $iddivjstree='iddivjstree', $donoresetalreadyloaded=0, $showfk=0, $moreparam='')
Recursive function to output a tree.
Definition: treeview.lib.php:114
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1741
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:11654
tree_showpad
tree_showpad(&$fulltree, $key, $silent=0)
Show indent and picto of a tree line.
Definition: treeview.lib.php:36