ILIAS  Release_4_2_x_branch Revision 61807
 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 
17 {
18  protected $confirm_change; // [bool]
19  protected $field_id; // [int]
20  protected $field_definition; // [array]
21 
22  function __construct()
23  {
24  global $lng, $ilCtrl;
25 
26  $lng->loadLanguageModule("user");
27  $lng->loadLanguageModule("administration");
28 
29  $this->field_id = $_REQUEST["field_id"];
30  $ilCtrl->saveParameter($this, "field_id", $this->field_id);
31 
32  if($this->field_id)
33  {
34  $user_field_definitions = ilUserDefinedFields::_getInstance();
35  $this->field_definition = $user_field_definitions->getDefinition($this->field_id);
36  }
37  }
38 
39  function &executeCommand()
40  {
41  global $ilCtrl;
42 
43  $next_class = $ilCtrl->getNextClass($this);
44  $cmd = $ilCtrl->getCmd();
45 
46  switch($next_class)
47  {
48  default:
49  if(!$cmd)
50  {
51  $cmd = "listUserDefinedFields";
52  }
53  $this->$cmd();
54  break;
55  }
56  return true;
57  }
58 
63  {
64  global $lng, $tpl, $ilToolbar, $ilCtrl;
65 
66  $ilToolbar->addButton($lng->txt("add_user_defined_field"),
67  $ilCtrl->getLinkTarget($this, "addField"));
68 
69  include_once("./Services/User/classes/class.ilCustomUserFieldSettingsTableGUI.php");
70  $tab = new ilCustomUserFieldSettingsTableGUI($this, "listUserDefinedFields");
71  if($this->confirm_change)
72  {
73  $tab->setConfirmChange();
74  }
75  $tpl->setContent($tab->getHTML());
76  }
77 
83  function addField($a_form = null)
84  {
85  global $tpl;
86 
87  if(!$a_form)
88  {
89  $a_form = $this->initForm();
90  }
91 
92  $tpl->setContent($a_form->getHTML());
93  }
94 
100  function getAccessOptions()
101  {
102  global $lng;
103 
104  $opts = array();
105  $opts["visible"] = $lng->txt("user_visible_in_profile");
106  $opts["visib_reg"] = $lng->txt("visible_registration");
107  $opts["visib_lua"] = $lng->txt("usr_settings_visib_lua");
108  $opts["course_export"] = $lng->txt("course_export");
109  $opts["group_export"] = $lng->txt("group_export");
110  $opts["changeable"] = $lng->txt("changeable");
111  $opts["changeable_lua"] = $lng->txt("usr_settings_changeable_lua");
112  $opts["required"] = $lng->txt("required_field");
113  $opts["export"] = $lng->txt("export");
114  $opts["searchable"] = $lng->txt("header_searchable");
115  return $opts;
116  }
117 
124  function initForm($a_mode = "create")
125  {
126  global $ilCtrl, $lng;
127 
128  include_once("Services/Membership/classes/class.ilMemberAgreement.php");
130  {
131  $lng->loadLanguageModule("ps");
132  ilUtil::sendInfo($lng->txt("ps_warning_modify"));
133  }
134 
135  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
136  $form = new ilPropertyFormGUI();
137  $form->setFormAction($ilCtrl->getFormAction($this));
138 
139  $name = new ilTextInputGUI($lng->txt("field_name"), "name");
140  $name->setRequired(true);
141  $form->addItem($name);
142 
143  // type
144  $radg = new ilRadioGroupInputGUI($lng->txt("field_type"), "field_type");
145  $radg->setRequired(true);
146  $op1 = new ilRadioOption($lng->txt("udf_type_text"), UDF_TYPE_TEXT);
147  $radg->addOption($op1);
148  $op2 = new ilRadioOption($lng->txt("udf_type_select"), UDF_TYPE_SELECT);
149  $radg->addOption($op2);
150  $op3 = new ilRadioOption($lng->txt("udf_type_wysiwyg"), UDF_TYPE_WYSIWYG);
151  $radg->addOption($op3);
152  $form->addItem($radg);
153 
154  // select values
155  $se_mu = new ilTextWizardInputGUI($lng->txt("value"), "selvalue");
156  $se_mu->setRequired(true);
157  $se_mu->setSize(32);
158  $se_mu->setMaxLength(128);
159  $se_mu->setValues(array(''));
160  $op2->addSubItem($se_mu);
161 
162  // access
163  $acc = new ilCheckboxGroupInputGUI($lng->txt("access"), "access");
164 
165  $acc_values = array();
166  foreach($this->getAccessOptions() as $id => $caption)
167  {
168  $opt = new ilCheckboxOption($caption, $id);
169  $acc->addOption($opt);
170 
171  if($this->field_definition && $this->field_definition[$id])
172  {
173  $acc_values[] = $id;
174  }
175  }
176 
177  $form->addItem($acc);
178 
179  if($a_mode == "create")
180  {
181  $radg->setValue(UDF_TYPE_TEXT);
182 
183 
184  $form->setTitle($lng->txt('add_new_user_defined_field'));
185 
186  $form->addCommandButton("create", $lng->txt("save"));
187  $form->addCommandButton("listUserDefinedFields", $lng->txt("cancel"));
188  }
189  else
190  {
191  $name->setValue($this->field_definition["field_name"]);
192  $radg->setValue($this->field_definition["field_type"]);
193  $radg->setDisabled(true);
194  $acc->setValue($acc_values);
195 
196  switch($this->field_definition["field_type"])
197  {
198  case UDF_TYPE_SELECT:
199  $se_mu->setValue($this->field_definition["field_values"]);
200  $form->setTitle($lng->txt("udf_update_select_field"));
201  break;
202 
203  case UDF_TYPE_TEXT:
204  $form->setTitle($lng->txt("udf_update_text_field"));
205  break;
206 
207  case UDF_TYPE_WYSIWYG:
208  $form->setTitle($lng->txt("udf_update_wysiwyg_field"));
209  break;
210  }
211 
212  $form->addCommandButton("update", $lng->txt("save"));
213  $form->addCommandButton("listUserDefinedFields", $lng->txt("cancel"));
214  }
215 
216  return $form;
217  }
218 
227  protected function validateForm($form, $user_field_definitions, array &$access)
228  {
229  global $lng;
230 
231  if($form->checkInput())
232  {
233  $valid = true;
234 
235  $incoming = (array)$form->getInput("access");
236  $access = array();
237  foreach(array_keys($this->getAccessOptions()) as $id)
238  {
239  $access[$id] = in_array($id, $incoming);
240  }
241 
242  if($access['required'] && !$access['visib_reg'])
243  {
244  $this->confirm_change = true;
245  $form->getItemByPostVar("access")->setAlert($lng->txt('udf_required_requires_visib_reg'));
246  $valid = false;
247  }
248 
249  if(!$this->field_id && $user_field_definitions->nameExists($form->getInput("name")))
250  {
251  $form->getItemByPostVar("name")->setAlert($lng->txt('udf_name_already_exists'));
252  $valid = false;
253  }
254 
255  if($form->getInput("field_type") == UDF_TYPE_SELECT)
256  {
257  $user_field_definitions->setFieldValues($form->getInput("selvalue"));
258  if($error = $user_field_definitions->validateValues())
259  {
260  switch($error)
261  {
263  $form->getItemByPostVar("selvalue")->setAlert($lng->txt('udf_duplicate_entries'));
264  $valid = false;
265  break;
266  }
267  }
268  }
269 
270  if(!$valid)
271  {
272  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
273  }
274  return $valid;
275  }
276 
277  return false;
278  }
279 
280  function create()
281  {
282  global $lng, $ilCtrl;
283 
284  $user_field_definitions = ilUserDefinedFields::_getInstance();
285  $user_field_definitions->setFieldType($_POST["field_type"]);
286 
287  $access = array();
288  $form = $this->initForm();
289  if($this->validateForm($form, $user_field_definitions, $access))
290  {
291  $user_field_definitions->setFieldName($form->getInput("name"));
292  $user_field_definitions->enableVisible($access['visible']);
293  $user_field_definitions->enableVisibleRegistration((int)$access['visib_reg']);
294  $user_field_definitions->enableVisibleLocalUserAdministration($access['visib_lua']);
295  $user_field_definitions->enableCourseExport($access['course_export']);
296  $user_field_definitions->enableGroupExport($access['group_export']);
297  $user_field_definitions->enableChangeable($access['changeable']);
298  $user_field_definitions->enableChangeableLocalUserAdministration($access['changeable_lua']);
299  $user_field_definitions->enableRequired($access['required']);
300  $user_field_definitions->enableExport($access['export']);
301  $user_field_definitions->enableSearchable($access['searchable']);
302  $user_field_definitions->add();
303 
304  if ($access['course_export'])
305  {
306  include_once('Services/Membership/classes/class.ilMemberAgreement.php');
308  }
309 
310  ilUtil::sendSuccess($lng->txt('udf_added_field'), true);
311  $ilCtrl->redirect($this);
312  }
313 
314  $form->setValuesByPost();
315  $this->addField($form);
316  }
317 
323  function edit($a_form = null)
324  {
325  global $tpl;
326 
327  if(!$a_form)
328  {
329  $a_form = $this->initForm("edit");
330  }
331 
332  $tpl->setContent($a_form->getHTML());
333  }
334 
335  function update()
336  {
337  global $lng, $ilCtrl;
338 
339  $user_field_definitions = ilUserDefinedFields::_getInstance();
340  $user_field_definitions->setFieldType($this->field_definition["field_type"]);
341 
342  $access = array();
343  $form = $this->initForm("edit");
344  if($this->validateForm($form, $user_field_definitions, $access) && $this->field_id)
345  {
346  // field values are set in validateForm()...
347 
348  $user_field_definitions->setFieldName($form->getInput("name"));
349  $user_field_definitions->enableVisible($access['visible']);
350  $user_field_definitions->enableVisibleRegistration((int)$access['visib_reg']);
351  $user_field_definitions->enableVisibleLocalUserAdministration($access['visib_lua']);
352  $user_field_definitions->enableCourseExport($access['course_export']);
353  $user_field_definitions->enableGroupExport($access['group_export']);
354  $user_field_definitions->enableChangeable($access['changeable']);
355  $user_field_definitions->enableChangeableLocalUserAdministration($access['changeable_lua']);
356  $user_field_definitions->enableRequired($access['required']);
357  $user_field_definitions->enableExport($access['export']);
358  $user_field_definitions->enableSearchable($access['searchable']);
359  $user_field_definitions->update($this->field_id);
360 
361  if ($access['course_export'])
362  {
363  include_once('Services/Membership/classes/class.ilMemberAgreement.php');
365  }
366 
367  ilUtil::sendSuccess($lng->txt('udf_added_field'), true);
368  $ilCtrl->redirect($this);
369  }
370 
371  $form->setValuesByPost();
372  $this->edit($form);
373  }
374 
375  function askDeleteField()
376  {
377  global $ilCtrl, $lng, $tpl;
378 
379  if(!$_POST["fields"])
380  {
381  ilUtil::sendFailure($lng->txt("select_one"));
382  return $this->listUserDefinedFields();
383  }
384 
385  include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
386  $confirmation_gui = new ilConfirmationGUI();
387  $confirmation_gui->setFormAction($ilCtrl->getFormAction($this));
388  $confirmation_gui->setHeaderText($lng->txt("udf_delete_sure"));
389  $confirmation_gui->setCancel($lng->txt("cancel"), "listUserDefinedFields");
390  $confirmation_gui->setConfirm($lng->txt("delete"), "deleteField");
391 
392  $user_field_definitions = ilUserDefinedFields::_getInstance();
393  foreach($_POST["fields"] as $id)
394  {
395  $definition = $user_field_definitions->getDefinition($id);
396  $confirmation_gui->addItem("fields[]", $id, $definition["field_name"]);
397  }
398 
399  $tpl->setContent($confirmation_gui->getHTML());
400 
401  return true;
402  }
403 
404  function deleteField()
405  {
406  global $lng, $ilCtrl;
407 
408  $user_field_definitions = ilUserDefinedFields::_getInstance();
409  foreach($_POST["fields"] as $id)
410  {
411  $user_field_definitions->delete($id);
412  }
413 
414  ilUtil::sendSuccess($lng->txt('udf_field_deleted'), true);
415  $ilCtrl->redirect($this);
416  }
417 
421  function updateFields($action = "")
422  {
423  global $lng, $ilCtrl;
424 
425  $user_field_definitions = ilUserDefinedFields::_getInstance();
426  $a_fields = $user_field_definitions->getDefinitions();
427 
428  foreach($a_fields as $field_id => $definition)
429  {
430  if( isset($_POST['chb']['required_'.$field_id]) && (int)$_POST['chb']['required_'.$field_id] &&
431  (!isset($_POST['chb']['visib_reg_'.$field_id]) || !(int)$_POST['chb']['visib_reg_'.$field_id]))
432  {
433  $this->confirm_change = true;
434 
435  ilUtil::sendFailure($lng->txt('invalid_visible_required_options_selected'));
436  $this->listUserDefinedFields();
437  return false;
438  }
439  }
440 
441  foreach($a_fields as $field_id => $definition)
442  {
443  $user_field_definitions->setFieldName($definition['field_name']);
444  $user_field_definitions->setFieldType($definition['field_type']);
445  $user_field_definitions->setFieldValues($definition['field_values']);
446  $user_field_definitions->enableVisible((int)$_POST['chb']['visible_'.$field_id]);
447  $user_field_definitions->enableChangeable((int)$_POST['chb']['changeable_'.$field_id]);
448  $user_field_definitions->enableRequired((int)$_POST['chb']['required_'.$field_id]);
449  $user_field_definitions->enableSearchable((int)$_POST['chb']['searchable_'.$field_id]);
450  $user_field_definitions->enableExport((int)$_POST['chb']['export_'.$field_id]);
451  $user_field_definitions->enableCourseExport((int)$_POST['chb']['course_export_'.$field_id]);
452  $user_field_definitions->enableVisibleLocalUserAdministration((int)$_POST['chb']['visib_lua_'.$field_id]);
453  $user_field_definitions->enableChangeableLocalUserAdministration((int)$_POST['chb']['changeable_lua_'.$field_id]);
454  $user_field_definitions->enableGroupExport((int)$_POST['chb']['group_export_'.$field_id]);
455  $user_field_definitions->enableVisibleRegistration((int)$_POST['chb']['visib_reg_'.$field_id]);
456 
457  $user_field_definitions->update($field_id);
458  }
459 
460  ilUtil::sendSuccess($lng->txt('settings_saved'), true);
461  $ilCtrl->redirect($this);
462  }
463 }
464 
465 ?>