ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 */
3require_once("class.ilOrgUnitSimpleImport.php");
11
15 protected $tabs_gui;
19 protected $toolbar;
23 protected $ctrl;
27 protected $tpl;
31 protected $parent_object;
35 protected $lng;
39 protected $ilAccess;
40
41
45 function __construct($parent_gui) {
46 global $tpl, $ilCtrl, $ilTabs, $ilToolbar, $lng, $ilAccess, $ilLog;
47 $this->tpl = $tpl;
48 $this->ctrl = $ilCtrl;
49 $this->parent_gui = $parent_gui;
50 $this->parent_object = $parent_gui->object;
51 $this->tabs_gui = $this->parent_gui->tabs_gui;
52 $this->toolbar = $ilToolbar;
53 $this->lng = $lng;
54 $this->ilAccess = $ilAccess;
55 $this->lng->loadLanguageModule('user');
56 $this->ilLog = $ilLog;
57 if (! $this->ilAccess->checkaccess("write", "", $this->parent_gui->object->getRefId())) {
58 ilUtil::sendFailure($this->lng->txt("permission_denied"), true);
59 }
60 }
61
65 public function executeCommand()
66 {
67 $cmd = $this->ctrl->getCmd();
68
69 switch ($cmd) {
70 case 'chooseImport':
71 $this->chooseImport();
72 break;
73 case 'importScreen':
74 $this->tabs_gui->clearTargets();
75 $this->tabs_gui->setBackTarget($this->lng->txt("back"), $this->ctrl->getLinkTarget($this,'chooseImport'));
76 $this->importScreen();
77 break;
78 case 'startImport':
79 $this->tabs_gui->clearTargets();
80 $this->tabs_gui->setBackTarget($this->lng->txt("back"), $this->ctrl->getLinkTarget($this,'chooseImport'));
81 $this->startImport();
82 break;
83 }
84 return true;
85 }
86
87 public function chooseImport() {
88 if (!$this->ilAccess->checkAccess("write", "", $_GET["ref_id"]) OR !$this->parent_object->getRefId() == ilObjOrgUnit::getRootOrgRefId()) {
89 ilUtil::sendFailure($this->lng->txt("msg_no_perm_edit"));
90 $this->ctrl->redirectByClass('ilinfoscreengui', '');
91 }
92
93 $this->tabs_gui->setTabActive("import");
94 $this->tabs_gui->removeSubTab("page_editor");
95 $this->tabs_gui->removeSubTab("ordering"); // Mantis 0014728
96
97 if ($this->ilAccess->checkAccess("write", "", $_GET["ref_id"]) AND $this->parent_object->getRefId() == ilObjOrgUnit::getRootOrgRefId()) {
98 $this->toolbar->addButton($this->lng->txt("simple_import"), $this->ctrl->getLinkTargetByClass("ilOrgUnitSimpleImportGUI", "importScreen"));
99 $this->toolbar->addButton($this->lng->txt("simple_user_import"), $this->ctrl->getLinkTargetByClass("ilOrgUnitSimpleUserImportGUI", "userImportScreen"));
100 }
101 }
102
103 public function importScreen() {
104 $form = $this->initForm("startImport");
105 $this->tpl->setContent($form->getHTML());
106 }
107
108
109 protected function initForm($submit_action) {
110 $form = new ilPropertyFormGUI();
111 $input = new ilFileInputGUI($this->lng->txt("import_xml_file"), "import_file");
112 $input->setRequired(true);
113 $input->setSuffixes(array('zip', 'xml'));
114 $form->addItem($input);
115 $form->setFormAction($this->ctrl->getFormAction($this));
116 $form->addCommandButton($submit_action, $this->lng->txt("import"));
117
118 return $form;
119 }
120
121
122 public function startImport() {
123 $form = $this->initForm("startImport");
124 if (! $form->checkInput()) {
125 $this->tpl->setContent($form->getHTML());
126 } else {
127 $file = $form->getInput("import_file");
128 $importer = new ilOrgUnitSimpleImport();
129 try {
130 $file_path = $file["tmp_name"];
131 $file_type = pathinfo($file["name"], PATHINFO_EXTENSION);
132 $file_name = pathinfo($file["name"], PATHINFO_FILENAME);
133
134 if($file_type == "zip") {
135 $extract_path = $file_path.'_extracted/';
136 $extracted_file = $extract_path.$file_name.'/manifest.xml';
137
138 $zip = new ZipArchive();
139 $res = $zip->open($file_path);
140 if ($res === true) {
141 $zip->extractTo($extract_path);
142 $zip->close();
143
144 if(file_exists($extracted_file)) {
145 $file_path = $extracted_file;
146 }
147 }
148 }
149
150 $importer->simpleImport($file_path);
151 } catch (Exception $e) {
152 $this->ilLog->write($e->getMessage() . " - " . $e->getTraceAsString());
153 ilUtil::sendFailure($this->lng->txt("import_failed"), true);
154 $this->ctrl->redirect($this, "render");
155 }
156 $this->displayImportResults($importer);
157 }
158 }
159
163 public function displayImportResults($importer) {
164 if (! $importer->hasErrors() && ! $importer->hasWarnings()) {
165 $stats = $importer->getStats();
166 ilUtil::sendSuccess(sprintf($this->lng->txt("import_successful"), $stats["created"], $stats["updated"], $stats["deleted"]), true);
167 }
168 if ($importer->hasWarnings()) {
169 $msg = $this->lng->txt("import_terminated_with_warnings") . " <br/>";
170 foreach ($importer->getWarnings() as $warning) {
171 $msg .= "-" . $this->lng->txt($warning["lang_var"]) . " (Import ID: " . $warning["import_id"] . ")<br />";
172 }
173 ilUtil::sendInfo($msg, true);
174 }
175 if ($importer->hasErrors()) {
176 $msg = $this->lng->txt("import_terminated_with_errors") . "<br/>";
177 foreach ($importer->getErrors() as $warning) {
178 $msg .= "- " . $this->lng->txt($warning["lang_var"]) . " (Import ID: " . $warning["import_id"] . ")<br />";
179 }
180 ilUtil::sendFailure($msg, true);
181 }
182 }
183
184
185}
186?>
print $file
$_GET["client_id"]
This class represents a file property in a property form.
logging
Definition: class.ilLog.php:19
write($a_msg, $a_log_level=NULL)
static getRootOrgRefId()
Class ilOrgUnitSimpleImportGUI.
Class ilOrgUnitSimpleImport.
This class represents a property form user interface.
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
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 $ilCtrl
Definition: ilias.php:18
$cmd
Definition: sahs_server.php:35