ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
4include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
5include_once("./Services/JSON/classes/class.ilJsonUtil.php");
6
18{
22 protected $form;
26 public function executeCommand()
27 {
28 global $DIC;
29 $ilCtrl = $DIC['ilCtrl'];
30
31 $cmd = $ilCtrl->getCmd();
32
33 switch ($cmd)
34 {
35 default:
36 $this->$cmd();
37 break;
38 }
39 }
40
41 function asyncUploadFile()
42 {
43 global $DIC;
44 $ilTabs = $DIC['ilTabs'];
45
46 $ilTabs->activateTab("content");
47 $this->initUploadForm();
48 echo $this->form->getHTML();
49
50
51 $options = new stdClass();
52 $options->dropZone = "#ilFileUploadDropZone_1";
53 $options->fileInput = "#ilFileUploadInput_1";
54 $options->submitButton = "uploadFiles";
55 $options->cancelButton = "cancelAll";
56 $options->dropArea = "#ilFileUploadDropArea_1";
57 $options->fileList = "#ilFileUploadList_1";
58 $options->fileSelectButton = "#ilFileUploadFileSelect_1";
59 echo "<script language='javascript' type='text/javascript'>var fileUpload1 = new ilFileUpload(1, " . ilJsonUtil::encode($options) . ");</script>";
60
61 $_SESSION["cld_folder_id"] = $_POST["folder_id"];
62
63 exit;
64 }
65
66 public function initUploadForm()
67 {
68 global $DIC;
69 $ilCtrl = $DIC['ilCtrl'];
70 $lng = $DIC['lng'];
71
72 include_once("./Services/Form/classes/class.ilDragDropFileInputGUI.php");
73 include_once("./Services/jQuery/classes/class.iljQueryUtil.php");
74
75 $this->form = new ilPropertyFormGUI();
76 $this->form->setId("upload");
77 $this->form->setMultipart(true);
78 $this->form->setHideLabels();
79
80 $file = new ilDragDropFileInputGUI($lng->txt("cld_upload_files"), "upload_files");
81 $file->setRequired(true);
82 $this->form->addItem($file);
83
84 $this->form->addCommandButton("uploadFiles", $lng->txt("upload"));
85 $this->form->addCommandButton("cancelAll", $lng->txt("cancel"));
86
87 $this->form->setTableWidth("100%");
88 $this->form->setTitle($lng->txt("upload_files_title"));
89// $this->form->setTitleIcon(ilUtil::getImagePath('icon_file.gif'), $lng->txt('obj_file'));
90 $this->form->setTitleIcon(ilUtil::getImagePath('icon_dcl_file.svg'), $lng->txt('obj_file'));
91
92 $this->form->setTitle($lng->txt("upload_files"));
93 $this->form->setFormAction($ilCtrl->getFormAction($this, "uploadFiles"));
94 $this->form->setTarget("cld_blank_target");
95 }
96
97 public function cancelAll()
98 {
99 echo "<script language='javascript' type='text/javascript'>window.parent.il.CloudFileList.afterUpload('cancel');</script>";
100 exit;
101 }
102
107 public function uploadFiles()
108 {
109 $response = new stdClass();
110 $response->error = null;
111 $response->debug = null;
112
113 $this->initUploadForm();
114 if ($this->form->checkInput())
115 {
116 try
117 {
118 $fileresult = $this->handleFileUpload($this->form->getInput("upload_files"));
119 if ($fileresult)
120 {
121 $response = (object)array_merge((array)$response, (array)$fileresult);
122 }
123 } catch (ilException $e)
124 {
125 $response->error = $e->getMessage();
126 }
127 } else
128 {
130 $response->error = $error->getMessage();
131 }
132
133 // send response object (don't use 'application/json' as IE wants to download it!)
134 header('Vary: Accept');
135 header('Content-type: text/plain');
136 echo ilJsonUtil::encode($response);
137 exit;
138 }
139
140 function handleFileUpload($file_upload)
141 {
142 // create answer object
143 $response = new stdClass();
144 $response->fileName = $_POST["title"];
145 $response->fileSize = intval($file_upload["size"]);
146 $response->fileType = $file_upload["type"];
147 $response->fileUnzipped = $file_upload["extract"];
148 $response->error = null;
149
151
152 if ($file_upload["extract"])
153 {
154 $newdir = ilUtil::ilTempnam();
155 ilUtil::makeDir($newdir);
156
157 include_once './Services/Utilities/classes/class.ilFileUtils.php';
158 try
159 {
160 ilFileUtils::processZipFile($newdir, $file_upload["tmp_name"], $file_upload["keep_structure"]);
161 }
162 catch (Exception $e)
163 {
164 $response->error = $e->getMessage();
165 ilUtil::delDir($newdir);
166 exit;
167 }
168
169 try
170 {
171 $this->uploadDirectory($newdir, $_SESSION["cld_folder_id"], $file_tree, $file_upload["keep_structure"]);
172 }
173 catch (Exception $e)
174 {
175 $response->error = $e->getMessage();
176 ilUtil::delDir($newdir);
177 exit;
178 }
179
180 ilUtil::delDir($newdir);
181 return $response;
182 }
183 else
184 {
185 $file_tree->uploadFileToService($_SESSION["cld_folder_id"], $file_upload["tmp_name"], $_POST["title"]);
186 return $response;
187 }
188
189 }
190
200 protected function uploadDirectory($dir, $parent_id, $file_tree, $keep_structure = true) {
201 $dirlist = opendir($dir);
202
203 while (false !== ($file = readdir ($dirlist)))
204 {
205
206 if (!is_file($dir . "/" . $file) && !is_dir($dir . "/" . $file))
207 {
208 global $DIC;
209 $lng = $DIC['lng'];
210 throw new ilCloudException($lng->txt("filenames_not_supported") , ilFileUtilsException::$BROKEN_FILE);
211 }
212 if ($file != '.' && $file != '..')
213 {
214 $newpath = $dir.'/'.$file;
215 if (is_dir($newpath))
216 {
217 if($keep_structure)
218 {
219 $newnode = $file_tree->addFolderToService($parent_id, basename($newpath));
220 $this->uploadDirectory($newpath, $newnode->getId(), $file_tree);
221 }
222 else
223 {
224 $this->uploadDirectory($newpath, $parent_id, $file_tree, false);
225 }
226 }
227 else
228 {
229 $file_tree->uploadFileToService($parent_id, $newpath, basename($newpath));
230 }
231 }
232 }
233 closedir($dirlist);
234 }
235}
236
237?>
$error
Definition: Error.php:17
$_POST["username"]
$_SESSION["AccountId"]
An exception for terminatinating execution or to throw for unit testing.
Class ilCloudException.
Class ilCloudPluginGUI.
Class ilCloudPluginUploadGUI.
uploadDirectory($dir, $parent_id, $file_tree, $keep_structure=true)
Recursive Method to upload a directory.
This class represents a file input property where multiple files can be dopped in a property form.
Base class for ILIAS Exception handling.
static processZipFile($a_directory, $a_file, $structure, $ref_id=null, $containerType=null, $tree=null, $access_handler=null)
unzips in given directory and processes uploaded zip for use as single files
static encode($mixed, $suppress_native=false)
This class represents a property form user interface.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
global $ilCtrl
Definition: ilias.php:18
global $lng
Definition: privfeed.php:17
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
$cmd
Definition: sahs_server.php:35
if(!is_array($argv)) $options
global $DIC