ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilDclTableEditGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
25 {
26  private ?int $table_id;
27  private ilDclTable $table;
28  protected \ILIAS\UI\Factory $ui_factory;
29  protected \ILIAS\UI\Renderer $ui_renderer;
30  protected ilLanguage $lng;
31  protected ilCtrl $ctrl;
34  protected Form $form;
35  protected ilHelpGUI $help;
39  protected int $obj_id;
40 
41  public function __construct(ilDclTableListGUI $a_parent_obj)
42  {
43  global $DIC;
44 
45  $locator = $DIC['ilLocator'];
46 
47  $this->ctrl = $DIC->ctrl();
48  $this->lng = $DIC->language();
49  $this->tpl = $DIC->ui()->mainTemplate();
50  $this->toolbar = $DIC->toolbar();
51  $this->parent_object = $a_parent_obj;
52  $this->obj_id = $a_parent_obj->getObjId();
53  $this->help = $DIC->help();
54  $this->http = $DIC->http();
55  $this->refinery = $DIC->refinery();
56  $this->ui_factory = $DIC->ui()->factory();
57  $this->ui_renderer = $DIC->ui()->renderer();
58 
59  $table_id = null;
60  if ($this->http->wrapper()->query()->has('table_id')) {
61  $table_id = $this->http->wrapper()->query()->retrieve('table_id', $this->refinery->kindlyTo()->int());
62  }
63 
64  $this->table_id = $table_id;
65  $this->table = ilDclCache::getTableCache($this->table_id);
66 
67  $this->ctrl->saveParameter($this, 'table_id');
68  if ($this->table->getTitle()) {
69  $locator->addItem($this->table->getTitle(), $this->ctrl->getLinkTarget($this, 'edit'));
70  }
71  $this->tpl->setLocator();
72 
73  if (!$this->checkAccess()) {
74  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
75  $this->ctrl->redirectByClass(ilDclRecordListGUI::class, 'listRecords');
76  }
77  }
78 
79  public function executeCommand(): void
80  {
81  $cmd = $this->ctrl->getCmd();
82  if ($cmd === 'update') {
83  $this->save(false);
84  } else {
85  $this->$cmd();
86  }
87  }
88 
89  public function create(): void
90  {
91  $this->help->setSubScreenId('create');
92  $this->tpl->setContent($this->lng->txt('dcl_new_table') . $this->ui_renderer->render($this->initForm()));
93  }
94 
95  public function edit(): void
96  {
97  $this->help->setSubScreenId('edit');
98  $this->tpl->setContent(
99  sprintf($this->lng->txt('dcl_edit_table'), $this->table->getTitle()) .
100  $this->ui_renderer->render($this->initForm(false))
101  );
102  }
103 
104  public function cancel(): void
105  {
106  $this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
107  }
108 
109  public function initForm(bool $create = true): Form
110  {
111  $f = $this->ui_factory->input()->field();
112  $inputs = [];
113 
114  $edit = [];
115  $edit['title'] = $f->text($this->lng->txt('title'))->withRequired(true);
116  $edit['description'] = $f->markdown(new ilUIMarkdownPreviewGUI(), $this->lng->txt('additional_info'));
117  $edit['visible'] = $this->checkbox('visible');
118  $inputs['edit'] = $f->section($edit, $this->lng->txt('general_settings'));
119 
120  $table = [];
121  if (!$create) {
122  $options = [];
123  foreach ($this->table->getFields() as $field) {
124  if ($field->getId() !== 'comments' && $field->getRecordQuerySortObject() !== null) {
125  $options[$field->getId()] = $field->getTitle();
126  }
127  }
128  $table['default_sort_field'] = $f->select(
129  $this->lng->txt('dcl_default_sort_field'),
130  $options
131  );
132 
133  $table['default_sort_field_order'] = $f->select(
134  $this->lng->txt('dcl_default_sort_field_order'),
135  ['asc' => $this->lng->txt('dcl_asc'), 'desc' => $this->lng->txt('dcl_desc')],
136  $this->lng->txt('dcl_default_sort_field_order_desc')
137  );
138  }
139  $table['export_enabled'] = $this->checkbox('export_enabled');
140  $table['import_enabled'] = $this->checkbox('import_enabled');
141  $table['comments_enabled'] = $this->checkbox('comments');
142  $inputs['table'] = $f->section($table, $this->lng->txt('dcl_table_settings'));
143 
144  $record = [];
145  $record['add_perm'] = $f->optionalGroup(
146  ['save_confirmation' => $this->checkbox('save_confirmation')],
147  $this->lng->txt('dcl_add_perm'),
148  $this->lng->txt('dcl_add_perm_desc')
149  )->withValue(['save_confirmation' => false]);
150  $record['edit_perm'] = $f->radio($this->lng->txt('dcl_edit_perm'))
151  ->withOption('all', $this->lng->txt('dcl_all_entries'))
152  ->withOption('own', $this->lng->txt('dcl_own_entries'))
153  ->withOption('none', $this->lng->txt('dcl_no_entries'))
154  ->withValue('own');
155  $record['delete_perm'] = $f->radio($this->lng->txt('dcl_delete_perm'))
156  ->withOption('all', $this->lng->txt('dcl_all_entries'))
157  ->withOption('own', $this->lng->txt('dcl_own_entries'))
158  ->withOption('none', $this->lng->txt('dcl_no_entries'))
159  ->withValue('own');
160  $record['view_own_records_perm'] = $this->checkbox('view_own_records_perm');
161  $record['limited'] = $f->optionalGroup(
162  [
163  'limit_start' => $f->dateTime($this->lng->txt('dcl_limit_start'))->withUseTime(true),
164  'limit_end' => $f->dateTime($this->lng->txt('dcl_limit_end'))->withUseTime(true)
165  ],
166  $this->lng->txt('dcl_limited'),
167  $this->lng->txt('dcl_limited_desc')
168  )->withValue(null);
169  $inputs['record'] = $f->section($record, $this->lng->txt('dcl_record_settings'));
170 
171  if (!$create) {
172  $inputs = $this->setValues($inputs);
173  }
174 
175  $this->ctrl->setParameter($this, 'table_id', $this->table_id);
176  return $this->ui_factory->input()->container()->form()->standard(
177  $this->ctrl->getFormAction($this, $create ? 'save' : 'update'),
178  $inputs
179  );
180  }
181 
182  private function checkbox(string $label): Checkbox
183  {
184  return $this->ui_factory->input()->field()->checkbox(
185  $this->lng->txt('dcl_' . $label),
186  $this->lng->txt('dcl_' . $label . '_desc')
187  );
188  }
189 
190  protected function setValues(array $inputs): array
191  {
192  $inputs['edit'] = $inputs['edit']->withValue([
193  'title' => $this->table->getTitle(),
194  'description' => $this->table->getDescription(),
195  'visible' => $this->table->getIsVisible(),
196  ]);
197  $sort_field = $this->table->getDefaultSortField();
198  $inputs['table'] = $inputs['table']->withValue([
199  'default_sort_field' => in_array($sort_field, $this->table->getFieldIds()) ? $sort_field : '',
200  'default_sort_field_order' => $this->table->getDefaultSortFieldOrder(),
201  'export_enabled' => $this->table->getExportEnabled(),
202  'import_enabled' => $this->table->getImportEnabled(),
203  'comments_enabled' => $this->table->getPublicCommentsEnabled()
204  ]);
205  $inputs['record'] = $inputs['record']->withValue([
206  'add_perm' => $this->table->getAddPerm() ? ['save_confirmation' => $this->table->getSaveConfirmation()] : null,
207  'edit_perm' => $this->table->getEditPerm() ? ($this->table->getEditByOwner() ? 'own' : 'all') : 'none',
208  'delete_perm' => $this->table->getDeletePerm() ? ($this->table->getDeleteByOwner() ? 'own' : 'all') : 'none',
209  'view_own_records_perm' => $this->table->getViewOwnRecordsPerm(),
210  'limited' => $this->table->getLimited() ? ['limit_start' => $this->table->getLimitStart(), 'limit_end' => $this->table->getLimitEnd()] : null
211  ]);
212 
213  return $inputs;
214  }
215 
216  public function save(bool $create = true): void
217  {
218  if (!ilObjDataCollectionAccess::checkActionForObjId('write', $this->obj_id)) {
219  return;
220  }
221 
222  $form = $this->initForm($create)->withRequest($this->http->request());
223  $data = $form->getData();
224 
225  if ($data !== null) {
226  if ($create) {
227  $this->table = new ilDclTable();
228  }
229  foreach (ilObjectFactory::getInstanceByObjId($this->obj_id)->getTables() as $table) {
230  if ($table->getTitle() === $data['edit']['title'] && $table->getId() !== $this->table->getId()) {
231  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_FAILURE, $this->lng->txt('dcl_table_title_unique'));
232  $this->tpl->setContent($this->ui_renderer->render($form));
233  return;
234  }
235  }
236 
237  $this->table->setObjId($this->obj_id);
238  $this->table->setTitle($data['edit']['title']);
239  $this->table->setDescription($data['edit']['description']);
240  $this->table->setIsVisible($data['edit']['visible']);
241 
242  $this->table->setExportEnabled($data['table']['export_enabled']);
243  $this->table->setImportEnabled($data['table']['import_enabled']);
244  $this->table->setPublicCommentsEnabled($data['table']['comments_enabled']);
245 
246  $this->table->setAddPerm($data['record']['add_perm'] !== null);
247  $this->table->setSaveConfirmation($data['record']['add_perm']['save_confirmation'] ?? false);
248  $this->table->setEditPerm($data['record']['edit_perm'] !== 'none');
249  $this->table->setEditByOwner($data['record']['edit_perm'] === 'own');
250  $this->table->setDeletePerm($data['record']['delete_perm'] !== 'none');
251  $this->table->setDeleteByOwner($data['record']['delete_perm'] === 'own');
252  $this->table->setViewOwnRecordsPerm($data['record']['view_own_records_perm']);
253  $this->table->setLimited($data['record']['limited'] !== null);
254  if ($data['record']['limited']['limit_start'] ?? null !== null) {
255  $this->table->setLimitStart($data['record']['limited']['limit_start']->format('Y-m-d H:i:s'));
256  } else {
257  $this->table->setLimitStart('');
258  }
259  if ($data['record']['limited']['limit_end'] ?? null !== null) {
260  $this->table->setLimitEnd($data['record']['limited']['limit_end']->format('Y-m-d H:i:s'));
261  } else {
262  $this->table->setLimitEnd('');
263  }
264 
265  if ($create) {
266  $this->table->doCreate();
267  $this->ctrl->setParameter($this, 'table_id', $this->table->getId());
268  $message = 'dcl_msg_table_created';
269  } else {
270  $this->table->setDefaultSortField($data['table']['default_sort_field']);
271  $this->table->setDefaultSortFieldOrder($data['table']['default_sort_field_order']);
272  $this->table->doUpdate();
273  $message = 'dcl_msg_table_edited';
274  }
275  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt($message), true);
276  $this->ctrl->redirectByClass(ilDclTableEditGUI::class, 'edit');
277  } else {
278  $this->tpl->setContent($this->ui_renderer->render($form));
279  }
280  }
281 
282  public function confirmDelete(): void
283  {
284  $conf = new ilConfirmationGUI();
285  $conf->setFormAction($this->ctrl->getFormAction($this));
286  $conf->setHeaderText($this->lng->txt('dcl_confirm_delete_table'));
287 
288  $conf->addItem('table', (string) $this->table->getId(), $this->table->getTitle());
289 
290  $conf->setConfirm($this->lng->txt('delete'), 'delete');
291  $conf->setCancel($this->lng->txt('cancel'), 'cancelDelete');
292 
293  $this->tpl->setContent($conf->getHTML());
294  }
295 
296  public function cancelDelete(): void
297  {
298  $this->ctrl->redirectByClass("ilDclTableListGUI", "listTables");
299  }
300 
301  public function delete(): void
302  {
303  if (count($this->table->getCollectionObject()->getTables()) < 2) {
304  $this->tpl->setOnScreenMessage(
305  $this->tpl::MESSAGE_TYPE_FAILURE,
306  $this->lng->txt("dcl_cant_delete_last_table"),
307  true
308  );
309  $this->table->doDelete(true);
310  } else {
311  $this->table->doDelete();
312  }
313  $this->ctrl->clearParameterByClass("ilobjdatacollectiongui", "table_id");
314  $this->ctrl->redirectByClass("ildcltablelistgui", "listtables");
315  }
316 
317  public function setAsDefault(): void
318  {
319  $object = ilObjectFactory::getInstanceByObjId($this->obj_id);
320  $order = 20;
321  foreach ($object->getTables() as $table) {
322  if ($table->getId() === $this->table->getId()) {
323  $table->setOrder(10);
324  } else {
325  $table->setOrder($order);
326  $order += 10;
327  }
328  $table->doUpdate();
329  }
330  $this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
331  }
332 
333  protected function checkAccess(): bool
334  {
335  $ref_id = $this->parent_object->getDataCollectionObject()->getRefId();
336 
337  return $this->table_id ? ilObjDataCollectionAccess::hasAccessToEditTable(
338  $ref_id,
339  $this->table_id
341  }
342 }
This describes commonalities between all forms.
Definition: Form.php:32
Help GUI class.
ILIAS UI Renderer $ui_renderer
ilDclTableListGUI: ilDclFieldListGUI, ilDclFieldEditGUI, ilDclTableViewGUI, ilDclTableEditGUI ...
This describes checkbox inputs.
Definition: Checkbox.php:28
getId()
Get table id.
ilDclTableListGUI $parent_object
ILIAS HTTP Services $http
ILIAS Refinery Factory $refinery
setOrder(int $table_order)
ilGlobalTemplateInterface $tpl
$ref_id
Definition: ltiauth.php:66
static http()
Fetches the global http state from ILIAS.
static getTableCache(int $table_id=null)
global $DIC
Definition: shib_login.php:25
static hasWriteAccess(int $ref, ?int $user_id=0)
__construct(ilDclTableListGUI $a_parent_obj)
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
ILIAS UI Factory $ui_factory
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
initForm(bool $create=true)
getData()
Get the data in the form if all inputs are ok, where the transformation is applied if one was added...
static hasAccessToEditTable(int $ref_id, int $table_id)
static checkActionForObjId(string $action, int $obj_id)