ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCloudPluginUploadGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
5 include_once("./Services/JSON/classes/class.ilJsonUtil.php");
6 
18 {
22  protected $form;
26  public function executeCommand()
27  {
28  global $ilCtrl;
29 
30  $cmd = $ilCtrl->getCmd();
31 
32  switch ($cmd)
33  {
34  default:
35  $this->$cmd();
36  break;
37  }
38  }
39 
40  function asyncUploadFile()
41  {
42  global $ilTabs;
43 
44  $ilTabs->activateTab("content");
45  $this->initUploadForm();
46  echo $this->form->getHTML();
47 
48 
49  $options = new stdClass();
50  $options->dropZone = "#ilFileUploadDropZone_1";
51  $options->fileInput = "#ilFileUploadInput_1";
52  $options->submitButton = "uploadFiles";
53  $options->cancelButton = "cancelAll";
54  $options->dropArea = "#ilFileUploadDropArea_1";
55  $options->fileList = "#ilFileUploadList_1";
56  $options->fileSelectButton = "#ilFileUploadFileSelect_1";
57  echo "<script language='javascript' type='text/javascript'>var fileUpload1 = new ilFileUpload(1, " . ilJsonUtil::encode($options) . ");</script>";
58 
59  $_SESSION["cld_folder_id"] = $_POST["folder_id"];
60 
61  exit;
62  }
63 
64  public function initUploadForm()
65  {
66  global $ilCtrl, $lng;
67 
68  include_once("./Services/Form/classes/class.ilDragDropFileInputGUI.php");
69  include_once("./Services/jQuery/classes/class.iljQueryUtil.php");
70 
71  $this->form = new ilPropertyFormGUI();
72  $this->form->setId("upload");
73  $this->form->setMultipart(true);
74  $this->form->setHideLabels();
75 
76  $file = new ilDragDropFileInputGUI($lng->txt("cld_upload_files"), "upload_files");
77  $file->setRequired(true);
78  $this->form->addItem($file);
79 
80  $this->form->addCommandButton("uploadFiles", $lng->txt("upload"));
81  $this->form->addCommandButton("cancelAll", $lng->txt("cancel"));
82 
83  $this->form->setTableWidth("100%");
84  $this->form->setTitle($lng->txt("upload_files_title"));
85 // $this->form->setTitleIcon(ilUtil::getImagePath('icon_file.gif'), $lng->txt('obj_file'));
86  $this->form->setTitleIcon(ilUtil::getImagePath('icon_dcl_file.svg'), $lng->txt('obj_file'));
87 
88  $this->form->setTitle($lng->txt("upload_files"));
89  $this->form->setFormAction($ilCtrl->getFormAction($this, "uploadFiles"));
90  $this->form->setTarget("cld_blank_target");
91  }
92 
93  public function cancelAll()
94  {
95  echo "<script language='javascript' type='text/javascript'>window.parent.il.CloudFileList.afterUpload('cancel');</script>";
96  exit;
97  }
98 
103  public function uploadFiles()
104  {
105  $response = new stdClass();
106  $response->error = null;
107  $response->debug = null;
108 
109  $this->initUploadForm();
110  if ($this->form->checkInput())
111  {
112  try
113  {
114  $fileresult = $this->handleFileUpload($this->form->getInput("upload_files"));
115  if ($fileresult)
116  {
117  $response = (object)array_merge((array)$response, (array)$fileresult);
118  }
119  } catch (ilException $e)
120  {
121  $response->error = $e->getMessage();
122  }
123  } else
124  {
126  $response->error = $error->getMessage();
127  }
128 
129  // send response object (don't use 'application/json' as IE wants to download it!)
130  header('Vary: Accept');
131  header('Content-type: text/plain');
132  echo ilJsonUtil::encode($response);
133  exit;
134  }
135 
136  function handleFileUpload($file_upload)
137  {
138  // create answer object
139  $response = new stdClass();
140  $response->fileName = $_POST["title"];
141  $response->fileSize = intval($file_upload["size"]);
142  $response->fileType = $file_upload["type"];
143  $response->fileUnzipped = $file_upload["extract"];
144  $response->error = null;
145 
147 
148  if ($file_upload["extract"])
149  {
150  $newdir = ilUtil::ilTempnam();
151  ilUtil::makeDir($newdir);
152 
153  include_once './Services/Utilities/classes/class.ilFileUtils.php';
154  try
155  {
156  ilFileUtils::processZipFile($newdir, $file_upload["tmp_name"], $file_upload["keep_structure"]);
157  }
158  catch (Exception $e)
159  {
160  $response->error = $e->getMessage();
161  ilUtil::delDir($newdir);
162  exit;
163  }
164 
165  try
166  {
167  $this->uploadDirectory($newdir, $_SESSION["cld_folder_id"], $file_tree, $file_upload["keep_structure"]);
168  }
169  catch (Exception $e)
170  {
171  $response->error = $e->getMessage();
172  ilUtil::delDir($newdir);
173  exit;
174  }
175 
176  ilUtil::delDir($newdir);
177  return $response;
178  }
179  else
180  {
181  $file_tree->uploadFileToService($_SESSION["cld_folder_id"], $file_upload["tmp_name"], $_POST["title"]);
182  return $response;
183  }
184 
185  }
186 
196  protected function uploadDirectory($dir, $parent_id, $file_tree, $keep_structure = true) {
197  $dirlist = opendir($dir);
198 
199  while (false !== ($file = readdir ($dirlist)))
200  {
201 
202  if (!is_file($dir . "/" . $file) && !is_dir($dir . "/" . $file))
203  {
204  global $lng;
205  throw new ilCloudException($lng->txt("filenames_not_supported") , ilFileUtilsException::$BROKEN_FILE);
206  }
207  if ($file != '.' && $file != '..')
208  {
209  $newpath = $dir.'/'.$file;
210  if (is_dir($newpath))
211  {
212  if($keep_structure)
213  {
214  $newnode = $file_tree->addFolderToService($parent_id, basename($newpath));
215  $this->uploadDirectory($newpath, $newnode->getId(), $file_tree);
216  }
217  else
218  {
219  $this->uploadDirectory($newpath, $parent_id, $file_tree, false);
220  }
221  }
222  else
223  {
224  $file_tree->uploadFileToService($parent_id, $newpath, basename($newpath));
225  }
226  }
227  }
228  closedir($dirlist);
229  }
230 }
231 
232 ?>