ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilOrgUnitSimpleUserImportGUI.php
Go to the documentation of this file.
1 <?php
2 
19 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
20 
28 {
29  protected ilTabsGUI $tabs_gui;
31  protected ilCtrl $ctrl;
33  protected object $parent_object;
34  protected object $parent_gui;
35  protected ilLanguage $lng;
37  protected \ILIAS\DI\LoggingServices $ilLog;
38 
39 
40  public function __construct(object $parent_gui)
41  {
42  global $DIC;
43  $this->tpl = $DIC->ui()->mainTemplate();
44  $this->ctrl = $DIC->ctrl();
45  $this->parent_gui = $parent_gui;
46  $this->parent_object = $parent_gui->getObject();
47  $this->tabs_gui = $DIC->tabs();
48  $this->toolbar = $DIC->toolbar();
49  $this->lng = $DIC->language();
50  $this->ilLog = $DIC->logger();
51  $this->ilAccess = $DIC->access();
52  $this->lng->loadLanguageModule('user');
53  if (!$this->ilAccess->checkaccess('write', '', $this->parent_gui->getObject()->getRefId())) {
54  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
55  }
56  }
57 
58  public function executeCommand(): bool
59  {
60  $cmd = $this->ctrl->getCmd();
61 
62  $this->tabs_gui->clearTargets();
63  $this->tabs_gui->setBackTarget(
64  $this->lng->txt("back"),
65  $this->ctrl->getLinkTargetByClass('ilOrgUnitSimpleImportGUI', 'chooseImport')
66  );
67 
68  switch ($cmd) {
69  case 'userImportScreen':
70  $this->userImportScreen();
71  break;
72  case 'startImport':
73  $this->startImport();
74  break;
75  }
76 
77  return true;
78  }
79 
80  public function userImportScreen(): void
81  {
82  $form = $this->initForm();
83  $this->tpl->setContent($form->getHTML());
84  }
85 
86  private function initForm(): ilPropertyFormGUI
87  {
88  $form = new ilPropertyFormGUI();
89  $input = new ilFileInputGUI($this->lng->txt('import_xml_file'), 'import_file');
90  $input->setRequired(true);
91  $form->addItem($input);
92  $form->setFormAction($this->ctrl->getFormAction($this));
93  $form->addCommandButton('startImport', $this->lng->txt('import'));
94 
95  return $form;
96  }
97 
98  public function startImport(): void
99  {
100  $form = $this->initForm();
101  if (!$form->checkInput()) {
102  $this->tpl->setContent($form->getHTML());
103  } else {
104  $file = $form->getInput('import_file');
105  $importer = new ilOrgUnitSimpleUserImport();
106  try {
107  $importer->simpleUserImport($file['tmp_name']);
108  } catch (Exception $e) {
109  $this->ilLog->write($e->getMessage() . ' - ' . $e->getTraceAsString());
110  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('import_failed'), true);
111  $this->ctrl->redirect($this, 'render');
112  }
113  $this->displayImportResults($importer);
114  }
115  }
116 
117  public function displayImportResults(ilOrgUnitImporter $importer): void
118  {
119  if (!$importer->hasErrors() and !$importer->hasWarnings()) {
120  $stats = $importer->getStats();
121  $this->tpl->setOnScreenMessage(
122  'success',
123  sprintf($this->lng->txt('user_import_successful'), $stats['created'], $stats['removed']),
124  true
125  );
126  }
127  if ($importer->hasWarnings()) {
128  $msg = $this->lng->txt('import_terminated_with_warnings') . '<br>';
129  foreach ($importer->getWarnings() as $warning) {
130  $msg .= '-' . $this->lng->txt($warning['lang_var']) . ' (Import ID: ' . $warning['import_id'] . ')<br>';
131  }
132  $this->tpl->setOnScreenMessage('info', $msg, true);
133  }
134  if ($importer->hasErrors()) {
135  $msg = $this->lng->txt('import_terminated_with_errors') . '<br>';
136  foreach ($importer->getErrors() as $warning) {
137  $msg .= '- ' . $this->lng->txt($warning['lang_var']) . ' (Import ID: ' . $warning['import_id'] . ')<br>';
138  }
139  $this->tpl->setOnScreenMessage('failure', $msg, true);
140  }
141  }
142 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a file 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...
Definition: class.ilLog.php:30
displayImportResults(ilOrgUnitImporter $importer)
global $DIC
Definition: shib_login.php:25
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setRequired(bool $a_required)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
write(string $a_msg, $a_log_level=null)
logging