ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilDclFieldEditGUI.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/Fields/Base/class.ilDclBaseFieldModel.php");
4 require_once("./Modules/DataCollection/classes/Fields/Base/class.ilDclDatatype.php");
5 require_once("./Modules/DataCollection/classes/Table/class.ilDclTable.php");
6 require_once("./Modules/DataCollection/classes/Helpers/class.ilDclCache.php");
7 require_once('./Services/Form/classes/class.ilNonEditableValueGUI.php');
8 require_once("./Modules/DataCollection/classes/Fields/Base/class.ilDclFieldProperty.php");
9 require_once('./Modules/DataCollection/classes/Fields/Plugin/class.ilDclFieldTypePlugin.php');
10 
28  protected $obj_id;
29 
33  protected $table_id;
34 
38  protected $parent_obj;
39 
43  protected $table;
44 
48  protected $form;
52  protected $field_obj;
53 
54 
62  public function __construct(ilDclTableListGUI $a_parent_obj) {
63  global $DIC;
64  $ilCtrl = $DIC['ilCtrl'];
65 
66  $this->obj_id = $a_parent_obj->obj_id;
67  $this->parent_obj = $a_parent_obj;
68 
69  $this->table_id = $_GET["table_id"];
70  $this->field_id = $_GET['field_id'];
71 
72  if ($this->field_id) {
73  $this->field_obj = ilDclCache::getFieldCache($this->field_id);
74  } else {
75  $datatype = null;
76  if(isset($_POST['datatype']) && in_array($_POST['datatype'], array_keys(ilDclDatatype::getAllDatatype()))) {
77  $datatype = $_POST['datatype'];
78  }
79  $this->field_obj = ilDclFieldFactory::getFieldModelInstance($this->field_id, $datatype);
80  if (!$this->table_id) {
81  $ilCtrl->redirectByClass("ilDclTableListGUI", "listFields");
82  }
83  $this->field_obj->setTableId($this->table_id);
84  $ilCtrl->saveParameter($this, "table_id");
85  }
86 
87  $this->table = ilDclCache::getTableCache($this->table_id);
88  }
89 
90 
94  public function executeCommand() {
95  global $DIC;
96  $ilCtrl = $DIC['ilCtrl'];
97  $ilCtrl->saveParameter($this, 'field_id');
98 
99  $cmd = $ilCtrl->getCmd();
100 
101  if (!$this->checkAccess()) {
102  $this->permissionDenied();
103 
104  return;
105  }
106 
107  switch ($cmd) {
108  case "update":
109  $this->save("update");
110  break;
111  default:
112  $this->$cmd();
113  break;
114  }
115 
116  return true;
117  }
118 
119 
123  public function create() {
124  global $DIC;
125  $tpl = $DIC['tpl'];
126 
127  $this->initForm();
128  $tpl->setContent($this->form->getHTML());
129  }
130 
131 
135  public function edit() {
136  global $DIC;
137  $tpl = $DIC['tpl'];
138 
139  $this->initForm("edit");
140  $this->getValues();
141 
142  $tpl->setContent($this->form->getHTML());
143  }
144 
145 
146  /*
147  * permissionDenied
148  */
149  public function permissionDenied() {
150  global $DIC;
151  $tpl = $DIC['tpl'];
152  $tpl->setContent("Permission denied");
153  }
154 
155 
159  public function confirmDelete() {
160  global $DIC;
161  $ilCtrl = $DIC['ilCtrl'];
162  $lng = $DIC['lng'];
163  $tpl = $DIC['tpl'];
164 
165  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
166  $conf = new ilConfirmationGUI();
167  $conf->setFormAction($ilCtrl->getFormAction($this));
168  $conf->setHeaderText($lng->txt('dcl_confirm_delete_field'));
169 
170  $conf->addItem('field_id', (int)$this->field_obj->getId(), $this->field_obj->getTitle());
171 
172  $conf->setConfirm($lng->txt('delete'), 'delete');
173  $conf->setCancel($lng->txt('cancel'), 'cancelDelete');
174 
175  $tpl->setContent($conf->getHTML());
176  }
177 
178 
182  public function cancelDelete() {
183  global $DIC;
184  $ilCtrl = $DIC['ilCtrl'];
185 
186  $ilCtrl->redirectByClass("ildclfieldlistgui", "listFields");
187  }
188 
189 
190  /*
191  * delete
192  */
193  public function delete() {
194  global $DIC;
195  $ilCtrl = $DIC['ilCtrl'];
196 
197  $this->table->deleteField($this->field_obj->getId());
198  $ilCtrl->redirectByClass("ildclfieldlistgui", "listFields");
199  }
200 
201 
202  /*
203  * cancel
204  */
205  public function cancel() {
206  global $DIC;
207  $ilCtrl = $DIC['ilCtrl'];
208  $ilCtrl->redirectByClass("ildclfieldlistgui", "listFields");
209  }
210 
211 
217  public function initForm($a_mode = "create") {
218  global $DIC;
219  $ilCtrl = $DIC['ilCtrl'];
220  $lng = $DIC['lng'];
221 
222  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
223  $this->form = new ilPropertyFormGUI();
224 
225  if ($a_mode == "edit") {
226  $this->form->setTitle($lng->txt('dcl_edit_field'));
227  $hidden_prop = new ilHiddenInputGUI("field_id");
228  $this->form->addItem($hidden_prop);
229 
230  $this->form->setFormAction($ilCtrl->getFormAction($this), "update");
231 
232  $this->form->addCommandButton('update', $lng->txt('dcl_update_field'));
233  } else {
234  $this->form->setTitle($lng->txt('dcl_new_field'));
235  $hidden_prop = new ilHiddenInputGUI("table_id");
236  $hidden_prop->setValue($this->field_obj->getTableId());
237  $this->form->addItem($hidden_prop);
238 
239  $this->form->setFormAction($ilCtrl->getFormAction($this), "save");
240 
241  $this->form->addCommandButton('save', $lng->txt('dcl_create_field'));
242  }
243  $this->form->addCommandButton('cancel', $lng->txt('cancel'));
244 
245  $text_prop = new ilTextInputGUI($lng->txt("title"), "title");
246  $text_prop->setRequired(true);
247  $text_prop->setInfo(sprintf($lng->txt('fieldtitle_allow_chars'), ilDclBaseFieldModel::_getTitleInvalidChars(false)));
248  $text_prop->setValidationRegexp(ilDclBaseFieldModel::_getTitleInvalidChars(true));
249  $this->form->addItem($text_prop);
250 
251  // Description
252  $text_prop = new ilTextAreaInputGUI($lng->txt("dcl_field_description"), "description");
253  $this->form->addItem($text_prop);
254 
255  $edit_datatype = new ilRadioGroupInputGUI($lng->txt('dcl_datatype'), 'datatype');
256 
257  foreach (ilDclDatatype::getAllDatatype() as $datatype) {
258  $model = new ilDclBaseFieldModel();
259  $model->setDatatypeId($datatype->getId());
260 
261  if($a_mode == 'edit' && $datatype->getId() == $this->field_obj->getDatatypeId()) {
262  $model = $this->field_obj;
263  }
264 
265  $field_representation = ilDclFieldFactory::getFieldRepresentationInstance($model);
266  $field_representation->addFieldCreationForm($edit_datatype, $this->getDataCollectionObject(), $a_mode);
267  }
268  $edit_datatype->setRequired(true);
269 
270  //you can't change type but we still need it in POST
271  if ($a_mode == "edit") {
272  $edit_datatype->setDisabled(true);
273  }
274  $this->form->addItem($edit_datatype);
275 
276  // Required
277  $cb = new ilCheckboxInputGUI($lng->txt("dcl_field_required"), "required");
278  $this->form->addItem($cb);
279 
280  //Unique
281  $cb = new ilCheckboxInputGUI($lng->txt("dcl_unique"), "unique");
282  $cb->setInfo($lng->txt('dcl_unique_desc'));
283  $this->form->addItem($cb);
284  }
285 
286 
290  public function getValues() {
291  //Std-Values
292  $values = array(
293  'table_id' => $this->field_obj->getTableId(),
294  'field_id' => $this->field_obj->getId(),
295  'title' => $this->field_obj->getTitle(),
296  'datatype' => $this->field_obj->getDatatypeId(),
297  'description' => $this->field_obj->getDescription(),
298  'required' => $this->field_obj->getRequired(),
299  'unique' => $this->field_obj->isUnique(),
300  );
301 
302  $properties = $this->field_obj->getValidFieldProperties();
303  foreach ($properties as $prop) {
304  $values['prop_' . $prop] = $this->field_obj->getProperty($prop);
305  }
306 
307  $this->form->setValuesByArray($values);
308 
309  return true;
310  }
311 
312 
318  public function save($a_mode = "create") {
319  global $DIC;
320  $ilCtrl = $DIC['ilCtrl'];
321  $lng = $DIC['lng'];
322  $tpl = $DIC['tpl'];
323 
324  $this->initForm($a_mode == "update" ? "edit" : "create");
325 
326  if ($this->checkInput($a_mode)) {
327  $title = $this->form->getInput("title");
328  if ($a_mode != "create" && $title != $this->field_obj->getTitle()) {
329  ilUtil::sendInfo($lng->txt("dcl_field_title_change_warning"), true);
330  }
331 
332  $this->field_obj->setTitle($title);
333  $this->field_obj->setDescription($this->form->getInput("description"));
334  $this->field_obj->setDatatypeId($this->form->getInput("datatype"));
335  $this->field_obj->setRequired($this->form->getInput("required"));
336  $this->field_obj->setUnique($this->form->getInput("unique"));
337 
338  if ($a_mode == "update") {
339  $this->field_obj->doUpdate();
340  } else {
341  $this->field_obj->setOrder($this->table->getNewFieldOrder());
342  $this->field_obj->doCreate();
343  }
344 
345  // Get possible properties and save them
346  $field_props = $this->field_obj->getValidFieldProperties();
347  foreach ($field_props as $property) {
348  $representation = ilDclFieldFactory::getFieldRepresentationInstance($this->field_obj);
349  $value = $this->form->getInput($representation->getPropertyInputFieldId($property));
350 
351  // save non empty values and set them to null, when they already exist. Do not override plugin-hook when already set.
352  if(!empty($value) || ($this->field_obj->getPropertyInstance($property) != NULL && $property != ilDclBaseFieldModel::PROP_PLUGIN_HOOK_NAME)) {
353  $this->field_obj->setProperty($property, $value)->store();
354  }
355  }
356 
357  $ilCtrl->setParameter($this, "field_id", $this->field_obj->getId());
358 
359  if ($a_mode == "update") {
360  ilUtil::sendSuccess($lng->txt("dcl_msg_field_modified"), true);
361  } else {
362  $this->table->addField($this->field_obj);
363  $this->table->buildOrderFields();
364  ilUtil::sendSuccess($lng->txt("msg_field_created"), false);
365  }
366  $ilCtrl->redirectByClass(strtolower("ilDclFieldListGUI"), "listFields");
367  } else {
368  $this->form->setValuesByPost();
369  $tpl->setContent($this->form->getHTML());
370  }
371  }
372 
373 
381  protected function checkInput($a_mode) {
382  global $DIC;
383  $lng = $DIC['lng'];
384  $return = $this->form->checkInput();
385 
386  // load specific model for input checking
387  $datatype_id = $this->form->getInput('datatype');
388  if($datatype_id != null && is_numeric($datatype_id)) {
389  $base_model = new ilDclBaseFieldModel();
390  $base_model->setDatatypeId($datatype_id);
391  $field_validation_class = ilDclFieldFactory::getFieldModelInstanceByClass($base_model);
392 
393  if(!$field_validation_class->checkFieldCreationInput($this->form)) {
394  $return = false;
395  }
396  }
397 
398  // Don't allow multiple fields with the same title in this table
399  if ($a_mode == 'create') {
400  if ($title = $this->form->getInput('title')) {
401  if (ilDclTable::_hasFieldByTitle($title, $this->table_id)) {
402  $inputObj = $this->form->getItemByPostVar('title');
403  $inputObj->setAlert($lng->txt("dcl_field_title_unique"));
404  $return = false;
405  }
406  }
407  }
408 
409  if (!$return) {
410  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
411  }
412 
413  return $return;
414  }
415 
416 
420  protected function checkAccess() {
421  if ($field_id = $this->field_obj->getId()) {
422  return ilObjDataCollectionAccess::hasAccessToField($this->getDataCollectionObject()->ref_id, $this->table_id, $field_id);
423  } else {
424  return ilObjDataCollectionAccess::hasAccessToFields($this->getDataCollectionObject()->ref_id, $this->table_id);
425  }
426  }
427 
431  public function getDataCollectionObject() {
432  return $this->parent_obj->getDataCollectionObject();
433  }
434 }
435 
436 ?>
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
Class ilDclBaseFieldModel.
static _getTitleInvalidChars($a_as_regex=true)
All valid chars for filed titles.
This class represents a property form user interface.
static getAllDatatype()
Get all possible Datatypes.
static _hasFieldByTitle($title, $obj_id)
Checks if a table has a field with the given title.
$_GET["client_id"]
static getFieldCache($field_id=0)
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
Returns FieldRepresentation from BaseFieldModel.
$cmd
Definition: sahs_server.php:35
edit()
create field edit form
This class represents a checkbox property in a property form.
Class ilDclTableListGUI.
create()
create field add form
static getTableCache($table_id=0)
global $tpl
Definition: ilias.php:8
save($a_mode="create")
save Field
global $ilCtrl
Definition: ilias.php:18
initForm($a_mode="create")
initEditCustomForm
setInfo($a_info)
Set Information Text.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
This class represents a hidden form property in a property form.
__construct(ilDclTableListGUI $a_parent_obj)
Constructor.
executeCommand()
execute command
This class represents a property in a property form.
static getFieldModelInstance($field_id, $datatype=null)
Get FieldModel from field-id and datatype.
static hasAccessToFields($ref_id, $table_id)
This class represents a text property in a property form.
checkInput($a_mode)
Check input of form.
Create styles array
The data for the language used.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
Class ilDclFieldEditGUI.
static getFieldModelInstanceByClass(ilDclBaseFieldModel $field, $field_id=null)
Gets the correct instance of a fieldModel class Checks if a field is a plugin a replaces the fieldMod...
global $lng
Definition: privfeed.php:17
This class represents a text area property in a property form.
global $DIC
static hasAccessToField($ref_id, $table_id, $field_id)
$_POST["username"]
setRequired($a_required)
Set Required.
Confirmation screen class.