ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCustomUserFieldsGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once './Services/User/classes/class.ilUserDefinedFields.php';
5 include_once './Services/User/classes/class.ilUDFPermissionHelper.php';
6 
18 {
19  protected $confirm_change; // [bool]
20  protected $field_id; // [int]
21  protected $field_definition; // [array]
22  protected $permissions; // [ilUDFPermissionHelper]
23 
24  function __construct()
25  {
26  global $lng, $ilCtrl;
27 
28  $lng->loadLanguageModule("user");
29  $lng->loadLanguageModule("administration");
30 
31  $this->field_id = $_REQUEST["field_id"];
32  $ilCtrl->saveParameter($this, "field_id", $this->field_id);
33 
34  if($this->field_id)
35  {
36  $user_field_definitions = ilUserDefinedFields::_getInstance();
37  $this->field_definition = $user_field_definitions->getDefinition($this->field_id);
38  }
39 
40  $this->permissions = ilUDFPermissionHelper::getInstance();
41  }
42 
43  protected function getPermissions()
44  {
45  return $this->permissions;
46  }
47 
48  function &executeCommand()
49  {
50  global $ilCtrl;
51 
52  $next_class = $ilCtrl->getNextClass($this);
53  $cmd = $ilCtrl->getCmd();
54 
55  switch($next_class)
56  {
57  default:
58  if(!$cmd)
59  {
60  $cmd = "listUserDefinedFields";
61  }
62  $this->$cmd();
63  break;
64  }
65  return true;
66  }
67 
72  {
73  global $lng, $tpl, $ilToolbar, $ilCtrl;
74 
75  if($this->getPermissions()->hasPermission(ilUDFPermissionHelper::CONTEXT_UDF,
77  {
78  $ilToolbar->addButton($lng->txt("add_user_defined_field"),
79  $ilCtrl->getLinkTarget($this, "addField"));
80  }
81 
82  include_once("./Services/User/classes/class.ilCustomUserFieldSettingsTableGUI.php");
83  $tab = new ilCustomUserFieldSettingsTableGUI($this, "listUserDefinedFields", $this->getPermissions());
84  if($this->confirm_change)
85  {
86  $tab->setConfirmChange();
87  }
88  $tpl->setContent($tab->getHTML());
89  }
90 
96  function addField($a_form = null)
97  {
98  global $tpl;
99 
100  if(!$a_form)
101  {
102  $a_form = $this->initForm();
103  }
104 
105  $tpl->setContent($a_form->getHTML());
106  }
107 
113  function getAccessOptions()
114  {
115  global $lng;
116 
117  $opts = array();
118  $opts["visible"] = $lng->txt("user_visible_in_profile");
119  $opts["visib_reg"] = $lng->txt("visible_registration");
120  $opts["visib_lua"] = $lng->txt("usr_settings_visib_lua");
121  $opts["course_export"] = $lng->txt("course_export");
122  $opts["group_export"] = $lng->txt("group_export");
123  $opts["changeable"] = $lng->txt("changeable");
124  $opts["changeable_lua"] = $lng->txt("usr_settings_changeable_lua");
125  $opts["required"] = $lng->txt("required_field");
126  $opts["export"] = $lng->txt("export");
127  $opts["searchable"] = $lng->txt("header_searchable");
128  $opts["certificate"] = $lng->txt("certificate");
129  return $opts;
130  }
131 
132  public static function getAccessPermissions()
133  {
145  );
146  }
147 
154  function initForm($a_mode = "create")
155  {
156  global $ilCtrl, $lng;
157 
158  include_once("Services/Membership/classes/class.ilMemberAgreement.php");
160  {
161  $lng->loadLanguageModule("ps");
162  ilUtil::sendInfo($lng->txt("ps_warning_modify"));
163  }
164 
165  if($this->field_definition)
166  {
167  $perms = $this->permissions->hasPermissions(ilUDFPermissionHelper::CONTEXT_FIELD,
168  $this->field_definition["field_id"], array(
195  ));
196 
197  $perm_map = self::getAccessPermissions();
198  }
199 
200  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
201  $form = new ilPropertyFormGUI();
202  $form->setFormAction($ilCtrl->getFormAction($this));
203 
204  $name = new ilTextInputGUI($lng->txt("field_name"), "name");
205  $name->setRequired(true);
206  $form->addItem($name);
207 
209  {
210  $name->setDisabled(true);
211  }
212 
213  // type
214  $radg = new ilRadioGroupInputGUI($lng->txt("field_type"), "field_type");
215  $radg->setRequired(true);
216  $op1 = new ilRadioOption($lng->txt("udf_type_text"), UDF_TYPE_TEXT);
217  $radg->addOption($op1);
218  $op2 = new ilRadioOption($lng->txt("udf_type_select"), UDF_TYPE_SELECT);
219  $radg->addOption($op2);
220  $op3 = new ilRadioOption($lng->txt("udf_type_wysiwyg"), UDF_TYPE_WYSIWYG);
221  $radg->addOption($op3);
222  $form->addItem($radg);
223 
224  // select values
225  $se_mu = new ilTextWizardInputGUI($lng->txt("value"), "selvalue");
226  $se_mu->setRequired(true);
227  $se_mu->setSize(32);
228  $se_mu->setMaxLength(128);
229  $se_mu->setValues(array(''));
230  $op2->addSubItem($se_mu);
231 
233  {
234  $se_mu->setDisabled(true);
235  $se_mu->setRequired(false);
236  }
237 
238  // access
239  $acc = new ilCheckboxGroupInputGUI($lng->txt("access"), "access");
240 
241  $acc_values = array();
242  foreach($this->getAccessOptions() as $id => $caption)
243  {
244  $opt = new ilCheckboxOption($caption, $id);
245  $acc->addOption($opt);
246 
247  if($this->field_definition && $this->field_definition[$id])
248  {
249  $acc_values[] = $id;
250  }
251 
252  if($perms && !$perms[ilUDFPermissionHelper::ACTION_FIELD_EDIT_ACCESS][$perm_map[$id]])
253  {
254  $opt->setDisabled(true);
255  }
256  }
257 
258  $form->addItem($acc);
259 
260  if($a_mode == "create")
261  {
262  $radg->setValue(UDF_TYPE_TEXT);
263 
264  $form->setTitle($lng->txt('add_new_user_defined_field'));
265 
266  $form->addCommandButton("create", $lng->txt("save"));
267  $form->addCommandButton("listUserDefinedFields", $lng->txt("cancel"));
268  }
269  else
270  {
271  $name->setValue($this->field_definition["field_name"]);
272  $radg->setValue($this->field_definition["field_type"]);
273  $radg->setDisabled(true);
274  $acc->setValue($acc_values);
275 
276  switch($this->field_definition["field_type"])
277  {
278  case UDF_TYPE_SELECT:
279  $se_mu->setValue($this->field_definition["field_values"]);
280  $form->setTitle($lng->txt("udf_update_select_field"));
281  break;
282 
283  case UDF_TYPE_TEXT:
284  $form->setTitle($lng->txt("udf_update_text_field"));
285  break;
286 
287  case UDF_TYPE_WYSIWYG:
288  $form->setTitle($lng->txt("udf_update_wysiwyg_field"));
289  break;
290  }
291 
292  $form->addCommandButton("update", $lng->txt("save"));
293  $form->addCommandButton("listUserDefinedFields", $lng->txt("cancel"));
294  }
295 
296  return $form;
297  }
298 
308  protected function validateForm($form, $user_field_definitions, array &$access, array $a_field_permissions = null)
309  {
310  global $lng;
311 
312  if($form->checkInput())
313  {
314  $valid = true;
315 
316  $incoming = (array)$form->getInput("access");
317 
318  if($a_field_permissions)
319  {
320  $perm_map = self::getAccessPermissions();
321  }
322 
323  $access = array();
324  foreach(array_keys($this->getAccessOptions()) as $id)
325  {
326  $access[$id] = in_array($id, $incoming);
327 
328  // disabled fields
329  if($a_field_permissions && !$a_field_permissions[ilUDFPermissionHelper::ACTION_FIELD_EDIT_ACCESS][$perm_map[$id]])
330  {
331  $access[$id] = $this->field_definition[$id];
332  }
333  }
334 
335  if($access['required'] && !$access['visib_reg'])
336  {
337  $this->confirm_change = true;
338  $form->getItemByPostVar("access")->setAlert($lng->txt('udf_required_requires_visib_reg'));
339  $valid = false;
340  }
341 
342  if(!$this->field_id && $user_field_definitions->nameExists($form->getInput("name")))
343  {
344  $form->getItemByPostVar("name")->setAlert($lng->txt('udf_name_already_exists'));
345  $valid = false;
346  }
347 
348  if($form->getInput("field_type") == UDF_TYPE_SELECT &&
350  {
351  $user_field_definitions->setFieldValues($form->getInput("selvalue"));
352  if($error = $user_field_definitions->validateValues())
353  {
354  switch($error)
355  {
357  $form->getItemByPostVar("selvalue")->setAlert($lng->txt('udf_duplicate_entries'));
358  $valid = false;
359  break;
360  }
361  }
362  }
363 
364  if(!$valid)
365  {
366  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
367  }
368  return $valid;
369  }
370 
371  return false;
372  }
373 
374  function create()
375  {
376  global $lng, $ilCtrl;
377 
378  $user_field_definitions = ilUserDefinedFields::_getInstance();
379  $user_field_definitions->setFieldType($_POST["field_type"]);
380 
381  $access = array();
382  $form = $this->initForm();
383  if($this->validateForm($form, $user_field_definitions, $access))
384  {
385  $user_field_definitions->setFieldName($form->getInput("name"));
386  $user_field_definitions->enableVisible($access['visible']);
387  $user_field_definitions->enableVisibleRegistration((int)$access['visib_reg']);
388  $user_field_definitions->enableVisibleLocalUserAdministration($access['visib_lua']);
389  $user_field_definitions->enableCourseExport($access['course_export']);
390  $user_field_definitions->enableGroupExport($access['group_export']);
391  $user_field_definitions->enableChangeable($access['changeable']);
392  $user_field_definitions->enableChangeableLocalUserAdministration($access['changeable_lua']);
393  $user_field_definitions->enableRequired($access['required']);
394  $user_field_definitions->enableExport($access['export']);
395  $user_field_definitions->enableSearchable($access['searchable']);
396  $user_field_definitions->enableCertificate($access['certificate']);
397  $user_field_definitions->add();
398 
399  if ($access['course_export'])
400  {
401  include_once('Services/Membership/classes/class.ilMemberAgreement.php');
403  }
404 
405  ilUtil::sendSuccess($lng->txt('udf_added_field'), true);
406  $ilCtrl->redirect($this);
407  }
408 
409  $form->setValuesByPost();
410  $this->addField($form);
411  }
412 
418  function edit($a_form = null)
419  {
420  global $tpl;
421 
422  if(!$a_form)
423  {
424  $a_form = $this->initForm("edit");
425  }
426 
427  $tpl->setContent($a_form->getHTML());
428  }
429 
430  function update()
431  {
432  global $lng, $ilCtrl;
433 
434  $user_field_definitions = ilUserDefinedFields::_getInstance();
435  $user_field_definitions->setFieldType($this->field_definition["field_type"]);
436 
437  // gather old select options
438  $old_options = null;
439  if($this->field_id)
440  {
441  $old_values = $user_field_definitions->getDefinition($this->field_id);
442  if($old_values["field_type"] == UDF_TYPE_SELECT)
443  {
444  $old_options = $old_values["field_values"];
445  }
446 
447  $perms = $this->permissions->hasPermissions(ilUDFPermissionHelper::CONTEXT_FIELD,
448  $this->field_id, array(
475  ));
476  }
477 
478  $access = array();
479  $form = $this->initForm("edit");
480  if($this->validateForm($form, $user_field_definitions, $access, $perms) && $this->field_id)
481  {
482  // field values are set in validateForm()...
483 
485  {
486  // diff old select options against new to handle deleted values properly
487  if(is_array($old_options))
488  {
489  foreach($old_options as $old_option)
490  {
491  if(!in_array($old_option, $user_field_definitions->getFieldValues()))
492  {
493  ilUserDefinedData::deleteFieldValue($this->field_id, $old_option);
494  }
495  }
496  }
497  }
498  // disabled fields
499  else if(is_array($old_options))
500  {
501  $user_field_definitions->setFieldValues($old_options);
502  }
503 
505  {
506  $user_field_definitions->setFieldName($form->getInput("name"));
507  }
508  else
509  {
510  $user_field_definitions->setFieldName($this->field_definition["field_name"]);
511  }
512 
513  $user_field_definitions->enableVisible($access['visible']);
514  $user_field_definitions->enableVisibleRegistration((int)$access['visib_reg']);
515  $user_field_definitions->enableVisibleLocalUserAdministration($access['visib_lua']);
516  $user_field_definitions->enableCourseExport($access['course_export']);
517  $user_field_definitions->enableGroupExport($access['group_export']);
518  $user_field_definitions->enableChangeable($access['changeable']);
519  $user_field_definitions->enableChangeableLocalUserAdministration($access['changeable_lua']);
520  $user_field_definitions->enableRequired($access['required']);
521  $user_field_definitions->enableExport($access['export']);
522  $user_field_definitions->enableSearchable($access['searchable']);
523  $user_field_definitions->enableCertificate($access['certificate']);
524  $user_field_definitions->update($this->field_id);
525 
526  if ($access['course_export'])
527  {
528  include_once('Services/Membership/classes/class.ilMemberAgreement.php');
530  }
531 
532  ilUtil::sendSuccess($lng->txt('udf_added_field'), true);
533  $ilCtrl->redirect($this);
534  }
535 
536  $form->setValuesByPost();
537  $this->edit($form);
538  }
539 
540  function askDeleteField()
541  {
542  global $ilCtrl, $lng, $tpl;
543 
544  if(!$_POST["fields"])
545  {
546  ilUtil::sendFailure($lng->txt("select_one"));
547  return $this->listUserDefinedFields();
548  }
549 
550  include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
551  $confirmation_gui = new ilConfirmationGUI();
552  $confirmation_gui->setFormAction($ilCtrl->getFormAction($this));
553  $confirmation_gui->setHeaderText($lng->txt("udf_delete_sure"));
554  $confirmation_gui->setCancel($lng->txt("cancel"), "listUserDefinedFields");
555  $confirmation_gui->setConfirm($lng->txt("delete"), "deleteField");
556 
557  $user_field_definitions = ilUserDefinedFields::_getInstance();
558  foreach($_POST["fields"] as $id)
559  {
560  $definition = $user_field_definitions->getDefinition($id);
561  $confirmation_gui->addItem("fields[]", $id, $definition["field_name"]);
562  }
563 
564  $tpl->setContent($confirmation_gui->getHTML());
565 
566  return true;
567  }
568 
569  function deleteField()
570  {
571  global $lng, $ilCtrl;
572 
573  $user_field_definitions = ilUserDefinedFields::_getInstance();
574 
575  // all fields have to be deletable
576  $fail = array();
577  foreach($_POST["fields"] as $id)
578  {
579  if(!$this->getPermissions()->hasPermission(
581  $id,
583  {
584  $field = $user_field_definitions->getDefinition($id);
585  $fail[] = $field["field_name"];
586  }
587  }
588  if($fail)
589  {
590  ilUtil::sendFailure($lng->txt('msg_no_perm_delete')." ".implode(", ", $fail), true);
591  $ilCtrl->redirect($this, "listUserDefinedFields");
592  }
593 
594  foreach($_POST["fields"] as $id)
595  {
596  $user_field_definitions->delete($id);
597  }
598 
599  ilUtil::sendSuccess($lng->txt('udf_field_deleted'), true);
600  $ilCtrl->redirect($this);
601  }
602 
606  function updateFields($action = "")
607  {
608  global $lng, $ilCtrl;
609 
610  $user_field_definitions = ilUserDefinedFields::_getInstance();
611  $a_fields = $user_field_definitions->getDefinitions();
612 
613  $perm_map = self::getAccessPermissions();
614 
615  foreach($a_fields as $field_id => $definition)
616  {
617  $perms = $this->permissions->hasPermissions(ilUDFPermissionHelper::CONTEXT_FIELD,
618  $field_id, array(
641  ));
642 
643  // disabled field
644  foreach($perm_map as $prop => $perm)
645  {
647  {
648  $_POST['chb'][$prop.'_'.$field_id] = $definition[$prop];
649  }
650  }
651  }
652 
653  foreach($a_fields as $field_id => $definition)
654  {
655  if( isset($_POST['chb']['required_'.$field_id]) && (int)$_POST['chb']['required_'.$field_id] &&
656  (!isset($_POST['chb']['visib_reg_'.$field_id]) || !(int)$_POST['chb']['visib_reg_'.$field_id]))
657  {
658  $this->confirm_change = true;
659 
660  ilUtil::sendFailure($lng->txt('invalid_visible_required_options_selected'));
661  $this->listUserDefinedFields();
662  return false;
663  }
664  }
665 
666  foreach($a_fields as $field_id => $definition)
667  {
668  $user_field_definitions->setFieldName($definition['field_name']);
669  $user_field_definitions->setFieldType($definition['field_type']);
670  $user_field_definitions->setFieldValues($definition['field_values']);
671  $user_field_definitions->enableVisible((int)$_POST['chb']['visible_'.$field_id]);
672  $user_field_definitions->enableChangeable((int)$_POST['chb']['changeable_'.$field_id]);
673  $user_field_definitions->enableRequired((int)$_POST['chb']['required_'.$field_id]);
674  $user_field_definitions->enableSearchable((int)$_POST['chb']['searchable_'.$field_id]);
675  $user_field_definitions->enableExport((int)$_POST['chb']['export_'.$field_id]);
676  $user_field_definitions->enableCourseExport((int)$_POST['chb']['course_export_'.$field_id]);
677  $user_field_definitions->enableVisibleLocalUserAdministration((int)$_POST['chb']['visib_lua_'.$field_id]);
678  $user_field_definitions->enableChangeableLocalUserAdministration((int)$_POST['chb']['changeable_lua_'.$field_id]);
679  $user_field_definitions->enableGroupExport((int)$_POST['chb']['group_export_'.$field_id]);
680  $user_field_definitions->enableVisibleRegistration((int)$_POST['chb']['visib_reg_'.$field_id]);
681  $user_field_definitions->enableCertificate((int)$_POST['chb']['certificate_'.$field_id]);
682 
683  $user_field_definitions->update($field_id);
684  }
685 
686  ilUtil::sendSuccess($lng->txt('settings_saved'), true);
687  $ilCtrl->redirect($this);
688  }
689 }
690 
691 ?>