ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilExportGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 use ILIAS\Export\ExportHandler\I\Consumer\Context\HandlerInterface as ilExportHandlerConsumerContextInterface;
25 use ILIAS\Export\ExportHandler\I\Consumer\ExportOption\CollectionInterface as ilExportHandlerConsumerExportOptionCollectionInterface;
26 use ILIAS\Export\ExportHandler\I\Consumer\ExportOption\HandlerInterface as ilExportHandlerConsumerExportOptionInterface;
27 use ILIAS\Export\ExportHandler\Factory as ilExportHandler;
31 
38 {
39  public const CMD_LIST_EXPORT_FILES = "listExportFiles";
40  public const CMD_EXPORT_XML = "createXmlExportFile";
41  protected const CMD_SAVE_ITEM_SELECTION = "saveItemSelection";
42  protected const CMD_EXPORT_OPTION_PREFIX = "exportOption";
43 
44  protected ilExportHandlerConsumerExportOptionCollectionInterface $export_options;
46  protected ilHTTPServices $http;
48  protected ilObjUser $il_user;
49  protected ilLanguage $lng;
50  protected ilObject $obj;
55 
58  protected ilTree $tree;
59  protected ilExportHandler $export_handler;
60  protected ilExportHandlerConsumerContextInterface $context;
62  protected object $parent_gui;
63  protected bool $public_access_enabled;
64 
65  public function __construct(
66  object $a_parent_gui,
67  ?ilObject $a_main_obj = null,
68  bool $public_access_enabled = true,
69  bool $default_export_option_enabled = true
70  ) {
71  global $DIC;
72  $this->ui_services = $DIC->ui();
73  $this->http = $DIC->http();
74  $this->refinery = $DIC->refinery();
75  $this->il_user = $DIC->user();
76  $this->lng = $DIC->language();
77  $this->lng->loadLanguageModule("exp");
78  $this->tpl = $DIC->ui()->mainTemplate();
79  $this->ctrl = $DIC->ctrl();
80  $this->access = $DIC->access();
81  $this->error = $DIC['ilErr'];
82  $this->toolbar = $DIC->toolbar();
83  $this->parent_gui = $a_parent_gui;
84  $this->obj_definition = $DIC['objDefinition'];
85  $this->tree = $DIC->repositoryTree();
86  $this->obj = $a_main_obj ?? $a_parent_gui->getObject();
87  $this->export_handler = new ilExportHandler();
88  $this->context = $this->export_handler->consumer()->context()->handler($this, $this->obj);
89  $this->export_options = $this->export_handler->consumer()->exportOption()->collection();
90  $this->data_factory = new ilDataFactory();
91  $this->public_access_enabled = $public_access_enabled;
92  $this->initExportOptions();
93  if ($default_export_option_enabled) {
94  $this->enableStandardXMLExport();
95  }
96  }
97 
98  final public function isPublicAccessEnabled(): bool
99  {
101  }
102 
103  public function executeCommand(): void
104  {
105  // this should work (at least) for repository objects
106  if (method_exists($this->obj, 'getRefId') and $this->obj->getRefId()) {
107  if (!$this->access->checkAccess('write', '', $this->obj->getRefId())) {
108  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->WARNING);
109  }
110  // check export activation of container
111  $exp_limit = new ilExportLimitation();
112  if ($this->obj_definition->isContainer(ilObject::_lookupType($this->obj->getRefId(), true)) &&
113  $exp_limit->getLimitationMode() == ilExportLimitation::SET_EXPORT_DISABLED) {
114  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("exp_error_disabled"));
115  return;
116  }
117  }
118  $cmd = $this->ctrl->getCmd(self::CMD_LIST_EXPORT_FILES);
119  if (str_starts_with($cmd, self::CMD_EXPORT_OPTION_PREFIX)) {
120  foreach ($this->export_options as $export_option) {
121  if ($cmd === $this->builtExportOptionCommand($export_option)) {
122  $export_option->onExportOptionSelected($this->context);
123  }
124  }
125  }
126  switch ($cmd) {
127  case self::CMD_EXPORT_XML:
128  $this->createXMLExportFile();
129  break;
130  case self::CMD_SAVE_ITEM_SELECTION:
131  $this->saveItemSelection();
132  break;
133  case self::CMD_LIST_EXPORT_FILES:
134  default:
135  $this->displayExportFiles();
136  break;
137  }
138  }
139 
143  public function addFormat(): void
144  {
145 
146  }
147 
148  public function listExportFiles(): void
149  {
150  $this->displayExportFiles();
151  }
152 
156  public function getFormats(): array
157  {
158  return [];
159  }
160 
161  final protected function initExportOptionsFromPost(): array
162  {
163  $options = [];
164  if ($this->http->wrapper()->post()->has('cp_options')) {
165  $custom_transformer = $this->refinery->custom()->transformation(
166  function ($array) {
167  return $array;
168  }
169  );
170  $options = $this->http->wrapper()->post()->retrieve(
171  'cp_options',
172  $custom_transformer
173  );
174  }
175  return $options;
176  }
177 
178  final protected function builtExportOptionCommand(ilExportHandlerConsumerExportOptionInterface $export_option): string
179  {
180  return self::CMD_EXPORT_OPTION_PREFIX . $export_option->getExportOptionId();
181  }
182 
183  final protected function enableStandardXMLExport(): void
184  {
185  $this->export_options = $this->export_options->withElement(new ilExportExportOptionXML());
186  }
187 
188  final protected function initExportOptions(): void
189  {
190  $export_options = $this->export_handler->consumer()->exportOption()->allExportOptions();
191  foreach ($export_options as $export_option) {
192  if (
193  in_array($this->obj->getType(), $export_option->getSupportedRepositoryObjectTypes()) and
194  $export_option->isObjectSupported($this->data_factory->objId($this->obj->getId()))
195  ) {
196  $this->export_options = $this->export_options->withElement($export_option);
197  }
198  }
199  }
200 
201  final protected function displayExportFiles(): void
202  {
203  if ($this->export_options->count() === 0) {
204  return;
205  }
206  $table = $this->export_handler->table()->handler()
207  ->withExportOptions($this->export_options)
208  ->withContext($this->context)
209  ->withPublicAccessEnabled($this->isPublicAccessEnabled());
210  $table->handleCommands();
211  $infos = [];
212  foreach ($this->export_options as $export_option) {
213  # Do not display export option if the label is missing.
214  if ($export_option->getLabel() === '') {
215  continue;
216  }
217  $infos[$export_option->getLabel()] = $this->ctrl->getLinkTarget($this, $this->builtExportOptionCommand($export_option));
218  }
219  if (count($infos) === 1) {
220  $this->toolbar->addComponent($this->ui_services->factory()->button()->standard(
221  sprintf($this->lng->txt("exp_export_single_option"), array_keys($infos)[0]),
222  array_values($infos)[0]
223  ));
224  }
225  if (count($infos) > 1) {
226  $links = [];
227  foreach ($infos as $label => $link) {
228  $links[] = $this->ui_services->factory()->link()->standard($label, $link);
229  }
230  $this->toolbar->addComponent(
231  $this->ui_services->factory()->dropdown()->standard($links)
232  ->withLabel($this->lng->txt("exp_export_dropdown"))
233  );
234  }
235  $this->tpl->setContent($table->getHTML());
236  }
237 
238  final protected function createXMLExportFile(): void
239  {
240  if ($this->parent_gui instanceof ilContainerGUI) {
241  $this->showItemSelection();
242  return;
243  }
244  $this->createXMLExport($this->export_handler->consumer()->exportConfig()->allExportConfigs());
245  $this->tpl->setOnScreenMessage(
247  $this->lng->txt("exp_file_created"),
248  true
249  );
250  $this->ctrl->redirect($this, self::CMD_LIST_EXPORT_FILES);
251  }
252 
256  final protected function showItemSelection(): void
257  {
258  $this->tpl->addJavaScript('assets/js/ilContainer.js');
259  $this->tpl->setVariable('BODY_ATTRIBUTES', 'onload="ilDisableChilds(\'cmd\');"');
260  $table = new ilExportSelectionTableGUI($this, self::CMD_LIST_EXPORT_FILES, $this->export_handler);
261  $table->parseContainer($this->parent_gui->getObject()->getRefId());
262  $this->tpl->setContent($table->getHTML());
263  }
264 
265  final protected function saveItemSelection(): void
266  {
267  // check export limitation
268  $cp_options = $this->initExportOptionsFromPost();
269  $exp_limit = new ilExportLimitation();
270  try {
271  $exp_limit->checkLimitation(
272  $this->parent_gui->getObject()->getRefId(),
273  $cp_options
274  );
275  } catch (Exception $e) {
276  $this->tpl->setOnScreenMessage('failure', $e->getMessage());
277  $this->showItemSelection();
278  return;
279  }
280  // create export
281  $this->createXMLContainerExport();
282  $this->tpl->setOnScreenMessage('success', $this->lng->txt('export_created'), true);
283  $this->ctrl->redirect($this, self::CMD_LIST_EXPORT_FILES);
284  }
285 
286  final protected function createXMLExport(
287  ExportConfigCollectionInterface $export_configs
288  ): void {
289  $manager = $this->export_handler->manager()->handler();
290  $manager->createExportOfObject($this->obj, $this->il_user->getId(), $export_configs);
291  }
292 
293  final protected function createXMLContainerExport(): void
294  {
295  $tree_nodes = $this->tree->getSubTree($this->tree->getNodeData($this->parent_gui->getObject()->getRefId()));
296  $post_export_options = $this->initExportOptionsFromPost();
298  $eo->addOption(ilExportOptions::KEY_ROOT, 0, 0, $this->obj->getId());
299  $items_selected = $eo->addOptions(
300  $this->parent_gui->getObject()->getRefId(),
303  $tree_nodes,
304  $post_export_options
305  );
306  $eo->read();
307  $ref_ids_export = [$this->parent_gui->getObject()->getRefId()];
308  $ref_ids_all = [$this->parent_gui->getObject()->getRefId()];
309  $tree_ref_ids = array_map(function ($node) { return (int) $node['ref_id']; }, $tree_nodes);
310  $post_ref_ids = array_map(function ($key) {return (int) $key; }, array_keys($post_export_options));
311  $valid_ref_ids = array_intersect($post_ref_ids, $tree_ref_ids);
312  foreach ($valid_ref_ids as $ref_id) {
313  $info = $post_export_options[$ref_id];
314  $export_option_id = (int) $info["type"];
315  if (
316  $export_option_id === ilExportOptions::EXPORT_OMIT ||
317  !$this->obj_definition->allowExport(ilObject::_lookupType($ref_id, true)) ||
318  !$this->access->checkAccess('write', '', $ref_id)
319  ) {
320  continue;
321  }
322  if ($export_option_id === ilExportOptions::EXPORT_BUILD) {
323  $ref_ids_export[] = $ref_id;
324  }
325  if (
326  $export_option_id === ilExportOptions::EXPORT_BUILD ||
327  $export_option_id === ilExportOptions::EXPORT_EXISTING
328  ) {
329  $ref_ids_all[] = $ref_id;
330  }
331  }
332  $export_configs = $this->export_handler->consumer()->exportConfig()->allExportConfigs();
333  $manager = $this->export_handler->manager()->handler();
334  if (count($ref_ids_all) === 1) {
335  $manager->createExportOfObject($this->obj, $this->il_user->getId(), $export_configs);
336  }
337  if (count($ref_ids_all) > 1) {
338  $obj_ids_export = array_map(function (int $ref_id) { return ilObject::_lookupObjId($ref_id); }, $ref_ids_export);
339  $obj_ids_all = array_map(function (int $ref_id) { return ilObject::_lookupObjId($ref_id); }, $ref_ids_all);
340  $object_id_collection_builder = $manager->getObjectIdCollectioBuilder();
341  foreach ($obj_ids_all as $obj_id) {
342  $object_id_collection_builder = $object_id_collection_builder->addObjectId(
343  $this->data_factory->objId($obj_id),
344  in_array($obj_id, $obj_ids_export)
345  );
346  }
347  $container_export_info = $manager->getContainerExportInfo(
348  $this->data_factory->objId($obj_ids_all[0]),
349  $object_id_collection_builder->getCollection(),
350  $export_configs
351  );
352  $element = $manager->createContainerExport($this->il_user->getId(), $container_export_info);
353  }
354  $eo->delete();
355  }
356 }
builtExportOptionCommand(ilExportHandlerConsumerExportOptionInterface $export_option)
ilExportHandlerConsumerExportOptionCollectionInterface $export_options
ilObjUser $il_user
showItemSelection()
Show container item selection table.
ilCtrlInterface $ctrl
static newInstance(int $a_export_id)
ilUIServices $ui_services
ilGlobalTemplateInterface $tpl
ilToolbarGUI $toolbar
static _lookupObjId(int $ref_id)
ilDataFactory $data_factory
ilExportHandler $export_handler
$ref_id
Definition: ltiauth.php:66
static http()
Fetches the global http state from ILIAS.
bool $public_access_enabled
ilErrorHandling $error
ilExportHandlerConsumerContextInterface $context
ilAccessHandler $access
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:25
ilRefineryFactory $refinery
ilHTTPServices $http
__construct(object $a_parent_gui, ?ilObject $a_main_obj=null, bool $public_access_enabled=true, bool $default_export_option_enabled=true)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CMD_SAVE_ITEM_SELECTION
Class ilContainerGUI This is a base GUI class for all container objects in ILIAS: root folder...
ilObjectDefinition $obj_definition
const CMD_LIST_EXPORT_FILES
ilLanguage $lng
static _lookupType(int $id, bool $reference=false)
const CMD_EXPORT_OPTION_PREFIX
createXMLExport(ExportConfigCollectionInterface $export_configs)