card.php 19 KB

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