ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilDclFieldEditGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  protected int $obj_id;
24  protected int $table_id;
25 
27  protected ilDclTable $table;
31  protected ilHelpGUI $help;
34  protected int $field_id;
35 
39  public function __construct(ilDclTableListGUI $a_parent_obj)
40  {
41  global $DIC;
42  $this->main_tpl = $DIC->ui()->mainTemplate();
43  $ilCtrl = $DIC['ilCtrl'];
44 
45  $this->obj_id = $a_parent_obj->getObjId();
46  $this->parent_obj = $a_parent_obj;
47  $this->help = $DIC->help();
48  $this->http = $DIC->http();
49  $this->refinery = $DIC->refinery();
50 
51  $this->table_id = $this->http->wrapper()->query()->retrieve('table_id', $this->refinery->kindlyTo()->int());
52 
53  $hasFieldId = $this->http->wrapper()->query()->has('field_id');
54  if ($hasFieldId) {
55  $this->field_id = $this->http->wrapper()->query()->retrieve('field_id', $this->refinery->kindlyTo()->int());
56  } else {
57  $this->field_id = 0;
58  }
59 
60  if ($this->field_id) {
61  $this->field_obj = ilDclCache::getFieldCache($this->field_id);
62  } else {
63  $datatype = null;
64 
65  $has_datatype = $this->http->wrapper()->post()->has('datatype');
66 
67  if ($has_datatype) {
68  $datatype_value = $this->http->wrapper()->post()->retrieve(
69  'datatype',
70  $this->refinery->kindlyTo()->int()
71  );
72  if (in_array(
73  $datatype_value,
74  array_keys(ilDclDatatype::getAllDatatype())
75  )) {
76  $datatype = $datatype_value;
77  }
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 
93  public function executeCommand(): void
94  {
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  return;
104  }
105 
106  switch ($cmd) {
107  case "update":
108  $this->save("update");
109  break;
110  default:
111  $this->$cmd();
112  break;
113  }
114  }
115 
119  public function create(): void
120  {
121  $this->help->setSubScreenId('create');
122 
123  $this->initForm();
124  $this->main_tpl->setContent($this->form->getHTML());
125  }
126 
130  public function edit(): void
131  {
132  $this->help->setSubScreenId('edit');
133 
134  $this->initForm("edit");
135  $this->field_obj->fillPropertiesForm($this->form);
136  $this->main_tpl->setContent($this->form->getHTML());
137  }
138 
139  /*
140  * permissionDenied
141  */
142  public function permissionDenied(): void
143  {
144  global $DIC;
145  $tpl = $DIC['tpl'];
146  $tpl->setContent("Permission denied");
147  }
148 
152  public function confirmDelete(): void
153  {
154  global $DIC;
155  $ilCtrl = $DIC['ilCtrl'];
156  $lng = $DIC['lng'];
157  $tpl = $DIC['tpl'];
158 
159  $conf = new ilConfirmationGUI();
160  $conf->setFormAction($ilCtrl->getFormAction($this));
161  $conf->setHeaderText($lng->txt('dcl_confirm_delete_field'));
162 
163  $conf->addItem('field_id', $this->field_obj->getId(), $this->field_obj->getTitle());
164 
165  $conf->setConfirm($lng->txt('delete'), 'delete');
166  $conf->setCancel($lng->txt('cancel'), 'cancelDelete');
167 
168  $tpl->setContent($conf->getHTML());
169  }
170 
174  public function cancelDelete(): void
175  {
176  global $DIC;
177  $ilCtrl = $DIC['ilCtrl'];
178 
179  $ilCtrl->redirectByClass("ildclfieldlistgui", "listFields");
180  }
181 
182  /*
183  * delete
184  */
185  public function delete(): void
186  {
187  global $DIC;
188  $ilCtrl = $DIC['ilCtrl'];
189 
190  $this->table->deleteField((int) $this->field_obj->getId());
191  $ilCtrl->redirectByClass("ildclfieldlistgui", "listFields");
192  }
193 
194  /*
195  * cancel
196  */
197  public function cancel(): void
198  {
199  global $DIC;
200  $ilCtrl = $DIC['ilCtrl'];
201  $ilCtrl->redirectByClass("ildclfieldlistgui", "listFields");
202  }
203 
208  public function initForm(string $a_mode = "create"): void
209  {
210  global $DIC;
211  $ilCtrl = $DIC['ilCtrl'];
212  $lng = $DIC['lng'];
213 
214  $this->form = new ilPropertyFormGUI();
215 
216  if ($a_mode == "edit") {
217  $this->form->setTitle($lng->txt('dcl_edit_field'));
218  $hidden_prop = new ilHiddenInputGUI("field_id");
219  $this->form->addItem($hidden_prop);
220 
221  $this->form->setFormAction($ilCtrl->getFormAction($this));
222 
223  $this->form->addCommandButton('update', $lng->txt('dcl_update_field'));
224  } else {
225  $this->form->setTitle($lng->txt('dcl_new_field'));
226  $hidden_prop = new ilHiddenInputGUI("table_id");
227  $hidden_prop->setValue((string) $this->field_obj->getTableId());
228  $this->form->addItem($hidden_prop);
229 
230  $this->form->setFormAction($ilCtrl->getFormAction($this));
231 
232  $this->form->addCommandButton('save', $lng->txt('dcl_create_field'));
233  }
234  $this->form->addCommandButton('cancel', $lng->txt('cancel'));
235 
236  $text_prop = new ilTextInputGUI($lng->txt("title"), "title");
237  $text_prop->setRequired(true);
238  $text_prop->setInfo(sprintf(
239  $lng->txt('fieldtitle_allow_chars'),
241  ));
242  $text_prop->setValidationRegexp(ilDclBaseFieldModel::_getTitleInvalidChars());
243  $this->form->addItem($text_prop);
244 
245  // Description
246  $text_prop = new ilTextAreaInputGUI($lng->txt("dcl_field_description"), "description");
247  $this->form->addItem($text_prop);
248 
249  $edit_datatype = new ilRadioGroupInputGUI($lng->txt('dcl_datatype'), 'datatype');
250 
251  if ($a_mode === 'edit') {
252  $field_representation = ilDclFieldFactory::getFieldRepresentationInstance($this->field_obj);
253  $field_representation->addFieldCreationForm($edit_datatype, $this->getDataCollectionObject(), $a_mode);
254  $edit_datatype->setDisabled(true);
255  } else {
256  foreach (ilDclDatatype::getAllDatatype() as $datatype) {
257  $model = new ilDclBaseFieldModel();
258  $model->setDatatypeId($datatype->getId());
260  $field_representation = ilDclFieldFactory::getFieldRepresentationInstance($model);
261  $field_representation->addFieldCreationForm($edit_datatype, $this->getDataCollectionObject());
262  }
263  }
264  $edit_datatype->setRequired(true);
265  $this->form->addItem($edit_datatype);
266 
267  //Unique
268  $cb = new ilCheckboxInputGUI($lng->txt("dcl_unique"), "unique");
269  $cb->setInfo($lng->txt('dcl_unique_desc'));
270  $this->form->addItem($cb);
271  }
272 
277  public function save(string $a_mode = "create"): void
278  {
279  global $DIC;
280  $ilCtrl = $DIC['ilCtrl'];
281  $lng = $DIC['lng'];
282  $tpl = $DIC['tpl'];
283 
284  $this->initForm($a_mode == "update" ? "edit" : "create");
285 
286  if ($this->checkInput($a_mode)) {
287 
288  // check if confirmation is needed and if so, fetch and render confirmationGUI
289  if (($a_mode == "update") && !($this->form->getInput('confirmed')) && $this->field_obj->isConfirmationRequired($this->form)) {
290  $ilConfirmationGUI = $this->field_obj->getConfirmationGUI($this->form);
291  $tpl->setContent($ilConfirmationGUI->getHTML());
292 
293  return;
294  }
295 
296  $title = $this->form->getInput("title");
297  if ($a_mode != "create" && $title != $this->field_obj->getTitle()) {
298  $this->main_tpl->setOnScreenMessage('info', $lng->txt("dcl_field_title_change_warning"), true);
299  }
300 
301  $this->field_obj->setTitle($title);
302  $this->field_obj->setDescription($this->form->getInput("description"));
303  $this->field_obj->setDatatypeId((int) $this->form->getInput("datatype"));
304  $this->field_obj->setUnique((bool) $this->form->getInput("unique"));
305 
306  if ($a_mode == "update") {
307  $this->field_obj->doUpdate();
308  } else {
309  $this->field_obj->setOrder($this->table->getNewFieldOrder());
310  $this->field_obj->doCreate();
311  }
312 
313  // Get possible properties and save them
314  $this->field_obj->storePropertiesFromForm($this->form);
315 
316  $ilCtrl->setParameter($this, "field_id", $this->field_obj->getId());
317 
318  if ($a_mode == "update") {
319  $this->main_tpl->setOnScreenMessage('success', $lng->txt("dcl_msg_field_modified"), true);
320  } else {
321  $this->table->addField($this->field_obj);
322  $this->table->buildOrderFields();
323  $this->main_tpl->setOnScreenMessage('success', $lng->txt("msg_field_created"));
324  }
325  $ilCtrl->redirectByClass(strtolower("ilDclFieldListGUI"), "listFields");
326  } else {
327  $this->form->setValuesByPost();
328  $tpl->setContent($this->form->getHTML());
329  }
330  }
331 
337  protected function checkInput(string $a_mode): bool
338  {
339  global $DIC;
340  $lng = $DIC['lng'];
341  $return = $this->form->checkInput();
342 
343  // load specific model for input checking
344  $datatype_id = $this->form->getInput('datatype');
345  if ($datatype_id != null && is_numeric($datatype_id)) {
346  $base_model = new ilDclBaseFieldModel();
347  $base_model->setDatatypeId((int) $datatype_id);
348  $field_validation_class = ilDclFieldFactory::getFieldModelInstanceByClass($base_model);
349 
350  if (!$field_validation_class->checkFieldCreationInput($this->form)) {
351  $return = false;
352  }
353  }
354 
355  // Don't allow multiple fields with the same title in this table
356  if ($a_mode == 'create') {
357  if ($title = $this->form->getInput('title')) {
358  if (ilDclTable::_hasFieldByTitle($title, $this->table_id)) {
359  $inputObj = $this->form->getItemByPostVar('title');
360  $inputObj->setAlert($lng->txt("dcl_field_title_unique"));
361  $return = false;
362  }
363  }
364  }
365 
366  if (!$return) {
367  $this->main_tpl->setOnScreenMessage('failure', $lng->txt("form_input_not_valid"));
368  }
369 
370  return $return;
371  }
372 
376  protected function checkAccess(): bool
377  {
378  if ($field_id = $this->field_obj->getId()) {
380  $this->getDataCollectionObject()->getRefId(),
381  $this->table_id,
382  (int) $field_id
383  );
384  } else {
386  $this->getDataCollectionObject()->getRefId(),
387  $this->table_id
388  );
389  }
390  }
391 
396  {
397  return $this->parent_obj->getDataCollectionObject();
398  }
399 }
checkInput(string $a_mode)
Check input of form.
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
Help GUI class.
static getFieldCache(int $field_id=0)
ILIAS HTTP Services $http
ilDclTableListGUI $parent_obj
save(string $a_mode="create")
save Field
edit()
create field edit form
ilDclTableListGUI: ilDclFieldListGUI, ilDclFieldEditGUI, ilDclTableViewGUI, ilDclTableEditGUI ...
create()
create field add form
global $DIC
Definition: feed.php:28
static hasAccessToFields(int $ref_id, int $table_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilDclTableListGUI $a_parent_obj)
Constructor.
executeCommand()
execute command
static http()
Fetches the global http state from ILIAS.
This class represents a property in a property form.
static getFieldModelInstanceByClass(ilDclBaseFieldModel $field, ?int $field_id=null)
Gets the correct instance of a fieldModel class Checks if a field is a plugin a replaces the fieldMod...
static getTableCache(int $table_id=null)
static getFieldModelInstance(int $field_id, ?int $datatype=null)
Get FieldModel from field-id and datatype.
$lng
static getAllDatatype(bool $force=false)
Get all possible Datatypes.
static hasAccessToField(int $ref_id, int $table_id, int $field_id)
ILIAS Refinery Factory $refinery
ilGlobalTemplateInterface $main_tpl
static _hasFieldByTitle(string $title, int $obj_id)
Checks if a table has a field with the given title.
form( $class_path, string $cmd, string $submit_caption="")
ilDclBaseFieldModel $field_obj
This class represents a text area property in a property form.
initForm(string $a_mode="create")
initEditCustomForm
static _getTitleInvalidChars(bool $a_as_regex=true)
All valid chars for filed titles.