ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDataCollectionFieldEditGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 require_once("./Modules/DataCollection/classes/class.ilDataCollectionField.php");
4 require_once("./Modules/DataCollection/classes/class.ilDataCollectionDatatype.php");
5 require_once("./Modules/DataCollection/classes/class.ilDataCollectionTable.php");
6 require_once "class.ilDataCollectionCache.php";
7 
22 {
23  private $obj_id;
24  private $table_id;
25  private $parent_obj;
26  private $table;
27 
35  public function __construct(ilObjDataCollectionGUI $a_parent_obj, $table_id, $field_id)
36  {
37  global $ilCtrl;
38 
39  $this->obj_id = $a_parent_obj->obj_id;
40  $this->parent_obj = $a_parent_obj;
41  $this->table_id = $table_id;
42  if(!$table_id)
43  $table_id = $_GET["table_id"];
44 
45  if(!isset($field_id))
46  $this->field_id = $_GET['field_id'];
47 
48  if(isset($field_id))
49  {
50  $this->field_obj = ilDataCollectionCache::getFieldCache($field_id);
51  }
52  else
53  {
54  $this->field_obj = ilDataCollectionCache::getFieldCache();
55  if(!$table_id)
56  $ilCtrl->redirectByClass("ilDataCollectionGUI", "listFields");
57  $this->field_obj->setTableId($table_id);
58  $ilCtrl->saveParameter($this, "table_id");
59  }
60 
62  }
63 
64 
68  public function executeCommand()
69  {
70  global $tpl, $ilCtrl, $ilUser;
71 
72  $cmd = $ilCtrl->getCmd();
73 
74  if(!$this->table->hasPermissionToFields($this->parent_obj->ref_id)){
75  $this->permissionDenied();
76  return;
77  }
78 
79  switch($cmd)
80  {
81  case "update":
82  $this->save("update");
83  break;
84  default:
85  $this->$cmd();
86  break;
87  }
88 
89  return true;
90  }
91 
95  public function create()
96  {
97  global $tpl;
98 
99  $this->initForm();
100  $tpl->setContent($this->form->getHTML());
101  }
102 
106  public function edit()
107  {
108  global $tpl;
109 
110  $this->initForm("edit");
111  $this->getValues();
112 
113  $tpl->setContent($this->form->getHTML());
114  }
115 
116  /*
117  * permissionDenied
118  */
119  public function permissionDenied()
120  {
121  global $tpl;
122  $tpl->setContent("Permission denied");
123  }
124 
125 
126 
130  public function confirmDelete()
131  {
132  global $ilCtrl, $lng, $tpl;
133 
134  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
135  $conf = new ilConfirmationGUI();
136  $conf->setFormAction($ilCtrl->getFormAction($this));
137  $conf->setHeaderText($lng->txt('dcl_confirm_delete_field'));
138 
139  $conf->addItem('field_id', (int) $this->field_obj->getId(), $this->field_obj->getTitle());
140 
141  $conf->setConfirm($lng->txt('delete'), 'delete');
142  $conf->setCancel($lng->txt('cancel'), 'cancelDelete');
143 
144  $tpl->setContent($conf->getHTML());
145  }
146 
150  public function cancelDelete()
151  {
152  global $ilCtrl;
153 
154  $ilCtrl->redirectByClass("ildatacollectionfieldlistgui", "listFields");
155  }
156 
157  /*
158  * delete
159  */
160  public function delete()
161  {
162  global $ilCtrl;
163 
164  $this->table->deleteField($this->field_obj->getId());
165  $ilCtrl->redirectByClass("ildatacollectionfieldlistgui", "listFields");
166  }
167 
168  /*
169  * cancel
170  */
171  public function cancel()
172  {
173  global $ilCtrl;
174  $ilCtrl->redirectByClass("ildatacollectionfieldlistgui", "listFields");
175  }
176 
182  public function initForm($a_mode = "create")
183  {
184  global $ilCtrl, $lng;
185 
186  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
187  $this->form = new ilPropertyFormGUI();
188 
189  if($a_mode == "edit")
190  {
191  $this->form->setTitle($lng->txt('dcl_edit_field'));
192  $hidden_prop = new ilHiddenInputGUI("field_id");
193  $this->form->addItem($hidden_prop);
194 
195  $this->form->setFormAction($ilCtrl->getFormAction($this),"update");
196 
197  $this->form->addCommandButton('update', $lng->txt('dcl_update_field'));
198  }
199  else
200  {
201  $this->form->setTitle($lng->txt('dcl_new_field'));
202  $hidden_prop = new ilHiddenInputGUI("table_id");
203  $hidden_prop->setValue($this->field_obj->getTableId());
204  $this->form->addItem($hidden_prop);
205 
206  $this->form->setFormAction($ilCtrl->getFormAction($this),"save");
207 
208  $this->form->addCommandButton('save', $lng->txt('dcl_create_field'));
209  }
210  $this->form->addCommandButton('cancel', $lng->txt('cancel'));
211 
212  $text_prop = new ilTextInputGUI($lng->txt("title"), "title");
213  $text_prop->setRequired(true);
214  $text_prop->setInfo(sprintf($lng->txt('fieldtitle_allow_chars'), ilDataCollectionField::_getTitleValidChars(false)));
215  $text_prop->setValidationRegexp(ilDataCollectionField::_getTitleValidChars(true));
216  $this->form->addItem($text_prop);
217 
218 
219  $edit_datatype = new ilRadioGroupInputGUI($lng->txt('dcl_datatype'),'datatype');
220  foreach(ilDataCollectionDatatype::getAllDatatypes() as $datatype)
221  {
222  $opt = new ilRadioOption($lng->txt('dcl_'.$datatype['title']), $datatype['id']);
223 
224  foreach(ilDataCollectionDatatype::getProperties($datatype['id']) as $property)
225  {
226  //Type Reference: List Tabels
228  {
229  // Get Tables
230  require_once("./Modules/DataCollection/classes/class.ilDataCollectionTable.php");
231  $tables = $this->parent_obj->getDataCollectionObject()->getTables();
232  foreach($tables as $table)
233  {
234  foreach($table->getRecordFields() as $field)
235  {
236  //referencing references may lead to endless loops.
237  if($field->getDatatypeId() != ilDataCollectionDatatype::INPUTFORMAT_REFERENCE)
238  {
239  $options[$field->getId()] = $table->getTitle()."->".$field->getTitle();
240  }
241  }
242  }
243  $table_selection = new ilSelectInputGUI(
244  '',
245  'prop_'.$property['id']
246  );
247  $table_selection->setOptions($options);
248  //$table_selection->setValue($this->table_id);
249  $opt->addSubItem($table_selection);
250 
251  }
252  //ReferenceList
254  {
255  // Get Tables
256  require_once("./Modules/DataCollection/classes/class.ilDataCollectionTable.php");
257  $tables = $this->parent_obj->getDataCollectionObject()->getTables();
258  foreach($tables as $table)
259  {
260  foreach($table->getRecordFields() as $field)
261  {
262  //referencing references may lead to endless loops.
263  if($field->getDatatypeId() != ilDataCollectionDatatype::INPUTFORMAT_REFERENCELIST)
264  {
265  $options[$field->getId()] = $table->getTitle()."->".$field->getTitle();
266  }
267  }
268  }
269  $table_selection = new ilSelectInputGUI(
270  '',
271  'prop_'.$property['id']
272  );
273  $table_selection->setOptions($options);
274  $opt->addSubItem($table_selection);
275  }
276  //All other Types: List properties saved in propertie definition table
277  elseif($property['datatype_id'] == $datatype['id'])
278  {
279  if($property['inputformat'] == ilDataCollectionDatatype::INPUTFORMAT_BOOLEAN)
280  {
281  $subitem = new ilCheckboxInputGUI($lng->txt('dcl_'.$property['title']), 'prop_'.$property['id']);
282  $opt->addSubItem($subitem);
283  }
284  else if ($property['inputformat'] == ilDataCollectionDatatype::INPUTFORMAT_NUMBER) {
285  $subitem = new ilNumberInputGUI($lng->txt('dcl_'.$property['title']), 'prop_'.$property['id']);
286  $subitem->setSize(5);
287  if ($property['title'] == 'length') {
288  $subitem->setMaxValue(4000);
289  }
290  $opt->addSubItem($subitem);
291  } else {
292  $subitem = new ilTextInputGUI($lng->txt('dcl_'.$property['title']), 'prop_'.$property['id']);
293  $opt->addSubItem($subitem);
294  }
295  }
296  }
297 
298 
299 
300 
301  $edit_datatype->addOption($opt);
302  }
303  $edit_datatype->setRequired(true);
304 
305  //you can't change type but we still need it in POST
306  if($a_mode == "edit")
307  {
308  $edit_datatype->setDisabled(true);
309  }
310  $this->form->addItem($edit_datatype);
311 
312  // Description
313  $text_prop = new ilTextAreaInputGUI($lng->txt("dcl_field_description"), "description");
314  $this->form->addItem($text_prop);
315 
316  // Required
317  $cb = new ilCheckboxInputGUI($lng->txt("dcl_field_required"), "required");
318  $this->form->addItem($cb);
319 
320  //Unique
321  $cb = new ilCheckboxInputGUI($lng->txt("dcl_unique"), "unique");
322  $this->form->addItem($cb);
323  }
324 
328  public function getValues()
329  {
330  //Std-Values
331  $values = array(
332  'table_id' => $this->field_obj->getTableId(),
333  'field_id' => $this->field_obj->getId(),
334  'title' => $this->field_obj->getTitle(),
335  'datatype' => $this->field_obj->getDatatypeId(),
336  'description' => $this->field_obj->getDescription(),
337  'required' => $this->field_obj->getRequired(),
338  'unique' => $this->field_obj->isUnique(),
339  );
340 
341  $propertyvalues = $this->field_obj->getPropertyvalues();
342 
343 
344  // Propertie-Values - Subitems
345  foreach(ilDataCollectionDatatype::getAllDatatypes() as $datatype)
346  {
347  foreach(ilDataCollectionDatatype::getProperties($datatype['id']) as $property)
348  {
349  $values['prop_'.$property['id']] = $propertyvalues[$property['id']];
350  }
351  }
352 
353  $this->form->setValuesByArray($values);
354 
355  return true;
356  }
357 
358 
364  public function save($a_mode = "create")
365  {
366  global $ilCtrl, $lng, $tpl;
367 
368  //check access
369  if(!$this->table->hasPermissionToFields($this->parent_obj->ref_id)){
370  $this->accessDenied();
371  return;
372  }
373 
374  $this->initForm($a_mode == "update"?"edit":"create");
375  if ($this->checkInput($a_mode))
376  {
377  $title = $this->form->getInput("title");
378  if($a_mode != "create" && $title != $this->field_obj->getTitle())
379  ilUtil::sendInfo($lng->txt("dcl_field_title_change_warning"), true);
380 
381  $this->field_obj->setTitle($title);
382  $this->field_obj->setDescription($this->form->getInput("description"));
383  $this->field_obj->setDatatypeId($this->form->getInput("datatype"));
384  $this->field_obj->setRequired($this->form->getInput("required"));
385  $this->field_obj->setUnique($this->form->getInput("unique"));
386 
387  if($a_mode == "update")
388  {
389  $this->field_obj->doUpdate();
390  }
391  else
392  {
393  $this->field_obj->setVisible(true);
394  $this->field_obj->setOrder($this->table->getNewOrder());
395  $this->field_obj->doCreate();
396  }
397 
398  // Get possible properties and save them
399  include_once("./Modules/DataCollection/classes/class.ilDataCollectionFieldProp.php");
400  foreach(ilDataCollectionDatatype::getProperties($this->field_obj->getDatatypeId()) as $property)
401  {
402  $fieldprop_obj = new ilDataCollectionFieldProp();
403  $fieldprop_obj->setDatatypePropertyId($property['id']);
404  $fieldprop_obj->setFieldId($this->field_obj->getId());
405  $fieldprop_obj->setValue($this->form->getInput("prop_".$property['id']));
406 
407  if($a_mode == "update")
408  {
409  $fieldprop_obj->doUpdate();
410  }
411  else
412  {
413  $fieldprop_obj->doCreate();
414  }
415 
416  }
417 
418  $ilCtrl->setParameter($this, "field_id", $this->field_obj->getId());
419 
420  if($a_mode == "update")
421  {
422  ilUtil::sendSuccess($lng->txt("dcl_msg_field_modified"),true);
423  }
424  else
425  {
426  $this->table->addField($this->field_obj);
427  $this->table->buildOrderFields();
428  ilUtil::sendSuccess($lng->txt("msg_field_created"), false);
429  }
430  $ilCtrl->redirectByClass(strtolower("ilDataCollectionFieldListGUI"), "listFields");
431  }
432  else
433  {
434  $this->form->setValuesByPost();
435  $tpl->setContent($this->form->getHTML());
436  }
437  }
438 
444  protected function checkInput($a_mode) {
445  global $lng;
446  $return = $this->form->checkInput();
447 
448  // Additional check for text fields: The length property should be max 200 if the textarea option is not set
449  if ($this->form->getInput('datatype') == ilDataCollectionDatatype::INPUTFORMAT_TEXT
450  && (int) $this->form->getInput('prop_'.ilDataCollectionField::PROPERTYID_LENGTH) > 200
451  && !$this->form->getInput('prop_'.ilDataCollectionField::PROPERTYID_TEXTAREA)) {
452  $inputObj = $this->form->getItemByPostVar('prop_'.ilDataCollectionField::PROPERTYID_LENGTH);
453  $inputObj->setAlert($lng->txt("form_msg_value_too_high"));
454  $return = false;
455  }
456 
457  // Don't allow multiple fields with the same title in this table
458  if ($a_mode == 'create') {
459  if ($title = $this->form->getInput('title')) {
460  if (ilDataCollectionTable::_hasFieldByTitle($title, $this->table_id)) {
461  $inputObj = $this->form->getItemByPostVar('title');
462  $inputObj->setAlert($lng->txt("dcl_field_title_unique"));
463  $return = false;
464  }
465  }
466  }
467 
468  if (!$return) ilUtil::sendFailure($lng->txt("form_input_not_valid"));
469 
470  return $return;
471  }
472 
473  /*
474  * accessDenied
475  */
476  private function accessDenied()
477  {
478  global $tpl;
479  $tpl->setContent("Access Denied");
480  }
481 }
482 
483 ?>