ILIAS  Release_4_4_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 
87  $this->form->setTitle($lng->txt("upload_files"));
88  $this->form->setFormAction($ilCtrl->getFormAction($this, "uploadFiles"));
89  $this->form->setTarget("cld_blank_target");
90  }
91 
92  public function cancelAll()
93  {
94  echo "<script language='javascript' type='text/javascript'>window.parent.il.CloudFileList.afterUpload('cancel');</script>";
95  exit;
96  }
97 
102  public function uploadFiles()
103  {
104  $response = new stdClass();
105  $response->error = null;
106  $response->debug = null;
107 
108  $this->initUploadForm();
109  if ($this->form->checkInput())
110  {
111  try
112  {
113  $fileresult = $this->handleFileUpload($this->form->getInput("upload_files"));
114  if ($fileresult)
115  {
116  $response = (object)array_merge((array)$response, (array)$fileresult);
117  }
118  } catch (ilException $e)
119  {
120  $response->error = $e->getMessage();
121  }
122  } else
123  {
125  $response->error = $error->getMessage();
126  }
127 
128  // send response object (don't use 'application/json' as IE wants to download it!)
129  header('Vary: Accept');
130  header('Content-type: text/plain');
131  echo ilJsonUtil::encode($response);
132  exit;
133  }
134 
135  function handleFileUpload($file_upload)
136  {
137  // create answer object
138  $response = new stdClass();
139  $response->fileName = $_POST["title"];
140  $response->fileSize = intval($file_upload["size"]);
141  $response->fileType = $file_upload["type"];
142  $response->fileUnzipped = $file_upload["extract"];
143  $response->error = null;
144 
146 
147  if ($file_upload["extract"])
148  {
149  $newdir = ilUtil::ilTempnam();
150  ilUtil::makeDir($newdir);
151 
152  include_once './Services/Utilities/classes/class.ilFileUtils.php';
153  try
154  {
155  ilFileUtils::processZipFile($newdir, $file_upload["tmp_name"], $file_upload["keep_structure"]);
156  }
157  catch (Exception $e)
158  {
159  $response->error = $e->getMessage();
160  ilUtil::delDir($newdir);
161  exit;
162  }
163 
164  try
165  {
166  $this->uploadDirectory($newdir, $_SESSION["cld_folder_id"], $file_tree, $file_upload["keep_structure"]);
167  }
168  catch (Exception $e)
169  {
170  $response->error = $e->getMessage();
171  ilUtil::delDir($newdir);
172  exit;
173  }
174 
175  ilUtil::delDir($newdir);
176  return $response;
177  }
178  else
179  {
180  $file_tree->uploadFileToService($_SESSION["cld_folder_id"], $file_upload["tmp_name"], $_POST["title"]);
181  return $response;
182  }
183 
184  }
185 
195  protected function uploadDirectory($dir, $parent_id, $file_tree, $keep_structure = true) {
196  $dirlist = opendir($dir);
197 
198  while (false !== ($file = readdir ($dirlist)))
199  {
200 
201  if (!is_file($dir . "/" . $file) && !is_dir($dir . "/" . $file))
202  {
203  global $lng;
204  throw new ilCloudException($lng->txt("filenames_not_supported") , ilFileUtilsException::$BROKEN_FILE);
205  }
206  if ($file != '.' && $file != '..')
207  {
208  $newpath = $dir.'/'.$file;
209  if (is_dir($newpath))
210  {
211  if($keep_structure)
212  {
213  $newnode = $file_tree->addFolderToService($parent_id, basename($newpath));
214  $this->uploadDirectory($newpath, $newnode->getId(), $file_tree);
215  }
216  else
217  {
218  $this->uploadDirectory($newpath, $parent_id, $file_tree, false);
219  }
220  }
221  else
222  {
223  $file_tree->uploadFileToService($parent_id, $newpath, basename($newpath));
224  }
225  }
226  }
227  closedir($dirlist);
228  }
229 }
230 
231 ?>