dolibarr  13.0.2
card.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2005-2015 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 
28 require '../../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
33 
34 // Defini si peux lire/modifier utilisateurs et permisssions
35 $canreadperms = ($user->admin || $user->rights->user->user->lire);
36 $caneditperms = ($user->admin || $user->rights->user->user->creer);
37 $candisableperms = ($user->admin || $user->rights->user->user->supprimer);
38 $feature2 = 'user';
39 
40 // Advanced permissions
41 if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS))
42 {
43  $canreadperms = ($user->admin || $user->rights->user->group_advance->read);
44  $caneditperms = ($user->admin || $user->rights->user->group_advance->write);
45  $candisableperms = ($user->admin || $user->rights->user->group_advance->delete);
46  $feature2 = 'group_advance';
47 }
48 
49 // Load translation files required by page
50 $langs->loadLangs(array('users', 'other'));
51 
52 $id = GETPOST('id', 'int');
53 $action = GETPOST('action', 'aZ09');
54 $cancel = GETPOST('cancel', 'aZ09');
55 $confirm = GETPOST('confirm', 'alpha');
56 $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'groupcard'; // To manage different context of search
57 $backtopage = GETPOST('backtopage', 'alpha');
58 
59 $userid = GETPOST('user', 'int');
60 
61 // Security check
62 $result = restrictedArea($user, 'user', $id, 'usergroup&usergroup', $feature2);
63 
64 // Users/Groups management only in master entity if transverse mode
65 if (!empty($conf->multicompany->enabled) && $conf->entity > 1 && $conf->global->MULTICOMPANY_TRANSVERSE_MODE)
66 {
68 }
69 
70 $object = new Usergroup($db);
71 $extrafields = new ExtraFields($db);
72 // fetch optionals attributes and labels
73 $extrafields->fetch_name_optionals_label($object->table_element);
74 
75 // Load object
76 include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
77 $object->getrights();
78 
79 // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
80 $hookmanager->initHooks(array('groupcard', 'globalcard'));
81 
82 
83 
88 $parameters = array('id' => $id, 'userid' => $userid, 'caneditperms' => $caneditperms);
89 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
90 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
91 
92 if (empty($reshook)) {
93  $backurlforlist = DOL_URL_ROOT.'/user/group/list.php';
94 
95  if (empty($backtopage) || ($cancel && empty($id))) {
96  if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
97  if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) $backtopage = $backurlforlist;
98  else $backtopage = dol_buildpath('/user/group/card.php', 1).'?id='.($id > 0 ? $id : '__ID__');
99  }
100  }
101 
102  if ($cancel)
103  {
104  header("Location: ".$backtopage);
105  exit;
106  }
107 
108  // Action remove group
109  if ($action == 'confirm_delete' && $confirm == "yes")
110  {
111  if ($caneditperms)
112  {
113  $object->fetch($id);
114  $object->delete($user);
115  header("Location: ".DOL_URL_ROOT."/user/group/list.php?restore_lastsearch_values=1");
116  exit;
117  } else {
118  $langs->load("errors");
119  setEventMessages($langs->trans('ErrorForbidden'), null, 'errors');
120  }
121  }
122 
123  // Action add group
124  if ($action == 'add')
125  {
126  if ($caneditperms)
127  {
128  if (!GETPOST("nom", "nohtml")) {
129  setEventMessages($langs->trans("NameNotDefined"), null, 'errors');
130  $action = "create"; // Go back to create page
131  } else {
132  $object->name = GETPOST("nom", 'nohtml');
133  $object->note = dol_htmlcleanlastbr(trim(GETPOST("note", 'restricthtml')));
134 
135  // Fill array 'array_options' with data from add form
136  $ret = $extrafields->setOptionalsFromPost(null, $object);
137  if ($ret < 0) $error++;
138 
139  if (!empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) $object->entity = 0;
140  else $object->entity = $_POST["entity"];
141 
142  $db->begin();
143 
144  $id = $object->create();
145 
146  if ($id > 0)
147  {
148  $db->commit();
149 
150  header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
151  exit;
152  } else {
153  $db->rollback();
154 
155  $langs->load("errors");
156  setEventMessages($langs->trans("ErrorGroupAlreadyExists", $object->name), null, 'errors');
157  $action = "create"; // Go back to create page
158  }
159  }
160  } else {
161  $langs->load("errors");
162  setEventMessages($langs->trans('ErrorForbidden'), null, 'errors');
163  }
164  }
165 
166  // Add/Remove user into group
167  if ($action == 'adduser' || $action == 'removeuser')
168  {
169  if ($caneditperms)
170  {
171  if ($userid > 0)
172  {
173  $object->fetch($id);
174  $object->oldcopy = clone $object;
175 
176  $edituser = new User($db);
177  $edituser->fetch($userid);
178  if ($action == 'adduser') $result = $edituser->SetInGroup($object->id, $object->entity);
179  if ($action == 'removeuser') $result = $edituser->RemoveFromGroup($object->id, $object->entity);
180 
181  if ($result > 0)
182  {
183  header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
184  exit;
185  } else {
186  setEventMessages($edituser->error, $edituser->errors, 'errors');
187  }
188  }
189  } else {
190  $langs->load("errors");
191  setEventMessages($langs->trans('ErrorForbidden'), null, 'errors');
192  }
193  }
194 
195 
196  if ($action == 'update')
197  {
198  if ($caneditperms)
199  {
200  $db->begin();
201 
202  $object->fetch($id);
203 
204  $object->oldcopy = clone $object;
205 
206  $object->name = GETPOST("nom", 'nohtml');
207  $object->note = dol_htmlcleanlastbr(trim(GETPOST("note", 'restricthtml')));
208 
209  // Fill array 'array_options' with data from add form
210  $ret = $extrafields->setOptionalsFromPost(null, $object);
211  if ($ret < 0) $error++;
212 
213  if (!empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) $object->entity = 0;
214  else $object->entity = $_POST["entity"];
215 
216  $ret = $object->update();
217 
218  if ($ret >= 0 && !count($object->errors))
219  {
220  setEventMessages($langs->trans("GroupModified"), null, 'mesgs');
221  $db->commit();
222  } else {
223  setEventMessages($object->error, $object->errors, 'errors');
224  $db->rollback();
225  }
226  } else {
227  $langs->load("errors");
228  setEventMessages($langs->trans('ErrorForbidden'), null, 'mesgs');
229  }
230  }
231 
232  // Actions to build doc
233  $upload_dir = $conf->usergroup->dir_output;
234  $permissiontoadd = $user->rights->user->user->creer;
235  include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
236 }
237 
238 
239 /*
240  * View
241  */
242 
243 llxHeader('', $langs->trans("GroupCard"));
244 
245 $form = new Form($db);
246 $fuserstatic = new User($db);
247 $form = new Form($db);
248 $formfile = new FormFile($db);
249 
250 if ($action == 'create')
251 {
252  print load_fiche_titre($langs->trans("NewGroup"), '', 'object_group');
253 
254  print dol_set_focus('#nom');
255 
256  print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
257  print '<input type="hidden" name="token" value="'.newToken().'">';
258  print '<input type="hidden" name="action" value="add">';
259  print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
260 
261  print dol_get_fiche_head('', '', '', 0, '');
262 
263  print '<table class="border centpercent tableforfieldcreate">';
264 
265  // Multicompany
266  if (!empty($conf->multicompany->enabled) && is_object($mc))
267  {
268  if (empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && $conf->entity == 1 && $user->admin && !$user->entity)
269  {
270  print "<tr>".'<td class="tdtop">'.$langs->trans("Entity").'</td>';
271  print "<td>".$mc->select_entities($conf->entity);
272  print "</td></tr>\n";
273  } else {
274  print '<input type="hidden" name="entity" value="'.$conf->entity.'" />';
275  }
276  }
277 
278  // Common attributes
279  include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_add.tpl.php';
280 
281  // Other attributes
282  include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
283 
284  print "</table>\n";
285 
287 
288  print '<div class="center">';
289  print '<input class="button" name="add" value="'.$langs->trans("CreateGroup").'" type="submit">';
290  print ' &nbsp; ';
291  print '<input class="button button-cancel" value="'.$langs->trans("Cancel").'" name="cancel" type="submit">';
292  print '</div>';
293 
294  print "</form>";
295 }
296 
297 
298 /* ************************************************************************** */
299 /* */
300 /* Visu et edition */
301 /* */
302 /* ************************************************************************** */
303 else {
304  if ($id)
305  {
306  $res = $object->fetch_optionals();
307 
308  $head = group_prepare_head($object);
309  $title = $langs->trans("Group");
310 
311  /*
312  * Confirmation suppression
313  */
314  if ($action == 'delete')
315  {
316  print $form->formconfirm($_SERVER['PHP_SELF']."?id=".$object->id, $langs->trans("DeleteAGroup"), $langs->trans("ConfirmDeleteGroup", $object->name), "confirm_delete", '', 0, 1);
317  }
318 
319  /*
320  * Fiche en mode visu
321  */
322 
323  if ($action != 'edit')
324  {
325  print dol_get_fiche_head($head, 'group', $title, -1, 'group');
326 
327  $linkback = '<a href="'.DOL_URL_ROOT.'/user/group/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
328 
329  dol_banner_tab($object, 'id', $linkback, $user->rights->user->user->lire || $user->admin);
330 
331  print '<div class="fichecenter">';
332  print '<div class="fichehalfleft">';
333  print '<div class="underbanner clearboth"></div>';
334 
335  print '<table class="border centpercent tableforfield">';
336 
337  // Name (already in dol_banner, we keep it to have the GlobalGroup picto, but we should move it in dol_banner)
338  if (!empty($conf->mutlicompany->enabled))
339  {
340  print '<tr><td class="titlefield">'.$langs->trans("Name").'</td>';
341  print '<td class="valeur">'.dol_escape_htmltag($object->name);
342  if (empty($object->entity))
343  {
344  print img_picto($langs->trans("GlobalGroup"), 'redstar');
345  }
346  print "</td></tr>\n";
347  }
348 
349  // Multicompany
350  if (!empty($conf->multicompany->enabled) && is_object($mc) && empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && $conf->entity == 1 && $user->admin && !$user->entity)
351  {
352  $mc->getInfo($object->entity);
353  print "<tr>".'<td class="titlefield">'.$langs->trans("Entity").'</td>';
354  print '<td class="valeur">'.dol_escape_htmltag($mc->label);
355  print "</td></tr>\n";
356  }
357 
358  unset($object->fields['nom']); // Name already displayed in banner
359 
360  // Common attributes
361  $keyforbreak = '';
362  include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
363 
364  // Other attributes
365  include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
366 
367  print '</table>';
368  print '</div>';
369  print '</div>';
370 
371  print '<div class="clearboth"></div>';
372 
374 
375 
376  /*
377  * Barre d'actions
378  */
379 
380  print '<div class="tabsAction">';
381 
382  $parameters = array();
383  $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
384  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
385 
386  if ($caneditperms)
387  {
388  print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=edit&amp;token='.newToken().'">'.$langs->trans("Modify").'</a>';
389  }
390 
391  if ($candisableperms)
392  {
393  print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=delete&amp;id='.$object->id.'&amp;token='.newToken().'">'.$langs->trans("DeleteGroup").'</a>';
394  }
395 
396  print "</div>\n";
397 
398  // List users in group
399 
400  print load_fiche_titre($langs->trans("ListOfUsersInGroup"), '', 'user');
401 
402  // On selectionne les users qui ne sont pas deja dans le groupe
403  $exclude = array();
404 
405  if (!empty($object->members))
406  {
407  foreach ($object->members as $useringroup)
408  {
409  $exclude[] = $useringroup->id;
410  }
411  }
412 
413  // Other form for add user to group
414  $parameters = array('caneditperms' => $caneditperms, 'exclude' => $exclude);
415  $reshook = $hookmanager->executeHooks('formAddUserToGroup', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
416  print $hookmanager->resPrint;
417 
418  if (empty($reshook))
419  {
420  if ($caneditperms)
421  {
422  print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="POST">'."\n";
423  print '<input type="hidden" name="token" value="'.newToken().'">';
424  print '<input type="hidden" name="action" value="adduser">';
425  print '<table class="noborder centpercent">'."\n";
426  print '<tr class="liste_titre"><td class="titlefield liste_titre">'.$langs->trans("NonAffectedUsers").'</td>'."\n";
427  print '<td class="liste_titre">';
428  print $form->select_dolusers('', 'user', 1, $exclude, 0, '', '', $object->entity, 0, 0, '', 0, '', 'maxwidth300');
429  print ' &nbsp; ';
430  print '<input type="hidden" name="entity" value="'.$conf->entity.'">';
431  print '<input type="submit" class="button buttongen" value="'.$langs->trans("Add").'">';
432  print '</td></tr>'."\n";
433  print '</table></form>'."\n";
434  print '<br>';
435  }
436 
437  /*
438  * Group members
439  */
440 
441  print '<table class="noborder centpercent">';
442  print '<tr class="liste_titre">';
443  print '<td class="liste_titre">'.$langs->trans("Login").'</td>';
444  print '<td class="liste_titre">'.$langs->trans("Lastname").'</td>';
445  print '<td class="liste_titre">'.$langs->trans("Firstname").'</td>';
446  print '<td class="liste_titre center" width="5">'.$langs->trans("Status").'</td>';
447  print '<td class="liste_titre right" width="5">&nbsp;</td>';
448  print "</tr>\n";
449 
450  if (!empty($object->members))
451  {
452  foreach ($object->members as $useringroup)
453  {
454  print '<tr class="oddeven">';
455  print '<td>';
456  print $useringroup->getNomUrl(-1, '', 0, 0, 24, 0, 'login');
457  if ($useringroup->admin && !$useringroup->entity) {
458  print img_picto($langs->trans("SuperAdministrator"), 'redstar');
459  } elseif ($useringroup->admin) {
460  print img_picto($langs->trans("Administrator"), 'star');
461  }
462  print '</td>';
463  print '<td>'.$useringroup->lastname.'</td>';
464  print '<td>'.$useringroup->firstname.'</td>';
465  print '<td class="center">'.$useringroup->getLibStatut(5).'</td>';
466  print '<td class="right">';
467  if (!empty($user->admin)) {
468  print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=removeuser&amp;user='.$useringroup->id.'">';
469  print img_picto($langs->trans("RemoveFromGroup"), 'unlink');
470  print '</a>';
471  } else {
472  print "-";
473  }
474  print "</td></tr>\n";
475  }
476  } else {
477  print '<tr><td colspan="6" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
478  }
479  print "</table>";
480  }
481 
482  print "<br>";
483 
484  print '<div class="fichecenter"><div class="fichehalfleft">';
485 
486  /*
487  * Documents generes
488  */
489 
490  $filename = dol_sanitizeFileName($object->ref);
491  $filedir = $conf->usergroup->dir_output."/".dol_sanitizeFileName($object->ref);
492  $urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
493  $genallowed = $user->rights->user->user->creer;
494  $delallowed = $user->rights->user->user->supprimer;
495 
496  $somethingshown = $formfile->showdocuments('usergroup', $filename, $filedir, $urlsource, $genallowed, $delallowed, $object->model_pdf, 1, 0, 0, 28, 0, '', 0, '', $soc->default_lang);
497 
498  // Show links to link elements
499  $linktoelem = $form->showLinkToObjectBlock($object, null, null);
500  $somethingshown = $form->showLinkedObjectBlock($object, $linktoelem);
501 
502  print '</div><div class="fichehalfright"><div class="ficheaddleft">';
503 
504  // List of actions on element
505  /*include_once DOL_DOCUMENT_ROOT . '/core/class/html.formactions.class.php';
506  $formactions = new FormActions($db);
507  $somethingshown = $formactions->showactions($object, 'usergroup', $socid, 1);*/
508 
509  print '</div></div></div>';
510  }
511 
512  /*
513  * Fiche en mode edition
514  */
515 
516  if ($action == 'edit' && $caneditperms)
517  {
518  print '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="updategroup" enctype="multipart/form-data">';
519  print '<input type="hidden" name="token" value="'.newToken().'">';
520  print '<input type="hidden" name="action" value="update">';
521  print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
522  print '<input type="hidden" name="id" value="'.$object->id.'">';
523 
524  print dol_get_fiche_head($head, 'group', $title, 0, 'group');
525 
526  print '<table class="border centpercent tableforfieldedit">'."\n";
527 
528  // Multicompany
529  if (!empty($conf->multicompany->enabled) && is_object($mc))
530  {
531  if (empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && $conf->entity == 1 && $user->admin && !$user->entity)
532  {
533  print "<tr>".'<td class="tdtop">'.$langs->trans("Entity").'</td>';
534  print "<td>".$mc->select_entities($object->entity);
535  print "</td></tr>\n";
536  } else {
537  print '<input type="hidden" name="entity" value="'.$conf->entity.'" />';
538  }
539  }
540 
541  // Common attributes
542  include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_edit.tpl.php';
543 
544  // Other attributes
545  include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_edit.tpl.php';
546 
547  print '</table>';
548 
550 
551  print '<div class="center"><input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
552  print ' &nbsp; <input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
553  print '</div>';
554 
555  print '</form>';
556  }
557  }
558 }
559 
560 // End of page
561 llxFooter();
562 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage Dolibarr users.
Definition: user.class.php:44
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
llxHeader()
Empty header.
Definition: wrapper.php:45
Class to manage standard extra fields.
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
group_prepare_head($object)
Prepare array with list of tabs.
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
accessforbidden($message= '', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
Class to offer components to list and upload files.
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
dol_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
newToken()
Return the value of token currently saved into session with name &#39;newtoken&#39;.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_banner_tab($object, $paramid, $morehtml= '', $shownav=1, $fieldid= 'rowid', $fieldref= 'ref', $morehtmlref= '', $moreparam= '', $nodbprefix=0, $morehtmlleft= '', $morehtmlstatus= '', $onlybanner=0, $morehtmlright= '')
Show tab footer of a card.
llxFooter()
Empty footer.
Definition: wrapper.php:59
dol_set_focus($selector)
Set focus onto field with selector (similar behaviour of &#39;autofocus&#39; HTML5 tag)