ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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 initForm(bool $create = true): Form
105  {
106  $f = $this->ui_factory->input()->field();
107  $inputs = [];
108 
109  $edit = [];
110 
111  $edit['title'] = $f->text($this->lng->txt('title'))->withRequired(true);
112  $edit['description'] = $f->markdown(new ilUIMarkdownPreviewGUI(), $this->lng->txt('additional_info'));
113  $edit['visible'] = $this->checkbox('visible');
114  $inputs['edit'] = $f->section($edit, $this->lng->txt('general_settings'));
115 
116  $table = [];
117  if (!$create) {
118  $options = [];
119  foreach ($this->table->getFields() as $field) {
120  if ($field->getId() !== 'comments' && $field->getRecordQuerySortObject() !== null) {
121  $options[$field->getId()] = $field->getTitle();
122  }
123  }
124  $table['default_sort_field'] = $f->select(
125  $this->lng->txt('dcl_default_sort_field'),
126  $options
127  );
128 
129  $table['default_sort_field_order'] = $f->select(
130  $this->lng->txt('dcl_default_sort_field_order'),
131  ['asc' => $this->lng->txt('dcl_asc'), 'desc' => $this->lng->txt('dcl_desc')],
132  $this->lng->txt('dcl_default_sort_field_order_desc')
133  );
134  }
135  $table['export_enabled'] = $this->checkbox('export_enabled');
136  $table['import_enabled'] = $this->checkbox('import_enabled');
137  $table['comments_enabled'] = $this->checkbox('comments');
138  $inputs['table'] = $f->section($table, $this->lng->txt('dcl_table_settings'));
139 
140  $record = [];
141  $record['add_perm'] = $f->optionalGroup(
142  ['save_confirmation' => $this->checkbox('save_confirmation')],
143  $this->lng->txt('dcl_add_perm'),
144  $this->lng->txt('dcl_add_perm_desc')
145  )->withValue(['save_confirmation' => false]);
146  $record['edit_perm'] = $f->radio($this->lng->txt('dcl_edit_perm'))
147  ->withOption('all', $this->lng->txt('dcl_all_entries'))
148  ->withOption('own', $this->lng->txt('dcl_own_entries'))
149  ->withOption('none', $this->lng->txt('dcl_no_entries'))
150  ->withValue('own');
151  $record['delete_perm'] = $f->radio($this->lng->txt('dcl_delete_perm'))
152  ->withOption('all', $this->lng->txt('dcl_all_entries'))
153  ->withOption('own', $this->lng->txt('dcl_own_entries'))
154  ->withOption('none', $this->lng->txt('dcl_no_entries'))
155  ->withValue('own');
156  $record['view_own_records_perm'] = $this->checkbox('view_own_records_perm');
157  $record['limited'] = $f->optionalGroup(
158  [
159  'limit_start' => $f->dateTime($this->lng->txt('dcl_limit_start'))->withUseTime(true),
160  'limit_end' => $f->dateTime($this->lng->txt('dcl_limit_end'))->withUseTime(true)
161  ],
162  $this->lng->txt('dcl_limited'),
163  $this->lng->txt('dcl_limited_desc')
164  )->withValue(null);
165  $inputs['record'] = $f->section($record, $this->lng->txt('dcl_record_settings'));
166 
167  if (!$create) {
168  $inputs = $this->setValues($inputs);
169  }
170 
171  $this->ctrl->setParameter($this, 'table_id', $this->table_id);
172  return $this->ui_factory->input()->container()->form()->standard(
173  $this->ctrl->getFormAction($this, $create ? 'save' : 'update'),
174  $inputs
175  );
176  }
177 
178  private function checkbox(string $label): Checkbox
179  {
180  return $this->ui_factory->input()->field()->checkbox(
181  $this->lng->txt('dcl_' . $label),
182  $this->lng->txt('dcl_' . $label . '_desc')
183  );
184  }
185 
186  protected function setValues(array $inputs): array
187  {
188  $inputs['edit'] = $inputs['edit']->withValue([
189  'title' => $this->table->getTitle(),
190  'description' => $this->table->getDescription(),
191  'visible' => $this->table->getIsVisible(),
192  ]);
193  $sort_field = $this->table->getDefaultSortField();
194  $inputs['table'] = $inputs['table']->withValue([
195  'default_sort_field' => in_array($sort_field, $this->table->getFieldIds()) ? $sort_field : '',
196  'default_sort_field_order' => $this->table->getDefaultSortFieldOrder(),
197  'export_enabled' => $this->table->getExportEnabled(),
198  'import_enabled' => $this->table->getImportEnabled(),
199  'comments_enabled' => $this->table->getPublicCommentsEnabled()
200  ]);
201  $inputs['record'] = $inputs['record']->withValue([
202  'add_perm' => $this->table->getAddPerm() ? ['save_confirmation' => $this->table->getSaveConfirmation()] : null,
203  'edit_perm' => $this->table->getEditPerm() ? ($this->table->getEditByOwner() ? 'own' : 'all') : 'none',
204  'delete_perm' => $this->table->getDeletePerm() ? ($this->table->getDeleteByOwner() ? 'own' : 'all') : 'none',
205  'view_own_records_perm' => $this->table->getViewOwnRecordsPerm(),
206  'limited' => $this->table->getLimited() ? ['limit_start' => $this->table->getLimitStart(), 'limit_end' => $this->table->getLimitEnd()] : null
207  ]);
208 
209  return $inputs;
210  }
211 
212  public function save(bool $create = true): void
213  {
214  if (!ilObjDataCollectionAccess::checkActionForObjId('write', $this->obj_id)) {
215  return;
216  }
217 
218  $form = $this->initForm($create)->withRequest($this->http->request());
219  $data = $form->getData();
220 
221  if ($data !== null) {
222  if ($create) {
223  $this->table = new ilDclTable();
224  }
225  foreach (ilObjectFactory::getInstanceByObjId($this->obj_id)->getTables() as $table) {
226  if ($table->getTitle() === $data['edit']['title'] && $table->getId() !== $this->table->getId()) {
227  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_FAILURE, $this->lng->txt('dcl_table_title_unique'));
228  $this->tpl->setContent($this->ui_renderer->render($form));
229  return;
230  }
231  }
232 
233  $this->table->setObjId($this->obj_id);
234  $this->table->setTitle($data['edit']['title']);
235  $this->table->setDescription($data['edit']['description']);
236  $this->table->setIsVisible($data['edit']['visible']);
237 
238  $this->table->setExportEnabled($data['table']['export_enabled']);
239  $this->table->setImportEnabled($data['table']['import_enabled']);
240  $this->table->setPublicCommentsEnabled($data['table']['comments_enabled']);
241 
242  $this->table->setAddPerm($data['record']['add_perm'] !== null);
243  $this->table->setSaveConfirmation($data['record']['add_perm']['save_confirmation'] ?? false);
244  $this->table->setEditPerm($data['record']['edit_perm'] !== 'none');
245  $this->table->setEditByOwner($data['record']['edit_perm'] === 'own');
246  $this->table->setDeletePerm($data['record']['delete_perm'] !== 'none');
247  $this->table->setDeleteByOwner($data['record']['delete_perm'] === 'own');
248  $this->table->setViewOwnRecordsPerm($data['record']['view_own_records_perm']);
249  $this->table->setLimited($data['record']['limited'] !== null);
250  if ($data['record']['limited']['limit_start'] ?? null !== null) {
251  $this->table->setLimitStart($data['record']['limited']['limit_start']->format('Y-m-d H:i:s'));
252  } else {
253  $this->table->setLimitStart('');
254  }
255  if ($data['record']['limited']['limit_end'] ?? null !== null) {
256  $this->table->setLimitEnd($data['record']['limited']['limit_end']->format('Y-m-d H:i:s'));
257  } else {
258  $this->table->setLimitEnd('');
259  }
260 
261  if ($create) {
262  $this->table->doCreate();
263  $message = 'dcl_msg_table_created';
264  } else {
265  $this->table->setDefaultSortField($data['table']['default_sort_field']);
266  $this->table->setDefaultSortFieldOrder($data['table']['default_sort_field_order']);
267  $this->table->doUpdate();
268  $message = 'dcl_msg_table_edited';
269  }
270  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt($message), true);
271  $this->ctrl->redirectByClass(ilDclTableEditGUI::class, 'edit');
272  } else {
273  $this->tpl->setContent($this->ui_renderer->render($form));
274  }
275  }
276 
277  public function confirmDelete(): void
278  {
279  $conf = new ilConfirmationGUI();
280  $conf->setFormAction($this->ctrl->getFormAction($this));
281  $conf->setHeaderText($this->lng->txt('dcl_confirm_delete_table'));
282 
283  $conf->addItem('table', (string) $this->table->getId(), $this->table->getTitle());
284 
285  $conf->setConfirm($this->lng->txt('delete'), 'delete');
286  $conf->setCancel($this->lng->txt('cancel'), 'cancelDelete');
287 
288  $this->tpl->setContent($conf->getHTML());
289  }
290 
291  public function cancelDelete(): void
292  {
293  $this->ctrl->redirectByClass("ilDclTableListGUI", "listTables");
294  }
295 
296  public function delete(): void
297  {
298  if (count($this->table->getCollectionObject()->getTables()) < 2) {
299  $this->tpl->setOnScreenMessage(
300  $this->tpl::MESSAGE_TYPE_FAILURE,
301  $this->lng->txt("dcl_cant_delete_last_table"),
302  true
303  );
304  $this->table->doDelete(true);
305  } else {
306  $this->table->doDelete();
307  }
308  $this->ctrl->clearParameterByClass("ilobjdatacollectiongui", "table_id");
309  $this->ctrl->redirectByClass("ildcltablelistgui", "listtables");
310  }
311 
312  public function setAsDefault(): void
313  {
314  $object = ilObjectFactory::getInstanceByObjId($this->obj_id);
315  $order = 20;
316  foreach ($object->getTables() as $table) {
317  if ($table->getId() === $this->table->getId()) {
318  $table->setOrder(10);
319  } else {
320  $table->setOrder($order);
321  $order += 10;
322  }
323  $table->doUpdate();
324  }
325  $this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
326  }
327 
328  protected function checkAccess(): bool
329  {
330  $ref_id = $this->parent_object->getDataCollectionObject()->getRefId();
331 
332  return $this->table_id ? ilObjDataCollectionAccess::hasAccessToEditTable(
333  $ref_id,
334  $this->table_id
336  }
337 }
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
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.
global $DIC
Definition: shib_login.php:26
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
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)
$message
Definition: xapiexit.php:31
static checkActionForObjId(string $action, int $obj_id)