ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
29 {
30  use \ILIAS\Modules\OrgUnit\ARHelper\DIC;
31 
32  const ASYNC_FILEUPLOAD = "async_fileupload";
38  protected static $count = 0;
44  protected $upload_url = '';
49  protected $max_files = 1;
54  protected $max_file_size;
58  protected $dropzone_message = '';
59 
60 
64  public function getUploadUrl()
65  {
66  return $this->upload_url;
67  }
68 
69 
79  public function setUploadUrl($upload_url)
80  {
81  $this->upload_url = $upload_url;
82 
83  return $this;
84  }
85 
86 
90  public function getMaxFiles()
91  {
92  return $this->max_files;
93  }
94 
95 
100  public function setMaxFiles($max_files)
101  {
102  $this->max_files = $max_files;
103  }
104 
105 
109  public function getMaxFilesize()
110  {
111  return $this->max_file_size;
112  }
113 
114 
120  public function setMaxFilesize(\ILIAS\Data\DataSize $max_file_size)
121  {
122  $this->max_file_size = $max_file_size;
123  }
124 
125 
129  public function getDropzoneMessage()
130  {
132  }
133 
134 
139  {
140  $this->dropzone_message = $dropzone_message;
141  }
142 
143 
147  public function render($a_mode = "")
148  {
149  $this->handleUploadURL();
150  $this->handleSuffixes();
151 
152  $f = $this->ui()->factory();
153  $r = $this->ui()->renderer();
154 
155  $this->initDropzoneMessage();
156 
157  $dropzone = $f->dropzone()
158  ->file()
159  ->standard($this->getUploadUrl())
160  ->withParameterName($this->getPostVar())
161  ->withMaxFiles($this->getMaxFiles())
162  ->withMessage($this->getDropzoneMessage())
163  ->withAllowedFileTypes($this->getSuffixes());
164  $dropzone = $this->handleMaxFileSize($dropzone);
165  if ($this->isFileNameSelectionEnabled()) {
166  $dropzone = $dropzone->withUserDefinedFileNamesEnabled(true);
167  }
168 
169  $render = $r->render($dropzone);
170 
171  $n = ++self::$count;
172  $out = "<div id='ilFileStandardDropzoneInputGUIWrapper{$n}'>" . $render . '</div>';
173  // We need some javascript magic
174 
175  $this->ui()->mainTemplate()->addJavaScript('./Services/Form/js/ilFileStandardDropzoneInputGUI.js');
176  $this->ui()->mainTemplate()->addOnLoadCode("ilFileStandardDropzoneInputGUI.init('ilFileStandardDropzoneInputGUIWrapper{$n}');");
177 
178  return $out;
179  }
180 
181 
185  public function checkInput()
186  {
187  $hasUploads = $this->dic()->upload()->hasUploads();
188  if ($this->getRequired() && !$hasUploads) {
189  return false; // No file uploaded but is was required
190  }
191 
192  if ($hasUploads) {
193  try {
194  $_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()];
195  } catch (Exception $e) {
196  return false;
197  }
198 
199  return true;
200  }
201 
202  return true;
203  }
204 
205 
206  protected function handleUploadURL()
207  {
208  if (!$this->getUploadUrl()) {
209  $parentWrapper = $this;
210  while (!$parentWrapper instanceof ilPropertyFormGUI && $parentWrapper !== null) {
211  $parentWrapper = $parentWrapper->getParent();
212  }
213 
214  $str_replace = str_replace("&amp;", "&", $parentWrapper->getFormAction());
215  $this->setUploadUrl($str_replace . "&" . self::ASYNC_FILEUPLOAD . "=true");
216  }
217  }
218 
219 
220  protected function handleSuffixes()
221  {
222  if (!is_array($this->getSuffixes())) {
223  $this->setSuffixes(array());
224  }
225  }
226 
227 
233  protected function handleMaxFileSize($dropzone)
234  {
235  if ($this->getMaxFilesize()) {
236  $dropzone = $dropzone->withFileSizeLimit($this->getMaxFilesize());
237  }
238 
239  return $dropzone;
240  }
241 
242 
243  protected function initDropzoneMessage()
244  {
245  if (!$this->getDropzoneMessage()) {
246  if ($this->getMaxFiles() === 1) {
247  $this->setDropzoneMessage($this->lng()->txt('drag_file_here'));
248  } else {
249  $this->setDropzoneMessage($this->lng()->txt('drag_files_here'));
250  }
251  }
252  }
253 }
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.
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
Create styles array
The data for the language used.
Class ilFileStandardDropzoneInputGUI.
setSuffixes($a_suffixes)
Set Accepted Suffixes.
$_POST["username"]