ILIAS  Release_4_4_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 
200  $tpl_shared->setCurrentBlock("fileupload_panel_tmpl");
201  $tpl_shared->setVariable("PANEL_TEMPLATE_HTML", $tpl_panel->get());
202  $tpl_shared->parseCurrentBlock();
203 
204  // load row template
205  $tpl_row = new ilTemplate("tpl.fileupload_row_template.html", true, true, "Services/FileUpload");
206  $tpl_row->setVariable("IMG_ALERT", ilUtil::getImagePath("icon_alert_s.gif"));
207  $tpl_row->setVariable("ALT_ALERT", $lng->txt("alert"));
208  $tpl_row->setVariable("TXT_CANCEL", $lng->txt("cancel"));
209  $tpl_row->setVariable("TXT_REMOVE", $lng->txt("remove"));
210  $tpl_row->setVariable("TXT_TITLE", $lng->txt("title"));
211  $tpl_row->setVariable("TXT_DESCRIPTION", $lng->txt("description"));
212  $tpl_row->setVariable("TXT_EXTRACT", $lng->txt("unzip"));
213  $tpl_row->setVariable("TXT_KEEP_STRUCTURE", $lng->txt("take_over_structure"));
214  $tpl_row->setVariable("TXT_KEEP_STRUCTURE_INFO", $lng->txt("take_over_structure_info"));
215  $tpl_row->setVariable("TXT_PENDING", $lng->txt("upload_pending"));
216 
217  $tpl_shared->setCurrentBlock("fileupload_row_tmpl");
218  $tpl_shared->setVariable("ROW_TEMPLATE_HTML", $tpl_row->get());
219  $tpl_shared->parseCurrentBlock();
220 
221  // shared code now loaded
222  self::$shared_code_loaded = true;
223 
224  // create HTML
225  return $tpl_shared->get();
226  }
227 
228  protected function buildSuffixList($suffixes)
229  {
230  $list = $delim = "";
231 
232  if (is_array($suffixes) && count($suffixes) > 0)
233  {
234  foreach($suffixes as $suffix)
235  {
236  $list .= $delim . "\"" . $suffix . "\"";
237  $delim = ", ";
238  }
239  }
240 
241  return $list;
242  }
243 
244  public function enableFormSubmit($input_field_id, $a_submit_name, $a_cancel_name)
245  {
246  $this->use_form = true;
247  $this->input_field_id = $input_field_id;
248  $this->submit_button_name = $a_submit_name;
249  $this->cancel_button_name = $a_cancel_name;
250  }
251 
257  public function setMaxFileSize($a_max)
258  {
259  $this->max_file_size = $a_max;
260  }
261 
265  public function getMaxFileSize()
266  {
267  return $this->max_file_size;
268  }
269 
275  public function setArchiveSuffixes($a_suffixes)
276  {
277  $this->archive_suffixes = $a_suffixes;
278  }
279 
285  public function getArchiveSuffixes()
286  {
288  }
289 
295  public function setSuffixes($a_suffixes)
296  {
297  $this->suffixes = $a_suffixes;
298  }
299 
305  public function getSuffixes()
306  {
307  return $this->suffixes;
308  }
309 
315  public function setInputFieldName($a_name)
316  {
317  $this->input_field_name = $a_name;
318  }
319 
325  public function getInputFieldName()
326  {
328  }
329 
330  public function setDropAreaId($a_id)
331  {
332  $this->drop_area_id = $a_id;
333  }
334 
335  public function getDropAreaId()
336  {
337  return $this->drop_area_id;
338  }
339 
340  public function setFileListId($a_id)
341  {
342  $this->file_list_id = $a_id;
343  }
344 
345  public function getFileListId()
346  {
347  return $this->file_list_id;
348  }
349 
350  public function setFileSelectButtonId($a_id)
351  {
352  $this->file_select_button_id = $a_id;
353  }
354 
355  public function getFileSelectButtonId()
356  {
358  }
359 
360  private function getUploadUrl()
361  {
362  global $ilCtrl;
363 
364  // return null when the form is used
365  if ($this->use_form)
366  return null;
367 
368  // check if supported
370  return false;
371 
372  // build upload URL
373  include_once("Modules/File/classes/class.ilObjFileGUI.php");
374  $ilCtrl->setParameterByClass(self::FILE_OBJ_GUI_CLASS, "ref_id", $this->ref_id);
375  $ilCtrl->setParameterByClass(self::FILE_OBJ_GUI_CLASS, "new_type", "file");
376 
377  return $ilCtrl->getFormActionByClass(self::FILE_OBJ_GUI_CLASS, "uploadFiles", "", true, false);
378  }
379 
380  private function makeJqueryId($a_id)
381  {
382  if ($a_id != null && count($a_id) > 0)
383  {
384  if ($a_id[0] != "#")
385  return "#" . $a_id;
386  }
387 
388  return $a_id;
389  }
390 }
391 ?>