ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilDclTableEditGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 {
25  private ?int $table_id;
26  private ilDclTable $table;
27  protected \ILIAS\UI\Factory $ui_factory;
28  protected \ILIAS\UI\Renderer $ui_renderer;
29  protected ilLanguage $lng;
30  protected ilCtrl $ctrl;
33  protected Form $form;
34  protected ilHelpGUI $help;
38  protected int $obj_id;
39 
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  $this->ui_renderer = $DIC->ui()->renderer();
57 
58  $table_id = null;
59  if ($this->http->wrapper()->query()->has('table_id')) {
60  $table_id = $this->http->wrapper()->query()->retrieve('table_id', $this->refinery->kindlyTo()->int());
61  }
62 
63  $this->table_id = $table_id;
64  $this->table = ilDclCache::getTableCache($this->table_id);
65 
66  $this->ctrl->saveParameter($this, 'table_id');
67  if ($this->table->getTitle()) {
68  $locator->addItem($this->table->getTitle(), $this->ctrl->getLinkTarget($this, 'edit'));
69  }
70  $this->tpl->setLocator();
71 
72  if (!$this->checkAccess()) {
73  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
74  $this->ctrl->redirectByClass(ilDclRecordListGUI::class, 'listRecords');
75  }
76  }
77 
78  public function executeCommand(): void
79  {
80  $cmd = $this->ctrl->getCmd();
81  if ($cmd === 'save_create') {
82  $this->save('create');
83  } else {
84  $this->$cmd();
85  }
86  }
87 
88  public function create(): void
89  {
90  $this->help->setSubScreenId('create');
91  $this->tpl->setContent($this->lng->txt('dcl_new_table') . $this->ui_renderer->render($this->initForm()));
92  }
93 
94  public function edit(): void
95  {
96  $this->help->setSubScreenId('edit');
97  $this->tpl->setContent(sprintf($this->lng->txt('dcl_edit_table'), $this->table->getTitle()) . $this->ui_renderer->render($this->initForm('edit')));
98  }
99 
100  public function cancel(): void
101  {
102  $this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
103  }
104 
105  public function initForm(string $a_mode = 'create'): Form
106  {
107  $inputs = [];
108 
109  $edit = [];
110  $edit['title'] = $this->ui_factory->input()->field()->text($this->lng->txt('title'))->withRequired(true);
111  if ($a_mode !== 'create') {
112  $options = [0 => $this->lng->txt('dcl_please_select')];
113  foreach ($this->table->getFields() as $field) {
114  if ($field->getId() !== 'comments' && $field->getRecordQuerySortObject() !== null) {
115  $options[$field->getId()] = $field->getTitle();
116  }
117  }
118  $edit['default_sort_field'] = $this->ui_factory->input()->field()->select(
119  $this->lng->txt('dcl_default_sort_field'),
120  $options,
121  $this->lng->txt('dcl_default_sort_field_desc')
122  );
123 
124  $edit['default_sort_field_order'] = $this->ui_factory->input()->field()->select(
125  $this->lng->txt('dcl_default_sort_field_order'),
126  ['asc' => $this->lng->txt('dcl_asc'), 'desc' => $this->lng->txt('dcl_desc')],
127  );
128  }
129  $edit['description'] = $this->ui_factory->input()->field()->markdown(
131  $this->lng->txt('additional_info'),
132  $this->lng->txt('dcl_additional_info_desc')
133  );
134  $inputs['edit'] = $this->ui_factory->input()->field()->section(
135  $edit,
136  $this->lng->txt('general_settings')
137  );
138 
139  $user = [];
140  $user['add_perm'] = $this->ui_factory->input()->field()->optionalGroup(
141  [
142  'save_confirmation' => $this->ui_factory->input()->field()->checkbox($this->lng->txt('dcl_save_confirmation'), $this->lng->txt('dcl_save_confirmation_desc'))
143  ],
144  $this->lng->txt('dcl_add_perm'),
145  $this->lng->txt('dcl_add_perm_desc')
146  )->withValue(['save_confirmation' => false]);
147  $user['edit_perm'] = $this->ui_factory->input()->field()->radio($this->lng->txt('dcl_edit_perm'))
148  ->withOption('all', $this->lng->txt('dcl_all_entries'))
149  ->withOption('own', $this->lng->txt('dcl_own_entries'))
150  ->withOption('none', $this->lng->txt('dcl_no_entries'))
151  ->withValue('own');
152  $user['delete_perm'] = $this->ui_factory->input()->field()->radio($this->lng->txt('dcl_delete_perm'))
153  ->withOption('all', $this->lng->txt('dcl_all_entries'))
154  ->withOption('own', $this->lng->txt('dcl_own_entries'))
155  ->withOption('none', $this->lng->txt('dcl_no_entries'))
156  ->withValue('own');
157  $user['view_own_records_perm'] = $this->ui_factory->input()->field()->checkbox(
158  $this->lng->txt('dcl_view_own_records_perm'),
159  $this->lng->txt('dcl_view_own_records_perm_desc'),
160  );
161  $user['export_enabled'] = $this->ui_factory->input()->field()->checkbox($this->lng->txt('dcl_export_enabled'), $this->lng->txt('dcl_export_enabled_desc'));
162  $user['import_enabled'] = $this->ui_factory->input()->field()->checkbox($this->lng->txt('dcl_import_enabled'), $this->lng->txt('dcl_import_enabled_desc'));
163  $user['limited'] = $this->ui_factory->input()->field()->optionalGroup(
164  [
165  'limit_start' => $this->ui_factory->input()->field()->dateTime($this->lng->txt('dcl_limit_start'))->withUseTime(true),
166  'limit_end' => $this->ui_factory->input()->field()->dateTime($this->lng->txt('dcl_limit_end'))->withUseTime(true)
167  ],
168  $this->lng->txt('dcl_limited'),
169  $this->lng->txt('dcl_limited_desc')
170  )->withValue(null);
171  $inputs['user'] = $this->ui_factory->input()->field()->section(
172  $user,
173  $this->lng->txt('dcl_permissions_form')
174  );
175 
176  if ($a_mode === 'edit') {
177  $inputs = $this->setValues($inputs);
178  }
179 
180  $this->ctrl->setParameter($this, 'table_id', $this->table_id);
181  return $this->ui_factory->input()->container()->form()->standard($this->ctrl->getFormAction($this, $a_mode === 'edit' ? 'save' : 'save_create'), $inputs);
182  }
183 
184  protected function setValues(array $inputs): array
185  {
186  $inputs['edit'] = $inputs['edit']->withValue([
187  'title' => $this->table->getTitle(),
188  'default_sort_field' => $this->table->getDefaultSortField(),
189  'default_sort_field_order' => $this->table->getDefaultSortFieldOrder(),
190  'description' => $this->table->getDescription(),
191  ]);
192  $inputs['user'] = $inputs['user']->withValue([
193  'add_perm' => $this->table->getAddPerm() ? ['save_confirmation' => $this->table->getSaveConfirmation()] : null,
194  'edit_perm' => $this->table->getEditPerm() ? ($this->table->getEditByOwner() ? 'own' : 'all') : 'none',
195  'delete_perm' => $this->table->getDeletePerm() ? ($this->table->getDeleteByOwner() ? 'own' : 'all') : 'none',
196  'view_own_records_perm' => $this->table->getViewOwnRecordsPerm(),
197  'export_enabled' => $this->table->getExportEnabled(),
198  'import_enabled' => $this->table->getImportEnabled(),
199  'limited' => $this->table->getLimited() ? ['limit_start' => $this->table->getLimitStart(), 'limit_end' => $this->table->getLimitEnd()] : null
200  ]);
201 
202  return $inputs;
203  }
204 
205  public function save(string $a_mode = 'edit'): void
206  {
207  if (!ilObjDataCollectionAccess::checkActionForObjId('write', $this->obj_id)) {
208  return;
209  }
210 
211  $form = $this->initForm($a_mode)->withRequest($this->http->request());
212  $data = $form->getData();
213 
214  if ($data !== null) {
215  if ($a_mode === 'create') {
216  $this->table = new ilDclTable();
217  }
218  foreach (ilObjectFactory::getInstanceByObjId($this->obj_id)->getTables() as $table) {
219  if ($table->getTitle() === $data['edit']['title'] && $table->getId() !== $this->table->getId()) {
220  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_FAILURE, $this->lng->txt('dcl_table_title_unique'));
221  $this->tpl->setContent($this->ui_renderer->render($form));
222  return;
223  }
224  }
225 
226  $this->table->setObjId($this->obj_id);
227  $this->table->setTitle($data['edit']['title']);
228  if ($a_mode !== 'create') {
229  $this->table->setDefaultSortField($data['edit']['default_sort_field']);
230  $this->table->setDefaultSortFieldOrder($data['edit']['default_sort_field_order']);
231  }
232  $this->table->setDescription($data['edit']['description']);
233 
234  $this->table->setAddPerm($data['user']['add_perm'] !== null);
235  $this->table->setSaveConfirmation($data['user']['add_perm']['save_confirmation'] ?? false);
236  $this->table->setEditPerm($data['user']['edit_perm'] !== 'none');
237  $this->table->setEditByOwner($data['user']['edit_perm'] === 'own');
238  $this->table->setDeletePerm($data['user']['delete_perm'] !== 'none');
239  $this->table->setDeleteByOwner($data['user']['delete_perm'] === 'own');
240  $this->table->setViewOwnRecordsPerm($data['user']['view_own_records_perm']);
241  $this->table->setExportEnabled($data['user']['export_enabled']);
242  $this->table->setImportEnabled($data['user']['import_enabled']);
243  $this->table->setLimited($data['user']['limited'] !== null);
244  if ($data['user']['limited']['limit_start'] ?? null !== null) {
245  $this->table->setLimitStart($data['user']['limited']['limit_start']->format('Y-m-d H:i:s'));
246  } else {
247  $this->table->setLimitStart('');
248  }
249  if ($data['user']['limited']['limit_end'] ?? null !== null) {
250  $this->table->setLimitEnd($data['user']['limited']['limit_end']->format('Y-m-d H:i:s'));
251  } else {
252  $this->table->setLimitEnd('');
253  }
254 
255  if ($a_mode === 'create') {
256  $this->table->doCreate();
257  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('dcl_msg_table_created'), true);
258  $this->ctrl->setParameterByClass(ilDclFieldListGUI::class, 'table_id', $this->table->getId());
259  $this->ctrl->redirectByClass(ilDclFieldListGUI::class, 'listFields');
260  } else {
261  $this->table->doUpdate();
262  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('dcl_msg_table_edited'), true);
263  $this->ctrl->redirectByClass(ilDclTableEditGUI::class, 'edit');
264  }
265  } else {
266  $this->tpl->setContent($this->ui_renderer->render($form));
267  }
268  }
269 
270  public function confirmDelete(): void
271  {
272  $conf = new ilConfirmationGUI();
273  $conf->setFormAction($this->ctrl->getFormAction($this));
274  $conf->setHeaderText($this->lng->txt('dcl_confirm_delete_table'));
275 
276  $conf->addItem('table', (string) $this->table->getId(), $this->table->getTitle());
277 
278  $conf->setConfirm($this->lng->txt('delete'), 'delete');
279  $conf->setCancel($this->lng->txt('cancel'), 'cancelDelete');
280 
281  $this->tpl->setContent($conf->getHTML());
282  }
283 
284  public function cancelDelete(): void
285  {
286  $this->ctrl->redirectByClass("ilDclTableListGUI", "listTables");
287  }
288 
289  public function delete(): void
290  {
291  if (count($this->table->getCollectionObject()->getTables()) < 2) {
292  $this->tpl->setOnScreenMessage(
293  'failure',
294  $this->lng->txt("dcl_cant_delete_last_table"),
295  true
296  ); //TODO change lng var
297  $this->table->doDelete(true);
298  } else {
299  $this->table->doDelete();
300  }
301  $this->ctrl->clearParameterByClass("ilobjdatacollectiongui", "table_id");
302  $this->ctrl->redirectByClass("ildcltablelistgui", "listtables");
303  }
304 
305  public function enableVisible(): void
306  {
307  $this->table->setIsVisible(true);
308  $this->table->doUpdate();
309  $this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
310  }
311 
312  public function disableVisible(): void
313  {
314  $this->table->setIsVisible(false);
315  $this->table->doUpdate();
316  $this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
317  }
318 
319  public function enableComments(): void
320  {
321  $this->table->setPublicCommentsEnabled(true);
322  $this->table->doUpdate();
323  $this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
324  }
325 
326  public function disableComments(): void
327  {
328  $this->table->setPublicCommentsEnabled(false);
329  $this->table->doUpdate();
330  $this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
331  }
332 
333  public function setAsDefault(): void
334  {
335  $object = ilObjectFactory::getInstanceByObjId($this->obj_id);
336  $order = 20;
337  foreach ($object->getTables() as $table) {
338  if ($table->getId() === $this->table->getId()) {
339  $table->setOrder(10);
340  } else {
341  $table->setOrder($order);
342  $order += 10;
343  }
344  $table->doUpdate();
345  }
346  $this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
347  }
348 
349  protected function checkAccess(): bool
350  {
351  $ref_id = $this->parent_object->getDataCollectionObject()->getRefId();
352 
353  return $this->table_id ? ilObjDataCollectionAccess::hasAccessToEditTable(
354  $ref_id,
355  $this->table_id
357  }
358 }
This describes commonalities between all forms.
Definition: Form.php:32
Help GUI class.
initForm(string $a_mode='create')
ILIAS UI Renderer $ui_renderer
ilDclTableListGUI: ilDclFieldListGUI, ilDclFieldEditGUI, ilDclTableViewGUI, ilDclTableEditGUI ...
getId()
Get table id.
ilDclTableListGUI $parent_object
ILIAS HTTP Services $http
ILIAS Refinery Factory $refinery
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setOrder(int $table_order)
ilGlobalTemplateInterface $tpl
$ref_id
Definition: ltiauth.php:65
static http()
Fetches the global http state from ILIAS.
save(string $a_mode='edit')
global $DIC
Definition: shib_login.php:22
static getTableCache(?int $table_id=null)
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:61
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
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)