card.php 19 KB

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