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