ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilDclTableEditGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  private ?int $table_id;
24  private ilDclTable $table;
25  protected \ILIAS\UI\Factory $ui_factory;
26  protected ilLanguage $lng;
27  protected ilCtrl $ctrl;
31  protected ilHelpGUI $help;
35  protected int $obj_id;
36 
40  public function __construct(ilDclTableListGUI $a_parent_obj)
41  {
42  global $DIC;
43 
44  $locator = $DIC['ilLocator'];
45 
46  $this->ctrl = $DIC->ctrl();
47  $this->lng = $DIC->language();
48  $this->tpl = $DIC->ui()->mainTemplate();
49  $this->toolbar = $DIC->toolbar();
50  $this->parent_object = $a_parent_obj;
51  $this->obj_id = $a_parent_obj->getObjId();
52  $this->help = $DIC->help();
53  $this->http = $DIC->http();
54  $this->refinery = $DIC->refinery();
55  $this->ui_factory = $DIC->ui()->factory();
56 
57  $table_id = null;
58  if ($this->http->wrapper()->query()->has("table_id")) {
59  $table_id = $this->http->wrapper()->query()->retrieve('table_id', $this->refinery->kindlyTo()->int());
60  }
61 
62  $this->table_id = $table_id;
63  $this->table = ilDclCache::getTableCache($this->table_id);
64 
65  $this->ctrl->saveParameter($this, 'table_id');
66  if ($this->table->getTitle()) {
67  $locator->addItem($this->table->getTitle(), $this->ctrl->getLinkTarget($this, 'edit'));
68  }
69  $this->tpl->setLocator();
70 
71  if (!$this->checkAccess()) {
72  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
73  $this->ctrl->redirectByClass(ilDclRecordListGUI::class, 'listRecords');
74  }
75  }
76 
77  public function executeCommand(): void
78  {
79  $cmd = $this->ctrl->getCmd();
80  switch ($cmd) {
81  case 'update':
82  $this->save(false);
83  break;
84  default:
85  $this->$cmd();
86  break;
87  }
88  }
89 
90  public function create(): void
91  {
92  $this->help->setSubScreenId('create');
93 
94  $this->initForm();
95  $this->getStandardValues();
96  $this->tpl->setContent($this->form->getHTML());
97  }
98 
99  public function edit(): void
100  {
101  $this->help->setSubScreenId('edit');
102 
103  if (!$this->table_id) {
104  $this->ctrl->redirectByClass(ilDclFieldEditGUI::class, "listFields");
105 
106  return;
107  } else {
108  $this->table = ilDclCache::getTableCache($this->table_id);
109  }
110  $this->initForm(false);
111  $this->getValues();
112  $this->tpl->setContent($this->form->getHTML());
113  }
114 
115  public function getValues(): void
116  {
117  $values = [
118  'title' => $this->table->getTitle(),
119  'add_perm' => (int) $this->table->getAddPerm(),
120  'edit_perm' => (int) $this->table->getEditPerm(),
121  'edit_perm_mode' => $this->table->getEditByOwner() ? 'own' : 'all',
122  'delete_perm' => (int) $this->table->getDeletePerm(),
123  'delete_perm_mode' => $this->table->getDeleteByOwner() ? 'own' : 'all',
124  'export_enabled' => $this->table->getExportEnabled(),
125  'import_enabled' => $this->table->getImportEnabled(),
126  'limited' => $this->table->getLimited(),
127  'limit_start' => substr($this->table->getLimitStart(), 0, 10) . " " . substr(
128  $this->table->getLimitStart(),
129  -8
130  ),
131  'limit_end' => substr($this->table->getLimitEnd(), 0, 10) . " " . substr($this->table->getLimitEnd(), -8),
132  'default_sort_field' => $this->table->getDefaultSortField(),
133  'default_sort_field_order' => $this->table->getDefaultSortFieldOrder(),
134  'description' => $this->table->getDescription(),
135  'view_own_records_perm' => $this->table->getViewOwnRecordsPerm(),
136  'save_confirmation' => $this->table->getSaveConfirmation(),
137  ];
138  if (!$this->table->getLimitStart()) {
139  $values['limit_start'] = null;
140  }
141  if (!$this->table->getLimitEnd()) {
142  $values['limit_end'] = null;
143  }
144  $this->form->setValuesByArray($values);
145  }
146 
147  public function getStandardValues(): void
148  {
149  $values = [
150  'title' => "",
151  'add_perm' => 1,
152  'edit_perm' => 1,
153  'edit_perm_mode' => 'own',
154  'delete_perm_mode' => 'own',
155  'delete_perm' => 1,
156  'edit_by_owner' => 1,
157  'export_enabled' => 0,
158  'import_enabled' => 0,
159  'limited' => 0,
160  'limit_start' => null,
161  'limit_end' => null,
162  ];
163  $this->form->setValuesByArray($values);
164  }
165 
166  public function cancel(): void
167  {
168  $this->ctrl->redirectByClass("ilDclTableListGUI", "listTables");
169  }
170 
171  public function initForm(bool $create = true): void
172  {
173  $this->form = new ilPropertyFormGUI();
174 
175  $item = new ilTextInputGUI($this->lng->txt('title'), 'title');
176  $item->setRequired(true);
177  $this->form->addItem($item);
178 
179  if (!$create) {
180  $switcher = new ilDclSwitcher($this->toolbar, $this->ui_factory, $this->ctrl, $this->lng);
181  $switcher->addTableSwitcherToToolbar(
182  $this->parent_object->getDataCollectionObject()->getTables(),
183  self::class,
184  'edit'
185  );
186 
187  $item = new ilSelectInputGUI($this->lng->txt('dcl_default_sort_field'), 'default_sort_field');
188  $item->setInfo($this->lng->txt('dcl_default_sort_field_desc'));
189  $fields = array_filter($this->table->getFields(), function (ilDclBaseFieldModel $field) {
190  return !is_null($field->getRecordQuerySortObject());
191  });
192  $options = [0 => $this->lng->txt('dcl_please_select')];
193  foreach ($fields as $field) {
194  if ($field->getId() == 'comments') {
195  continue;
196  }
197  $options[$field->getId()] = $field->getTitle();
198  }
199  $item->setOptions($options);
200  $this->form->addItem($item);
201 
202  $item = new ilSelectInputGUI($this->lng->txt('dcl_default_sort_field_order'), 'default_sort_field_order');
203  $options = ['asc' => $this->lng->txt('dcl_asc'), 'desc' => $this->lng->txt('dcl_desc')];
204  $item->setOptions($options);
205  $this->form->addItem($item);
206  }
207 
208  $item = new ilTextAreaInputGUI($this->lng->txt('additional_info'), 'description');
209  $item->setUseRte(true);
210  $item->setInfo($this->lng->txt('dcl_additional_info_desc'));
211  $item->setRteTagSet('mini');
212  $this->form->addItem($item);
213 
214  $section = new ilFormSectionHeaderGUI();
215  $section->setTitle($this->lng->txt('dcl_permissions_form'));
216  $this->form->addItem($section);
217 
218  $item = new ilCustomInputGUI();
219  $item->setHtml($this->lng->txt('dcl_table_info'));
220  $item->setTitle($this->lng->txt('dcl_table_info_title'));
221  $this->form->addItem($item);
222 
223  $item = new ilCheckboxInputGUI($this->lng->txt('dcl_add_perm'), 'add_perm');
224  $item->setInfo($this->lng->txt("dcl_add_perm_desc"));
225  $this->form->addItem($item);
226 
227  $item = new ilCheckboxInputGUI($this->lng->txt('dcl_save_confirmation'), 'save_confirmation');
228  $item->setInfo($this->lng->txt('dcl_save_confirmation_desc'));
229  $this->form->addItem($item);
230 
231  $item = new ilCheckboxInputGUI($this->lng->txt('dcl_edit_perm'), 'edit_perm');
232  $this->form->addItem($item);
233 
234  $radios = new ilRadioGroupInputGUI('', 'edit_perm_mode');
235  $radios->addOption(new ilRadioOption($this->lng->txt('dcl_all_entries'), 'all'));
236  $radios->addOption(new ilRadioOption($this->lng->txt('dcl_own_entries'), 'own'));
237  $item->addSubItem($radios);
238 
239  $item = new ilCheckboxInputGUI($this->lng->txt('dcl_delete_perm'), 'delete_perm');
240  $this->form->addItem($item);
241 
242  $radios = new ilRadioGroupInputGUI('', 'delete_perm_mode');
243  $radios->addOption(new ilRadioOption($this->lng->txt('dcl_all_entries'), 'all'));
244  $radios->addOption(new ilRadioOption($this->lng->txt('dcl_own_entries'), 'own'));
245  $item->addSubItem($radios);
246 
247  $item = new ilCheckboxInputGUI($this->lng->txt('dcl_view_own_records_perm'), 'view_own_records_perm');
248  $this->form->addItem($item);
249 
250  $item = new ilCheckboxInputGUI($this->lng->txt('dcl_export_enabled'), 'export_enabled');
251  $item->setInfo($this->lng->txt('dcl_export_enabled_desc'));
252  $this->form->addItem($item);
253 
254  $item = new ilCheckboxInputGUI($this->lng->txt('dcl_import_enabled'), 'import_enabled');
255  $item->setInfo($this->lng->txt('dcl_import_enabled_desc'));
256  $this->form->addItem($item);
257 
258  $item = new ilCheckboxInputGUI($this->lng->txt('dcl_limited'), 'limited');
259  $sitem1 = new ilDateTimeInputGUI($this->lng->txt('dcl_limit_start'), 'limit_start');
260  $sitem1->setShowTime(true);
261  $sitem2 = new ilDateTimeInputGUI($this->lng->txt('dcl_limit_end'), 'limit_end');
262  $sitem2->setShowTime(true);
263  $item->setInfo($this->lng->txt("dcl_limited_desc"));
264  $item->addSubItem($sitem1);
265  $item->addSubItem($sitem2);
266  $this->form->addItem($item);
267 
268  $this->ctrl->setParameter($this, 'table_id', $this->table_id);
269  if ($create) {
270  $this->form->setTitle($this->lng->txt('dcl_new_table'));
271  $this->form->addCommandButton('save', $this->lng->txt('create'));
272  $this->form->setFormAction($this->ctrl->getFormAction($this, 'create'));
273  } else {
274  $this->form->setTitle($this->lng->txt('dcl_edit_table'));
275  $this->form->addCommandButton('update', $this->lng->txt('save'));
276  $this->form->setFormAction($this->ctrl->getFormAction($this, 'update'));
277  }
278  $this->form->addCommandButton('cancel', $this->lng->txt('cancel'));
279  }
280 
281  public function save(bool $create = true): void
282  {
283  global $DIC;
284  $ilTabs = $DIC['ilTabs'];
285 
286  if (!ilObjDataCollectionAccess::checkActionForObjId('write', $this->obj_id)) {
287  $this->accessDenied();
288 
289  return;
290  }
291 
292  $ilTabs->activateTab("id_fields");
293  $this->initForm($create);
294 
295  if ($this->checkInput()) {
296  if ($create) {
297  $this->table = ilDclCache::getTableCache();
298  } elseif ($this->table_id) {
299  $this->table = ilDclCache::getTableCache($this->table_id);
300  } else {
301  $this->ctrl->redirectByClass("ildclfieldeditgui", "listFields");
302  }
303 
304  $this->table->setTitle($this->form->getInput("title"));
305  $this->table->setObjId($this->obj_id);
306  $this->table->setSaveConfirmation((bool) $this->form->getInput('save_confirmation'));
307  $this->table->setAddPerm((bool) $this->form->getInput("add_perm"));
308  $this->table->setEditPerm((bool) $this->form->getInput("edit_perm"));
309  if ($this->table->getEditPerm()) {
310  $edit_by_owner = ($this->form->getInput('edit_perm_mode') == 'own');
311  $this->table->setEditByOwner($edit_by_owner);
312  }
313  $this->table->setDeletePerm((bool) $this->form->getInput("delete_perm"));
314  if ($this->table->getDeletePerm()) {
315  $delete_by_owner = ($this->form->getInput('delete_perm_mode') == 'own');
316  $this->table->setDeleteByOwner($delete_by_owner);
317  }
318  $this->table->setViewOwnRecordsPerm((bool) $this->form->getInput('view_own_records_perm'));
319  $this->table->setExportEnabled((bool) $this->form->getInput("export_enabled"));
320  $this->table->setImportEnabled((bool) $this->form->getInput("import_enabled"));
321  $this->table->setDefaultSortField($this->form->getInput("default_sort_field"));
322  $this->table->setDefaultSortFieldOrder($this->form->getInput("default_sort_field_order"));
323  $this->table->setLimited((bool) $this->form->getInput("limited"));
324  $this->table->setDescription($this->form->getInput('description'));
325  $this->table->setLimitStart((string) $this->form->getInput("limit_start"));
326  $this->table->setLimitEnd((string) $this->form->getInput("limit_end"));
327  if ($create) {
328  $this->table->doCreate();
329  $this->tpl->setOnScreenMessage('success', $this->lng->txt("dcl_msg_table_created"), true);
330  $this->ctrl->setParameterByClass("ildclfieldlistgui", "table_id", $this->table->getId());
331  $this->ctrl->redirectByClass("ildclfieldlistgui", "listFields");
332  } else {
333  $this->table->doUpdate();
334  $this->tpl->setOnScreenMessage('success', $this->lng->txt("dcl_msg_table_edited"), true);
335  $this->ctrl->redirectByClass("ildcltableeditgui", "edit");
336  }
337  } else {
338  $this->form->setValuesByPost();
339  $this->tpl->setContent($this->form->getHTML());
340  }
341  }
342 
343  protected function checkInput(): bool
344  {
345  $return = $this->form->checkInput();
346 
347  $title = $this->form->getInput('title');
348  foreach ($this->parent_object->getDataCollectionObject()->getTables() as $table) {
349  if ($table->getTitle() === $title && $table->getId() !== $this->table->getId()) {
350  $inputObj = $this->form->getItemByPostVar('title');
351  $inputObj->setAlert($this->lng->txt("dcl_table_title_unique"));
352  $return = false;
353  break;
354  }
355  }
356 
357  if (!$return) {
358  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("form_input_not_valid"));
359  }
360 
361  return $return;
362  }
363 
364  public function accessDenied(): void
365  {
366  $this->tpl->setContent("Access denied.");
367  }
368 
369  public function confirmDelete(): void
370  {
371  $conf = new ilConfirmationGUI();
372  $conf->setFormAction($this->ctrl->getFormAction($this));
373  $conf->setHeaderText($this->lng->txt('dcl_confirm_delete_table'));
374 
375  $conf->addItem('table', (string) $this->table->getId(), $this->table->getTitle());
376 
377  $conf->setConfirm($this->lng->txt('delete'), 'delete');
378  $conf->setCancel($this->lng->txt('cancel'), 'cancelDelete');
379 
380  $this->tpl->setContent($conf->getHTML());
381  }
382 
383  public function cancelDelete(): void
384  {
385  $this->ctrl->redirectByClass("ilDclTableListGUI", "listTables");
386  }
387 
388  public function delete(): void
389  {
390  if (count($this->table->getCollectionObject()->getTables()) < 2) {
391  $this->tpl->setOnScreenMessage(
392  'failure',
393  $this->lng->txt("dcl_cant_delete_last_table"),
394  true
395  ); //TODO change lng var
396  $this->table->doDelete(true);
397  } else {
398  $this->table->doDelete();
399  }
400  $this->ctrl->clearParameterByClass("ilobjdatacollectiongui", "table_id");
401  $this->ctrl->redirectByClass("ildcltablelistgui", "listtables");
402  }
403 
404  protected function checkAccess(): bool
405  {
406  $ref_id = $this->parent_object->getDataCollectionObject()->getRefId();
407 
408  return $this->table_id ? ilObjDataCollectionAccess::hasAccessToEditTable(
409  $ref_id,
410  $this->table_id
412  }
413 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a selection list property in a property form.
Help GUI class.
setShowTime(bool $a_showtime)
ilDclTableListGUI: ilDclFieldListGUI, ilDclFieldEditGUI, ilDclTableViewGUI, ilDclTableEditGUI ...
setOptions(array $a_options)
getId()
Get table id.
ilDclTableListGUI $parent_object
ILIAS HTTP Services $http
ILIAS Refinery Factory $refinery
This class represents a date/time property in a property form.
global $DIC
Definition: feed.php:28
ilGlobalTemplateInterface $tpl
$ref_id
Definition: ltiauth.php:67
static http()
Fetches the global http state from ILIAS.
This class represents a property in a property form.
static getTableCache(int $table_id=null)
static hasWriteAccess(int $ref, ?int $user_id=0)
__construct(ilDclTableListGUI $a_parent_obj)
Constructor.
ILIAS UI Factory $ui_factory
initForm(bool $create=true)
form( $class_path, string $cmd, string $submit_caption="")
This class represents a text area property in a property form.
setUseRte(bool $a_usert, string $version='')
static hasAccessToEditTable(int $ref_id, int $table_id)
static checkActionForObjId(string $action, int $obj_id)