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