ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilOrgUnitSimpleImportGUI.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
11{
12
16 protected $tabs_gui;
20 protected $toolbar;
24 protected $ctrl;
28 protected $tpl;
32 protected $parent_object;
36 protected $lng;
40 protected $ilAccess;
41
42
46 public function __construct($parent_gui)
47 {
48 global $DIC;
49 $tpl = $DIC['tpl'];
50 $ilCtrl = $DIC['ilCtrl'];
51 $ilTabs = $DIC['ilTabs'];
52 $ilToolbar = $DIC['ilToolbar'];
53 $lng = $DIC['lng'];
54 $ilAccess = $DIC['ilAccess'];
55 $ilLog = $DIC['ilLog'];
56 $this->tpl = $tpl;
57 $this->ctrl = $ilCtrl;
58 $this->parent_gui = $parent_gui;
59 $this->parent_object = $parent_gui->object;
60 $this->tabs_gui = $this->parent_gui->tabs_gui;
61 $this->toolbar = $ilToolbar;
62 $this->lng = $lng;
63 $this->ilAccess = $ilAccess;
64 $this->lng->loadLanguageModule('user');
65 $this->ilLog = $ilLog;
66 if (!$this->ilAccess->checkaccess("write", "", $this->parent_gui->object->getRefId())) {
67 ilUtil::sendFailure($this->lng->txt("permission_denied"), true);
68 }
69 }
70
71
75 public function executeCommand()
76 {
77 $cmd = $this->ctrl->getCmd();
78
79 switch ($cmd) {
80 case 'chooseImport':
81 $this->chooseImport();
82 break;
83 case 'importScreen':
84 $this->tabs_gui->clearTargets();
85 $this->tabs_gui->setBackTarget($this->lng->txt("back"), $this->ctrl->getLinkTarget($this, 'chooseImport'));
86 $this->importScreen();
87 break;
88 case 'startImport':
89 $this->tabs_gui->clearTargets();
90 $this->tabs_gui->setBackTarget($this->lng->txt("back"), $this->ctrl->getLinkTarget($this, 'chooseImport'));
91 $this->startImport();
92 break;
93 }
94
95 return true;
96 }
97
98
99 public function chooseImport()
100 {
101 if (!$this->ilAccess->checkAccess("write", "", $_GET["ref_id"]) or !$this->parent_object->getRefId() == ilObjOrgUnit::getRootOrgRefId()) {
102 ilUtil::sendFailure($this->lng->txt("msg_no_perm_edit"));
103 $this->ctrl->redirectByClass('ilinfoscreengui', '');
104 }
105
106 $this->tabs_gui->setTabActive('view_content');
107 $this->tabs_gui->removeSubTab("page_editor");
108 $this->tabs_gui->removeSubTab("ordering"); // Mantis 0014728
109
110 if ($this->ilAccess->checkAccess("write", "", $_GET["ref_id"]) and $this->parent_object->getRefId() == ilObjOrgUnit::getRootOrgRefId()) {
111 $this->toolbar->addButton($this->lng->txt("simple_import"), $this->ctrl->getLinkTargetByClass("ilOrgUnitSimpleImportGUI", "importScreen"));
112 $this->toolbar->addButton($this->lng->txt("simple_user_import"), $this->ctrl->getLinkTargetByClass("ilOrgUnitSimpleUserImportGUI", "userImportScreen"));
113 }
114 }
115
116
117 public function importScreen()
118 {
119 $form = $this->initForm("startImport");
120 $this->tpl->setContent($form->getHTML());
121 }
122
123
124 protected function initForm($submit_action)
125 {
126 $form = new ilPropertyFormGUI();
127 $input = new ilFileInputGUI($this->lng->txt("import_xml_file"), "import_file");
128 $input->setRequired(true);
129 $input->setSuffixes(array('zip', 'xml'));
130 $form->addItem($input);
131 $form->setFormAction($this->ctrl->getFormAction($this));
132 $form->addCommandButton($submit_action, $this->lng->txt("import"));
133
134 return $form;
135 }
136
137
138 public function startImport()
139 {
140 $form = $this->initForm("startImport");
141 if (!$form->checkInput()) {
142 $this->tpl->setContent($form->getHTML());
143 } else {
144 $file = $form->getInput("import_file");
145 $importer = new ilOrgUnitSimpleImport();
146 try {
147 $file_path = $file["tmp_name"];
148 $file_type = pathinfo($file["name"], PATHINFO_EXTENSION);
149 $file_name = pathinfo($file["name"], PATHINFO_FILENAME);
150
151 if ($file_type == "zip") {
152 $extract_path = $file_path . '_extracted/';
153 $extracted_file = $extract_path . $file_name . '/manifest.xml';
154
155 $zip = new ZipArchive();
156 $res = $zip->open($file_path);
157 if ($res === true) {
158 $zip->extractTo($extract_path);
159 $zip->close();
160
161 if (file_exists($extracted_file)) {
162 $file_path = $extracted_file;
163 }
164 }
165 }
166
167 $importer->simpleImport($file_path);
168 } catch (Exception $e) {
169 $this->ilLog->write($e->getMessage() . " - " . $e->getTraceAsString());
170 ilUtil::sendFailure($this->lng->txt("import_failed"), true);
171 $this->ctrl->redirect($this, "render");
172 }
173 $this->displayImportResults($importer);
174 }
175 }
176
177
181 public function displayImportResults($importer)
182 {
183 if (!$importer->hasErrors() && !$importer->hasWarnings()) {
184 $stats = $importer->getStats();
185 ilUtil::sendSuccess(sprintf($this->lng->txt("import_successful"), $stats["created"], $stats["updated"], $stats["deleted"]), true);
186 }
187 if ($importer->hasWarnings()) {
188 $msg = $this->lng->txt("import_terminated_with_warnings") . " <br/>";
189 foreach ($importer->getWarnings() as $warning) {
190 $msg .= "-" . $this->lng->txt($warning["lang_var"]) . " (Import ID: " . $warning["import_id"] . ")<br />";
191 }
192 ilUtil::sendInfo($msg, true);
193 }
194 if ($importer->hasErrors()) {
195 $msg = $this->lng->txt("import_terminated_with_errors") . "<br/>";
196 foreach ($importer->getErrors() as $warning) {
197 $msg .= "- " . $this->lng->txt($warning["lang_var"]) . " (Import ID: " . $warning["import_id"] . ")<br />";
198 }
199 ilUtil::sendFailure($msg, true);
200 }
201 }
202}
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
Class ilAccessHandler.
checkAccess($a_permission, $a_cmd, $a_ref_id, $a_type="", $a_obj_id="", $a_tree_id="")
check access for an object (provide $a_type and $a_obj_id if available for better performance)
This class represents a file property in a property form.
logging
Definition: class.ilLog.php:19
write($a_msg, $a_log_level=null)
logging
static getRootOrgRefId()
Class ilOrgUnitSimpleImportGUI.
Class ilOrgUnitSimpleImport.
This class represents a property form user interface.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
global $DIC
Definition: goto.php:24
foreach($_POST as $key=> $value) $res