dolibarr 19.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
36function 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
114function 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 // 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 "\n".'<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>';
166 print '<td class="tdoverflowmax300">';
167 print '<span class="paddingleft paddingright">'.$tab[$x]['title'].'</span>';
168 print '<span class="opacitymedium">(fk_mainmenu='.$tab[$x]['fk_mainmenu'].' fk_leftmenu='.$tab[$x]['fk_leftmenu'].')</span>';
169 print '</td>';
170 print '<td class="right nowraponall">';
171 print $tab[$x]['buttons'];
172 print '</td></tr></table>';
173 } else {
174 // Show the badge with color for the category
175 print $tab[$x]['entry'];
176 }
177 //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";
178 $tree_recur_alreadyadded[$tab[$x]['rowid']] = ($rang + 1);
179 // And now we search all its sons of lower level
180 tree_recur($tab, $tab[$x], $rang + 1, 'iddivjstree', 0, $showfk);
181 print '</li>';
182 } elseif (!empty($tab[$x]['rowid']) && ((int) $tab[$x]['fk_menu']) == -1 && $tab[$x]['fk_mainmenu'] == $pere['mainmenu'] && $tab[$x]['fk_leftmenu'] == $pere['leftmenu']) {
183 //print 'rang='.$rang.'-x='.$x." rowid=".$tab[$x]['rowid']." tab[x]['fk_leftmenu'] = ".$tab[$x]['fk_leftmenu']." leftmenu pere = ".$pere['leftmenu']."<br>\n";
184 if (empty($ulprinted) && !empty($pere['rowid'])) {
185 if (!empty($tree_recur_alreadyadded[$tab[$x]['rowid']])) {
186 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);
187 //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.';
188 continue;
189 }
190
191 print "\n".'<ul'.(empty($pere['rowid']) ? ' id="treeData"' : '').'>';
192 $ulprinted++;
193 }
194 print "\n".'<li '.(!empty($tab[$x]['statut']) ? ' class="liuseractive"' : 'class="liuserdisabled"').'>';
195 if ($showfk) {
196 print '<table class="nobordernopadding centpercent"><tr>';
197 print '<td class="tdoverflowmax200">';
198 print '<strong class="paddingleft paddingright">';
199 print '<a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&token='.newToken().'&menuId='.$tab[$x]['rowid'].$moreparam.'">';
200 print $tab[$x]['title'];
201 print '</a>';
202 print '</strong>';
203 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>';
204 print '</td>';
205 print '<td class="right nowraponall">';
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'])) {
220 print '</ul>'."\n";
221 }
222
223 if ($rang == 0) {
224 print '</ul>';
225 }
226}
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.