dolibarr 21.0.0-beta
hierarchy.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
3 * Copyright (C) 2005 Eric Seigne <eric.seigne@ryxeo.com>
4 * Copyright (C) 2006-2015 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
6 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
7 * Copyright (C) 2019-2024 Frédéric France <frederic.france@free.fr>
8 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30// Load Dolibarr environment
31require '../main.inc.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
33
42// Load translation files required by page
43$langs->loadLangs(array('users', 'companies', 'hrm', 'salaries'));
44
45// Security check (for external users)
46$socid = 0;
47if ($user->socid > 0) {
48 $socid = $user->socid;
49}
50
51$optioncss = GETPOST('optioncss', 'alpha');
52$contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'userlist'; // To manage different context of search
53$mode = GETPOST("mode", 'alpha');
54if (empty($mode)) {
55 $mode = 'hierarchy';
56}
57
58$sortfield = GETPOST('sortfield', 'aZ09comma');
59$sortorder = GETPOST('sortorder', 'aZ09comma');
60$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
61
62
63$search_status = GETPOST('search_status', 'intcomma');
64if ($search_status == '') {
65 $search_status = '1';
66}
67
68if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // Both test are required to be compatible with all browsers
69 $search_status = "";
70}
71
72$search_employee = -1;
73if ($contextpage == 'employeelist') {
74 $search_employee = 1;
75}
76
77$userstatic = new User($db);
78
79// Define value to know what current user can do on users
80$permissiontoadd = (!empty($user->admin) || $user->hasRight("user", "user", "write"));
81
82// Permission to list
83if (isModEnabled('salaries') && $contextpage == 'employeelist' && $search_employee == 1) {
84 if (!$user->hasRight("salaries", "read")) {
86 }
87} else {
88 if (!$user->hasRight("user", "user", "read") && empty($user->admin)) {
90 }
91}
92
93$childids = $user->getAllChildIds(1);
94
95
96
97/*
98 * View
99 */
100
101$form = new Form($db);
102
103$help_url = 'EN:Module_Users|FR:Module_Utilisateurs|ES:M&oacute;dulo_Usuarios|DE:Modul_Benutzer';
104if ($contextpage == 'employeelist' && $search_employee == 1) {
105 $title = $langs->trans("Employees");
106} else {
107 $title = $langs->trans("Users");
108}
109$arrayofjs = array(
110 '/includes/jquery/plugins/jquerytreeview/jquery.treeview.js',
111 '/includes/jquery/plugins/jquerytreeview/lib/jquery.cookie.js',
112);
113$arrayofcss = array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.css');
114
115llxHeader('', $title, $help_url, '', 0, 0, $arrayofjs, $arrayofcss, '', 'bodyforlist mod-user page-hierarchy');
116
117$filters = [];
118if (($search_status != '' && $search_status >= 0)) {
119 $filters[] = "statut = ".((int) $search_status);
120}
121if (($search_employee != '' && $search_employee >= 0)) {
122 $filters[] = "employee = ".((int) $search_employee);
123}
124$sqlfilter = '';
125if (!empty($filters)) {
126 $sqlfilter = implode(' AND ', $filters);
127}
128// Load hierarchy of users
129$user_arbo_all = $userstatic->get_full_tree(0, '');
130if ($sqlfilter) {
131 $user_arbo = $userstatic->get_full_tree(0, $sqlfilter);
132} else {
133 $user_arbo = $user_arbo_all;
134}
135
136// Count total nb of records
137$nbtotalofrecords = count($user_arbo);
138
139
140if (!is_array($user_arbo) && $user_arbo < 0) {
141 setEventMessages($userstatic->error, $userstatic->errors, 'warnings');
142} else {
143 // Define fulltree array
144 $fulltree = $user_arbo;
145 //var_dump($fulltree);
146 // Define data (format for treeview)
147 $data = array();
148 $data[0] = array('rowid' => 0, 'fk_menu' => -1, 'title' => 'racine', 'mainmenu' => '', 'leftmenu' => '', 'fk_mainmenu' => '', 'fk_leftmenu' => '');
149
150 foreach ($fulltree as $key => $val) {
151 $userstatic->id = $val['id'];
152 $userstatic->ref = (string) $val['id'];
153 $userstatic->login = $val['login'];
154 $userstatic->firstname = $val['firstname'];
155 $userstatic->lastname = $val['lastname'];
156 $userstatic->status = $val['statut'];
157 $userstatic->email = $val['email'];
158 $userstatic->gender = $val['gender'];
159 $userstatic->socid = $val['fk_soc'];
160 $userstatic->admin = $val['admin'];
161 $userstatic->entity = $val['entity'];
162 $userstatic->photo = $val['photo'];
163
164 $entity = $val['entity'];
165 $entitystring = '';
166
167 // TODO Set of entitystring should be done with a hook
168 if (isModEnabled('multicompany') && is_object($mc)) {
169 if (empty($entity)) {
170 $entitystring = $langs->trans("AllEntities");
171 } else {
172 $mc->getInfo($entity);
173 $entitystring = $mc->label;
174 }
175 }
176
177 $li = $userstatic->getNomUrl(-1, '', 0, 1);
178 if (isModEnabled('multicompany') && $userstatic->admin && !$userstatic->entity) {
179 $li .= img_picto($langs->trans("SuperAdministratorDesc"), 'redstar', 'class="valignmiddle paddingright paddingleft"');
180 } elseif ($userstatic->admin) {
181 $li .= img_picto($langs->trans("AdministratorDesc"), 'star', 'class="valignmiddle paddingright paddingleft"');
182 }
183 $li .= ' <span class="opacitymedium">('.$val['login'].($entitystring ? ' - '.$entitystring : '').')</span>';
184
185 $entry = '<table class="nobordernopadding centpercent"><tr class="trtree"><td class="'.($val['statut'] ? 'usertdenabled' : 'usertddisabled').'">'.$li.'</td><td align="right" class="'.($val['statut'] ? 'usertdenabled' : 'usertddisabled').'">'.$userstatic->getLibStatut(2).'</td></tr></table>';
186
187 $data[$val['rowid']] = array(
188 'rowid' => $val['rowid'],
189 'fk_menu' => $val['fk_user'], // TODO Replace fk_menu with fk_parent
190 'statut' => $val['statut'],
191 'entry' => $entry
192 );
193 }
194
195 // Loop on $data to link user linked to a parent that was excluded by the filter
196 foreach ($data as $key => $tmpdata) {
197 $idparent = $tmpdata['fk_menu'];
198 // Loop to check if parent exists
199 if ($idparent > 0) {
200 $parentfound = array_key_exists($idparent, $data) ? 1 : 0;
201
202 $i = 0;
203 while (!$parentfound && $i < 50) {
204 // Parent was not found but we need it to show the child, so we reintroduce the parent
205 if (!empty($user_arbo_all[$idparent])) {
206 $val = $user_arbo_all[$idparent];
207 $userstatic->id = $val['id'];
208 $userstatic->ref = (string) $val['id'];
209 $userstatic->login = $val['login'];
210 $userstatic->firstname = $val['firstname'];
211 $userstatic->lastname = $val['lastname'];
212 $userstatic->status = $val['statut'];
213 $userstatic->email = $val['email'];
214 $userstatic->gender = $val['gender'];
215 $userstatic->socid = $val['fk_soc'];
216 $userstatic->admin = $val['admin'];
217 $userstatic->entity = $val['entity'];
218 $userstatic->photo = $val['photo'];
219
220 $entity = $val['entity'];
221 $entitystring = '';
222
223 // TODO Set of entitystring should be done with a hook
224 if (isModEnabled('multicompany') && is_object($mc)) {
225 if (empty($entity)) {
226 $entitystring = $langs->trans("AllEntities");
227 } else {
228 $mc->getInfo($entity);
229 $entitystring = $mc->label;
230 }
231 }
232
233 $li = '<span class="opacitymedium">';
234 $li .= $userstatic->getNomUrl(-1, '', 0, 1);
235 if (isModEnabled('multicompany') && $userstatic->admin && !$userstatic->entity) {
236 $li .= img_picto($langs->trans("SuperAdministrator"), 'redstar');
237 } elseif ($userstatic->admin) {
238 $li .= img_picto($langs->trans("Administrator"), 'star');
239 }
240 $li .= ' <span class="opacitymedium">('.$val['login'].($entitystring ? ' - '.$entitystring : '').')</span>';
241 $li .= ' - <span class="opacitymedium">'.$langs->trans("ExcludedByFilter").'</span>';
242 $li .= '</span>';
243
244 $entry = '<table class="nobordernopadding centpercent"><tr class="trtree"><td class="'.($val['statut'] ? 'usertdenabled' : 'usertddisabled').'">'.$li.'</td><td align="right" class="'.($val['statut'] ? 'usertdenabled' : 'usertddisabled').'">'.$userstatic->getLibStatut(2).'</td></tr></table>';
245
246 $data[$idparent] = array(
247 'rowid' => $idparent,
248 'fk_menu' => $user_arbo_all[$idparent]['fk_user'],
249 'statut' => $user_arbo_all[$idparent]['statut'],
250 'entry' => $entry
251 );
252 $idparent = $user_arbo_all[$idparent]['fk_user'];
253 if ($idparent > 0) {
254 $parentfound = array_key_exists($idparent, $data) ? 1 : 0;
255 } else {
256 $parentfound = 1;
257 }
258 //var_dump($data[$idparent]);
259 } else {
260 // We should not be here. If a record has a parent id, parent id should be into $user_arbo_all
261 $data[$key]['fk_menu'] = -2;
262 if (empty($data[-2])) {
263 $li = '<span class="opacitymedium">'.$langs->trans("WarningParentIDDoesNotExistAnymore").'</span>';
264 $entry = '<table class="nobordernopadding centpercent"><tr class="trtree"><td class="usertddisabled">'.$li.'</td><td align="right" class="usertddisabled"></td></tr></table>';
265 $data[-2] = array(
266 'rowid' => -2,
267 'fk_menu' => null,
268 'statut' => 1,
269 'entry' => $entry
270 );
271 }
272 $parentfound = 1;
273 }
274
275 $i++;
276 }
277 }
278 }
279 //var_dump($data);exit;
280
281 $param = "&search_status=".urlencode($search_status);
282 $param = "&contextpage=".urlencode($contextpage);
283
284 $newcardbutton = '';
285 $newcardbutton .= dolGetButtonTitle($langs->trans('ViewList'), '', 'fa fa-bars paddingleft imgforviewmode', DOL_URL_ROOT.'/user/list.php?mode=common'.preg_replace('/(&|\?)*mode=[^&]+/', '', $param), '', ((empty($mode) || $mode == 'common') ? 2 : 1), array('morecss' => 'reposition'));
286 $newcardbutton .= dolGetButtonTitle($langs->trans('HierarchicView'), '', 'fa fa-stream paddingleft imgforviewmode', DOL_URL_ROOT.'/user/hierarchy.php?mode=hierarchy'.preg_replace('/(&|\?)*mode=[^&]+/', '', $param), '', (($mode == 'hierarchy') ? 2 : 1), array('morecss' => 'reposition'));
287 $newcardbutton .= dolGetButtonTitle($langs->trans('ViewKanban'), '', 'fa fa-th-list imgforviewmode', DOL_URL_ROOT.'/user/list.php?mode=kanban'.preg_replace('/(&|\?)*mode=[^&]+/', '', $param), '', ($mode == 'kanban' ? 2 : 1), array('morecss' => 'reposition'));
288 $newcardbutton .= dolGetButtonTitleSeparator();
289 $newcardbutton .= dolGetButtonTitle($langs->trans('NewUser'), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/user/card.php?action=create'.($mode == 'employee' ? '&employee=1' : '').'&leftmenu=', '', (int) $permissiontoadd);
290
291 $massactionbutton = '';
292 $num = 0;
293 $limit = 0;
294
295 print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'user', 0, $newcardbutton, '', $limit, 0, 0, 1);
296
297 print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">'."\n";
298 if ($optioncss != '') {
299 print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
300 }
301 print '<input type="hidden" name="token" value="'.newToken().'">';
302 print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
303 print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
304 print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
305 print '<input type="hidden" name="page" value="'.$page.'">';
306 print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
307 print '<input type="hidden" name="mode" value="'.$mode.'">';
308
309 print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
310 print '<table class="liste nohover centpercent">';
311
312 print '<tr class="liste_titre_filter">';
313 // Action column
314 if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
315 print '<td class="liste_titre maxwidthsearch">';
316 $searchpicto = $form->showFilterAndCheckAddButtons(0);
317 print $searchpicto;
318 print '</td>';
319 }
320 print '<td class="liste_titre">&nbsp;</td>';
321 print '<td class="liste_titre">&nbsp;</td>';
322 // Status
323 print '<td class="liste_titre right parentonrightofpage">';
324 print $form->selectarray('search_status', array('-1' => '', '0' => $langs->trans('Disabled'), '1' => $langs->trans('Enabled')), $search_status, 0, 0, 0, '', 0, 0, 0, '', 'minwidth75imp onrightofpage width100');
325 print '</td>';
326 // Action column
327 if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
328 print '<td class="liste_titre maxwidthsearch">';
329 $searchpicto = $form->showFilterAndCheckAddButtons(0);
330 print $searchpicto;
331 print '</td>';
332 }
333 print '</tr>';
334
335 print '<tr class="liste_titre">';
336 // Action column
337 if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
338 print_liste_field_titre('', $_SERVER["PHP_SELF"], "", '', '', '', '', '', 'maxwidthsearch ');
339 }
340 print_liste_field_titre("HierarchicView");
341 print_liste_field_titre('<div id="iddivjstreecontrol"><a href="#">'.img_picto('', 'folder', 'class="paddingright"').'<span class="hideonsmartphone">'.$langs->trans("UndoExpandAll").'</span></a> | <a href="#">'.img_picto('', 'folder-open', 'class="paddingright"').'<span class="hideonsmartphone">'.$langs->trans("ExpandAll").'</span></a></div>', $_SERVER['PHP_SELF'], "", '', "", 'align="center"');
342 print_liste_field_titre("Status", $_SERVER['PHP_SELF'], "", '', "", '', '', '', 'right onrightofpage');
343 // Action column
344 if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
345 print_liste_field_titre('', $_SERVER["PHP_SELF"], "", '', '', '', '', '', 'maxwidthsearch ');
346 }
347 print '</tr>';
348
349
350 $nbofentries = (count($data) - 1);
351
352 if ($nbofentries > 0) {
353 print '<tr>';
354 // Action column
355 if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
356 print '<td></td>';
357 }
358 print '<td colspan="3">';
359 tree_recur($data, $data[0], 0);
360 print '</td>';
361 // Action column
362 if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
363 print '<td></td>';
364 }
365 print '</tr>';
366 } else {
367 print '<tr class="oddeven">';
368 print '<td colspan="3">';
369 print '<table class="nobordernopadding"><tr class="nobordernopadding"><td>'.img_picto_common('', 'treemenu/branchbottom.gif').'</td>';
370 print '<td valign="middle">';
371 print $langs->trans("NoCategoryYet");
372 print '</td>';
373 print '<td>&nbsp;</td>';
374 print '</table>';
375 print '</td>';
376 print '<td></td>';
377 print '</tr>';
378 }
379
380 print "</table>";
381 print '</div>';
382
383 print "</form>\n";
384}
385
386//
387/*print '<script type="text/javascript">
388jQuery(document).ready(function() {
389 function init_myfunc()
390 {
391 jQuery(".usertddisabled").hide();
392 }
393 init_myfunc();
394});
395</script>';
396*/
397
398// End of page
399llxFooter();
400$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
Class to manage generation of HTML components Only common components must be here.
Class to manage Dolibarr users.
llxFooter()
Footer empty.
Definition document.php:107
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
print_barre_liste($title, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $selectlimitsuffix=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dolGetButtonTitle($label, $helpText='', $iconClass='fa fa-file', $url='', $id='', $status=1, $params=array())
Function dolGetButtonTitle : this kind of buttons are used in title in list.
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
dolGetButtonTitleSeparator($moreClass="")
Add space between dolGetButtonTitle.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
tree_recur($tab, $pere, $rang, $iddivjstree='iddivjstree', $donoresetalreadyloaded=0, $showfk=0, $moreparam='')
Recursive function to output a tree.