hierarchy.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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-2021 Frédéric France <frederic.france@netlogic.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/user/hierarchy.php
  24. * \ingroup user
  25. * \brief Page of hierarchy view of user module
  26. */
  27. // Load Dolibarr environment
  28. require '../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
  30. // Load translation files required by page
  31. $langs->loadLangs(array('users', 'companies', 'hrm', 'salaries'));
  32. // Security check (for external users)
  33. $socid = 0;
  34. if ($user->socid > 0) {
  35. $socid = $user->socid;
  36. }
  37. $optioncss = GETPOST('optioncss', 'alpha');
  38. $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'userlist'; // To manage different context of search
  39. $mode = GETPOST("mode", 'alpha');
  40. if (empty($mode)) {
  41. $mode = 'hierarchy';
  42. }
  43. $sortfield = GETPOST('sortfield', 'aZ09comma');
  44. $sortorder = GETPOST('sortorder', 'aZ09comma');
  45. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  46. $search_status = GETPOST('search_statut', 'int') ? GETPOST('search_statut', 'int') : GETPOST('search_status', 'int');
  47. if ($search_status === '') {
  48. $search_status = '1';
  49. }
  50. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // Both test are required to be compatible with all browsers
  51. $search_status = "";
  52. }
  53. $search_employee = -1;
  54. if ($contextpage == 'employeelist') {
  55. $search_employee = 1;
  56. }
  57. $userstatic = new User($db);
  58. // Define value to know what current user can do on users
  59. $canadduser = (!empty($user->admin) || $user->hasRight("user", "user", "write"));
  60. // Permission to list
  61. if (isModEnabled('salaries') && $contextpage == 'employeelist' && $search_employee == 1) {
  62. if (!$user->hasRight("salaries", "read")) {
  63. accessforbidden();
  64. }
  65. } else {
  66. if (!$user->hasRight("user", "user", "read") && empty($user->admin)) {
  67. accessforbidden();
  68. }
  69. }
  70. $childids = $user->getAllChildIds(1);
  71. /*
  72. * View
  73. */
  74. $form = new Form($db);
  75. $help_url = 'EN:Module_Users|FR:Module_Utilisateurs|ES:M&oacute;dulo_Usuarios|DE:Modul_Benutzer';
  76. if ($contextpage == 'employeelist' && $search_employee == 1) {
  77. $title = $langs->trans("Employees");
  78. } else {
  79. $title = $langs->trans("Users");
  80. }
  81. $arrayofjs = array(
  82. '/includes/jquery/plugins/jquerytreeview/jquery.treeview.js',
  83. '/includes/jquery/plugins/jquerytreeview/lib/jquery.cookie.js',
  84. );
  85. $arrayofcss = array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.css');
  86. llxHeader('', $title, $help_url, '', 0, 0, $arrayofjs, $arrayofcss, '', 'bodyforlist');
  87. $filters = [];
  88. if (($search_status != '' && $search_status >= 0)) {
  89. $filters[] = "statut = ".((int) $search_status);
  90. }
  91. if (($search_employee != '' && $search_employee >= 0)) {
  92. $filters[] = "employee = ".((int) $search_employee);
  93. }
  94. $sqlfilter= '';
  95. if (!empty($filters)) {
  96. $sqlfilter = join(' AND ', $filters);
  97. }
  98. // Load hierarchy of users
  99. $user_arbo_all = $userstatic->get_full_tree(0, '');
  100. if ($sqlfilter) {
  101. $user_arbo = $userstatic->get_full_tree(0, $sqlfilter);
  102. } else {
  103. $user_arbo = $user_arbo_all;
  104. }
  105. // Count total nb of records
  106. $nbtotalofrecords = count($user_arbo);
  107. if (!is_array($user_arbo) && $user_arbo < 0) {
  108. setEventMessages($userstatic->error, $userstatic->errors, 'warnings');
  109. } else {
  110. // Define fulltree array
  111. $fulltree = $user_arbo;
  112. //var_dump($fulltree);
  113. // Define data (format for treeview)
  114. $data = array();
  115. $data[0] = array('rowid'=>0, 'fk_menu'=>-1, 'title'=>"racine", 'mainmenu'=>'', 'leftmenu'=>'', 'fk_mainmenu'=>'', 'fk_leftmenu'=>'');
  116. foreach ($fulltree as $key => $val) {
  117. $userstatic->id = $val['id'];
  118. $userstatic->ref = $val['id'];
  119. $userstatic->login = $val['login'];
  120. $userstatic->firstname = $val['firstname'];
  121. $userstatic->lastname = $val['lastname'];
  122. $userstatic->statut = $val['statut'];
  123. $userstatic->email = $val['email'];
  124. $userstatic->gender = $val['gender'];
  125. $userstatic->socid = $val['fk_soc'];
  126. $userstatic->admin = $val['admin'];
  127. $userstatic->entity = $val['entity'];
  128. $userstatic->photo = $val['photo'];
  129. $entity = $val['entity'];
  130. $entitystring = '';
  131. // TODO Set of entitystring should be done with a hook
  132. if (isModEnabled('multicompany') && is_object($mc)) {
  133. if (empty($entity)) {
  134. $entitystring = $langs->trans("AllEntities");
  135. } else {
  136. $mc->getInfo($entity);
  137. $entitystring = $mc->label;
  138. }
  139. }
  140. $li = $userstatic->getNomUrl(-1, '', 0, 1);
  141. if (isModEnabled('multicompany') && $userstatic->admin && !$userstatic->entity) {
  142. $li .= img_picto($langs->trans("SuperAdministratorDesc"), 'redstar', 'class="valignmiddle paddingright paddingleft"');
  143. } elseif ($userstatic->admin) {
  144. $li .= img_picto($langs->trans("AdministratorDesc"), 'star', 'class="valignmiddle paddingright paddingleft"');
  145. }
  146. $li .= ' <span class="opacitymedium">('.$val['login'].($entitystring ? ' - '.$entitystring : '').')</span>';
  147. $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>';
  148. $data[$val['rowid']] = array(
  149. 'rowid'=>$val['rowid'],
  150. 'fk_menu'=>$val['fk_user'], // TODO Replace fk_menu with fk_parent
  151. 'statut'=>$val['statut'],
  152. 'entry'=>$entry
  153. );
  154. }
  155. // Loop on $data to link user linked to a parent that was excluded by the filter
  156. foreach ($data as $key => $tmpdata) {
  157. $idparent = $tmpdata['fk_menu'];
  158. // Loop to check if parent exists
  159. if ($idparent > 0) {
  160. $parentfound = array_key_exists($idparent, $data) ? 1 : 0;
  161. $i = 0;
  162. while (!$parentfound && $i < 50) {
  163. // Parent was not found but we need it to show the child, so we reintroduce the parent
  164. if (!empty($user_arbo_all[$idparent])) {
  165. $val = $user_arbo_all[$idparent];
  166. $userstatic->id = $val['id'];
  167. $userstatic->ref = $val['id'];
  168. $userstatic->login = $val['login'];
  169. $userstatic->firstname = $val['firstname'];
  170. $userstatic->lastname = $val['lastname'];
  171. $userstatic->statut = $val['statut'];
  172. $userstatic->email = $val['email'];
  173. $userstatic->gender = $val['gender'];
  174. $userstatic->socid = $val['fk_soc'];
  175. $userstatic->admin = $val['admin'];
  176. $userstatic->entity = $val['entity'];
  177. $userstatic->photo = $val['photo'];
  178. $li = '<span class="opacitymedium">';
  179. $li .= $userstatic->getNomUrl(-1, '', 0, 1);
  180. if (isModEnabled('multicompany') && $userstatic->admin && !$userstatic->entity) {
  181. $li .= img_picto($langs->trans("SuperAdministrator"), 'redstar');
  182. } elseif ($userstatic->admin) {
  183. $li .= img_picto($langs->trans("Administrator"), 'star');
  184. }
  185. $li .= ' <span class="opacitymedium">('.$val['login'].($entitystring ? ' - '.$entitystring : '').')</span>';
  186. $li .= ' - <span class="opacitymedium">'.$langs->trans("ExcludedByFilter").'</span>';
  187. $li .= '</span>';
  188. $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>';
  189. $data[$idparent] = array(
  190. 'rowid' => $idparent,
  191. 'fk_menu' => $user_arbo_all[$idparent]['fk_user'],
  192. 'statut' => $user_arbo_all[$idparent]['statut'],
  193. 'entry' => $entry
  194. );
  195. $idparent = $user_arbo_all[$idparent]['fk_user'];
  196. if ($idparent > 0) {
  197. $parentfound = array_key_exists($idparent, $data) ? 1 : 0;
  198. } else {
  199. $parentfound = 1;
  200. }
  201. //var_dump($data[$idparent]);
  202. } else {
  203. // We should not be here. If a record has a parent id, parent id should be into $user_arbo_all
  204. $data[$key]['fk_menu'] = -2;
  205. if (empty($data[-2])) {
  206. $li = '<span class="opacitymedium">'.$langs->trans("ParentIDDoesNotExistAnymore").'</span>';
  207. $entry = '<table class="nobordernopadding centpercent"><tr class="trtree"><td class="usertddisabled">'.$li.'</td><td align="right" class="usertddisabled"></td></tr></table>';
  208. $data[-2] = array(
  209. 'rowid'=>'-2',
  210. 'fk_menu'=>null,
  211. 'statut'=>'1',
  212. 'entry'=>$entry
  213. );
  214. }
  215. $parentfound = 1;
  216. }
  217. $i++;
  218. }
  219. }
  220. }
  221. //var_dump($data);exit;
  222. $param = "&search_status=".urlencode($search_status);
  223. $param = "&contextpage=".urlencode($contextpage);
  224. $newcardbutton = '';
  225. $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'));
  226. $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'));
  227. $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'));
  228. $newcardbutton .= dolGetButtonTitleSeparator();
  229. $newcardbutton .= dolGetButtonTitle($langs->trans('NewUser'), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/user/card.php?action=create'.($mode == 'employee' ? '&employee=1' : '').'&leftmenu=', '', $canadduser);
  230. $massactionbutton = '';
  231. $num = 0;
  232. $limit = 0;
  233. print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'user', 0, $newcardbutton, '', $limit, 0, 0, 1);
  234. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">'."\n";
  235. if ($optioncss != '') {
  236. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  237. }
  238. print '<input type="hidden" name="token" value="'.newToken().'">';
  239. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  240. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  241. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  242. print '<input type="hidden" name="page" value="'.$page.'">';
  243. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  244. print '<input type="hidden" name="mode" value="'.$mode.'">';
  245. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  246. print '<table class="liste nohover centpercent">';
  247. print '<tr class="liste_titre_filter">';
  248. // Action column
  249. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  250. print '<td class="liste_titre maxwidthsearch">';
  251. $searchpicto = $form->showFilterAndCheckAddButtons(0);
  252. print $searchpicto;
  253. print '</td>';
  254. }
  255. print '<td class="liste_titre">&nbsp;</td>';
  256. print '<td class="liste_titre">&nbsp;</td>';
  257. // Status
  258. print '<td class="liste_titre right parentonrightofpage">';
  259. 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');
  260. print '</td>';
  261. // Action column
  262. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  263. print '<td class="liste_titre maxwidthsearch">';
  264. $searchpicto = $form->showFilterAndCheckAddButtons(0);
  265. print $searchpicto;
  266. print '</td>';
  267. }
  268. print '</tr>';
  269. print '<tr class="liste_titre">';
  270. // Action column
  271. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  272. print_liste_field_titre('', $_SERVER["PHP_SELF"], "", '', '', '', '', '', 'maxwidthsearch ');
  273. }
  274. print_liste_field_titre("HierarchicView");
  275. 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"');
  276. print_liste_field_titre("Status", $_SERVER['PHP_SELF'], "", '', "", '', '', '', 'right onrightofpage');
  277. // Action column
  278. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  279. print_liste_field_titre('', $_SERVER["PHP_SELF"], "", '', '', '', '', '', 'maxwidthsearch ');
  280. }
  281. print '</tr>';
  282. $nbofentries = (count($data) - 1);
  283. if ($nbofentries > 0) {
  284. print '<tr>';
  285. // Action column
  286. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  287. print '<td></td>';
  288. }
  289. print '<td colspan="3">';
  290. tree_recur($data, $data[0], 0);
  291. print '</td>';
  292. // Action column
  293. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  294. print '<td></td>';
  295. }
  296. print '</tr>';
  297. } else {
  298. print '<tr class="oddeven">';
  299. print '<td colspan="3">';
  300. print '<table class="nobordernopadding"><tr class="nobordernopadding"><td>'.img_picto_common('', 'treemenu/branchbottom.gif').'</td>';
  301. print '<td valign="middle">';
  302. print $langs->trans("NoCategoryYet");
  303. print '</td>';
  304. print '<td>&nbsp;</td>';
  305. print '</table>';
  306. print '</td>';
  307. print '<td></td>';
  308. print '</tr>';
  309. }
  310. print "</table>";
  311. print '</div>';
  312. print "</form>\n";
  313. }
  314. //
  315. /*print '<script type="text/javascript">
  316. jQuery(document).ready(function() {
  317. function init_myfunc()
  318. {
  319. jQuery(".usertddisabled").hide();
  320. }
  321. init_myfunc();
  322. });
  323. </script>';
  324. */
  325. // End of page
  326. llxFooter();
  327. $db->close();