ILIAS  release_8 Revision v8.24
class.ilDclFieldEditGUI.php
Go to the documentation of this file.
1<?php
2
20{
21 protected int $obj_id;
22 protected int $table_id;
23
25 protected ilDclTable $table;
31 protected int $field_id;
32
36 public function __construct(ilDclTableListGUI $a_parent_obj)
37 {
38 global $DIC;
39 $this->main_tpl = $DIC->ui()->mainTemplate();
40 $ilCtrl = $DIC['ilCtrl'];
41
42 $this->obj_id = $a_parent_obj->getObjId();
43 $this->parent_obj = $a_parent_obj;
44 $this->http = $DIC->http();
45 $this->refinery = $DIC->refinery();
46
47 $this->table_id = $this->http->wrapper()->query()->retrieve('table_id', $this->refinery->kindlyTo()->int());
48
49 $hasFieldId = $this->http->wrapper()->query()->has('field_id');
50 if ($hasFieldId) {
51 $this->field_id = $this->http->wrapper()->query()->retrieve('field_id', $this->refinery->kindlyTo()->int());
52 } else {
53 $this->field_id = 0;
54 }
55
56 if ($this->field_id) {
57 $this->field_obj = ilDclCache::getFieldCache($this->field_id);
58 } else {
59 $datatype = null;
60
61 $has_datatype = $this->http->wrapper()->post()->has('datatype');
62
63 if ($has_datatype) {
64 $datatype_value = $this->http->wrapper()->post()->retrieve(
65 'datatype',
66 $this->refinery->kindlyTo()->string()
67 );
68 if (in_array(
69 $datatype_value,
71 )) {
72 $datatype = $datatype_value;
73 }
74 }
75 $this->field_obj = ilDclFieldFactory::getFieldModelInstance($this->field_id, $datatype);
76 if (!$this->table_id) {
77 $ilCtrl->redirectByClass("ilDclTableListGUI", "listFields");
78 }
79 $this->field_obj->setTableId($this->table_id);
80 $ilCtrl->saveParameter($this, "table_id");
81 }
82
83 $this->table = ilDclCache::getTableCache($this->table_id);
84 }
85
89 public function executeCommand(): void
90 {
91 global $DIC;
92 $ilCtrl = $DIC['ilCtrl'];
93 $ilCtrl->saveParameter($this, 'field_id');
94
95 $cmd = $ilCtrl->getCmd();
96
97 if (!$this->checkAccess()) {
98 $this->permissionDenied();
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
115 public function create(): void
116 {
117 global $DIC;
118 $tpl = $DIC['tpl'];
119
120 $this->initForm();
121 $tpl->setContent($this->form->getHTML());
122 }
123
127 public function edit(): void
128 {
129 global $DIC;
130 $tpl = $DIC['tpl'];
131
132 $this->initForm("edit");
133
134 $this->field_obj->fillPropertiesForm($this->form);
135
136 $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', (int) $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($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($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(true));
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 foreach (ilDclDatatype::getAllDatatype() as $datatype) {
252 $model = new ilDclBaseFieldModel();
253 $model->setDatatypeId($datatype->getId());
255
256 if ($a_mode == 'edit' && $datatype->getId() === $this->field_obj->getDatatypeId()) {
257 $model = $this->field_obj;
258 }
259
260 $field_representation = ilDclFieldFactory::getFieldRepresentationInstance($model);
261 $field_representation->addFieldCreationForm($edit_datatype, $this->getDataCollectionObject(), $a_mode);
262 }
263 $edit_datatype->setRequired(true);
264
265 //you can't change type but we still need it in POST
266 if ($a_mode == "edit") {
267 $edit_datatype->setDisabled(true);
268 }
269 $this->form->addItem($edit_datatype);
270
271 //Unique
272 $cb = new ilCheckboxInputGUI($lng->txt("dcl_unique"), "unique");
273 $cb->setInfo($lng->txt('dcl_unique_desc'));
274 $this->form->addItem($cb);
275 }
276
281 public function save(string $a_mode = "create"): void
282 {
283 global $DIC;
284 $ilCtrl = $DIC['ilCtrl'];
285 $lng = $DIC['lng'];
286 $tpl = $DIC['tpl'];
287
288 $this->initForm($a_mode == "update" ? "edit" : "create");
289
290 if ($this->checkInput($a_mode)) {
291
292 // check if confirmation is needed and if so, fetch and render confirmationGUI
293 if (($a_mode == "update") && !($this->form->getInput('confirmed')) && $this->field_obj->isConfirmationRequired($this->form)) {
294 $ilConfirmationGUI = $this->field_obj->getConfirmationGUI($this->form);
295 $tpl->setContent($ilConfirmationGUI->getHTML());
296
297 return;
298 }
299
300 $title = $this->form->getInput("title");
301 if ($a_mode != "create" && $title != $this->field_obj->getTitle()) {
302 $this->main_tpl->setOnScreenMessage('info', $lng->txt("dcl_field_title_change_warning"), true);
303 }
304
305 $this->field_obj->setTitle($title);
306 $this->field_obj->setDescription($this->form->getInput("description"));
307 $this->field_obj->setDatatypeId($this->form->getInput("datatype"));
308 $this->field_obj->setUnique($this->form->getInput("unique"));
309
310 if ($a_mode == "update") {
311 $this->field_obj->doUpdate();
312 } else {
313 $this->field_obj->setOrder($this->table->getNewFieldOrder());
314 $this->field_obj->doCreate();
315 }
316
317 // Get possible properties and save them
318 $this->field_obj->storePropertiesFromForm($this->form);
319
320 $ilCtrl->setParameter($this, "field_id", $this->field_obj->getId());
321
322 if ($a_mode == "update") {
323 $this->main_tpl->setOnScreenMessage('success', $lng->txt("dcl_msg_field_modified"), true);
324 } else {
325 $this->table->addField($this->field_obj);
326 $this->table->buildOrderFields();
327 $this->main_tpl->setOnScreenMessage('success', $lng->txt("msg_field_created"), false);
328 }
329 $ilCtrl->redirectByClass(strtolower("ilDclFieldListGUI"), "listFields");
330 } else {
331 $this->form->setValuesByPost();
332 $tpl->setContent($this->form->getHTML());
333 }
334 }
335
341 protected function checkInput(string $a_mode): bool
342 {
343 global $DIC;
344 $lng = $DIC['lng'];
345 $return = $this->form->checkInput();
346
347 // load specific model for input checking
348 $datatype_id = $this->form->getInput('datatype');
349 if ($datatype_id != null && is_numeric($datatype_id)) {
350 $base_model = new ilDclBaseFieldModel();
351 $base_model->setDatatypeId($datatype_id);
352 $field_validation_class = ilDclFieldFactory::getFieldModelInstanceByClass($base_model);
353
354 if (!$field_validation_class->checkFieldCreationInput($this->form)) {
355 $return = false;
356 }
357 }
358
359 // Don't allow multiple fields with the same title in this table
360 if ($a_mode == 'create') {
361 if ($title = $this->form->getInput('title')) {
362 if (ilDclTable::_hasFieldByTitle($title, $this->table_id)) {
363 $inputObj = $this->form->getItemByPostVar('title');
364 $inputObj->setAlert($lng->txt("dcl_field_title_unique"));
365 $return = false;
366 }
367 }
368 }
369
370 if (!$return) {
371 $this->main_tpl->setOnScreenMessage('failure', $lng->txt("form_input_not_valid"));
372 }
373
374 return $return;
375 }
376
380 protected function checkAccess(): bool
381 {
382 if ($field_id = $this->field_obj->getId()) {
384 $this->getDataCollectionObject()->getRefId(),
385 $this->table_id,
387 );
388 } else {
390 $this->getDataCollectionObject()->getRefId(),
391 $this->table_id
392 );
393 }
394 }
395
400 {
401 return $this->parent_obj->getDataCollectionObject();
402 }
403}
Builds data types.
Definition: Factory.php:21
Class Services.
Definition: Services.php:38
This class represents a checkbox property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getTitleInvalidChars(bool $a_as_regex=true)
All valid chars for filed titles.
static getFieldCache(int $field_id=0)
static getTableCache(int $table_id=null)
static getAllDatatype(bool $force=false)
Get all possible Datatypes.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
save(string $a_mode="create")
save Field
edit()
create field edit form
create()
create field add form
__construct(ilDclTableListGUI $a_parent_obj)
Constructor.
ilDclTableListGUI $parent_obj
ilDclBaseFieldModel $field_obj
executeCommand()
execute command
ilGlobalTemplateInterface $main_tpl
ILIAS HTTP Services $http
ILIAS Refinery Factory $refinery
initForm(string $a_mode="create")
initEditCustomForm
checkInput(string $a_mode)
Check input of form.
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
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 getFieldModelInstance(int $field_id, ?int $datatype=null)
Get FieldModel from field-id and datatype.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _hasFieldByTitle(string $title, int $obj_id)
Checks if a table has a field with the given title.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static hasAccessToFields(int $ref_id, int $table_id)
static hasAccessToField(int $ref_id, int $table_id, int $field_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a property form user interface.
This class represents a property in a property form.
This class represents a text area property in a property form.
This class represents a text property in a property form.
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
static http()
Fetches the global http state from ILIAS.
form( $class_path, string $cmd)
$lng