ILIAS  release_8 Revision v8.24
class.ilExportGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21use ILIAS\Refinery\Factory as Factory;
22use ILIAS\HTTP\Services as Services;
23
30{
31 protected Factory $refinery;
32 protected Services $http;
33 protected array $formats = array();
34 protected array $custom_columns = array();
35 protected array $custom_multi_commands = array();
36
37 private object $parent_gui;
38 protected ilObject $obj;
39 protected ilLanguage $lng;
46 protected ilTree $tree;
47
48
49 public function __construct(object $a_parent_gui, ?ilObject $a_main_obj = null)
50 {
51 global $DIC;
52
53 $this->lng = $DIC->language();
54 $this->lng->loadLanguageModule("exp");
55
56 $this->http = $DIC->http();
57 $this->refinery = $DIC->refinery();
58
59 $this->tpl = $DIC->ui()->mainTemplate();
60 $this->ctrl = $DIC->ctrl();
61 $this->access = $DIC->access();
62 $this->error = $DIC['ilErr'];
63 $this->toolbar = $DIC->toolbar();
64 $this->parent_gui = $a_parent_gui;
65 $this->objDefinition = $DIC['objDefinition'];
66 $this->tree = $DIC->repositoryTree();
67 if ($a_main_obj == null) {
68 $this->obj = $a_parent_gui->getObject();
69 } else {
70 $this->obj = $a_main_obj;
71 }
72 }
73
74 protected function initFileIdentifierFromQuery(): string
75 {
76 if ($this->http->wrapper()->query()->has('file')) {
77 return $this->http->wrapper()->query()->retrieve(
78 'file',
79 $this->refinery->kindlyTo()->string()
80 );
81 }
82 return '';
83 }
84
85 protected function initFileIdentifiersFromPost(): array
86 {
87 if ($this->http->wrapper()->post()->has('file')) {
88 return $this->http->wrapper()->post()->retrieve(
89 'file',
90 $this->refinery->kindlyTo()->listOf(
91 $this->refinery->kindlyTo()->string()
92 )
93 );
94 }
95 return [];
96 }
97
98 protected function initFormatFromPost(): string
99 {
100 if ($this->http->wrapper()->post()->has('format')) {
101 return $this->http->wrapper()->post()->retrieve(
102 'format',
103 $this->refinery->kindlyTo()->string()
104 );
105 }
106 return '';
107 }
108
109 protected function initExportOptionsFromPost(): array
110 {
111 $options = [];
112 if ($this->http->wrapper()->post()->has('cp_options')) {
113 $custom_transformer = $this->refinery->custom()->transformation(
114 function ($array) {
115 return $array;
116 }
117 );
118 $options = $this->http->wrapper()->post()->retrieve(
119 'cp_options',
120 $custom_transformer
121 );
122 }
123 return $options;
124 }
125
126
128 {
129 return new ilExportTableGUI($this, "listExportFiles", $this->obj);
130 }
131
132 protected function getParentGUI(): object
133 {
134 return $this->parent_gui;
135 }
136
137 public function addFormat(
138 string $a_key,
139 string $a_txt = "",
140 object $a_call_obj = null,
141 string $a_call_func = ""
142 ): void {
143 if ($a_txt == "") {
144 $a_txt = $this->lng->txt("exp_" . $a_key);
145 }
146 $this->formats[] = array(
147 "key" => $a_key,
148 "txt" => $a_txt,
149 "call_obj" => $a_call_obj,
150 "call_func" => $a_call_func
151 );
152 }
153
154 public function getFormats(): array
155 {
156 return $this->formats;
157 }
158
159 public function addCustomColumn(string $a_txt, object $a_obj, string $a_func): void
160 {
161 $this->custom_columns[] = array("txt" => $a_txt,
162 "obj" => $a_obj,
163 "func" => $a_func
164 );
165 }
166
167 public function addCustomMultiCommand(string $a_txt, object $a_obj, string $a_func): void
168 {
169 $this->custom_multi_commands[] = array("txt" => $a_txt,
170 "obj" => $a_obj,
171 "func" => $a_func
172 );
173 }
174
175 public function getCustomMultiCommands(): array
176 {
177 return $this->custom_multi_commands;
178 }
179
180 public function getCustomColumns(): array
181 {
182 return $this->custom_columns;
183 }
184
185 public function executeCommand(): void
186 {
187 // this should work (at least) for repository objects
188 if (method_exists($this->obj, 'getRefId') and $this->obj->getRefId()) {
189 if (!$this->access->checkAccess('write', '', $this->obj->getRefId())) {
190 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->WARNING);
191 }
192
193 // check export activation of container
194 $exp_limit = new ilExportLimitation();
195 if ($this->objDefinition->isContainer(ilObject::_lookupType($this->obj->getRefId(), true)) &&
196 $exp_limit->getLimitationMode() == ilExportLimitation::SET_EXPORT_DISABLED) {
197 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("exp_error_disabled"));
198 return;
199 }
200 }
201
202 $cmd = $this->ctrl->getCmd("listExportFiles");
203
204 switch ($cmd) {
205 case "listExportFiles":
206 $this->$cmd();
207 break;
208
209 default:
210 if (substr($cmd, 0, 7) == "create_") {
211 $this->createExportFile();
212 } elseif (substr($cmd, 0, 6) == "multi_") { // custom multi command
213 $this->handleCustomMultiCommand();
214 } else {
215 $this->$cmd();
216 }
217 break;
218 }
219 }
220
221 public function listExportFiles(): void
222 {
223 $button = ilSubmitButton::getInstance();
224
225 // creation buttons
226 $this->toolbar->setFormAction($this->ctrl->getFormAction($this));
227 if (count($this->getFormats()) > 1) {
228 // type selection
229 $options = [];
230 foreach ($this->getFormats() as $f) {
231 $options[$f["key"]] = $f["txt"];
232 }
233 $si = new ilSelectInputGUI($this->lng->txt("type"), "format");
234 $si->setOptions($options);
235 $this->toolbar->addInputItem($si, true);
236
237 $button->setCaption("exp_create_file");
238 $button->setCommand("createExportFile");
239 } else {
240 $format = $this->getFormats();
241 $format = $format[0];
242
243 $button->setCaption($this->lng->txt("exp_create_file") . " (" . $format["txt"] . ")", false);
244 $button->setCommand("create_" . $format["key"]);
245 }
246
247 $this->toolbar->addButtonInstance($button);
248
249 $table = $this->buildExportTableGUI();
250 $table->setSelectAllCheckbox("file");
251 foreach ($this->getCustomColumns() as $c) {
252 $table->addCustomColumn($c["txt"], $c["obj"], $c["func"]);
253 }
254 foreach ($this->getCustomMultiCommands() as $c) {
255 $table->addCustomMultiCommand($c["txt"], "multi_" . $c["func"]);
256 }
257 $this->tpl->setContent($table->getHTML());
258 }
259
260 public function createExportFile(): void
261 {
262 if ($this->ctrl->getCmd() == "createExportFile") {
263 $format = $this->initFormatFromPost();
264 } else {
265 $format = substr($this->ctrl->getCmd(), 7);
266 }
267 foreach ($this->getFormats() as $f) {
268 if ($f["key"] == $format) {
269 if (is_object($f["call_obj"])) {
270 $f["call_obj"]->{$f["call_func"]}();
271 } elseif ($this->getParentGUI() instanceof ilContainerGUI) {
272 $this->showItemSelection();
273 return;
274 } elseif ($format == "xml") { // standard procedure
275 $exp = new ilExport();
276 $exp->exportObject($this->obj->getType(), $this->obj->getId());
277 }
278 }
279 }
280
281 $this->tpl->setOnScreenMessage('success', $this->lng->txt("exp_file_created"), true);
282 $this->ctrl->redirect($this, "listExportFiles");
283 }
284
288 public function confirmDeletion(): void
289 {
290 $files = $this->initFileIdentifiersFromPost();
291 if (!count($files)) {
292 $this->tpl->setOnScreenMessage('info', $this->lng->txt("no_checkbox"), true);
293 $this->ctrl->redirect($this, "listExportFiles");
294 } else {
295 $cgui = new ilConfirmationGUI();
296 $cgui->setFormAction($this->ctrl->getFormAction($this));
297 $cgui->setHeaderText($this->lng->txt("exp_really_delete"));
298 $cgui->setCancel($this->lng->txt("cancel"), "listExportFiles");
299 $cgui->setConfirm($this->lng->txt("delete"), "delete");
300
301 foreach ($files as $i) {
302 if (strpos($i, ':') !== false) {
303 $iarr = explode(":", $i);
304 $filename = $iarr[1];
305 } else {
306 $filename = $i;
307 }
308 $cgui->addItem("file[]", $i, $filename);
309 }
310 $this->tpl->setContent($cgui->getHTML());
311 }
312 }
313
314 public function delete(): void
315 {
316 $files = $this->initFileIdentifiersFromPost();
317 foreach ($files as $file) {
318 $file = explode(":", $file);
319
320 $file[1] = basename($file[1]);
321
322 $export_dir = ilExport::_getExportDirectory(
323 $this->obj->getId(),
324 str_replace("..", "", $file[0]),
325 $this->obj->getType()
326 );
327
328 $exp_file = $export_dir . "/" . str_replace("..", "", $file[1]);
329 $exp_dir = $export_dir . "/" . substr($file[1], 0, strlen($file[1]) - 4);
330 if (is_file($exp_file)) {
331 unlink($exp_file);
332 }
333 if (is_dir($exp_dir)) {
334 ilFileUtils::delDir($exp_dir);
335 }
336
337 // delete entry in database
338 $info = new ilExportFileInfo($this->obj->getId(), $file[0], $file[1]);
339 $info->delete();
340 }
341 if (count($files) > 0) {
342 $this->tpl->setOnScreenMessage('success', $this->lng->txt('export_files_deleted'), true);
343 }
344 $this->ctrl->redirect($this, "listExportFiles");
345 }
346
350 public function download(): void
351 {
352 $file = $this->initFileIdentifierFromQuery();
353 if (!$file) {
354 $this->ctrl->redirect($this, "listExportFiles");
355 }
356
357 $file = explode(":", trim($file));
358 $export_dir = ilExport::_getExportDirectory(
359 $this->obj->getId(),
360 str_replace("..", "", $file[0]),
361 $this->obj->getType()
362 );
363
364 $file[1] = basename($file[1]);
365
367 $export_dir . "/" . $file[1],
368 $file[1]
369 );
370 }
371
372 public function handleCustomMultiCommand(): void
373 {
374 $cmd = substr($this->ctrl->getCmd(), 6);
375 foreach ($this->getCustomMultiCommands() as $c) {
376 if ($c["func"] == $cmd) {
377 $c["obj"]->{$c["func"]}($this->initFileIdentifiersFromPost());
378 }
379 }
380 }
381
385 protected function showItemSelection(): void
386 {
387 $this->tpl->addJavaScript('./Services/CopyWizard/js/ilContainer.js');
388 $this->tpl->setVariable('BODY_ATTRIBUTES', 'onload="ilDisableChilds(\'cmd\');"');
389
390 $table = new ilExportSelectionTableGUI($this, 'listExportFiles');
391 $table->parseContainer($this->getParentGUI()->getObject()->getRefId());
392 $this->tpl->setContent($table->getHTML());
393 }
394
395 protected function saveItemSelection(): void
396 {
398 $eo->addOption(ilExportOptions::KEY_ROOT, 0, 0, $this->obj->getId());
399
400 $cp_options = $this->initExportOptionsFromPost();
401
402 // check export limitation
403 $exp_limit = new ilExportLimitation();
404 try {
405 $exp_limit->checkLimitation(
406 $this->getParentGUI()->getObject()->getRefId(),
407 $cp_options
408 );
409 } catch (Exception $e) {
410 $this->tpl->setOnScreenMessage('failure', $e->getMessage());
411 $this->showItemSelection();
412 return;
413 }
414
415 $items_selected = false;
416 foreach ($this->tree->getSubTree($root = $this->tree->getNodeData($this->getParentGUI()->getObject()->getRefId())) as $node) {
417 if ($node['type'] === 'rolf') {
418 continue;
419 }
420 if ($node['ref_id'] == $this->getParentGUI()->getObject()->getRefId()) {
421 $eo->addOption(
423 (int) $node['ref_id'],
424 (int) $node['obj_id'],
426 );
427 continue;
428 }
429 // no export available or no access
430 if (!$this->objDefinition->allowExport($node['type']) || !$this->access->checkAccess(
431 'write',
432 '',
433 (int) $node['ref_id']
434 )) {
435 $eo->addOption(
437 (int) $node['ref_id'],
438 (int) $node['obj_id'],
440 );
441 continue;
442 }
443
444 $mode = $cp_options[$node['ref_id']]['type'] ?? ilExportOptions::EXPORT_OMIT;
445 $eo->addOption(
447 (int) $node['ref_id'],
448 (int) $node['obj_id'],
449 $mode
450 );
451 if ($mode != ilExportOptions::EXPORT_OMIT) {
452 $items_selected = true;
453 }
454 }
455
456 if ($items_selected) {
457 // TODO: move this to background soap
458 $eo->read();
459 $exp = new ilExport();
460 foreach ($eo->getSubitemsForCreation($this->obj->getRefId()) as $ref_id) {
462 $type = ilObject::_lookupType($obj_id);
463 $exp->exportObject($type, $obj_id);
464 }
465 // Fixme: there is a naming conflict between the container settings xml and the container subitem xml.
466 sleep(1);
467 // Export container
468 $cexp = new ilExportContainer($eo);
469 $cexp->exportObject($this->obj->getType(), $this->obj->getId());
470 } else {
471 $exp = new ilExport();
472 $exp->exportObject($this->obj->getType(), $this->obj->getId());
473 }
474
475 // Delete export options
476 $eo->delete();
477
478 $this->tpl->setOnScreenMessage('success', $this->lng->txt('export_created'), true);
479 $this->ctrl->redirect($this, "listExportFiles");
480 }
481}
$filename
Definition: buildRTE.php:78
Builds data types.
Definition: Factory.php:21
Class Services.
Definition: Services.php:38
error(string $a_errmsg)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilContainerGUI This is a base GUI class for all container objects in ILIAS: root folder,...
Error Handling & global info handling uses PEAR error class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilCtrlInterface $ctrl
array $custom_multi_commands
ilToolbarGUI $toolbar
confirmDeletion()
Confirm file deletion.
ilAccessHandler $access
addCustomColumn(string $a_txt, object $a_obj, string $a_func)
showItemSelection()
Show container item selection table.
addFormat(string $a_key, string $a_txt="", object $a_call_obj=null, string $a_call_func="")
download()
Download file.
ilLanguage $lng
__construct(object $a_parent_gui, ?ilObject $a_main_obj=null)
addCustomMultiCommand(string $a_txt, object $a_obj, string $a_func)
ilObjectDefinition $objDefinition
ilGlobalTemplateInterface $tpl
ilErrorHandling $error
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static newInstance(int $a_export_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getExportDirectory(int $a_obj_id, string $a_type="xml", string $a_obj_type="", string $a_entity="")
Get export directory for an repository object.
static deliverFileLegacy(string $a_file, ?string $a_filename=null, ?string $a_mime=null, ?bool $isInline=false, ?bool $removeAfterDelivery=false, ?bool $a_exit_after=true)
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
language handling
parses the objects.xml it handles the xml-description of all ilias objects
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupType(int $id, bool $reference=false)
static _lookupObjId(int $ref_id)
This class represents a selection list property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$c
Definition: cli.php:38
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
$formats
Definition: date.php:77
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ref_id
Definition: ltiauth.php:67
$format
Definition: metadata.php:235
$i
Definition: metadata.php:41
static http()
Fetches the global http state from ILIAS.
$type