ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilOrgUnitSimpleImportGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
30 protected ilCtrl $ctrl;
33 protected object $parent_object;
34 protected ilLanguage $lng;
36 protected \ILIAS\DI\LoggingServices $ilLog;
37 protected \ILIAS\UI\Factory $ui_factory;
38
39 protected bool $may_create_orgus = false;
40 protected bool $is_top_level_orgu = false;
41
43 {
44 global $DIC;
45 $this->tpl = $DIC->ui()->mainTemplate();
46 $this->ctrl = $DIC->ctrl();
47 $this->parent_gui = $parent_gui;
48 $this->parent_object = $parent_gui->getObject();
49 $this->tabs_gui = $DIC->tabs();
50 $this->toolbar = $DIC->toolbar();
51 $this->lng = $DIC->language();
52 $this->ilAccess = $DIC->access();
53 $this->lng->loadLanguageModule('user');
54 $this->ilLog = $DIC->logger();
55 $this->ui_factory = $DIC['ui.factory'];
56
57 $this->may_create_orgus = $this->ilAccess->checkAccess("create_orgu", "", $this->parent_gui->getRefId(), 'orgu');
58 $this->is_top_level_orgu = ($this->parent_object->getRefId() == ilObjOrgUnit::getRootOrgRefId());
59
60 if (!$this->may_create_orgus) {
61 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("permission_denied"), true);
62 }
63 }
64
68 public function executeCommand()
69 {
70 $cmd = $this->ctrl->getCmd();
71
72 switch ($cmd) {
73 case 'chooseImport':
74 $this->chooseImport();
75 break;
76 case 'importScreen':
77 $this->tabs_gui->clearTargets();
78 $this->tabs_gui->setBackTarget(
79 $this->lng->txt("back"),
80 $this->ctrl->getLinkTarget($this, 'chooseImport')
81 );
82 $this->importScreen();
83 break;
84 case 'startImport':
85 $this->tabs_gui->clearTargets();
86 $this->tabs_gui->setBackTarget(
87 $this->lng->txt("back"),
88 $this->ctrl->getLinkTarget($this, 'chooseImport')
89 );
90 $this->startImport();
91 break;
92 }
93
94 return true;
95 }
96
97 public function chooseImport()
98 {
99 if (!$this->may_create_orgus || !$this->is_top_level_orgu) {
100 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("msg_no_perm_edit"));
101 $this->ctrl->redirectByClass('ilinfoscreengui', '');
102 }
103
104 $this->tabs_gui->setTabActive('view_content');
105 $this->tabs_gui->removeSubTab("page_editor");
106 $this->tabs_gui->removeSubTab("ordering"); // Mantis 0014728
107
108 if ($this->may_create_orgus && $this->is_top_level_orgu) {
109 $this->toolbar->addComponent(
110 $this->ui_factory->link()->standard(
111 $this->lng->txt('simple_import'),
112 $this->ctrl->getLinkTargetByClass("ilOrgUnitSimpleImportGUI", "importScreen")
113 )
114 );
115 $this->toolbar->addComponent(
116 $this->ui_factory->link()->standard(
117 $this->lng->txt('simple_user_import'),
118 $this->ctrl->getLinkTargetByClass("ilOrgUnitSimpleUserImportGUI", "userImportScreen")
119 )
120 );
121 }
122 }
123
124 public function importScreen()
125 {
126 $form = $this->initForm("startImport");
127 $this->tpl->setContent($form->getHTML());
128 }
129
130 protected function initForm($submit_action)
131 {
132 $form = new ilPropertyFormGUI();
133 $input = new ilFileInputGUI($this->lng->txt("import_xml_file"), "import_file");
134 $input->setRequired(true);
135 $input->setSuffixes(array('zip', 'xml'));
136 $form->addItem($input);
137 $form->setFormAction($this->ctrl->getFormAction($this));
138 $form->addCommandButton($submit_action, $this->lng->txt("import"));
139
140 return $form;
141 }
142
143 public function startImport()
144 {
145 $form = $this->initForm("startImport");
146 if (!$form->checkInput()) {
147 $this->tpl->setContent($form->getHTML());
148 } else {
149 $file = $form->getInput("import_file");
150 $importer = new ilOrgUnitSimpleImport();
151 try {
152 $file_path = $file["tmp_name"];
153 $file_type = pathinfo($file["name"], PATHINFO_EXTENSION);
154 $file_name = pathinfo($file["name"], PATHINFO_FILENAME);
155
156 if ($file_type == "zip") {
157 $extract_path = $file_path . '_extracted/';
158 $extracted_file = $extract_path . $file_name . '/manifest.xml';
159
160 $zip = new ZipArchive();
161 $res = $zip->open($file_path);
162 if ($res === true) {
163 $zip->extractTo($extract_path);
164 $zip->close();
165
166 if (file_exists($extracted_file)) {
167 $file_path = $extracted_file;
168 }
169 }
170 }
171
172 $importer->simpleImport($file_path);
173 } catch (Exception $e) {
174 $this->ilLog->write($e->getMessage() . " - " . $e->getTraceAsString());
175 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("import_failed"), true);
176 $this->ctrl->redirect($this, "render");
177 }
178 $this->displayImportResults($importer);
179 }
180 }
181
185 public function displayImportResults($importer)
186 {
187 if (!$importer->hasErrors() && !$importer->hasWarnings()) {
188 $stats = $importer->getStats();
189 $this->tpl->setOnScreenMessage(
190 'success',
191 sprintf($this->lng->txt("import_successful"), $stats["created"], $stats["updated"], $stats["deleted"]),
192 true
193 );
194 }
195 if ($importer->hasWarnings()) {
196 $msg = $this->lng->txt("import_terminated_with_warnings") . " <br/>";
197 foreach ($importer->getWarnings() as $warning) {
198 $msg .= "-" . $this->lng->txt($warning["lang_var"]) . " (Import ID: " . $warning["import_id"] . ")<br />";
199 }
200 $this->tpl->setOnScreenMessage('info', $msg, true);
201 }
202 if ($importer->hasErrors()) {
203 $msg = $this->lng->txt("import_terminated_with_errors") . "<br/>";
204 foreach ($importer->getErrors() as $warning) {
205 $msg .= "- " . $this->lng->txt($warning["lang_var"]) . " (Import ID: " . $warning["import_id"] . ")<br />";
206 }
207 $this->tpl->setOnScreenMessage('failure', $msg, true);
208 }
209 }
210}
Class ilAccessHandler Checks access for ILIAS objects.
checkAccess(string $a_permission, string $a_cmd, int $a_ref_id, string $a_type="", ?int $a_obj_id=null, ?int $a_tree_id=null)
check access for an object (provide $a_type and $a_obj_id if available for better performance)
Class ilCtrl provides processing control methods.
This class represents a file property in a property form.
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: class.ilLog.php:31
write(string $a_msg, $a_log_level=null)
logging
static getRootOrgRefId()
Class ilObjectGUI Basic methods of all Output classes.
Class ilOrgUnitSimpleImportGUI.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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...
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...
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26