ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilFileStandardDropzoneInputGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
34 {
35  use \ILIAS\Modules\OrgUnit\ARHelper\DIC;
36 
37  const ASYNC_FILEUPLOAD = "async_fileupload";
43  protected static $count = 0;
49  protected $upload_url = '';
54  protected $max_files = 1;
59  protected $max_file_size;
63  protected $dropzone_message = '';
64 
68  protected $cancel_command;
69 
76  public function __construct($cancel_command, $a_title = "", $a_postvar = "")
77  {
78  $this->cancel_command = $cancel_command;
79  parent::__construct($a_title, $a_postvar);
80  }
81 
85  public function getUploadUrl()
86  {
87  return $this->upload_url;
88  }
89 
90 
100  public function setUploadUrl($upload_url)
101  {
102  $this->upload_url = $upload_url;
103 
104  return $this;
105  }
106 
107 
111  public function getMaxFiles()
112  {
113  return $this->max_files;
114  }
115 
116 
121  public function setMaxFiles($max_files)
122  {
123  $this->max_files = $max_files;
124  }
125 
126 
130  public function getMaxFilesize()
131  {
132  return $this->max_file_size;
133  }
134 
135 
141  public function setMaxFilesize(\ILIAS\Data\DataSize $max_file_size)
142  {
143  $this->max_file_size = $max_file_size;
144  }
145 
146 
150  public function getDropzoneMessage()
151  {
153  }
154 
155 
160  {
161  $this->dropzone_message = $dropzone_message;
162  }
163 
164 
168  public function render($a_mode = "")
169  {
170  $this->handleUploadURL();
171  $this->handleSuffixes();
172 
173  $f = $this->ui()->factory();
174  $r = $this->ui()->renderer();
175 
176  $this->initDropzoneMessage();
177 
178  $dropzone = $f->dropzone()
179  ->file()
180  ->standard($this->getUploadUrl())
181  ->withParameterName($this->getPostVar())
182  ->withMaxFiles($this->getMaxFiles())
183  ->withMessage($this->getDropzoneMessage())
184  ->withAllowedFileTypes($this->getSuffixes());
185  $dropzone = $this->handleMaxFileSize($dropzone);
186  if ($this->isFileNameSelectionEnabled()) {
187  $dropzone = $dropzone->withUserDefinedFileNamesEnabled(true);
188  }
189 
190  $render = $r->render($dropzone);
191 
192  $n = ++self::$count;
193  $out = "<div id='ilFileStandardDropzoneInputGUIWrapper{$n}'>" . $render . '</div>';
194  // We need some javascript magic
195 
196  $this->ui()->mainTemplate()->addJavaScript('./Services/Form/js/ilFileStandardDropzoneInputGUI.js');
197  $this->ui()->mainTemplate()->addOnLoadCode("ilFileStandardDropzoneInputGUI.init('ilFileStandardDropzoneInputGUIWrapper{$n}', '{$this->cancel_command}');");
198 
199  return $out;
200  }
201 
202 
206  public function checkInput()
207  {
208  $hasUploads = $this->dic()->upload()->hasUploads();
209  if ($this->getRequired() && !$hasUploads) {
210  return false; // No file uploaded but is was required
211  }
212 
213  if ($hasUploads) {
214  try {
215  $_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()];
216  } catch (Exception $e) {
217  return false;
218  }
219 
220  return true;
221  }
222 
223  return true;
224  }
225 
226 
227  protected function handleUploadURL()
228  {
229  if (!$this->getUploadUrl()) {
230  $parentWrapper = $this;
231  while (!$parentWrapper instanceof ilPropertyFormGUI && $parentWrapper !== null) {
232  $parentWrapper = $parentWrapper->getParent();
233  }
234 
235  $str_replace = str_replace("&amp;", "&", $parentWrapper->getFormAction());
236  $this->setUploadUrl($str_replace . "&" . self::ASYNC_FILEUPLOAD . "=true");
237  }
238  }
239 
240 
241  protected function handleSuffixes()
242  {
243  if (!is_array($this->getSuffixes())) {
244  $this->setSuffixes(array());
245  }
246  }
247 
248 
254  protected function handleMaxFileSize($dropzone)
255  {
256  if ($this->getMaxFilesize()) {
257  $dropzone = $dropzone->withFileSizeLimit($this->getMaxFilesize());
258  }
259 
260  return $dropzone;
261  }
262 
263 
264  protected function initDropzoneMessage()
265  {
266  if (!$this->getDropzoneMessage()) {
267  if ($this->getMaxFiles() === 1) {
268  $this->setDropzoneMessage($this->lng()->txt('drag_file_here'));
269  } else {
270  $this->setDropzoneMessage($this->lng()->txt('drag_files_here'));
271  }
272  }
273  }
274 }
getSuffixes()
Get Accepted Suffixes.
This class represents a property form user interface.
setMaxFilesize(\ILIAS\Data\DataSize $max_file_size)
getPostVar()
Get Post Variable.
This class represents a file property in a property form.
Class BaseForm.
Interface for property form input GUI classes that can be used in ilToolbarGUI.
__construct($cancel_command, $a_title="", $a_postvar="")
ilFileStandardDropzoneInputGUI constructor.
isFileNameSelectionEnabled()
Check if filename selection is enabled.
$r
Definition: example_031.php:79
setUploadUrl($upload_url)
Set the URL (using ilCtrl->getFormAction() ) to override the Endpoint the Form will be sent to...
$n
Definition: RandomTest.php:85
Class ilFileStandardDropzoneInputGUI.
setSuffixes($a_suffixes)
Set Accepted Suffixes.
$_POST["username"]