ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  public function __construct()
25  {
26  global $DIC;
27 
28  $lng = $DIC['lng'];
29  $ilCtrl = $DIC['ilCtrl'];
30 
31  $lng->loadLanguageModule("user");
32  $lng->loadLanguageModule("administration");
33 
34  $this->field_id = $_REQUEST["field_id"];
35  $ilCtrl->saveParameter($this, "field_id", $this->field_id);
36 
37  if ($this->field_id) {
38  $user_field_definitions = ilUserDefinedFields::_getInstance();
39  $this->field_definition = $user_field_definitions->getDefinition($this->field_id);
40  }
41 
42  $this->permissions = ilUDFPermissionHelper::getInstance();
43  }
44 
45  protected function getPermissions()
46  {
47  return $this->permissions;
48  }
49 
50  public function executeCommand()
51  {
52  global $DIC;
53 
54  $ilCtrl = $DIC['ilCtrl'];
55 
56  $next_class = $ilCtrl->getNextClass($this);
57  $cmd = $ilCtrl->getCmd();
58 
59  switch ($next_class) {
60  default:
61  if (!$cmd) {
62  $cmd = "listUserDefinedFields";
63  }
64  $this->$cmd();
65  break;
66  }
67  return true;
68  }
69 
73  public function listUserDefinedFields()
74  {
75  global $DIC;
76 
77  $lng = $DIC['lng'];
78  $tpl = $DIC['tpl'];
79  $ilToolbar = $DIC['ilToolbar'];
80  $ilCtrl = $DIC['ilCtrl'];
81 
82  if ($this->getPermissions()->hasPermission(
84  (int) $_GET["ref_id"],
86  )) {
87  $ilToolbar->addButton(
88  $lng->txt("add_user_defined_field"),
89  $ilCtrl->getLinkTarget($this, "addField")
90  );
91  }
92 
93  include_once("./Services/User/classes/class.ilCustomUserFieldSettingsTableGUI.php");
94  $tab = new ilCustomUserFieldSettingsTableGUI($this, "listUserDefinedFields", $this->getPermissions());
95  if ($this->confirm_change) {
96  $tab->setConfirmChange();
97  }
98  $tpl->setContent($tab->getHTML());
99  }
100 
106  public function addField($a_form = null)
107  {
108  global $DIC;
109 
110  $tpl = $DIC['tpl'];
111 
112  if (!$a_form) {
113  $a_form = $this->initForm('create');
114  }
115 
116  $tpl->setContent($a_form->getHTML());
117  }
118 
124  public function getAccessOptions()
125  {
126  global $DIC;
127 
128  $lng = $DIC['lng'];
129 
130  $opts = array();
131  $opts["visible"] = $lng->txt("user_visible_in_profile");
132  $opts["visib_reg"] = $lng->txt("visible_registration");
133  $opts["visib_lua"] = $lng->txt("usr_settings_visib_lua");
134  $opts["course_export"] = $lng->txt("course_export");
135  $opts["group_export"] = $lng->txt("group_export");
136  $opts["changeable"] = $lng->txt("changeable");
137  $opts["changeable_lua"] = $lng->txt("usr_settings_changeable_lua");
138  $opts["required"] = $lng->txt("required_field");
139  $opts["export"] = $lng->txt("export");
140  $opts["searchable"] = $lng->txt("header_searchable");
141  $opts["certificate"] = $lng->txt("certificate");
142  return $opts;
143  }
144 
145  public static function getAccessPermissions()
146  {
158  );
159  }
160 
161 
166  protected function initFieldDefinition()
167  {
168  global $DIC;
169 
170  $ilCtrl = $DIC['ilCtrl'];
171  $lng = $DIC['lng'];
172 
173  include_once("Services/Membership/classes/class.ilMemberAgreement.php");
175  $lng->loadLanguageModule("ps");
176  ilUtil::sendInfo($lng->txt("ps_warning_modify"));
177  }
178 
179  $perms = array();
180  if ($this->field_definition) {
181  $perms = $this->permissions->hasPermissions(
183  $this->field_definition["field_id"],
184  array(
211  )
212  );
213  }
214  return $perms;
215  }
216 
217  protected function initForm($a_mode = 'create')
218  {
219  global $ilCtrl, $lng;
220 
221  include_once("Services/Membership/classes/class.ilMemberAgreement.php");
223  $lng->loadLanguageModule("ps");
224  ilUtil::sendInfo($lng->txt("ps_warning_modify"));
225  }
226 
227  if ($this->field_definition) {
228  $perms = $this->initFieldDefinition();
229  $perm_map = self::getAccessPermissions();
230  }
231 
232  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
233  $form = new ilPropertyFormGUI();
234  $form->setFormAction($ilCtrl->getFormAction($this));
235 
236  $name = new ilTextInputGUI($lng->txt("field_name"), "name");
237  $name->setRequired(true);
238  $form->addItem($name);
239 
241  $name->setDisabled(true);
242  }
243 
244  // type
245  $radg = new ilRadioGroupInputGUI($lng->txt("field_type"), "field_type");
246  $radg->setRequired(true);
247  include_once './Services/User/classes/class.ilCustomUserFieldsHelper.php';
248  foreach (ilCustomUserFieldsHelper::getInstance()->getUDFTypes() as $udf_type => $udf_name) {
249  $op = new ilRadioOption($udf_name, $udf_type);
250  $radg->addOption($op);
251 
252  switch ($udf_type) {
253  case UDF_TYPE_TEXT:
254  case UDF_TYPE_WYSIWYG:
255  // do nothing
256  break;
257  case UDF_TYPE_SELECT:
258  // select values
259  $se_mu = new ilTextWizardInputGUI($lng->txt("value"), "selvalue");
260  $se_mu->setRequired(true);
261  $se_mu->setSize(32);
262  $se_mu->setMaxLength(128);
263  $se_mu->setValues(array(''));
264  $op->addSubItem($se_mu);
265  break;
266 
267  default:
268  $plugin = ilCustomUserFieldsHelper::getInstance()->getPluginForType($udf_type);
269  if ($plugin instanceof ilUDFDefinitionPlugin) {
270  $plugin->addDefinitionTypeOptionsToRadioOption($op, $this->field_id);
271  }
272  break;
273  }
274  }
275 
276  $form->addItem($radg);
277 
279  $se_mu->setDisabled(true);
280  $se_mu->setRequired(false);
281  }
282 
283 
284  // access
285  $acc = new ilCheckboxGroupInputGUI($lng->txt("access"), "access");
286 
287  $acc_values = array();
288  foreach ($this->getAccessOptions() as $id => $caption) {
289  $opt = new ilCheckboxOption($caption, $id);
290  $acc->addOption($opt);
291 
292  if ($this->field_definition && $this->field_definition[$id]) {
293  $acc_values[] = $id;
294  }
295 
296  if ($perms && !$perms[ilUDFPermissionHelper::ACTION_FIELD_EDIT_ACCESS][$perm_map[$id]]) {
297  $opt->setDisabled(true);
298  }
299  }
300 
301  $form->addItem($acc);
302 
303 
304  if ($a_mode == 'create') {
305  $radg->setValue(UDF_TYPE_TEXT);
306  $form->setTitle($lng->txt('add_new_user_defined_field'));
307  $form->addCommandButton("create", $lng->txt("save"));
308  $form->addCommandButton("listUserDefinedFields", $lng->txt("cancel"));
309  } else {
310  $name->setValue($this->field_definition["field_name"]);
311  $radg->setValue($this->field_definition["field_type"]);
312  $radg->setDisabled(true);
313  $acc->setValue($acc_values);
314 
315  switch ($this->field_definition["field_type"]) {
316  case UDF_TYPE_SELECT:
317  $se_mu->setValue($this->field_definition["field_values"]);
318  $form->setTitle($lng->txt("udf_update_select_field"));
319  break;
320 
321  case UDF_TYPE_TEXT:
322  $form->setTitle($lng->txt("udf_update_text_field"));
323  break;
324 
325  case UDF_TYPE_WYSIWYG:
326  $form->setTitle($lng->txt("udf_update_wysiwyg_field"));
327  break;
328 
329  default:
330  $plugin = ilCustomUserFieldsHelper::getInstance()->getPluginForType($udf_type);
331  if ($plugin instanceof ilUDFDefinitionPlugin) {
332  $form->setTitle($plugin->getDefinitionUpdateFormTitle());
333  }
334  break;
335  }
336  $form->addCommandButton("update", $lng->txt("save"));
337  $form->addCommandButton("listUserDefinedFields", $lng->txt("cancel"));
338  }
339  return $form;
340  }
341 
351  protected function validateForm($form, $user_field_definitions, array &$access, array $a_field_permissions = null)
352  {
353  global $DIC;
354 
355  $lng = $DIC['lng'];
356 
357  if ($form->checkInput()) {
358  $valid = true;
359 
360  $incoming = (array) $form->getInput("access");
361 
362  if ($a_field_permissions) {
363  $perm_map = self::getAccessPermissions();
364  }
365 
366  $access = array();
367  foreach (array_keys($this->getAccessOptions()) as $id) {
368  $access[$id] = in_array($id, $incoming);
369 
370  // disabled fields
371  if ($a_field_permissions && !$a_field_permissions[ilUDFPermissionHelper::ACTION_FIELD_EDIT_ACCESS][$perm_map[$id]]) {
372  $access[$id] = $this->field_definition[$id];
373  }
374  }
375 
376  if ($access['required'] && !$access['visib_reg']) {
377  $this->confirm_change = true;
378  $form->getItemByPostVar("access")->setAlert($lng->txt('udf_required_requires_visib_reg'));
379  $valid = false;
380  }
381 
382  if (!$this->field_id && $user_field_definitions->nameExists($form->getInput("name"))) {
383  $form->getItemByPostVar("name")->setAlert($lng->txt('udf_name_already_exists'));
384  $valid = false;
385  }
386 
387  if ($form->getInput("field_type") == UDF_TYPE_SELECT &&
389  $user_field_definitions->setFieldValues($form->getInput("selvalue"));
390  if ($error = $user_field_definitions->validateValues()) {
391  switch ($error) {
393  $form->getItemByPostVar("selvalue")->setAlert($lng->txt('udf_duplicate_entries'));
394  $valid = false;
395  break;
396  }
397  }
398  }
399 
400  if (!$valid) {
401  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
402  }
403  return $valid;
404  }
405 
406  return false;
407  }
408 
409  public function create()
410  {
411  global $DIC;
412 
413  $lng = $DIC['lng'];
414  $ilCtrl = $DIC['ilCtrl'];
415 
416  $user_field_definitions = ilUserDefinedFields::_getInstance();
417  $user_field_definitions->setFieldType($_POST["field_type"]);
418 
419  $access = array();
420  $form = $this->initForm('create');
421  if ($this->validateForm($form, $user_field_definitions, $access)) {
422  $user_field_definitions->setFieldName($form->getInput("name"));
423  $user_field_definitions->enableVisible($access['visible']);
424  $user_field_definitions->enableVisibleRegistration((int) $access['visib_reg']);
425  $user_field_definitions->enableVisibleLocalUserAdministration($access['visib_lua']);
426  $user_field_definitions->enableCourseExport($access['course_export']);
427  $user_field_definitions->enableGroupExport($access['group_export']);
428  $user_field_definitions->enableChangeable($access['changeable']);
429  $user_field_definitions->enableChangeableLocalUserAdministration($access['changeable_lua']);
430  $user_field_definitions->enableRequired($access['required']);
431  $user_field_definitions->enableExport($access['export']);
432  $user_field_definitions->enableSearchable($access['searchable']);
433  $user_field_definitions->enableCertificate($access['certificate']);
434  $new_id = $user_field_definitions->add();
435 
436  if ($user_field_definitions->isPluginType()) {
437  include_once './Services/User/classes/class.ilCustomUserFieldsHelper.php';
438  $plugin = ilCustomUserFieldsHelper::getInstance()->getPluginForType($user_field_definitions->getFieldType());
439  if ($plugin instanceof ilUDFDefinitionPlugin) {
440  $plugin->updateDefinitionFromForm($form, $new_id);
441  }
442  }
443  if ($access['course_export']) {
444  include_once('Services/Membership/classes/class.ilMemberAgreement.php');
446  }
447 
448  ilUtil::sendSuccess($lng->txt('udf_added_field'), true);
449  $ilCtrl->redirect($this);
450  }
451 
452  $form->setValuesByPost();
453  $this->addField($form);
454  }
455 
461  public function edit($a_form = null)
462  {
463  global $DIC;
464 
465  $tpl = $DIC['tpl'];
466 
467  if (!$a_form) {
468  $a_form = $this->initForm("edit");
469  }
470 
471  $tpl->setContent($a_form->getHTML());
472  }
473 
474  public function update()
475  {
476  global $DIC;
477 
478  $lng = $DIC['lng'];
479  $ilCtrl = $DIC['ilCtrl'];
480 
481  $user_field_definitions = ilUserDefinedFields::_getInstance();
482  $user_field_definitions->setFieldType($this->field_definition["field_type"]);
483 
484  // gather old select options
485  $old_options = null;
486  if ($this->field_id) {
487  $old_values = $user_field_definitions->getDefinition($this->field_id);
488  if ($old_values["field_type"] == UDF_TYPE_SELECT) {
489  $old_options = $old_values["field_values"];
490  }
491 
492  $perms = $this->permissions->hasPermissions(
494  $this->field_id,
495  array(
522  )
523  );
524  }
525 
526  $access = array();
527  $form = $this->initForm("edit");
528  if ($this->validateForm($form, $user_field_definitions, $access, $perms) && $this->field_id) {
529  // field values are set in validateForm()...
530 
532  // diff old select options against new to handle deleted values properly
533  if (is_array($old_options)) {
534  foreach ($old_options as $old_option) {
535  if (!in_array($old_option, $user_field_definitions->getFieldValues())) {
536  ilUserDefinedData::deleteFieldValue($this->field_id, $old_option);
537  }
538  }
539  }
540  }
541  // disabled fields
542  elseif (is_array($old_options)) {
543  $user_field_definitions->setFieldValues($old_options);
544  }
545 
547  $user_field_definitions->setFieldName($form->getInput("name"));
548  } else {
549  $user_field_definitions->setFieldName($this->field_definition["field_name"]);
550  }
551 
552  $user_field_definitions->enableVisible($access['visible']);
553  $user_field_definitions->enableVisibleRegistration((int) $access['visib_reg']);
554  $user_field_definitions->enableVisibleLocalUserAdministration($access['visib_lua']);
555  $user_field_definitions->enableCourseExport($access['course_export']);
556  $user_field_definitions->enableGroupExport($access['group_export']);
557  $user_field_definitions->enableChangeable($access['changeable']);
558  $user_field_definitions->enableChangeableLocalUserAdministration($access['changeable_lua']);
559  $user_field_definitions->enableRequired($access['required']);
560  $user_field_definitions->enableExport($access['export']);
561  $user_field_definitions->enableSearchable($access['searchable']);
562  $user_field_definitions->enableCertificate($access['certificate']);
563  $user_field_definitions->update($this->field_id);
564 
565  if ($user_field_definitions->isPluginType()) {
566  include_once './Services/User/classes/class.ilCustomUserFieldsHelper.php';
567  $plugin = ilCustomUserFieldsHelper::getInstance()->getPluginForType($user_field_definitions->getFieldType());
568  if ($plugin instanceof ilUDFDefinitionPlugin) {
569  $plugin->updateDefinitionFromForm($form, $this->field_id);
570  }
571  }
572 
573  if ($access['course_export']) {
574  include_once('Services/Membership/classes/class.ilMemberAgreement.php');
576  }
577 
578  ilUtil::sendSuccess($lng->txt('settings_saved'), true);
579  $ilCtrl->redirect($this);
580  }
581 
582  $form->setValuesByPost();
583  $this->edit($form);
584  }
585 
586  public function askDeleteField()
587  {
588  global $DIC;
589 
590  $ilCtrl = $DIC['ilCtrl'];
591  $lng = $DIC['lng'];
592  $tpl = $DIC['tpl'];
593 
594  if (!$_POST["fields"]) {
595  ilUtil::sendFailure($lng->txt("select_one"));
596  return $this->listUserDefinedFields();
597  }
598 
599  include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
600  $confirmation_gui = new ilConfirmationGUI();
601  $confirmation_gui->setFormAction($ilCtrl->getFormAction($this));
602  $confirmation_gui->setHeaderText($lng->txt("udf_delete_sure"));
603  $confirmation_gui->setCancel($lng->txt("cancel"), "listUserDefinedFields");
604  $confirmation_gui->setConfirm($lng->txt("delete"), "deleteField");
605 
606  $user_field_definitions = ilUserDefinedFields::_getInstance();
607  foreach ($_POST["fields"] as $id) {
608  $definition = $user_field_definitions->getDefinition($id);
609  $confirmation_gui->addItem("fields[]", $id, $definition["field_name"]);
610  }
611 
612  $tpl->setContent($confirmation_gui->getHTML());
613 
614  return true;
615  }
616 
617  public function deleteField()
618  {
619  global $DIC;
620 
621  $lng = $DIC['lng'];
622  $ilCtrl = $DIC['ilCtrl'];
623 
624  $user_field_definitions = ilUserDefinedFields::_getInstance();
625 
626  // all fields have to be deletable
627  $fail = array();
628  foreach ($_POST["fields"] as $id) {
629  if (!$this->getPermissions()->hasPermission(
631  $id,
633  )) {
634  $field = $user_field_definitions->getDefinition($id);
635  $fail[] = $field["field_name"];
636  }
637  }
638  if ($fail) {
639  ilUtil::sendFailure($lng->txt('msg_no_perm_delete') . " " . implode(", ", $fail), true);
640  $ilCtrl->redirect($this, "listUserDefinedFields");
641  }
642 
643  foreach ($_POST["fields"] as $id) {
644  $user_field_definitions->delete($id);
645  }
646 
647  ilUtil::sendSuccess($lng->txt('udf_field_deleted'), true);
648  $ilCtrl->redirect($this);
649  }
650 
654  public function updateFields($action = "")
655  {
656  global $DIC;
657 
658  $lng = $DIC['lng'];
659  $ilCtrl = $DIC['ilCtrl'];
660 
661  $user_field_definitions = ilUserDefinedFields::_getInstance();
662  $a_fields = $user_field_definitions->getDefinitions();
663 
664  $perm_map = self::getAccessPermissions();
665 
666  foreach ($a_fields as $field_id => $definition) {
667  $perms = $this->permissions->hasPermissions(
669  $field_id,
670  array(
693  )
694  );
695 
696  // disabled field
697  foreach ($perm_map as $prop => $perm) {
699  $_POST['chb'][$prop . '_' . $field_id] = $definition[$prop];
700  }
701  }
702  }
703 
704  foreach ($a_fields as $field_id => $definition) {
705  if (isset($_POST['chb']['required_' . $field_id]) && (int) $_POST['chb']['required_' . $field_id] &&
706  (!isset($_POST['chb']['visib_reg_' . $field_id]) || !(int) $_POST['chb']['visib_reg_' . $field_id])) {
707  $this->confirm_change = true;
708 
709  ilUtil::sendFailure($lng->txt('invalid_visible_required_options_selected'));
710  $this->listUserDefinedFields();
711  return false;
712  }
713  }
714 
715  foreach ($a_fields as $field_id => $definition) {
716  $user_field_definitions->setFieldName($definition['field_name']);
717  $user_field_definitions->setFieldType($definition['field_type']);
718  $user_field_definitions->setFieldValues($definition['field_values']);
719  $user_field_definitions->enableVisible((int) $_POST['chb']['visible_' . $field_id]);
720  $user_field_definitions->enableChangeable((int) $_POST['chb']['changeable_' . $field_id]);
721  $user_field_definitions->enableRequired((int) $_POST['chb']['required_' . $field_id]);
722  $user_field_definitions->enableSearchable((int) $_POST['chb']['searchable_' . $field_id]);
723  $user_field_definitions->enableExport((int) $_POST['chb']['export_' . $field_id]);
724  $user_field_definitions->enableCourseExport((int) $_POST['chb']['course_export_' . $field_id]);
725  $user_field_definitions->enableVisibleLocalUserAdministration((int) $_POST['chb']['visib_lua_' . $field_id]);
726  $user_field_definitions->enableChangeableLocalUserAdministration((int) $_POST['chb']['changeable_lua_' . $field_id]);
727  $user_field_definitions->enableGroupExport((int) $_POST['chb']['group_export_' . $field_id]);
728  $user_field_definitions->enableVisibleRegistration((int) $_POST['chb']['visib_reg_' . $field_id]);
729  $user_field_definitions->enableCertificate((int) $_POST['chb']['certificate_' . $field_id]);
730 
731  $user_field_definitions->update($field_id);
732  }
733 
734  ilUtil::sendSuccess($lng->txt('settings_saved'), true);
735  $ilCtrl->redirect($this);
736  }
737 }
const UDF_TYPE_SELECT
This class represents an option in a radio group.
static getInstance($a_user_id=null, $a_ref_id=null)
Factory.
This class represents an option in a checkbox group.
static _getInstance()
Get instance.
This class represents a property form user interface.
$action
addField($a_form=null)
Add field.
global $DIC
Definition: saml.php:7
$_GET["client_id"]
$tpl
Definition: ilias.php:10
$valid
if(!array_key_exists('StateId', $_REQUEST)) $id
edit($a_form=null)
Edit field.
listUserDefinedFields()
List all custom user fields.
static _reset()
Reset all.
getAccessOptions()
Get all access options, order is kept in forms.
global $ilCtrl
Definition: ilias.php:18
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
Class ilCustomUserFieldsGUI.
This class represents a property in a property form.
TableGUI class for custom defined user fields.
if(isset($_POST['submit'])) $form
$lng
UDF type deefinition plugin.
const UDF_TYPE_TEXT
This class represents a text property in a property form.
validateForm($form, $user_field_definitions, array &$access, array $a_field_permissions=null)
Validate field form.
This class represents a property in a property form.
static _hasAgreements()
Check if there is any user agreement.
const UDF_TYPE_WYSIWYG
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
updateFields($action="")
Update custom fields properties (from table gui)
initFieldDefinition()
init field definition
This class represents a text wizard property in a property form.
static deleteFieldValue($a_field_id, $a_value)
Delete data of particular value of a (selection) field.
const UDF_DUPLICATE_VALUES
$_POST["username"]
setRequired($a_required)
Set Required.
Confirmation screen class.