ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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");
63 
64  // needed styles
65  $tpl->addCss(ilUtil::getStyleSheetLocation("filesystem", "fileupload.css", "Services/FileUpload"));
66  }
67 
73  public function getHTML()
74  {
75  global $DIC;
76  $lng = $DIC['lng'];
77  $ilCtrl = $DIC['ilCtrl'];
78  $tpl = $DIC['tpl'];
79 
80  // get values
82 
83  // build options
84  $options = new stdClass();
85  $options->dropZone = $this->makeJqueryId($this->drop_zone_id);
86 
87  // set url
88  $url = $this->getUploadUrl();
89  if ($url === false) {
90  return "";
91  }
92 
93  if ($url != null) {
94  $options->url = $this->getUploadUrl();
95  }
96 
97  // get title and replace quotes with HTML entities
98  if ($this->ref_id != null && !$this->use_form) {
100  $title = str_replace("\"", "&quot;", $title);
101  $title = str_replace("'", "&#039;", $title);
102  $options->listTitle = $title;
103  }
104 
105  // input field (use id if specified)
106  if ($this->input_field_id != null) {
107  $options->fileInput = $this->makeJqueryId($this->input_field_id);
108  } elseif ($this->input_field_name != null) {
109  $options->fileInput = $this->input_field_name;
110  }
111 
112  // buttons
113  if ($this->submit_button_name != null) {
114  $options->submitButton = $this->submit_button_name;
115  }
116  if ($this->cancel_button_name != null) {
117  $options->cancelButton = $this->cancel_button_name;
118  }
119 
120  // drop area
121  if ($this->drop_area_id != null) {
122  $options->dropArea = $this->makeJqueryId($this->drop_area_id);
123  }
124 
125  // file list
126  if ($this->file_list_id != null) {
127  $options->fileList = $this->makeJqueryId($this->file_list_id);
128  }
129 
130  // file list
131  if ($this->file_select_button_id != null) {
132  $options->fileSelectButton = $this->makeJqueryId($this->file_select_button_id);
133  }
134 
135  // max size
136  $max_size = $this->getMaxFileSize();
137  if ($max_size != null) {
138  $options->maxFileSize = $max_size;
139  }
140 
141  // allowed extensions
142  $allowed_suffixes = $this->buildSuffixList($this->getSuffixes());
143  if ($allowed_suffixes != "") {
144  $options->allowedExtensions = "[" . $allowed_suffixes . "]";
145  }
146 
147  // supported archive extensions
148  $supported_archives = $this->buildSuffixList($this->getArchiveSuffixes());
149  if ($supported_archives != "") {
150  $options->supportedArchives = "[" . $supported_archives . "]";
151  }
152 
153  // inject load script
154  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
155 
156  $onLoadCode = "";
157  if ($this->use_form) {
158  $onLoadCode = "var fileUpload$id = new ilFileUpload($id, " . ilJsonUtil::encode($options) . ");";
159  } else {
160  $onLoadCode = "il.FileUpload.add(\"$id\", " . ilJsonUtil::encode($options) . ", " . ($this->current_obj ? "true" : "false") . ");";
161  }
162 
163  $tpl->addOnLoadCode($onLoadCode);
164 
165  // return shared code
166  return $this->getSharedHtml();
167  }
168 
174  protected function getSharedHtml()
175  {
176  global $DIC;
177  $lng = $DIC['lng'];
178 
179  // already loaded?
180  if (self::$shared_code_loaded) {
181  return "";
182  }
183 
184  // make sure required scripts are loaded
185  self::initFileUpload();
186 
187  // load script template
188  $tpl_shared = new ilTemplate("tpl.fileupload_shared.html", true, true, "Services/FileUpload");
189 
190  // initialize localized texts
191  $lng->loadLanguageModule("form");
192  $tpl_shared->setCurrentBlock("fileupload_texts");
193  $tpl_shared->setVariable("ERROR_MSG_FILE_TOO_LARGE", $lng->txt("form_msg_file_size_exceeds"));
194  $tpl_shared->setVariable("ERROR_MSG_WRONG_FILE_TYPE", $lng->txt("form_msg_file_wrong_file_type"));
195  $tpl_shared->setVariable("ERROR_MSG_EMPTY_FILE_OR_FOLDER", $lng->txt("error_empty_file_or_folder"));
196  $tpl_shared->setVariable("ERROR_MSG_UPLOAD_ZERO_BYTES", $lng->txt("error_upload_was_zero_bytes"));
197  $tpl_shared->setVariable("QUESTION_CANCEL_ALL", $lng->txt("cancel_file_upload"));
198  $tpl_shared->setVariable("ERROR_MSG_EXTRACT_FAILED", $lng->txt("error_extraction_failed"));
199  $tpl_shared->setVariable("PROGRESS_UPLOADING", $lng->txt("uploading"));
200  $tpl_shared->setVariable("PROGRESS_EXTRACTING", $lng->txt("extracting"));
201  $tpl_shared->setVariable("DROP_FILES_HERE", $lng->txt("drop_files_on_repo_obj_info"));
202  $tpl_shared->parseCurrentBlock();
203 
204  // initialize default values
205  $tpl_shared->setCurrentBlock("fileupload_defaults");
206  $tpl_shared->setVariable("CONCURRENT_UPLOADS", ilFileUploadSettings::getConcurrentUploads());
207  $tpl_shared->setVariable("MAX_FILE_SIZE", ilFileUploadUtil::getMaxFileSize());
208  $tpl_shared->setVariable("ALLOWED_SUFFIXES", "");
209  $tpl_shared->setVariable("SUPPORTED_ARCHIVES", "\"zip\"");
210  $tpl_shared->parseCurrentBlock();
211 
212  // load panel template
213  $tpl_panel = new ilTemplate("tpl.fileupload_panel_template.html", true, true, "Services/FileUpload");
214  $tpl_panel->setVariable("TXT_HEADER", $lng->txt("upload_files_title"));
215  $tpl_panel->setVariable("TXT_SHOW_ALL_DETAILS", $lng->txt('show_all_details'));
216  $tpl_panel->setVariable("TXT_HIDE_ALL_DETAILS", $lng->txt('hide_all_details'));
217  $tpl_panel->setVariable("TXT_SUBMIT", $lng->txt('upload_files'));
218  $tpl_panel->setVariable("TXT_CANCEL", $lng->txt('cancel'));
219 
220  $tpl_shared->setCurrentBlock("fileupload_panel_tmpl");
221  $tpl_shared->setVariable("PANEL_TEMPLATE_HTML", $tpl_panel->get());
222  $tpl_shared->parseCurrentBlock();
223 
224  // load row template
225  $tpl_row = new ilTemplate("tpl.fileupload_row_template.html", true, true, "Services/FileUpload");
226  $tpl_row->setVariable("IMG_ALERT", ilUtil::getImagePath("icon_alert.svg"));
227  $tpl_row->setVariable("ALT_ALERT", $lng->txt("alert"));
228  $tpl_row->setVariable("TXT_CANCEL", $lng->txt("cancel"));
229  $tpl_row->setVariable("TXT_REMOVE", $lng->txt("remove"));
230  $tpl_row->setVariable("TXT_TITLE", $lng->txt("title"));
231  $tpl_row->setVariable("TXT_DESCRIPTION", $lng->txt("description"));
232  $tpl_row->setVariable("TXT_EXTRACT", $lng->txt("unzip"));
233  $tpl_row->setVariable("TXT_KEEP_STRUCTURE", $lng->txt("take_over_structure"));
234  $tpl_row->setVariable("TXT_KEEP_STRUCTURE_INFO", $lng->txt("take_over_structure_info"));
235  $tpl_row->setVariable("TXT_PENDING", $lng->txt("upload_pending"));
236 
237  $tpl_shared->setCurrentBlock("fileupload_row_tmpl");
238  $tpl_shared->setVariable("ROW_TEMPLATE_HTML", $tpl_row->get());
239  $tpl_shared->parseCurrentBlock();
240 
241  // shared code now loaded
242  self::$shared_code_loaded = true;
243 
244  // create HTML
245  return $tpl_shared->get();
246  }
247 
248  protected function buildSuffixList($suffixes)
249  {
250  $list = $delim = "";
251 
252  if (is_array($suffixes) && count($suffixes) > 0) {
253  foreach ($suffixes as $suffix) {
254  $list .= $delim . "\"" . $suffix . "\"";
255  $delim = ", ";
256  }
257  }
258 
259  return $list;
260  }
261 
262  public function enableFormSubmit($input_field_id, $a_submit_name, $a_cancel_name)
263  {
264  $this->use_form = true;
265  $this->input_field_id = $input_field_id;
266  $this->submit_button_name = $a_submit_name;
267  $this->cancel_button_name = $a_cancel_name;
268  }
269 
275  public function setMaxFileSize($a_max)
276  {
277  $this->max_file_size = $a_max;
278  }
279 
283  public function getMaxFileSize()
284  {
285  return $this->max_file_size;
286  }
287 
293  public function setArchiveSuffixes($a_suffixes)
294  {
295  $this->archive_suffixes = $a_suffixes;
296  }
297 
303  public function getArchiveSuffixes()
304  {
306  }
307 
313  public function setSuffixes($a_suffixes)
314  {
315  $this->suffixes = $a_suffixes;
316  }
317 
323  public function getSuffixes()
324  {
325  return $this->suffixes;
326  }
327 
333  public function setInputFieldName($a_name)
334  {
335  $this->input_field_name = $a_name;
336  }
337 
343  public function getInputFieldName()
344  {
346  }
347 
348  public function setDropAreaId($a_id)
349  {
350  $this->drop_area_id = $a_id;
351  }
352 
353  public function getDropAreaId()
354  {
355  return $this->drop_area_id;
356  }
357 
358  public function setFileListId($a_id)
359  {
360  $this->file_list_id = $a_id;
361  }
362 
363  public function getFileListId()
364  {
365  return $this->file_list_id;
366  }
367 
368  public function setFileSelectButtonId($a_id)
369  {
370  $this->file_select_button_id = $a_id;
371  }
372 
373  public function getFileSelectButtonId()
374  {
376  }
377 
378  private function getUploadUrl()
379  {
380  global $DIC;
381  $ilCtrl = $DIC['ilCtrl'];
382 
383  // return null when the form is used
384  if ($this->use_form) {
385  return null;
386  }
387 
388  // check if supported
390  return false;
391  }
392 
393  // build upload URL
394  include_once("Modules/File/classes/class.ilObjFileGUI.php");
395  $ilCtrl->setParameterByClass(self::FILE_OBJ_GUI_CLASS, "ref_id", $this->ref_id);
396  $ilCtrl->setParameterByClass(self::FILE_OBJ_GUI_CLASS, "new_type", "file");
397 
398  return $ilCtrl->getFormActionByClass(self::FILE_OBJ_GUI_CLASS, "uploadFiles", "", true, false);
399  }
400 
405  private function makeJqueryId($a_id)
406  {
407  if (is_string($a_id) && strlen($a_id) > 0 && $a_id[0] !== '#') {
408  return '#' . $a_id;
409  }
410 
411  return $a_id;
412  }
413 }
static initFileUpload()
Initializes the file upload and loads the needed javascripts and styles.
setSuffixes($a_suffixes)
Set accepted suffixes.
if(isset($_REQUEST['delete'])) $list
Definition: registry.php:41
global $DIC
Definition: saml.php:7
static getStyleSheetLocation($mode="output", $a_css_name="", $a_css_location="")
get full style sheet file name (path inclusive) of current user
$tpl
Definition: ilias.php:10
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.
if(!array_key_exists('StateId', $_REQUEST)) $id
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)
special template class to simplify handling of ITX/PEAR
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.
$url
getSharedHtml()
Gets the code that is shared by all upload instances.
getHTML()
Gets the HTML code to enable the file upload.