ILIAS  release_8 Revision v8.24
class.ilOrgUnitSimpleImportGUI.php
Go to the documentation of this file.
1<?php
18/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
19
26{
29 protected ilCtrl $ctrl;
32 protected object $parent_object;
33 protected ilLanguage $lng;
35 protected \ILIAS\DI\LoggingServices $ilLog;
36
38 {
39 global $DIC;
40 $this->tpl = $DIC->ui()->mainTemplate();
41 $this->ctrl = $DIC->ctrl();
42 $this->parent_gui = $parent_gui;
43 $this->parent_object = $parent_gui->getObject();
44 $this->tabs_gui = $DIC->tabs();
45 $this->toolbar = $DIC->toolbar();
46 $this->lng = $DIC->language();
47 $this->ilAccess = $DIC->access();
48 $this->lng->loadLanguageModule('user');
49 $this->ilLog = $DIC->logger();
50
51 $this->may_create_orgus = $this->ilAccess->checkAccess("create_orgu", "", $this->parent_gui->getRefId(), 'orgu');
52 $this->is_top_level_orgu = ($this->parent_object->getRefId() == ilObjOrgUnit::getRootOrgRefId());
53
54 if (!$this->may_create_orgus) {
55 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("permission_denied"), true);
56 }
57 }
58
62 public function executeCommand()
63 {
64 $cmd = $this->ctrl->getCmd();
65
66 switch ($cmd) {
67 case 'chooseImport':
68 $this->chooseImport();
69 break;
70 case 'importScreen':
71 $this->tabs_gui->clearTargets();
72 $this->tabs_gui->setBackTarget(
73 $this->lng->txt("back"),
74 $this->ctrl->getLinkTarget($this, 'chooseImport')
75 );
76 $this->importScreen();
77 break;
78 case 'startImport':
79 $this->tabs_gui->clearTargets();
80 $this->tabs_gui->setBackTarget(
81 $this->lng->txt("back"),
82 $this->ctrl->getLinkTarget($this, 'chooseImport')
83 );
84 $this->startImport();
85 break;
86 }
87
88 return true;
89 }
90
91 public function chooseImport()
92 {
93 if (!$this->may_create_orgus || !$this->is_top_level_orgu) {
94 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("msg_no_perm_edit"));
95 $this->ctrl->redirectByClass('ilinfoscreengui', '');
96 }
97
98 $this->tabs_gui->setTabActive('view_content');
99 $this->tabs_gui->removeSubTab("page_editor");
100 $this->tabs_gui->removeSubTab("ordering"); // Mantis 0014728
101
102 if ($this->may_create_orgus && $this->is_top_level_orgu) {
103 $this->toolbar->addButton(
104 $this->lng->txt("simple_import"),
105 $this->ctrl->getLinkTargetByClass("ilOrgUnitSimpleImportGUI", "importScreen")
106 );
107 $this->toolbar->addButton(
108 $this->lng->txt("simple_user_import"),
109 $this->ctrl->getLinkTargetByClass("ilOrgUnitSimpleUserImportGUI", "userImportScreen")
110 );
111 }
112 }
113
114 public function importScreen()
115 {
116 $form = $this->initForm("startImport");
117 $this->tpl->setContent($form->getHTML());
118 }
119
120 protected function initForm($submit_action)
121 {
122 $form = new ilPropertyFormGUI();
123 $input = new ilFileInputGUI($this->lng->txt("import_xml_file"), "import_file");
124 $input->setRequired(true);
125 $input->setSuffixes(array('zip', 'xml'));
126 $form->addItem($input);
127 $form->setFormAction($this->ctrl->getFormAction($this));
128 $form->addCommandButton($submit_action, $this->lng->txt("import"));
129
130 return $form;
131 }
132
133 public function startImport()
134 {
135 $form = $this->initForm("startImport");
136 if (!$form->checkInput()) {
137 $this->tpl->setContent($form->getHTML());
138 } else {
139 $file = $form->getInput("import_file");
140 $importer = new ilOrgUnitSimpleImport();
141 try {
142 $file_path = $file["tmp_name"];
143 $file_type = pathinfo($file["name"], PATHINFO_EXTENSION);
144 $file_name = pathinfo($file["name"], PATHINFO_FILENAME);
145
146 if ($file_type == "zip") {
147 $extract_path = $file_path . '_extracted/';
148 $extracted_file = $extract_path . $file_name . '/manifest.xml';
149
150 $zip = new ZipArchive();
151 $res = $zip->open($file_path);
152 if ($res === true) {
153 $zip->extractTo($extract_path);
154 $zip->close();
155
156 if (file_exists($extracted_file)) {
157 $file_path = $extracted_file;
158 }
159 }
160 }
161
162 $importer->simpleImport($file_path);
163 } catch (Exception $e) {
164 $this->ilLog->write($e->getMessage() . " - " . $e->getTraceAsString());
165 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("import_failed"), true);
166 $this->ctrl->redirect($this, "render");
167 }
168 $this->displayImportResults($importer);
169 }
170 }
171
175 public function displayImportResults($importer)
176 {
177 if (!$importer->hasErrors() && !$importer->hasWarnings()) {
178 $stats = $importer->getStats();
179 $this->tpl->setOnScreenMessage(
180 'success',
181 sprintf($this->lng->txt("import_successful"), $stats["created"], $stats["updated"], $stats["deleted"]),
182 true
183 );
184 }
185 if ($importer->hasWarnings()) {
186 $msg = $this->lng->txt("import_terminated_with_warnings") . " <br/>";
187 foreach ($importer->getWarnings() as $warning) {
188 $msg .= "-" . $this->lng->txt($warning["lang_var"]) . " (Import ID: " . $warning["import_id"] . ")<br />";
189 }
190 $this->tpl->setOnScreenMessage('info', $msg, true);
191 }
192 if ($importer->hasErrors()) {
193 $msg = $this->lng->txt("import_terminated_with_errors") . "<br/>";
194 foreach ($importer->getErrors() as $warning) {
195 $msg .= "- " . $this->lng->txt($warning["lang_var"]) . " (Import ID: " . $warning["import_id"] . ")<br />";
196 }
197 $this->tpl->setOnScreenMessage('failure', $msg, true);
198 }
199 }
200}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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.
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 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...
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...
$res
Definition: ltiservices.php:69