ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilFileUploadGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once("./Services/FileUpload/classes/class.ilFileUploadUtil.php");
5 require_once("./Services/FileUpload/classes/class.ilFileUploadSettings.php");
6 
16 {
17  const FILE_OBJ_GUI_CLASS = "ilObjFileGUI";
18  private static $shared_code_loaded = false;
19 
20  private $drop_zone_id = null;
21  private $ref_id = null;
22  private $current_obj = false;
23  private $max_file_size = null;
24  private $suffixes = array();
25  private $archive_suffixes = array();
26  private $input_field_name = "upload_files";
27  private $input_field_id = null;
28  private $use_form = false;
29  private $drop_area_id = null;
30  private $submit_button_name = null;
31  private $cancel_button_name = null;
32  private $file_list_id = null;
33  private $file_select_button_id = null;
34 
38  public function __construct($a_drop_zone_id, $a_ref_id = null, $current_obj = false)
39  {
40  global $DIC;
41  $ilCtrl = $DIC['ilCtrl'];
42 
43  $this->drop_zone_id = $a_drop_zone_id;
44  $this->ref_id = $a_ref_id;
45  $this->current_obj = $current_obj;
46  }
47 
51  public static function initFileUpload()
52  {
53  global $DIC;
54  $tpl = $DIC['tpl'];
55 
56  // needed scripts
57  $tpl->addJavaScript("./Services/FileUpload/js/tmpl.js");
58  $tpl->addJavaScript("./Services/FileUpload/js/jquery.ui.widget.js");
59  $tpl->addJavaScript("./Services/FileUpload/js/jquery.iframe-transport.js");
60  $tpl->addJavaScript("./Services/FileUpload/js/jquery.fileupload.js");
61  $tpl->addJavaScript("./Services/FileUpload/js/jquery.ba-dotimeout.min.js");
62  $tpl->addJavaScript("./Services/FileUpload/js/ilFileUpload.js", true, 3);
63  // needed styles
64  $tpl->addCss(ilUtil::getStyleSheetLocation("filesystem", "fileupload.css", "Services/FileUpload"));
65  }
66 
72  public function getHTML()
73  {
74  global $DIC;
75  $lng = $DIC['lng'];
76  $ilCtrl = $DIC['ilCtrl'];
77  $tpl = $DIC['tpl'];
78 
79  // get values
80  $id = $this->ref_id;
81 
82  // build options
83  $options = new stdClass();
84  $options->dropZone = $this->makeJqueryId($this->drop_zone_id);
85 
86  // set url
87  $url = $this->getUploadUrl();
88  if ($url === false) {
89  return "";
90  }
91 
92  if ($url != null) {
93  $options->url = $this->getUploadUrl();
94  }
95 
96  // get title and replace quotes with HTML entities
97  if ($this->ref_id != null && !$this->use_form) {
98  $title = ilObject::_lookupTitle(ilObject::_lookupObjId($this->ref_id));
99  $title = str_replace("\"", "&quot;", $title);
100  $title = str_replace("'", "&#039;", $title);
101  $options->listTitle = $title;
102  }
103 
104  // input field (use id if specified)
105  if ($this->input_field_id != null) {
106  $options->fileInput = $this->makeJqueryId($this->input_field_id);
107  } elseif ($this->input_field_name != null) {
108  $options->fileInput = $this->input_field_name;
109  }
110 
111  // buttons
112  if ($this->submit_button_name != null) {
113  $options->submitButton = $this->submit_button_name;
114  }
115  if ($this->cancel_button_name != null) {
116  $options->cancelButton = $this->cancel_button_name;
117  }
118 
119  // drop area
120  if ($this->drop_area_id != null) {
121  $options->dropArea = $this->makeJqueryId($this->drop_area_id);
122  }
123 
124  // file list
125  if ($this->file_list_id != null) {
126  $options->fileList = $this->makeJqueryId($this->file_list_id);
127  }
128 
129  // file list
130  if ($this->file_select_button_id != null) {
131  $options->fileSelectButton = $this->makeJqueryId($this->file_select_button_id);
132  }
133 
134  // max size
135  $max_size = $this->getMaxFileSize();
136  if ($max_size != null) {
137  $options->maxFileSize = $max_size;
138  }
139 
140  // allowed extensions
141  $allowed_suffixes = $this->buildSuffixList($this->getSuffixes());
142  if ($allowed_suffixes != "") {
143  $options->allowedExtensions = "[" . $allowed_suffixes . "]";
144  }
145 
146  // supported archive extensions
147  $supported_archives = $this->buildSuffixList($this->getArchiveSuffixes());
148  if ($supported_archives != "") {
149  $options->supportedArchives = "[" . $supported_archives . "]";
150  }
151 
152  // inject load script
153  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
154 
155  $onLoadCode = "";
156  if ($this->use_form) {
157  $onLoadCode = "var fileUpload$id = new ilFileUpload($id, " . ilJsonUtil::encode($options) . ");";
158  } else {
159  $onLoadCode = "il.FileUpload.add(\"$id\", " . ilJsonUtil::encode($options) . ", " . ($this->current_obj ? "true" : "false") . ");";
160  }
161 
162  $tpl->addOnLoadCode($onLoadCode);
163 
164  // return shared code
165  return $this->getSharedHtml();
166  }
167 
173  protected function getSharedHtml()
174  {
175  global $DIC;
176  $lng = $DIC['lng'];
177 
178  // already loaded?
179  if (self::$shared_code_loaded) {
180  return "";
181  }
182 
183  // make sure required scripts are loaded
184  self::initFileUpload();
185 
186  // load script template
187  $tpl_shared = new ilTemplate("tpl.fileupload_shared.html", true, true, "Services/FileUpload");
188 
189  // initialize localized texts
190  $lng->loadLanguageModule("form");
191  $tpl_shared->setCurrentBlock("fileupload_texts");
192  $tpl_shared->setVariable("ERROR_MSG_FILE_TOO_LARGE", $lng->txt("form_msg_file_size_exceeds"));
193  $tpl_shared->setVariable("ERROR_MSG_WRONG_FILE_TYPE", $lng->txt("form_msg_file_wrong_file_type"));
194  $tpl_shared->setVariable("ERROR_MSG_EMPTY_FILE_OR_FOLDER", $lng->txt("error_empty_file_or_folder"));
195  $tpl_shared->setVariable("ERROR_MSG_UPLOAD_ZERO_BYTES", $lng->txt("error_upload_was_zero_bytes"));
196  $tpl_shared->setVariable("QUESTION_CANCEL_ALL", $lng->txt("cancel_file_upload"));
197  $tpl_shared->setVariable("ERROR_MSG_EXTRACT_FAILED", $lng->txt("error_extraction_failed"));
198  $tpl_shared->setVariable("PROGRESS_UPLOADING", $lng->txt("uploading"));
199  $tpl_shared->setVariable("PROGRESS_EXTRACTING", $lng->txt("extracting"));
200  $tpl_shared->setVariable("DROP_FILES_HERE", $lng->txt("drop_files_on_repo_obj_info"));
201  $tpl_shared->parseCurrentBlock();
202 
203  // initialize default values
204  $tpl_shared->setCurrentBlock("fileupload_defaults");
205  $tpl_shared->setVariable("CONCURRENT_UPLOADS", ilFileUploadSettings::getConcurrentUploads());
206  $tpl_shared->setVariable("MAX_FILE_SIZE", ilFileUploadUtil::getMaxFileSize());
207  $tpl_shared->setVariable("ALLOWED_SUFFIXES", "");
208  $tpl_shared->setVariable("SUPPORTED_ARCHIVES", "\"zip\"");
209  $tpl_shared->parseCurrentBlock();
210 
211  // load panel template
212  $tpl_panel = new ilTemplate("tpl.fileupload_panel_template.html", true, true, "Services/FileUpload");
213  $tpl_panel->setVariable("TXT_HEADER", $lng->txt("upload_files_title"));
214  $tpl_panel->setVariable("TXT_SHOW_ALL_DETAILS", $lng->txt('show_all_details'));
215  $tpl_panel->setVariable("TXT_HIDE_ALL_DETAILS", $lng->txt('hide_all_details'));
216  $tpl_panel->setVariable("TXT_SUBMIT", $lng->txt('upload_files'));
217  $tpl_panel->setVariable("TXT_CANCEL", $lng->txt('cancel'));
218 
219  $tpl_shared->setCurrentBlock("fileupload_panel_tmpl");
220  $tpl_shared->setVariable("PANEL_TEMPLATE_HTML", $tpl_panel->get());
221  $tpl_shared->parseCurrentBlock();
222 
223  // load row template
224  $tpl_row = new ilTemplate("tpl.fileupload_row_template.html", true, true, "Services/FileUpload");
225  $tpl_row->setVariable("IMG_ALERT", ilUtil::getImagePath("icon_alert.svg"));
226  $tpl_row->setVariable("ALT_ALERT", $lng->txt("alert"));
227  $tpl_row->setVariable("TXT_CANCEL", $lng->txt("cancel"));
228  $tpl_row->setVariable("TXT_REMOVE", $lng->txt("remove"));
229  $tpl_row->setVariable("TXT_TITLE", $lng->txt("title"));
230  $tpl_row->setVariable("TXT_DESCRIPTION", $lng->txt("description"));
231  $tpl_row->setVariable("TXT_EXTRACT", $lng->txt("unzip"));
232  $tpl_row->setVariable("TXT_KEEP_STRUCTURE", $lng->txt("take_over_structure"));
233  $tpl_row->setVariable("TXT_KEEP_STRUCTURE_INFO", $lng->txt("take_over_structure_info"));
234  $tpl_row->setVariable("TXT_PENDING", $lng->txt("upload_pending"));
235 
236  $tpl_shared->setCurrentBlock("fileupload_row_tmpl");
237  $tpl_shared->setVariable("ROW_TEMPLATE_HTML", $tpl_row->get());
238  $tpl_shared->parseCurrentBlock();
239 
240  // shared code now loaded
241  self::$shared_code_loaded = true;
242 
243  // create HTML
244  return $tpl_shared->get();
245  }
246 
247  protected function buildSuffixList($suffixes)
248  {
249  $list = $delim = "";
250 
251  if (is_array($suffixes) && count($suffixes) > 0) {
252  foreach ($suffixes as $suffix) {
253  $list .= $delim . "\"" . $suffix . "\"";
254  $delim = ", ";
255  }
256  }
257 
258  return $list;
259  }
260 
261  public function enableFormSubmit($input_field_id, $a_submit_name, $a_cancel_name)
262  {
263  $this->use_form = true;
264  $this->input_field_id = $input_field_id;
265  $this->submit_button_name = $a_submit_name;
266  $this->cancel_button_name = $a_cancel_name;
267  }
268 
274  public function setMaxFileSize($a_max)
275  {
276  $this->max_file_size = $a_max;
277  }
278 
282  public function getMaxFileSize()
283  {
284  return $this->max_file_size;
285  }
286 
292  public function setArchiveSuffixes($a_suffixes)
293  {
294  $this->archive_suffixes = $a_suffixes;
295  }
296 
302  public function getArchiveSuffixes()
303  {
305  }
306 
312  public function setSuffixes($a_suffixes)
313  {
314  $this->suffixes = $a_suffixes;
315  }
316 
322  public function getSuffixes()
323  {
324  return $this->suffixes;
325  }
326 
332  public function setInputFieldName($a_name)
333  {
334  $this->input_field_name = $a_name;
335  }
336 
342  public function getInputFieldName()
343  {
345  }
346 
347  public function setDropAreaId($a_id)
348  {
349  $this->drop_area_id = $a_id;
350  }
351 
352  public function getDropAreaId()
353  {
354  return $this->drop_area_id;
355  }
356 
357  public function setFileListId($a_id)
358  {
359  $this->file_list_id = $a_id;
360  }
361 
362  public function getFileListId()
363  {
364  return $this->file_list_id;
365  }
366 
367  public function setFileSelectButtonId($a_id)
368  {
369  $this->file_select_button_id = $a_id;
370  }
371 
372  public function getFileSelectButtonId()
373  {
375  }
376 
377  private function getUploadUrl()
378  {
379  global $DIC;
380  $ilCtrl = $DIC['ilCtrl'];
381 
382  // return null when the form is used
383  if ($this->use_form) {
384  return null;
385  }
386 
387  // check if supported
389  return false;
390  }
391 
392  // build upload URL
393  include_once("Modules/File/classes/class.ilObjFileGUI.php");
394  $ilCtrl->setParameterByClass(self::FILE_OBJ_GUI_CLASS, "ref_id", $this->ref_id);
395  $ilCtrl->setParameterByClass(self::FILE_OBJ_GUI_CLASS, "new_type", "file");
396 
397  return $ilCtrl->getFormActionByClass(self::FILE_OBJ_GUI_CLASS, "uploadFiles", "", true, false);
398  }
399 
404  private function makeJqueryId($a_id)
405  {
406  if (is_string($a_id) && strlen($a_id) > 0 && $a_id[0] !== '#') {
407  return '#' . $a_id;
408  }
409 
410  return $a_id;
411  }
412 }
static initFileUpload()
Initializes the file upload and loads the needed javascripts and styles.
setSuffixes($a_suffixes)
Set accepted suffixes.
if(isset($_FILES['img_file']['size']) && $_FILES['img_file']['size'] > 0) $tpl
static getStyleSheetLocation($mode="output", $a_css_name="", $a_css_location="")
get full style sheet file name (path inclusive) of current user
static getConcurrentUploads()
Gets the number of files that can be uploaded at the same time.
setInputFieldName($a_name)
Sets the name of the input field the files are submitted with.
enableFormSubmit($input_field_id, $a_submit_name, $a_cancel_name)
static _lookupTitle($a_id)
lookup object title
__construct($a_drop_zone_id, $a_ref_id=null, $current_obj=false)
Creates a new file upload GUI.
getMaxFileSize()
Gets the maximum file size in bytes.
global $ilCtrl
Definition: ilias.php:18
static getMaxFileSize()
Gets the maximum upload file size allowed in bytes.
setMaxFileSize($a_max)
Sets the maximum file size in bytes.
static encode($mixed, $suppress_native=false)
$lng
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
static _lookupObjId($a_id)
setArchiveSuffixes($a_suffixes)
Set accepted archive suffixes.
getArchiveSuffixes()
Get accepted archive suffixes.
getInputFieldName()
Gets the name of the input field the files are submitted with.
static isUploadSupported()
Determines whether file upload is supported at the current location.
getSuffixes()
Get accepted suffixes.
$DIC
Definition: xapitoken.php:46
$url
getSharedHtml()
Gets the code that is shared by all upload instances.
getHTML()
Gets the HTML code to enable the file upload.