ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 $ilCtrl;
41 
42  $this->drop_zone_id = $a_drop_zone_id;
43  $this->ref_id = $a_ref_id;
44  $this->current_obj = $current_obj;
45  }
46 
50  public static function initFileUpload()
51  {
52  global $tpl;
53 
54  // needed scripts
55  $tpl->addJavaScript("./Services/FileUpload/js/tmpl.js");
56  $tpl->addJavaScript("./Services/FileUpload/js/jquery.ui.widget.js");
57  $tpl->addJavaScript("./Services/FileUpload/js/jquery.iframe-transport.js");
58  $tpl->addJavaScript("./Services/FileUpload/js/jquery.fileupload.js");
59  $tpl->addJavaScript("./Services/FileUpload/js/jquery.ba-dotimeout.min.js");
60  $tpl->addJavaScript("./Services/FileUpload/js/ilFileUpload.js");
61 
62  // needed styles
63  $tpl->addCss(ilUtil::getStyleSheetLocation("filesystem", "fileupload.css", "Services/FileUpload"));
64  }
65 
71  public function getHTML()
72  {
73  global $lng, $ilCtrl, $tpl;
74 
75  // get values
76  $id = $this->ref_id;
77 
78  // build options
79  $options = new stdClass();
80  $options->dropZone = $this->makeJqueryId($this->drop_zone_id);
81 
82  // set url
83  $url = $this->getUploadUrl();
84  if ($url === false)
85  return "";
86 
87  if ($url != null)
88  $options->url = $this->getUploadUrl();
89 
90  // get title and replace quotes with HTML entities
91  if ($this->ref_id != null && !$this->use_form)
92  {
93  $title = ilObject::_lookupTitle(ilObject::_lookupObjId($this->ref_id));
94  $title = str_replace("\"", "&quot;", $title);
95  $title = str_replace("'", "&#039;", $title);
96  $options->listTitle = $title;
97  }
98 
99  // input field (use id if specified)
100  if ($this->input_field_id != null)
101  $options->fileInput = $this->makeJqueryId($this->input_field_id);
102  else if ($this->input_field_name != null)
103  $options->fileInput = $this->input_field_name;
104 
105  // buttons
106  if ($this->submit_button_name != null)
107  $options->submitButton = $this->submit_button_name;
108  if ($this->cancel_button_name != null)
109  $options->cancelButton = $this->cancel_button_name;
110 
111  // drop area
112  if ($this->drop_area_id != null)
113  $options->dropArea = $this->makeJqueryId($this->drop_area_id);
114 
115  // file list
116  if ($this->file_list_id != null)
117  $options->fileList = $this->makeJqueryId($this->file_list_id);
118 
119  // file list
120  if ($this->file_select_button_id != null)
121  $options->fileSelectButton = $this->makeJqueryId($this->file_select_button_id);
122 
123  // max size
124  $max_size = $this->getMaxFileSize();
125  if ($max_size != null)
126  $options->maxFileSize = $max_size;
127 
128  // allowed extensions
129  $allowed_suffixes = $this->buildSuffixList($this->getSuffixes());
130  if ($allowed_suffixes != "")
131  $options->allowedExtensions = "[" . $allowed_suffixes . "]";
132 
133  // supported archive extensions
134  $supported_archives = $this->buildSuffixList($this->getArchiveSuffixes());
135  if ($supported_archives != "")
136  $options->supportedArchives = "[" . $supported_archives . "]";
137 
138  // inject load script
139  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
140 
141  $onLoadCode = "";
142  if ($this->use_form)
143  $onLoadCode = "var fileUpload$id = new ilFileUpload($id, " . ilJsonUtil::encode($options) . ");";
144  else
145  $onLoadCode = "il.FileUpload.add(\"$id\", " . ilJsonUtil::encode($options) . ", " . ($this->current_obj ? "true" : "false") . ");";
146 
147  $tpl->addOnLoadCode($onLoadCode);
148 
149  // return shared code
150  return $this->getSharedHtml();
151  }
152 
158  protected function getSharedHtml()
159  {
160  global $lng;
161 
162  // already loaded?
163  if (self::$shared_code_loaded)
164  return "";
165 
166  // make sure required scripts are loaded
168 
169  // load script template
170  $tpl_shared = new ilTemplate("tpl.fileupload_shared.html", true, true, "Services/FileUpload");
171 
172  // initialize localized texts
173  $lng->loadLanguageModule("form");
174  $tpl_shared->setCurrentBlock("fileupload_texts");
175  $tpl_shared->setVariable("ERROR_MSG_FILE_TOO_LARGE", $lng->txt("form_msg_file_size_exceeds"));
176  $tpl_shared->setVariable("ERROR_MSG_WRONG_FILE_TYPE", $lng->txt("form_msg_file_wrong_file_type"));
177  $tpl_shared->setVariable("ERROR_MSG_EMPTY_FILE_OR_FOLDER", $lng->txt("error_empty_file_or_folder"));
178  $tpl_shared->setVariable("ERROR_MSG_UPLOAD_ZERO_BYTES", $lng->txt("error_upload_was_zero_bytes"));
179  $tpl_shared->setVariable("QUESTION_CANCEL_ALL", $lng->txt("cancel_file_upload"));
180  $tpl_shared->setVariable("ERROR_MSG_EXTRACT_FAILED", $lng->txt("error_extraction_failed"));
181  $tpl_shared->setVariable("PROGRESS_UPLOADING", $lng->txt("uploading"));
182  $tpl_shared->setVariable("PROGRESS_EXTRACTING", $lng->txt("extracting"));
183  $tpl_shared->setVariable("DROP_FILES_HERE", $lng->txt("drop_files_on_repo_obj_info"));
184  $tpl_shared->parseCurrentBlock();
185 
186  // initialize default values
187  $tpl_shared->setCurrentBlock("fileupload_defaults");
188  $tpl_shared->setVariable("CONCURRENT_UPLOADS", ilFileUploadSettings::getConcurrentUploads());
189  $tpl_shared->setVariable("MAX_FILE_SIZE", ilFileUploadUtil::getMaxFileSize());
190  $tpl_shared->setVariable("ALLOWED_SUFFIXES", "");
191  $tpl_shared->setVariable("SUPPORTED_ARCHIVES", "\"zip\"");
192  $tpl_shared->parseCurrentBlock();
193 
194  // load panel template
195  $tpl_panel = new ilTemplate("tpl.fileupload_panel_template.html", true, true, "Services/FileUpload");
196  $tpl_panel->setVariable("TXT_HEADER", $lng->txt("upload_files_title"));
197  $tpl_panel->setVariable("TXT_SHOW_ALL_DETAILS", $lng->txt('show_all_details'));
198  $tpl_panel->setVariable("TXT_HIDE_ALL_DETAILS", $lng->txt('hide_all_details'));
199  $tpl_panel->setVariable("TXT_SUBMIT", $lng->txt('upload_files'));
200  $tpl_panel->setVariable("TXT_CANCEL", $lng->txt('cancel'));
201 
202  $tpl_shared->setCurrentBlock("fileupload_panel_tmpl");
203  $tpl_shared->setVariable("PANEL_TEMPLATE_HTML", $tpl_panel->get());
204  $tpl_shared->parseCurrentBlock();
205 
206  // load row template
207  $tpl_row = new ilTemplate("tpl.fileupload_row_template.html", true, true, "Services/FileUpload");
208  $tpl_row->setVariable("IMG_ALERT", ilUtil::getImagePath("icon_alert.svg"));
209  $tpl_row->setVariable("ALT_ALERT", $lng->txt("alert"));
210  $tpl_row->setVariable("TXT_CANCEL", $lng->txt("cancel"));
211  $tpl_row->setVariable("TXT_REMOVE", $lng->txt("remove"));
212  $tpl_row->setVariable("TXT_TITLE", $lng->txt("title"));
213  $tpl_row->setVariable("TXT_DESCRIPTION", $lng->txt("description"));
214  $tpl_row->setVariable("TXT_EXTRACT", $lng->txt("unzip"));
215  $tpl_row->setVariable("TXT_KEEP_STRUCTURE", $lng->txt("take_over_structure"));
216  $tpl_row->setVariable("TXT_KEEP_STRUCTURE_INFO", $lng->txt("take_over_structure_info"));
217  $tpl_row->setVariable("TXT_PENDING", $lng->txt("upload_pending"));
218 
219  $tpl_shared->setCurrentBlock("fileupload_row_tmpl");
220  $tpl_shared->setVariable("ROW_TEMPLATE_HTML", $tpl_row->get());
221  $tpl_shared->parseCurrentBlock();
222 
223  // shared code now loaded
224  self::$shared_code_loaded = true;
225 
226  // create HTML
227  return $tpl_shared->get();
228  }
229 
230  protected function buildSuffixList($suffixes)
231  {
232  $list = $delim = "";
233 
234  if (is_array($suffixes) && count($suffixes) > 0)
235  {
236  foreach($suffixes as $suffix)
237  {
238  $list .= $delim . "\"" . $suffix . "\"";
239  $delim = ", ";
240  }
241  }
242 
243  return $list;
244  }
245 
246  public function enableFormSubmit($input_field_id, $a_submit_name, $a_cancel_name)
247  {
248  $this->use_form = true;
249  $this->input_field_id = $input_field_id;
250  $this->submit_button_name = $a_submit_name;
251  $this->cancel_button_name = $a_cancel_name;
252  }
253 
259  public function setMaxFileSize($a_max)
260  {
261  $this->max_file_size = $a_max;
262  }
263 
267  public function getMaxFileSize()
268  {
269  return $this->max_file_size;
270  }
271 
277  public function setArchiveSuffixes($a_suffixes)
278  {
279  $this->archive_suffixes = $a_suffixes;
280  }
281 
287  public function getArchiveSuffixes()
288  {
290  }
291 
297  public function setSuffixes($a_suffixes)
298  {
299  $this->suffixes = $a_suffixes;
300  }
301 
307  public function getSuffixes()
308  {
309  return $this->suffixes;
310  }
311 
317  public function setInputFieldName($a_name)
318  {
319  $this->input_field_name = $a_name;
320  }
321 
327  public function getInputFieldName()
328  {
330  }
331 
332  public function setDropAreaId($a_id)
333  {
334  $this->drop_area_id = $a_id;
335  }
336 
337  public function getDropAreaId()
338  {
339  return $this->drop_area_id;
340  }
341 
342  public function setFileListId($a_id)
343  {
344  $this->file_list_id = $a_id;
345  }
346 
347  public function getFileListId()
348  {
349  return $this->file_list_id;
350  }
351 
352  public function setFileSelectButtonId($a_id)
353  {
354  $this->file_select_button_id = $a_id;
355  }
356 
357  public function getFileSelectButtonId()
358  {
360  }
361 
362  private function getUploadUrl()
363  {
364  global $ilCtrl;
365 
366  // return null when the form is used
367  if ($this->use_form)
368  return null;
369 
370  // check if supported
372  return false;
373 
374  // build upload URL
375  include_once("Modules/File/classes/class.ilObjFileGUI.php");
376  $ilCtrl->setParameterByClass(self::FILE_OBJ_GUI_CLASS, "ref_id", $this->ref_id);
377  $ilCtrl->setParameterByClass(self::FILE_OBJ_GUI_CLASS, "new_type", "file");
378 
379  return $ilCtrl->getFormActionByClass(self::FILE_OBJ_GUI_CLASS, "uploadFiles", "", true, false);
380  }
381 
382  private function makeJqueryId($a_id)
383  {
384  if ($a_id != null && count($a_id) > 0)
385  {
386  if ($a_id[0] != "#")
387  return "#" . $a_id;
388  }
389 
390  return $a_id;
391  }
392 }
393 ?>