card.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2021 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2017 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2011 Herve Prot <herve.prot@symeos.com>
  6. * Copyright (C) 2012 Florian Henry <florian.henry@open-concept.pro>
  7. * Copyright (C) 2018 Juanjo Menent <jmenent@2byte.es>
  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/group/card.php
  24. * \brief Tab of a user group
  25. */
  26. // Load Dolibarr environment
  27. require '../../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  32. // Define if user can read permissions
  33. $canreadperms = ($user->admin || $user->hasRight("user", "user", "read"));
  34. $caneditperms = ($user->admin || $user->hasRight("user", "user", "write"));
  35. $candisableperms = ($user->admin || $user->hasRight("user", "user", "delete"));
  36. $feature2 = 'user';
  37. // Advanced permissions
  38. $advancedpermsactive = false;
  39. if (getDolGlobalString('MAIN_USE_ADVANCED_PERMS')) {
  40. $advancedpermsactive = true;
  41. $canreadperms = ($user->admin || ($user->hasRight("user", "group_advance", "read") && $user->hasRight("user", "group_advance", "readperms")));
  42. $caneditperms = ($user->admin || $user->hasRight("user", "group_advance", "write"));
  43. $candisableperms = ($user->admin || $user->hasRight("user", "group_advance", "delete"));
  44. $feature2 = 'group_advance';
  45. }
  46. // Load translation files required by page
  47. $langs->loadLangs(array('users', 'other'));
  48. $id = GETPOST('id', 'int');
  49. $action = GETPOST('action', 'aZ09');
  50. $cancel = GETPOST('cancel', 'aZ09');
  51. $confirm = GETPOST('confirm', 'alpha');
  52. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'groupcard'; // To manage different context of search
  53. $backtopage = GETPOST('backtopage', 'alpha');
  54. $userid = GETPOST('user', 'int');
  55. $object = new UserGroup($db);
  56. $extrafields = new ExtraFields($db);
  57. // fetch optionals attributes and labels
  58. $extrafields->fetch_name_optionals_label($object->table_element);
  59. // Load object
  60. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  61. $object->getrights();
  62. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
  63. $hookmanager->initHooks(array('groupcard', 'globalcard'));
  64. // Security check
  65. $result = restrictedArea($user, 'user', $id, 'usergroup&usergroup', $feature2);
  66. // Users/Groups management only in master entity if transverse mode
  67. if (isModEnabled('multicompany') && $conf->entity > 1 && $conf->global->MULTICOMPANY_TRANSVERSE_MODE) {
  68. accessforbidden();
  69. }
  70. /**
  71. * Actions
  72. */
  73. $parameters = array('id' => $id, 'userid' => $userid, 'caneditperms' => $caneditperms);
  74. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  75. if ($reshook < 0) {
  76. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  77. }
  78. if (empty($reshook)) {
  79. $backurlforlist = DOL_URL_ROOT.'/user/group/list.php';
  80. if (empty($backtopage) || ($cancel && empty($id))) {
  81. if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
  82. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  83. $backtopage = $backurlforlist;
  84. } else {
  85. $backtopage = DOL_URL_ROOT.'/user/group/card.php?id='.($id > 0 ? $id : '__ID__');
  86. }
  87. }
  88. }
  89. if ($cancel) {
  90. header("Location: ".$backtopage);
  91. exit;
  92. }
  93. // Action remove group
  94. if ($action == 'confirm_delete' && $confirm == "yes") {
  95. if ($caneditperms) {
  96. $object->fetch($id);
  97. $object->delete($user);
  98. header("Location: ".DOL_URL_ROOT."/user/group/list.php?restore_lastsearch_values=1");
  99. exit;
  100. } else {
  101. $langs->load("errors");
  102. setEventMessages($langs->trans('ErrorForbidden'), null, 'errors');
  103. }
  104. }
  105. // Action add group
  106. if ($action == 'add') {
  107. if ($caneditperms) {
  108. if (!GETPOST("nom", "alphanohtml")) {
  109. setEventMessages($langs->trans("NameNotDefined"), null, 'errors');
  110. $action = "create"; // Go back to create page
  111. } else {
  112. $object->name = GETPOST("nom", 'alphanohtml');
  113. $object->note = dol_htmlcleanlastbr(trim(GETPOST("note", 'restricthtml')));
  114. // Fill array 'array_options' with data from add form
  115. $ret = $extrafields->setOptionalsFromPost(null, $object);
  116. if ($ret < 0) {
  117. $error++;
  118. }
  119. if (isModEnabled('multicompany') && getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
  120. $object->entity = 0;
  121. } else {
  122. if ($conf->entity == 1 && $user->admin && !$user->entity) { // Same permissions test than the one used to show the combo of entities into the form
  123. $object->entity = GETPOSTISSET("entity") ? GETPOST("entity") : $conf->entity;
  124. } else {
  125. $object->entity = $conf->entity;
  126. }
  127. }
  128. $db->begin();
  129. $id = $object->create();
  130. if ($id > 0) {
  131. $db->commit();
  132. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  133. exit;
  134. } else {
  135. $db->rollback();
  136. $langs->load("errors");
  137. setEventMessages($langs->trans("ErrorGroupAlreadyExists", $object->name), null, 'errors');
  138. $action = "create"; // Go back to create page
  139. }
  140. }
  141. } else {
  142. $langs->load("errors");
  143. setEventMessages($langs->trans('ErrorForbidden'), null, 'errors');
  144. }
  145. }
  146. // Add/Remove user into group
  147. if ($action == 'adduser' || $action == 'removeuser') {
  148. if ($caneditperms) {
  149. if ($userid > 0) {
  150. $object->fetch($id);
  151. $object->oldcopy = clone $object;
  152. $edituser = new User($db);
  153. $edituser->fetch($userid);
  154. if ($action == 'adduser') {
  155. $result = $edituser->SetInGroup($object->id, $object->entity);
  156. }
  157. if ($action == 'removeuser') {
  158. $result = $edituser->RemoveFromGroup($object->id, $object->entity);
  159. }
  160. if ($result > 0) {
  161. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  162. exit;
  163. } else {
  164. setEventMessages($edituser->error, $edituser->errors, 'errors');
  165. }
  166. }
  167. } else {
  168. $langs->load("errors");
  169. setEventMessages($langs->trans('ErrorForbidden'), null, 'errors');
  170. }
  171. }
  172. if ($action == 'update') {
  173. if ($caneditperms) {
  174. $db->begin();
  175. $object->fetch($id);
  176. $object->oldcopy = clone $object;
  177. $object->name = GETPOST("nom", 'alphanohtml');
  178. $object->note = dol_htmlcleanlastbr(trim(GETPOST("note", 'restricthtml')));
  179. $object->tms = dol_now();
  180. // Fill array 'array_options' with data from add form
  181. $ret = $extrafields->setOptionalsFromPost(null, $object, '@GETPOSTISSET');
  182. if ($ret < 0) {
  183. $error++;
  184. }
  185. if (isModEnabled('multicompany') && getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
  186. $object->entity = 0;
  187. } elseif (GETPOSTISSET("entity")) {
  188. $object->entity = GETPOST("entity", "int");
  189. }
  190. $ret = $object->update();
  191. if ($ret >= 0 && !count($object->errors)) {
  192. setEventMessages($langs->trans("GroupModified"), null, 'mesgs');
  193. $db->commit();
  194. } else {
  195. setEventMessages($object->error, $object->errors, 'errors');
  196. $db->rollback();
  197. }
  198. } else {
  199. $langs->load("errors");
  200. setEventMessages($langs->trans('ErrorForbidden'), null, 'mesgs');
  201. }
  202. }
  203. // Actions to build doc
  204. $upload_dir = $conf->user->dir_output.'/usergroups';
  205. $permissiontoadd = $user->hasRight("user", "user", "write");
  206. include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
  207. }
  208. /*
  209. * View
  210. */
  211. $title = $object->name.' - '.$langs->trans("Card");
  212. if ($action == 'create') {
  213. $title = $langs->trans("NewGroup");
  214. }
  215. $help_url = "";
  216. llxHeader('', $title, $help_url);
  217. $form = new Form($db);
  218. $fuserstatic = new User($db);
  219. $form = new Form($db);
  220. $formfile = new FormFile($db);
  221. if ($action == 'create') {
  222. print load_fiche_titre($langs->trans("NewGroup"), '', 'object_group');
  223. dol_set_focus('#nom');
  224. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  225. print '<input type="hidden" name="token" value="'.newToken().'">';
  226. print '<input type="hidden" name="action" value="add">';
  227. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  228. print dol_get_fiche_head('', '', '', 0, '');
  229. print '<table class="border centpercent tableforfieldcreate">';
  230. // Multicompany
  231. if (isModEnabled('multicompany') && is_object($mc)) {
  232. if (!getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE') && $conf->entity == 1 && $user->admin && !$user->entity) {
  233. print "<tr>".'<td class="tdtop">'.$langs->trans("Entity").'</td>';
  234. print "<td>".$mc->select_entities($conf->entity);
  235. print "</td></tr>\n";
  236. } else {
  237. print '<input type="hidden" name="entity" value="'.$conf->entity.'" />';
  238. }
  239. }
  240. // Common attributes
  241. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_add.tpl.php';
  242. // Other attributes
  243. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
  244. print "</table>\n";
  245. print dol_get_fiche_end();
  246. print '<div class="center">';
  247. print '<input class="button" name="add" value="'.$langs->trans("CreateGroup").'" type="submit">';
  248. print ' &nbsp; ';
  249. print '<input class="button button-cancel" value="'.$langs->trans("Cancel").'" name="cancel" type="submit">';
  250. print '</div>';
  251. print "</form>";
  252. } else {
  253. /* ************************************************************************** */
  254. /* */
  255. /* Visu et edition */
  256. /* */
  257. /* ************************************************************************** */
  258. if ($id) {
  259. $head = group_prepare_head($object);
  260. $title = $langs->trans("Group");
  261. /*
  262. * Confirmation suppression
  263. */
  264. if ($action == 'delete') {
  265. print $form->formconfirm($_SERVER['PHP_SELF']."?id=".$object->id, $langs->trans("DeleteAGroup"), $langs->trans("ConfirmDeleteGroup", $object->name), "confirm_delete", '', 0, 1);
  266. }
  267. /*
  268. * Fiche en mode visu
  269. */
  270. if ($action != 'edit') {
  271. print dol_get_fiche_head($head, 'group', $title, -1, 'group');
  272. $linkback = '<a href="'.DOL_URL_ROOT.'/user/group/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  273. dol_banner_tab($object, 'id', $linkback, $user->hasRight("user", "user", "read") || $user->admin);
  274. print '<div class="fichecenter">';
  275. print '<div class="fichehalfleft">';
  276. print '<div class="underbanner clearboth"></div>';
  277. print '<table class="border centpercent tableforfield">';
  278. // Name (already in dol_banner, we keep it to have the GlobalGroup picto, but we should move it in dol_banner)
  279. if (!empty($conf->mutlicompany->enabled)) {
  280. print '<tr><td class="titlefield">'.$langs->trans("Name").'</td>';
  281. print '<td class="valeur">'.dol_escape_htmltag($object->name);
  282. if (empty($object->entity)) {
  283. print img_picto($langs->trans("GlobalGroup"), 'redstar');
  284. }
  285. print "</td></tr>\n";
  286. }
  287. // Multicompany
  288. if (isModEnabled('multicompany') && is_object($mc) && !getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE') && $conf->entity == 1 && $user->admin && !$user->entity) {
  289. $mc->getInfo($object->entity);
  290. print "<tr>".'<td class="titlefield">'.$langs->trans("Entity").'</td>';
  291. print '<td class="valeur">'.dol_escape_htmltag($mc->label);
  292. print "</td></tr>\n";
  293. }
  294. unset($object->fields['nom']); // Name already displayed in banner
  295. // Common attributes
  296. $keyforbreak = '';
  297. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
  298. // Other attributes
  299. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  300. print '</table>';
  301. print '</div>';
  302. print '</div>';
  303. print '<div class="clearboth"></div>';
  304. print dol_get_fiche_end();
  305. /*
  306. * Action bar
  307. */
  308. print '<div class="tabsAction">';
  309. $parameters = array();
  310. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  311. if ($reshook < 0) {
  312. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  313. }
  314. if ($caneditperms) {
  315. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=edit&token='.newToken().'">'.$langs->trans("Modify").'</a>';
  316. }
  317. if ($candisableperms) {
  318. print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&id='.$object->id.'">'.$langs->trans("DeleteGroup").'</a>';
  319. }
  320. print "</div>\n";
  321. // List users in group
  322. print load_fiche_titre($langs->trans("ListOfUsersInGroup"), '', 'user');
  323. // On selectionne les users qui ne sont pas deja dans le groupe
  324. $exclude = array();
  325. if (!empty($object->members)) {
  326. foreach ($object->members as $useringroup) {
  327. $exclude[] = $useringroup->id;
  328. }
  329. }
  330. // Other form for add user to group
  331. $parameters = array('caneditperms' => $caneditperms, 'exclude' => $exclude);
  332. $reshook = $hookmanager->executeHooks('formAddUserToGroup', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  333. print $hookmanager->resPrint;
  334. if (empty($reshook)) {
  335. if ($caneditperms) {
  336. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="POST">'."\n";
  337. print '<input type="hidden" name="token" value="'.newToken().'">';
  338. print '<input type="hidden" name="action" value="adduser">';
  339. print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  340. print '<table class="noborder centpercent">'."\n";
  341. print '<tr class="liste_titre"><td class="titlefield liste_titre">'.$langs->trans("NonAffectedUsers").'</td>'."\n";
  342. print '<td class="liste_titre">';
  343. print $form->select_dolusers('', 'user', 1, $exclude, 0, '', '', $object->entity, 0, 0, '', 0, '', 'minwidth200 maxwidth500');
  344. print ' &nbsp; ';
  345. print '<input type="hidden" name="entity" value="'.$conf->entity.'">';
  346. print '<input type="submit" class="button buttongen button-add" value="'.$langs->trans("Add").'">';
  347. print '</td></tr>'."\n";
  348. print '</table>';
  349. print '</div>';
  350. print '</form>'."\n";
  351. //print '<br>';
  352. }
  353. /*
  354. * Group members
  355. */
  356. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  357. print '<table class="noborder centpercent">';
  358. print '<tr class="liste_titre">';
  359. print '<td class="liste_titre">'.$langs->trans("Login").'</td>';
  360. print '<td class="liste_titre">'.$langs->trans("Lastname").'</td>';
  361. print '<td class="liste_titre">'.$langs->trans("Firstname").'</td>';
  362. print '<td class="liste_titre center" width="5">'.$langs->trans("Status").'</td>';
  363. print '<td class="liste_titre right" width="5">&nbsp;</td>';
  364. print "</tr>\n";
  365. $object->fetch($object->id, '', true); // true to force load of all users, member of the group
  366. if (!empty($object->members)) {
  367. foreach ($object->members as $useringroup) {
  368. print '<tr class="oddeven">';
  369. print '<td class="tdoverflowmax150">';
  370. print $useringroup->getNomUrl(-1, '', 0, 0, 24, 0, 'login');
  371. if (isModEnabled('multicompany') && $useringroup->admin && empty($useringroup->entity)) {
  372. print img_picto($langs->trans("SuperAdministratorDesc"), 'redstar');
  373. } elseif ($useringroup->admin) {
  374. print img_picto($langs->trans("AdministratorDesc"), 'star');
  375. }
  376. print '</td>';
  377. print '<td>'.$useringroup->lastname.'</td>';
  378. print '<td>'.$useringroup->firstname.'</td>';
  379. print '<td class="center">'.$useringroup->getLibStatut(5).'</td>';
  380. print '<td class="right">';
  381. if (!empty($user->admin)) {
  382. print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=removeuser&token='.newToken().'&user='.$useringroup->id.'">';
  383. print img_picto($langs->trans("RemoveFromGroup"), 'unlink');
  384. print '</a>';
  385. } else {
  386. print "-";
  387. }
  388. print "</td></tr>\n";
  389. }
  390. } else {
  391. print '<tr><td colspan="6"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  392. }
  393. print "</table>";
  394. print '</div>';
  395. }
  396. print "<br>";
  397. print '<div class="fichecenter"><div class="fichehalfleft">';
  398. /*
  399. * Generated documents
  400. */
  401. $filename = dol_sanitizeFileName($object->ref);
  402. $filedir = $conf->user->dir_output."/usergroups/".dol_sanitizeFileName($object->ref);
  403. $urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
  404. $genallowed = $user->hasRight("user", "user", "write");
  405. $delallowed = $user->hasRight("user", "user", "delete");
  406. $somethingshown = $formfile->showdocuments('usergroup', $filename, $filedir, $urlsource, $genallowed, $delallowed, $object->model_pdf, 1, 0, 0, 28, 0, '', 0, '', $mysoc->default_lang);
  407. // Show links to link elements
  408. $linktoelem = $form->showLinkToObjectBlock($object, null, null);
  409. $somethingshown = $form->showLinkedObjectBlock($object, $linktoelem);
  410. print '</div><div class="fichehalfright">';
  411. // List of actions on element
  412. /*include_once DOL_DOCUMENT_ROOT . '/core/class/html.formactions.class.php';
  413. $formactions = new FormActions($db);
  414. $somethingshown = $formactions->showactions($object, 'usergroup', $socid, 1);*/
  415. print '</div></div>';
  416. }
  417. /*
  418. * Fiche en mode edition
  419. */
  420. if ($action == 'edit' && $caneditperms) {
  421. print '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="updategroup" enctype="multipart/form-data">';
  422. print '<input type="hidden" name="token" value="'.newToken().'">';
  423. print '<input type="hidden" name="action" value="update">';
  424. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  425. print '<input type="hidden" name="id" value="'.$object->id.'">';
  426. print dol_get_fiche_head($head, 'group', $title, 0, 'group');
  427. print '<table class="border centpercent tableforfieldedit">'."\n";
  428. // Multicompany
  429. if (isModEnabled('multicompany') && is_object($mc)) {
  430. if (!getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE') && $conf->entity == 1 && $user->admin && !$user->entity) {
  431. print "<tr>".'<td class="tdtop">'.$langs->trans("Entity").'</td>';
  432. print "<td>".$mc->select_entities($object->entity);
  433. print "</td></tr>\n";
  434. } else {
  435. print '<input type="hidden" name="entity" value="'.$conf->entity.'" />';
  436. }
  437. }
  438. // Common attributes
  439. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_edit.tpl.php';
  440. // Other attributes
  441. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_edit.tpl.php';
  442. print '</table>';
  443. print dol_get_fiche_end();
  444. print $form->buttonsSaveCancel();
  445. print '</form>';
  446. }
  447. }
  448. }
  449. // End of page
  450. llxFooter();
  451. $db->close();