ILIAS  release_4-3 Revision
 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 
253  //All other Types: List properties saved in propertie definition table
254  elseif($property['datatype_id'] == $datatype['id'])
255  {
256  if($property['inputformat'] == ilDataCollectionDatatype::INPUTFORMAT_BOOLEAN)
257  {
258  $subitem = new ilCheckboxInputGUI($lng->txt('dcl_'.$property['title']), 'prop_'.$property['id']);
259  $opt->addSubItem($subitem);
260  }
261  else
262  {
263  $subitem = new ilTextInputGUI($lng->txt('dcl_'.$property['title']), 'prop_'.$property['id']);
264  $opt->addSubItem($subitem);
265  }
266  }
267  }
268  $edit_datatype->addOption($opt);
269  }
270  $edit_datatype->setRequired(true);
271 
272  //you can't change type but we still need it in POST
273  if($a_mode == "edit")
274  {
275  $edit_datatype->setDisabled(true);
276  }
277  $this->form->addItem($edit_datatype);
278 
279  // Description
280  $text_prop = new ilTextAreaInputGUI($lng->txt("dcl_field_description"), "description");
281  $this->form->addItem($text_prop);
282 
283  // Required
284  $cb = new ilCheckboxInputGUI($lng->txt("dcl_field_required"), "required");
285  $this->form->addItem($cb);
286 
287  //Unique
288  $cb = new ilCheckboxInputGUI($lng->txt("dcl_unique"), "unique");
289  $this->form->addItem($cb);
290  }
291 
295  public function getValues()
296  {
297  //Std-Values
298  $values = array(
299  'table_id' => $this->field_obj->getTableId(),
300  'field_id' => $this->field_obj->getId(),
301  'title' => $this->field_obj->getTitle(),
302  'datatype' => $this->field_obj->getDatatypeId(),
303  'description' => $this->field_obj->getDescription(),
304  'required' => $this->field_obj->getRequired(),
305  'unique' => $this->field_obj->isUnique(),
306  );
307 
308  $propertyvalues = $this->field_obj->getPropertyvalues();
309 
310 
311  // Propertie-Values - Subitems
312  foreach(ilDataCollectionDatatype::getAllDatatypes() as $datatype)
313  {
314  foreach(ilDataCollectionDatatype::getProperties($datatype['id']) as $property)
315  {
316  $values['prop_'.$property['id']] = $propertyvalues[$property['id']];
317  }
318  }
319 
320  $this->form->setValuesByArray($values);
321 
322  return true;
323  }
324 
325 
331  public function save($a_mode = "create")
332  {
333  global $ilCtrl, $lng;
334 
335  //check access
336  if(!$this->table->hasPermissionToFields($this->parent_obj->ref_id)){
337  $this->accessDenied();
338  return;
339  }
340 
341  $this->initForm($a_mode == "update"?"edit":"create");
342  if ($this->checkInput($a_mode))
343  {
344  $title = $this->form->getInput("title");
345  if($a_mode != "create" && $title != $this->field_obj->getTitle())
346  ilUtil::sendInfo($lng->txt("dcl_field_title_change_warning"), true);
347 
348  $this->field_obj->setTitle($title);
349  $this->field_obj->setDescription($this->form->getInput("description"));
350  $this->field_obj->setDatatypeId($this->form->getInput("datatype"));
351  $this->field_obj->setRequired($this->form->getInput("required"));
352  $this->field_obj->setUnique($this->form->getInput("unique"));
353 
354  if($a_mode == "update")
355  {
356  $this->field_obj->doUpdate();
357  }
358  else
359  {
360  $this->field_obj->setVisible(true);
361  $this->field_obj->setOrder($this->table->getNewOrder());
362  $this->field_obj->doCreate();
363  }
364 
365  // Get possible properties and save them
366  include_once("./Modules/DataCollection/classes/class.ilDataCollectionFieldProp.php");
367  foreach(ilDataCollectionDatatype::getProperties($this->field_obj->getDatatypeId()) as $property)
368  {
369  $fieldprop_obj = new ilDataCollectionFieldProp();
370  $fieldprop_obj->setDatatypePropertyId($property['id']);
371  $fieldprop_obj->setFieldId($this->field_obj->getId());
372  $fieldprop_obj->setValue($this->form->getInput("prop_".$property['id']));
373  if($a_mode == "update")
374  {
375  $fieldprop_obj->doUpdate();
376  }
377  else
378  {
379  $fieldprop_obj->doCreate();
380  }
381 
382  }
383 
384  $ilCtrl->setParameter($this, "field_id", $this->field_obj->getId());
385 
386  if($a_mode == "update")
387  {
388  ilUtil::sendSuccess($lng->txt("dcl_msg_field_modified"),true);
389  }
390  else
391  {
392  $this->table->addField($this->field_obj);
393  $this->table->buildOrderFields();
394  ilUtil::sendSuccess($lng->txt("msg_field_created"), false);
395  }
396  $ilCtrl->redirectByClass(strtolower("ilDataCollectionFieldListGUI"), "listFields");
397  }
398  else
399  {
400  global $tpl;
401  $this->form->setValuesByPost();
402  $tpl->setContent($this->form->getHTML());
403  }
404  }
405 
411  protected function checkInput($a_mode) {
412  global $lng;
413  $return = $this->form->checkInput();
414 
415  // Additional check for text fields: The length property should be max 200 if the textarea option is not set
416  if ($this->form->getInput('datatype') == ilDataCollectionDatatype::INPUTFORMAT_TEXT
417  && (int) $this->form->getInput('prop_'.ilDataCollectionField::PROPERTYID_LENGTH) > 200
418  && !$this->form->getInput('prop_'.ilDataCollectionField::PROPERTYID_TEXTAREA)) {
419  $inputObj = $this->form->getItemByPostVar('prop_'.ilDataCollectionField::PROPERTYID_LENGTH);
420  $inputObj->setAlert($lng->txt("form_msg_value_too_high"));
421  $return = false;
422  }
423 
424  // Don't allow multiple fields with the same title in this table
425  if ($a_mode == 'create') {
426  if ($title = $this->form->getInput('title')) {
427  if (ilDataCollectionTable::_hasFieldByTitle($title, $this->table_id)) {
428  $inputObj = $this->form->getItemByPostVar('title');
429  $inputObj->setAlert($lng->txt("dcl_field_title_unique"));
430  $return = false;
431  }
432  }
433  }
434 
435  if (!$return) ilUtil::sendFailure($lng->txt("form_input_not_valid"));
436 
437  return $return;
438  }
439 
440  /*
441  * accessDenied
442  */
443  private function accessDenied()
444  {
445  global $tpl;
446  $tpl->setContent("Access Denied");
447  }
448 }
449 
450 ?>