ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilDclRecordListGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 public const GET_TABLE_ID = 'table_id';
24 public const GET_TABLEVIEW_ID = 'tableview_id';
25 public const GET_MODE = "mode";
26 public const MODE_VIEW = "view";
27 public const MODE_MANAGE = "manage";
28 public const CMD_LIST_RECORDS = 'listRecords';
29 public const CMD_SHOW = 'show';
30 public const CMD_CONFIRM_DELETE_RECORDS = 'confirmDeleteRecords';
31 public const CMD_CANCEL_DELETE = 'cancelDelete';
32 public const CMD_DELETE_RECORDS = 'deleteRecords';
33 public const CMD_SHOW_IMPORT_EXCEL = 'showImportExcel';
34
37 protected \ILIAS\UI\Factory $ui_factory;
38 protected \ILIAS\UI\Renderer $renderer;
39
43 protected string $mode = self::MODE_VIEW;
45 protected ?int $table_id;
46 protected int $obj_id;
48 protected ?int $tableview_id;
50
51 protected ilCtrl $ctrl;
53 protected ilLanguage $lng;
54 protected ilTabsGUI $tabs;
57 protected \ILIAS\ResourceStorage\Services $irss;
58 protected bool $filter_changed = false;
59
63 public function __construct(ilObjDataCollectionGUI $a_parent_obj, int $table_id, int $tableview_id)
64 {
65 global $DIC;
66
67 $this->ctrl = $DIC->ctrl();
68 $this->toolbar = $DIC->toolbar();
69 $this->lng = $DIC->language();
70 $this->tabs = $DIC->tabs();
71 $this->http = $DIC->http();
72 $this->refinery = $DIC->refinery();
73 $this->irss = $DIC->resourceStorage();
74 $this->access = $DIC->access();
75 $this->tpl = $DIC->ui()->mainTemplate();
76 $this->ui_factory = $DIC->ui()->factory();
77 $this->renderer = $DIC->ui()->renderer();
78
79 $this->table_id = $table_id;
80 $this->tableview_id = $tableview_id;
81
82 $this->obj_id = $a_parent_obj->getObject()->getId();
83 $this->parent_obj = $a_parent_obj;
84 $this->table_obj = ilDclCache::getTableCache($table_id);
85
86 $this->ctrl->setParameterByClass(ilDclRecordEditGUI::class, self::GET_TABLE_ID, $this->table_id);
87 $this->ctrl->setParameterByClass(ilDclRecordEditGUI::class, self::GET_TABLEVIEW_ID, $this->tableview_id);
88 $this->ctrl->setParameterByClass(ilDclDetailedViewGUI::class, self::GET_TABLE_ID, $this->table_id);
89 $this->ctrl->setParameterByClass(ilDclDetailedViewGUI::class, self::GET_TABLEVIEW_ID, $this->tableview_id);
90
91 $this->mode = self::MODE_VIEW;
92
93 if ($this->http->wrapper()->query()->has(self::GET_MODE)) {
94 $mode = $this->http->wrapper()->query()->retrieve(self::GET_MODE, $this->refinery->kindlyTo()->string());
95 if (in_array($mode, self::$available_modes, true)) {
96 $this->mode = $mode;
97 }
98 }
99 }
100
101 public function getRefId(): int
102 {
103 return $this->parent_obj->getRefId();
104 }
105
106 public function getObjId(): int
107 {
108 return $this->parent_obj->getObject()->getId();
109 }
110
115 public function executeCommand(): void
116 {
117 if (!ilObjDataCollectionAccess::hasAccessTo($this->getRefId(), $this->table_id, $this->tableview_id)) {
118 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
119 return;
120 }
121
122 $this->ctrl->saveParameter($this, self::GET_MODE);
123 $cmd = $this->ctrl->getCmd(self::CMD_SHOW);
124
125 // 'show' fills all filters with the predefined values from the tableview,
126 // whereas 'listRecords' handels the filters "normally", filling them from the POST-variable
127 switch ($cmd) {
128 case self::CMD_SHOW:
131 $this->setSubTabs($this->mode);
132 $this->listRecords();
133 break;
135 $this->confirmDeleteRecords();
136 break;
138 $this->deleteRecords();
139 break;
141 $this->tabs->setBackTarget($this->lng->txt('back'), $this->ctrl->getLinkTarget($this));
142 $this->$cmd();
143 break;
144
145 default:
146 $this->$cmd();
147 break;
148 }
149 }
150
151 public function listRecords(): void
152 {
153 $list = $this->getRecordListTableGUI();
154
155 $this->createSwitchers();
156
157 $this->ctrl->setParameter($this, 'table_id', $this->table_id);
158 $this->ctrl->setParameter($this, 'tableview_id', $this->tableview_id);
159
160 $permission_to_add_or_import = ilObjDataCollectionAccess::hasPermissionToAddRecord(
161 $this->parent_obj->getRefId(),
162 $this->table_id
163 ) && $this->table_obj->hasCustomFields();
164 if ($permission_to_add_or_import) {
165 $this->ctrl->setParameterByClass(ilDclRecordEditGUI::class, "record_id", null);
166
167 $add_new = $this->ui_factory->button()->primary(
168 $this->lng->txt("dcl_add_new_record"),
169 $this->ctrl->getFormActionByClass(ilDclRecordEditGUI::class, "create")
170 );
171 $this->toolbar->addStickyItem($add_new);
172 }
173
174 if ($permission_to_add_or_import && $this->table_obj->getImportEnabled()) {
175 $this->ctrl->setParameterByClass(ilDclRecordEditGUI::class, "record_id", null);
176
177 $import_button = $this->ui_factory->button()->standard(
178 $this->lng->txt("dcl_import_records .xls"),
179 $this->ctrl->getFormActionByClass(ilDclRecordListGUI::class, self::CMD_SHOW_IMPORT_EXCEL)
180 );
181 $this->toolbar->addComponent($import_button);
182 }
183
184 if (count($this->table_obj->getRecordFields()) == 0) {
185 $message = $this->lng->txt("dcl_no_fields_yet") . " "
187 $this->parent_obj->getRefId(),
188 $this->table_id
189 ) ? $this->lng->txt("dcl_create_fields") : "");
190 $this->tpl->setOnScreenMessage('info', $message, true);
191 }
192
193 $target = '';
194 if ($this->http->wrapper()->query()->has('table_id')) {
195 $target .= $this->http->wrapper()->query()->retrieve('table_id', $this->refinery->to()->string());
196 }
197 if ($this->http->wrapper()->query()->has('tableview_id')) {
198 $target .= '_' . $this->http->wrapper()->query()->retrieve('tableview_id', $this->refinery->to()->string());
199 }
200
201 $this->tpl->setPermanentLink("dcl", $this->parent_obj->getRefId(), $target);
202
203 if ($desc = $this->table_obj->getDescription()) {
204 $desc = $this->refinery->string()->markdown()->toHTML()->transform($desc);
205 $desc = '<div class="ilDclTableDescription">' . $desc . '</div>';
206 }
207 $this->tpl->setContent($desc . $list->getHTML());
208 }
209
210 public function showImportExcel(?ilPropertyFormGUI $form = null): void
211 {
212 if (!$form) {
213 $form = $this->initImportForm();
214 }
215 $this->tpl->setContent($form->getHTML());
216 }
217
222 {
223 $form = new ilPropertyFormGUI();
224
225 $item = new ilCustomInputGUI();
226 $item->setHtml($this->lng->txt('dcl_file_format_description'));
227 $item->setTitle("Info");
228 $form->addItem($item);
229
230 $file = new ilFileInputGUI($this->lng->txt("import_file"), "import_file");
231 $file->setSuffixes(['xlsx']);
232 $file->setRequired(true);
233 $form->addItem($file);
234
235 $cb = new ilCheckboxInputGUI($this->lng->txt("dcl_simulate_import"), "simulate");
236 $cb->setInfo($this->lng->txt("dcl_simulate_info"));
237 $form->addItem($cb);
238
239 $form->addCommandButton("importExcel", $this->lng->txt("import"));
240
241 return $form;
242 }
243
247 public function importExcel(): void
248 {
250 $this->parent_obj->getRefId(),
251 $this->table_id
252 )) || !$this->table_obj->getImportEnabled()) {
253 throw new ilDclException($this->lng->txt("access_denied"));
254 }
255 $form = $this->initImportForm();
256 if ($form->checkInput()) {
257 $file = $form->getInput("import_file");
258 $file_location = $file["tmp_name"];
259 $simulate = $form->getInput("simulate");
260 $this->importRecords($file_location, (bool) $simulate);
261 } else {
262 $this->showImportExcel($form);
263 }
264 }
265
269 private function importRecords(string $file, bool $simulate = false): void
270 {
271 $importer = new ilDclContentImporter($this->parent_obj->object->getRefId(), $this->table_id);
272 $result = $importer->import($file, $simulate);
273
274 $this->endImport($result['line'], $result['warnings']);
275 }
276
281 public function endImport(int $i, array $warnings): void
282 {
283 $output = new ilTemplate("tpl.dcl_import_terminated.html", true, true, "components/ILIAS/DataCollection");
284 $output->setVariable("IMPORT_TERMINATED", $this->lng->txt("dcl_import_terminated") . ": " . $i);
285 foreach ($warnings as $warning) {
286 $output->setCurrentBlock("warnings");
287 $output->setVariable("WARNING", $warning);
288 $output->parseCurrentBlock();
289 }
290
291 $output->setVariable("BACK_LINK", $this->ctrl->getLinkTargetByClass(ilDclRecordListGUI::class, "listRecords"));
292 $output->setVariable("BACK", $this->lng->txt("back"));
293 $this->tpl->setContent($output->get());
294 }
295
299 protected function applyFilter(): void
300 {
301 $table = new ilDclRecordListTableGUI($this, "listRecords", $this->table_obj, $this->tableview_id);
302 $table->initFilter();
303 $table->resetOffset();
304 $table->writeFilterToSession();
305 $this->filter_changed = true;
306 $this->listRecords();
307 }
308
312 protected function resetFilter(): void
313 {
314 $table = new ilDclRecordListTableGUI($this, "show", $this->table_obj, $this->tableview_id);
315 $table->initFilter();
316 $table->resetOffset();
317 $table->resetFilter();
318 $this->filter_changed = true;
319 $this->listRecords();
320 }
321
325 public function sendFile(): void
326 {
327 $hasIlFileHash = $this->http->wrapper()->query()->has('ilfilehash');
328 //need read access to receive file
329 if ($this->access->checkAccess('read', "", $this->parent_obj->getRefId())) {
330 // deliver temp-files
331 if ($hasIlFileHash) {
332 $filehash = $this->http->wrapper()->query()->retrieve(
333 'ilfilehash',
334 $this->refinery->kindlyTo()->string()
335 );
336 $field_id = $this->http->wrapper()->query()->retrieve('field_id', $this->refinery->kindlyTo()->int());
338
339 $filepath = $_FILES["field_" . $field_id]['tmp_name'];
340 $filetitle = $_FILES["field_" . $field_id]['name'];
341
342 ilFileDelivery::deliverFileLegacy($filepath, $filetitle);
343 } else {
344 $rec_id = $this->http->wrapper()
345 ->query()
346 ->retrieve('record_id', $this->refinery->kindlyTo()->int());
347
348 $record = ilDclCache::getRecordCache($rec_id);
349 if (!$this->recordBelongsToCollection($record)) {
350 return;
351 }
352
353 $field_id = $this->http->wrapper()
354 ->query()
355 ->retrieve('field_id', $this->refinery->kindlyTo()->string());
356
357
358
359 // Find the current revision
360 $rid_string = $record->getRecordFieldValue($field_id);
361 $identification = $this->irss->manage()->find($rid_string);
362 if ($identification === null) {
363 return;
364 }
365 $current_revision = $this->irss->manage()->getCurrentRevision($identification);
366
367 // Download the File
368 $this->irss->consume()
369 ->download($identification)
370 ->overrideFileName($current_revision->getTitle())
371 ->run();
372 }
373 }
374 }
375
379 public function confirmDeleteRecords(): void
380 {
381 $this->tabs->clearTargets();
382
383 $conf = new ilConfirmationGUI();
384 $conf->setFormAction($this->ctrl->getFormAction($this));
385 $conf->setHeaderText($this->lng->txt('dcl_confirm_delete_records'));
386
387 $has_record_ids = $this->http->wrapper()->post()->has('record_ids');
388 $record_ids = [];
389 if ($has_record_ids) {
390 $record_ids = $this->http->wrapper()->post()->retrieve(
391 'record_ids',
392 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
393 );
394 }
395 $all_fields = $this->table_obj->getRecordFields();
396 foreach ($record_ids as $record_id) {
398 $record = ilDclCache::getRecordCache($record_id);
399 $record_data = "";
400 foreach ($all_fields as $field) {
401 $field_record = ilDclCache::getRecordFieldCache($record, $field);
402
403 $record_representation = ilDclCache::getRecordRepresentation($field_record);
404 if ($record_representation->getConfirmationHTML() != false) {
405 $record_data .= $field->getTitle() . ": " . $record_representation->getConfirmationHTML() . "<br />";
406 }
407 }
408 $conf->addItem('record_ids[]', (string) $record->getId(), $record_data);
409 }
410 $conf->addHiddenItem('table_id', (string) $this->table_id);
411 $conf->setConfirm($this->lng->txt('dcl_delete_records'), self::CMD_DELETE_RECORDS);
412 $conf->setCancel($this->lng->txt('cancel'), self::CMD_CANCEL_DELETE);
413 $this->tpl->setContent($conf->getHTML());
414 }
415
419 public function deleteRecords(): void
420 {
421 $has_record_ids = $this->http->wrapper()->post()->has('record_ids');
422 $record_ids = [];
423 if ($has_record_ids) {
424 $record_ids = $this->http->wrapper()->post()->retrieve(
425 'record_ids',
426 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
427 );
428 }
429
430 // Invoke deletion
431 $n_skipped = 0;
432 foreach ($record_ids as $record_id) {
434 $record = ilDclCache::getRecordCache($record_id);
435 $ref_id = $this->parent_obj->getRefId();
436
437 if ($record->hasPermissionToDelete($ref_id)) {
438 $record->doDelete();
439 } else {
440 $n_skipped++;
441 }
442 }
443
444 $n_deleted = (count($record_ids) - $n_skipped);
445 if ($n_deleted) {
446 $message = sprintf(
447 $this->lng->txt('dcl_deleted_records'),
448 $n_deleted
449 );
450 $this->tpl->setOnScreenMessage('success', $message, true);
451 }
452 if ($n_skipped) {
453 $message = sprintf(
454 $this->lng->txt('dcl_skipped_delete_records'),
455 $n_skipped
456 );
457 $this->tpl->setOnScreenMessage('info', $message, true);
458 }
459 $this->ctrl->redirect($this, self::CMD_LIST_RECORDS);
460 }
461
462 private function recordBelongsToCollection(ilDclBaseRecordModel $record): bool
463 {
464 $table = $record->getTable();
465 $obj_id = $this->parent_obj->object->getId();
466 $obj_id_rec = $table->getCollectionObject()->getId();
467
468 return $obj_id == $obj_id_rec;
469 }
470
471 protected function setSubTabs(string $active_mode = self::GET_MODE): void
472 {
473 $this->ctrl->setParameter($this, self::GET_MODE, self::MODE_VIEW);
474 if ($this->http->wrapper()->query()->has(self::GET_TABLEVIEW_ID)) {
475 $this->ctrl->setParameter(
476 $this,
477 self::GET_TABLEVIEW_ID,
478 $this->http->wrapper()->query()->retrieve(self::GET_TABLEVIEW_ID, $this->refinery->kindlyTo()->int())
479 );
480 }
481 $this->tabs->addSubTab(
482 self::MODE_VIEW,
483 $this->lng->txt('view'),
484 $this->ctrl->getLinkTarget($this, self::CMD_LIST_RECORDS)
485 );
486
487 $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
488 if ($this->table_obj->hasPermissionToDeleteRecords($ref_id)) {
489 $this->ctrl->setParameter($this, self::GET_MODE, self::MODE_MANAGE);
490 $this->tabs->addSubTab(
491 self::MODE_MANAGE,
492 $this->lng->txt('dcl_manage'),
493 $this->ctrl->getLinkTarget($this, self::CMD_LIST_RECORDS)
494 );
495 }
496 $this->tabs->activateSubTab($active_mode);
497 $this->ctrl->clearParameters($this);
498 }
499
501 {
503
504 $list = new ilDclRecordListTableGUI($this, "listRecords", $table_obj, $this->tableview_id, $this->mode);
505 $list->initFilter();
506
507 $list->setExternalSegmentation(true);
508 $list->setExternalSorting(true);
509 $list->determineOffsetAndOrder();
510
511 if ($this->filter_changed) {
512 $list->resetOffset();
513 }
514
515 $limit = $list->getLimit();
516 $offset = $list->getOffset();
517
519 (string) $this->getRefId(),
520 $list->getOrderField(),
521 $list->getOrderDirection(),
522 $limit,
523 $offset,
524 $list->getFilter()
525 );
526 $records = $data['records'];
527 $total = $data['total'];
528
529 $list->setMaxCount($total);
530 $list->setRecordData($records);
531
532 $list->determineOffsetAndOrder();
533 $list->determineLimit();
534
535 return $list;
536 }
537
538 protected function createSwitchers(): void
539 {
540 if (ilObjDataCollectionAccess::hasWriteAccess($this->parent_obj->getRefId())) {
541 $tables = $this->parent_obj->object->getTables();
542 } else {
543 $tables = $this->parent_obj->object->getVisibleTables();
544 }
545
546 $switcher = new ilDclSwitcher($this->toolbar, $this->ui_factory, $this->ctrl, $this->lng);
547 $switcher->addTableSwitcherToToolbar(
548 $tables,
549 self::class,
550 self::CMD_SHOW,
551 $this->getTableId()
552 );
553
554 $switcher->addViewSwitcherToToolbar(
555 $this->table_obj->getVisibleTableViews(),
556 $this->getTableId(),
557 self::class,
558 self::CMD_SHOW,
559 $this->getTableviewId()
560 );
561 }
562
563 public function getTableId(): int
564 {
565 return $this->table_id;
566 }
567
568 public function getTableviewId(): int
569 {
570 return $this->tableview_id;
571 }
572}
renderer()
Builds data types.
Definition: Factory.php:36
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...
Class ilCtrl provides processing control methods.
This class represents a custom property in a property form.
static getRecordCache(?int $record_id)
static getRecordFieldCache(object $record, object $field)
static getTableCache(?int $table_id=null)
static getRecordRepresentation(ilDclBaseRecordFieldModel $record_field)
Returns a record representation.
static rebuildTempFileByHash(string $hash)
ilObjDataCollectionGUI $parent_obj
endImport(int $i, array $warnings)
End import.
ILIAS HTTP Services $http
importExcel()
Import Data from Excel sheet.
showImportExcel(?ilPropertyFormGUI $form=null)
__construct(ilObjDataCollectionGUI $a_parent_obj, int $table_id, int $tableview_id)
ILIAS Refinery Factory $refinery
recordBelongsToCollection(ilDclBaseRecordModel $record)
string $mode
Stores current mode active.
ILIAS ResourceStorage Services $irss
setSubTabs(string $active_mode=self::GET_MODE)
ilGlobalTemplateInterface $tpl
importRecords(string $file, bool $simulate=false)
Import records from Excel file.
getPartialRecords(string $ref_id, string $sort, string $direction, ?int $limit, int $offset, array $filter=[])
Return only the needed subset of record objects for the table, according to sorting,...
static deliverFileLegacy(string $a_file, ?string $a_filename=null, ?string $a_mime=null, ?bool $isInline=false, ?bool $removeAfterDelivery=false, ?bool $a_exit_after=true)
This class represents a file property in a property form.
language handling
static hasAccessToFields(int $ref_id, int $table_id)
static hasWriteAccess(int $ref, ?int $user_id=0)
static hasPermissionToAddRecord(int $ref_id, int $table_id)
@ilCtrl_Calls ilObjDataCollectionGUI: ilInfoScreenGUI, ilNoteGUI, ilCommonActionDispatcherGUI @ilCtrl...
This class represents a property form user interface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
special template class to simplify handling of ITX/PEAR
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
$ref_id
Definition: ltiauth.php:66
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26