ILIAS  Release_5_0_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 require_once('./Services/Form/classes/class.ilNonEditableValueGUI.php');
8 
23 
24 
25  const REFERENCE_SEPARATOR = " -> ";
26 
30  protected $obj_id;
31 
35  protected $table_id;
36 
40  protected $parent_obj;
41 
45  protected $table;
46 
50  protected $form;
51 
59  public function __construct(ilObjDataCollectionGUI $a_parent_obj, $table_id, $field_id) {
60  global $ilCtrl;
61 
62  $this->obj_id = $a_parent_obj->obj_id;
63  $this->parent_obj = $a_parent_obj;
64  $this->table_id = $table_id;
65  if (!$table_id) {
66  $table_id = $_GET["table_id"];
67  }
68 
69  if (!isset($field_id)) {
70  $this->field_id = $_GET['field_id'];
71  }
72 
73  if (isset($field_id)) {
74  $this->field_obj = ilDataCollectionCache::getFieldCache($field_id);
75  } else {
76  $this->field_obj = ilDataCollectionCache::getFieldCache();
77  if (!$table_id) {
78  $ilCtrl->redirectByClass("ilDataCollectionGUI", "listFields");
79  }
80  $this->field_obj->setTableId($table_id);
81  $ilCtrl->saveParameter($this, "table_id");
82  }
83 
85  }
86 
87 
91  public function executeCommand() {
92  global $tpl, $ilCtrl, $ilUser;
93 
94  $cmd = $ilCtrl->getCmd();
95 
96  if (!$this->table->hasPermissionToFields($this->parent_obj->ref_id)) {
97  $this->permissionDenied();
98 
99  return;
100  }
101 
102  switch ($cmd) {
103  case "update":
104  $this->save("update");
105  break;
106  default:
107  $this->$cmd();
108  break;
109  }
110 
111  return true;
112  }
113 
114 
118  public function create() {
119  global $tpl;
120 
121  $this->initForm();
122  $tpl->setContent($this->form->getHTML());
123  }
124 
125 
129  public function edit() {
130  global $tpl;
131 
132  $this->initForm("edit");
133  $this->getValues();
134 
135  $tpl->setContent($this->form->getHTML());
136  }
137 
138 
139  /*
140  * permissionDenied
141  */
142  public function permissionDenied() {
143  global $tpl;
144  $tpl->setContent("Permission denied");
145  }
146 
147 
151  public function confirmDelete() {
152  global $ilCtrl, $lng, $tpl;
153 
154  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
155  $conf = new ilConfirmationGUI();
156  $conf->setFormAction($ilCtrl->getFormAction($this));
157  $conf->setHeaderText($lng->txt('dcl_confirm_delete_field'));
158 
159  $conf->addItem('field_id', (int)$this->field_obj->getId(), $this->field_obj->getTitle());
160 
161  $conf->setConfirm($lng->txt('delete'), 'delete');
162  $conf->setCancel($lng->txt('cancel'), 'cancelDelete');
163 
164  $tpl->setContent($conf->getHTML());
165  }
166 
167 
171  public function cancelDelete() {
172  global $ilCtrl;
173 
174  $ilCtrl->redirectByClass("ildatacollectionfieldlistgui", "listFields");
175  }
176 
177 
178  /*
179  * delete
180  */
181  public function delete() {
182  global $ilCtrl;
183 
184  $this->table->deleteField($this->field_obj->getId());
185  $ilCtrl->redirectByClass("ildatacollectionfieldlistgui", "listFields");
186  }
187 
188 
189  /*
190  * cancel
191  */
192  public function cancel() {
193  global $ilCtrl;
194  $ilCtrl->redirectByClass("ildatacollectionfieldlistgui", "listFields");
195  }
196 
197 
203  public function initForm($a_mode = "create") {
204  global $ilCtrl, $lng;
205 
206  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
207  $this->form = new ilPropertyFormGUI();
208 
209  if ($a_mode == "edit") {
210  $this->form->setTitle($lng->txt('dcl_edit_field'));
211  $hidden_prop = new ilHiddenInputGUI("field_id");
212  $this->form->addItem($hidden_prop);
213 
214  $this->form->setFormAction($ilCtrl->getFormAction($this), "update");
215 
216  $this->form->addCommandButton('update', $lng->txt('dcl_update_field'));
217  } else {
218  $this->form->setTitle($lng->txt('dcl_new_field'));
219  $hidden_prop = new ilHiddenInputGUI("table_id");
220  $hidden_prop->setValue($this->field_obj->getTableId());
221  $this->form->addItem($hidden_prop);
222 
223  $this->form->setFormAction($ilCtrl->getFormAction($this), "save");
224 
225  $this->form->addCommandButton('save', $lng->txt('dcl_create_field'));
226  }
227  $this->form->addCommandButton('cancel', $lng->txt('cancel'));
228 
229  $text_prop = new ilTextInputGUI($lng->txt("title"), "title");
230  $text_prop->setRequired(true);
231  $text_prop->setInfo(sprintf($lng->txt('fieldtitle_allow_chars'), ilDataCollectionField::_getTitleValidChars(false)));
232  $text_prop->setValidationRegexp(ilDataCollectionField::_getTitleValidChars(true));
233  $this->form->addItem($text_prop);
234 
235  $edit_datatype = new ilRadioGroupInputGUI($lng->txt('dcl_datatype'), 'datatype');
236  foreach (ilDataCollectionDatatype::getAllDatatypes() as $datatype) {
237  $opt = new ilRadioOption($lng->txt('dcl_' . $datatype['title']), $datatype['id']);
238 
239  foreach (ilDataCollectionDatatype::getProperties($datatype['id']) as $property) {
240  //Type Reference: List Tabels
241  if ($datatype['id'] == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE AND
243  ) {
244  $options = array();
245  // Get Tables
246  require_once("./Modules/DataCollection/classes/class.ilDataCollectionTable.php");
247  $tables = $this->parent_obj->getDataCollectionObject()->getTables();
248  foreach ($tables as $table) {
249  foreach ($table->getRecordFields() as $field) {
250  //referencing references may lead to endless loops.
251  if ($field->getDatatypeId() != ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
252  $options[$field->getId()] = $table->getTitle() . self::REFERENCE_SEPARATOR . $field->getTitle();
253  }
254  }
255  }
256  $table_selection = new ilSelectInputGUI('', 'prop_' . $property['id']);
257  $table_selection->setOptions($options);
258  //$table_selection->setValue($this->table_id);
259  $opt->addSubItem($table_selection);
260  } //ReferenceList
261  elseif ($datatype['id'] == ilDataCollectionDatatype::INPUTFORMAT_REFERENCELIST AND
263  ) {
264  // Get Tables
265  require_once("./Modules/DataCollection/classes/class.ilDataCollectionTable.php");
266  $tables = $this->parent_obj->getDataCollectionObject()->getTables();
267  foreach ($tables as $table) {
268  foreach ($table->getRecordFields() as $field) {
269  //referencing references may lead to endless loops.
270  if ($field->getDatatypeId() != ilDataCollectionDatatype::INPUTFORMAT_REFERENCELIST) {
271  $options[$field->getId()] = $table->getTitle() . self::REFERENCE_SEPARATOR . $field->getTitle();
272  }
273  }
274  }
275  $table_selection = new ilSelectInputGUI('', 'prop_' . $property['id']);
276  $table_selection->setOptions($options);
277  $opt->addSubItem($table_selection);
278  } elseif ($property['id'] == ilDataCollectionField::PROPERTYID_FORMULA_EXPRESSION) {
280  $fields = array();
281  foreach ($table->getFieldsForFormula() as $f) {
282  $placeholder = ($f->isStandardField()) ? $f->getId() : $f->getTitle();
283  $fields[] = '<a class="dclPropExpressionField" data-placeholder="' . $placeholder . '">' . $f->getTitle() . '</a>';
284  }
285  $subitem = new ilTextAreaInputGUI($lng->txt('dcl_prop_expression'), 'prop_' . $property['id']);
286  $operators = implode(', ', array_keys(ilDclExpressionParser::getOperators()));
287  $functions = implode(', ', ilDclExpressionParser::getFunctions());
288  $subitem->setInfo(sprintf($lng->txt('dcl_prop_expression_info'), $operators, $functions, implode('<br>', $fields)));
289  $opt->addSubItem($subitem);
290  } elseif ($property['datatype_id'] == $datatype['id']) {
291  //All other Types: List properties saved in propertie definition table
292  if ($property['inputformat'] == ilDataCollectionDatatype::INPUTFORMAT_BOOLEAN) {
293  $subitem = new ilCheckboxInputGUI($lng->txt('dcl_' . $property['title']), 'prop_' . $property['id']);
294  $opt->addSubItem($subitem);
295  } elseif ($property['inputformat'] == ilDataCollectionDatatype::INPUTFORMAT_NUMBER) {
296  $subitem = new ilNumberInputGUI($lng->txt('dcl_' . $property['title']), 'prop_' . $property['id']);
297 
298  // TODO: Nicer way to add additional info to fields (need changes in language-logic)
299  /*if($lng->txt('dcl_'.$property['title'].'_info') != '-dcl_'.$property['title'].'_info-') {
300  $subitem->setInfo($lng->txt('dcl_'.$property['title'].'_info'));
301  }*/
302  $subitem->setSize(5);
303  if ($property['title'] == 'length') {
304  $subitem->setMaxValue(4000);
305  $subitem->setInfo($lng->txt('dcl_'.$property['title'].'_info'));
306  }
307  $opt->addSubItem($subitem);
308  } elseif ($property['inputformat'] == ilDataCollectionDatatype::INPUTFORMAT_NON_EDITABLE_VALUE) {
309  $subitem = new ilNonEditableValueGUI($lng->txt('dcl_'.$property['title']));
310  $subitem->setValue(implode(', ', ilDataCollectionDatatype::$mob_suffixes));
311  $opt->addSubItem($subitem);
312  } else {
313  $subitem = new ilTextInputGUI($lng->txt('dcl_' . $property['title']), 'prop_' . $property['id']);
314  // TODO: Nicer way to add additional info to fields (need changes in language-logic)
315  /*if($lng->txt('dcl_'.$property['title'].'_info') != '-dcl_'.$property['title'].'_info-') {
316  $subitem->setInfo($lng->txt('dcl_'.$property['title'].'_info'));
317  }*/
318  if($property['title'] == 'regex') {
319  $subitem->setInfo($lng->txt('dcl_'.$property['title'].'_info'));
320  }
321  $opt->addSubItem($subitem);
322  }
323  }
324  }
325 
326  $edit_datatype->addOption($opt);
327  }
328  $edit_datatype->setRequired(true);
329 
330  //you can't change type but we still need it in POST
331  if ($a_mode == "edit") {
332  $edit_datatype->setDisabled(true);
333  }
334  $this->form->addItem($edit_datatype);
335 
336  // Description
337  $text_prop = new ilTextAreaInputGUI($lng->txt("dcl_field_description"), "description");
338  $this->form->addItem($text_prop);
339 
340  // Required
341  $cb = new ilCheckboxInputGUI($lng->txt("dcl_field_required"), "required");
342  $this->form->addItem($cb);
343 
344  //Unique
345  $cb = new ilCheckboxInputGUI($lng->txt("dcl_unique"), "unique");
346  $this->form->addItem($cb);
347  }
348 
349 
353  public function getValues() {
354  //Std-Values
355  $values = array(
356  'table_id' => $this->field_obj->getTableId(),
357  'field_id' => $this->field_obj->getId(),
358  'title' => $this->field_obj->getTitle(),
359  'datatype' => $this->field_obj->getDatatypeId(),
360  'description' => $this->field_obj->getDescription(),
361  'required' => $this->field_obj->getRequired(),
362  'unique' => $this->field_obj->isUnique(),
363  );
364 
365  $propertyvalues = $this->field_obj->getPropertyvalues();
366 
367  // Propertie-Values - Subitems
368  foreach (ilDataCollectionDatatype::getAllDatatypes() as $datatype) {
369  foreach (ilDataCollectionDatatype::getProperties($datatype['id']) as $property) {
370  $values['prop_' . $property['id']] = $propertyvalues[$property['id']];
371  }
372  }
373 
374  $this->form->setValuesByArray($values);
375 
376  return true;
377  }
378 
379 
385  public function save($a_mode = "create") {
386  global $ilCtrl, $lng, $tpl;
387 
388  //check access
389  if (!$this->table->hasPermissionToFields($this->parent_obj->ref_id)) {
390  $this->accessDenied();
391 
392  return;
393  }
394 
395  $this->initForm($a_mode == "update" ? "edit" : "create");
396  if ($this->checkInput($a_mode)) {
397  $title = $this->form->getInput("title");
398  if ($a_mode != "create" && $title != $this->field_obj->getTitle()) {
399  ilUtil::sendInfo($lng->txt("dcl_field_title_change_warning"), true);
400  }
401 
402  $this->field_obj->setTitle($title);
403  $this->field_obj->setDescription($this->form->getInput("description"));
404  $this->field_obj->setDatatypeId($this->form->getInput("datatype"));
405  $this->field_obj->setRequired($this->form->getInput("required"));
406  $this->field_obj->setUnique($this->form->getInput("unique"));
407 
408  if ($a_mode == "update") {
409  $this->field_obj->doUpdate();
410  } else {
411  $this->field_obj->setVisible(true);
412  $this->field_obj->setOrder($this->table->getNewOrder());
413  $this->field_obj->doCreate();
414  }
415 
416  // Get possible properties and save them
417  include_once("./Modules/DataCollection/classes/class.ilDataCollectionFieldProp.php");
418  foreach (ilDataCollectionDatatype::getProperties($this->field_obj->getDatatypeId()) as $property) {
419  $fieldprop_obj = new ilDataCollectionFieldProp();
420  $fieldprop_obj->setDatatypePropertyId($property['id']);
421  $fieldprop_obj->setFieldId($this->field_obj->getId());
422  $fieldprop_obj->setValue($this->form->getInput("prop_" . $property['id']));
423 
424  if ($a_mode == "update") {
425  $fieldprop_obj->doUpdate();
426  } else {
427  $fieldprop_obj->doCreate();
428  }
429  }
430 
431  $ilCtrl->setParameter($this, "field_id", $this->field_obj->getId());
432 
433  if ($a_mode == "update") {
434  ilUtil::sendSuccess($lng->txt("dcl_msg_field_modified"), true);
435  } else {
436  $this->table->addField($this->field_obj);
437  $this->table->buildOrderFields();
438  ilUtil::sendSuccess($lng->txt("msg_field_created"), false);
439  }
440  $ilCtrl->redirectByClass(strtolower("ilDataCollectionFieldListGUI"), "listFields");
441  } else {
442  $this->form->setValuesByPost();
443  $tpl->setContent($this->form->getHTML());
444  }
445  }
446 
447 
455  protected function checkInput($a_mode) {
456  global $lng;
457  $return = $this->form->checkInput();
458 
459  // Additional check for text fields: The length property should be max 200 if the textarea option is not set
460  if ($this->form->getInput('datatype') == ilDataCollectionDatatype::INPUTFORMAT_TEXT
461  && (int)$this->form->getInput('prop_' . ilDataCollectionField::PROPERTYID_LENGTH) > 200
462  && !$this->form->getInput('prop_' . ilDataCollectionField::PROPERTYID_TEXTAREA)
463  ) {
464  $inputObj = $this->form->getItemByPostVar('prop_' . ilDataCollectionField::PROPERTYID_LENGTH);
465  $inputObj->setAlert($lng->txt("form_msg_value_too_high"));
466  $return = false;
467  }
468 
469  // Don't allow multiple fields with the same title in this table
470  if ($a_mode == 'create') {
471  if ($title = $this->form->getInput('title')) {
472  if (ilDataCollectionTable::_hasFieldByTitle($title, $this->table_id)) {
473  $inputObj = $this->form->getItemByPostVar('title');
474  $inputObj->setAlert($lng->txt("dcl_field_title_unique"));
475  $return = false;
476  }
477  }
478  }
479 
480  if (!$return) {
481  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
482  }
483 
484  return $return;
485  }
486 
487 
488  /*
489  * accessDenied
490  */
491  private function accessDenied() {
492  global $tpl;
493  $tpl->setContent("Access Denied");
494  }
495 }
496 
497 ?>