ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilDragDropFileInputGUI.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 include_once("./Services/Form/classes/class.ilFileInputGUI.php");
5 
14 {
18  protected $lng;
19 
20  private $uniqueId = 0;
21  private $archive_suffixes = array();
22  private $submit_button_name = null;
23  private $cancel_button_name = null;
24 
25  private static $uniqueInc = 1;
26 
27  private static function getNextUniqueId()
28  {
29  return self::$uniqueInc++;
30  }
31 
38  public function __construct($a_title = "", $a_postvar = "")
39  {
40  global $DIC;
41 
42  $this->lng = $DIC->language();
43  parent::__construct($a_title, $a_postvar);
44  $this->uniqueId = self::getNextUniqueId();
45  }
46 
52  public function setArchiveSuffixes($a_suffixes)
53  {
54  $this->archive_suffixes = $a_suffixes;
55  }
56 
62  public function getArchiveSuffixes()
63  {
65  }
66 
67  public function setCommandButtonNames($a_submit_name, $a_cancel_name)
68  {
69  $this->submit_button_name = $a_submit_name;
70  $this->cancel_button_name = $a_cancel_name;
71  }
72 
76  public function render($a_mode = "")
77  {
78  $lng = $this->lng;
79 
80  $quota_exceeded = $quota_legend = false;
81  if (self::$check_wsp_quota) {
82  include_once "Services/DiskQuota/classes/class.ilDiskQuotaHandler.php";
84  $lng->loadLanguageModule("file");
85  return $lng->txt("personal_workspace_quota_exceeded_warning");
86  } else {
87  $quota_legend = ilDiskQuotaHandler::getStatusLegend();
88  }
89  }
90 
91  // make sure jQuery is loaded
93 
94  // add file upload scripts
95  include_once("./Services/FileUpload/classes/class.ilFileUploadGUI.php");
97 
98  // load template
99  $this->tpl = new ilTemplate("tpl.prop_dndfiles.html", true, true, "Services/Form");
100 
101  // general variables
102  $this->tpl->setVariable("UPLOAD_ID", $this->uniqueId);
103 
104  // input
105  $this->tpl->setVariable("FILE_SELECT_ICON", ilObject::_getIcon("", "", "fold"));
106  $this->tpl->setVariable("TXT_SHOW_ALL_DETAILS", $lng->txt('show_all_details'));
107  $this->tpl->setVariable("TXT_HIDE_ALL_DETAILS", $lng->txt('hide_all_details'));
108  $this->tpl->setVariable("TXT_SELECTED_FILES", $lng->txt('selected_files'));
109  $this->tpl->setVariable("TXT_DRAG_FILES_HERE", $lng->txt('drag_files_here'));
110  $this->tpl->setVariable("TXT_NUM_OF_SELECTED_FILES", $lng->txt('num_of_selected_files'));
111  $this->tpl->setVariable("TXT_SELECT_FILES_FROM_COMPUTER", $lng->txt('select_files_from_computer'));
112  $this->tpl->setVariable("TXT_OR", $lng->txt('logic_or'));
113  $this->tpl->setVariable("INPUT_ACCEPT_SUFFIXES", $this->getInputAcceptSuffixes($this->getSuffixes()));
114 
115  // info
116  $this->tpl->setCurrentBlock("max_size");
117  $this->tpl->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice") . " " . $this->getMaxFileSizeString());
118  $this->tpl->parseCurrentBlock();
119 
120  if ($quota_legend) {
121  $this->tpl->setVariable("TXT_MAX_SIZE", $quota_legend);
122  $this->tpl->parseCurrentBlock();
123  }
124 
125  $this->outputSuffixes($this->tpl);
126 
127  // create file upload object
128  $upload = new ilFileUploadGUI("ilFileUploadDropZone_" . $this->uniqueId, $this->uniqueId, false);
129  $upload->enableFormSubmit("ilFileUploadInput_" . $this->uniqueId, $this->submit_button_name, $this->cancel_button_name);
130  $upload->setDropAreaId("ilFileUploadDropArea_" . $this->uniqueId);
131  $upload->setFileListId("ilFileUploadList_" . $this->uniqueId);
132  $upload->setFileSelectButtonId("ilFileUploadFileSelect_" . $this->uniqueId);
133 
134  $this->tpl->setVariable("FILE_UPLOAD", $upload->getHTML());
135 
136  return $this->tpl->get();
137  }
138 
144  public function checkInput()
145  {
146  $lng = $this->lng;
147 
148  // if no information is received, something went wrong
149  // this is e.g. the case, if the post_max_size has been exceeded
150  if (!is_array($_FILES[$this->getPostVar()])) {
151  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
152  return false;
153  }
154 
155  // empty file, could be a folder
156  if ($_FILES[$this->getPostVar()]["size"] < 1) {
157  $this->setAlert($lng->txt("error_upload_was_zero_bytes"));
158  return false;
159  }
160 
161  // call base
162  $inputValid = parent::checkInput();
163 
164  // set additionally sent input on post array
165  if ($inputValid) {
166  $_POST[$this->getPostVar()]["extract"] = isset($_POST["extract"]) ? (bool) $_POST["extract"] : false;
167  $_POST[$this->getPostVar()]["title"] = isset($_POST["title"]) ? $_POST["title"] : "";
168  $_POST[$this->getPostVar()]["description"] = isset($_POST["description"]) ? $_POST["description"] : "";
169  $_POST[$this->getPostVar()]["keep_structure"] = isset($_POST["keep_structure"]) ? (bool) $_POST["keep_structure"] : true;
170 
171  include_once("./Services/Utilities/classes/class.ilStr.php");
172  $_POST[$this->getPostVar()]["name"] = ilStr::normalizeUtf8String($_POST[$this->getPostVar()]["name"]);
173  $_POST[$this->getPostVar()]["title"] = ilStr::normalizeUtf8String($_POST[$this->getPostVar()]["title"]);
174  }
175 
176  return $inputValid;
177  }
178 
179  protected function getInputAcceptSuffixes($suffixes)
180  {
181  $list = $delim = "";
182 
183  if (is_array($suffixes) && count($suffixes) > 0) {
184  foreach ($suffixes as $suffix) {
185  $list .= $delim . "." . $suffix;
186  $delim = ",";
187  }
188  }
189 
190  return $list;
191  }
192 
193  protected function buildSuffixList($suffixes)
194  {
195  $list = $delim = "";
196 
197  if (is_array($suffixes) && count($suffixes) > 0) {
198  foreach ($suffixes as $suffix) {
199  $list .= $delim . "\"" . $suffix . "\"";
200  $delim = ", ";
201  }
202  }
203 
204  return $list;
205  }
206 
207  protected function getMaxFileSize()
208  {
209  // get the value for the maximal uploadable filesize from the php.ini (if available)
210  $umf = ini_get("upload_max_filesize");
211  // get the value for the maximal post data from the php.ini (if available)
212  $pms = ini_get("post_max_size");
213 
214  //convert from short-string representation to "real" bytes
215  $multiplier_a = array("K" => 1024, "M" => 1024 * 1024, "G" => 1024 * 1024 * 1024);
216 
217  $umf_parts = preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
218  $pms_parts = preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
219 
220  if (count($umf_parts) == 2) {
221  $umf = $umf_parts[0] * $multiplier_a[$umf_parts[1]];
222  }
223  if (count($pms_parts) == 2) {
224  $pms = $pms_parts[0] * $multiplier_a[$pms_parts[1]];
225  }
226 
227  // use the smaller one as limit
228  $max_filesize = min($umf, $pms);
229 
230  if (!$max_filesize) {
231  $max_filesize = max($umf, $pms);
232  }
233 
234  return $max_filesize;
235  }
236 }
static initFileUpload()
Initializes the file upload and loads the needed javascripts and styles.
getSuffixes()
Get Accepted Suffixes.
if(isset($_REQUEST['delete'])) $list
Definition: registry.php:41
getArchiveSuffixes()
Get accepted archive suffixes.
global $DIC
Definition: saml.php:7
getPostVar()
Get Post Variable.
This class represents a file property in a property form.
static normalizeUtf8String($a_str)
Normalize UTF8 string.
setArchiveSuffixes($a_suffixes)
Set accepted archive suffixes.
setAlert($a_alert)
Set Alert Text.
__construct($a_title="", $a_postvar="")
Constructor.
render($a_mode="")
Render html.
special template class to simplify handling of ITX/PEAR
static isUploadPossible($a_additional_size=null)
setCommandButtonNames($a_submit_name, $a_cancel_name)
checkInput()
Check input, strip slashes etc.
outputSuffixes($a_tpl, $a_block="allowed_suffixes")
static initjQuery($a_tpl=null)
inits and adds the jQuery JS-File to the global or a passed template
$_POST["username"]
This class represents a file input property where multiple files can be dopped in a property form...