-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UI Service: Dropzones #586
Changes from 212 commits
1f96a8c
8dc546a
63fc928
3687895
703985a
3f4dbaa
ac67831
294c522
0641d23
d80f0de
20ea633
d6291df
3e2cfc5
9b98fe7
6b9fed5
a490664
809f5e9
9c2283e
713907d
7b69065
4b35cee
2bb03bc
624bd1b
0891f83
5140de8
f946b6e
ec85cc7
2d394fb
4820db3
74caee5
7974fa1
fb69840
84b53d8
90c9cab
f89aab2
37fed19
9973555
2b7617a
8171102
506ad69
162c35e
5c4b8f5
6e806a9
688314c
317d072
363ee4c
7f65b7b
9fd7799
72a3e47
17bad8e
2ce1aef
e00ae5c
dfaf8f3
37a67d9
3879664
e59a2d8
c85a2bd
eac34e6
54e0591
f84a73c
731fc11
8befee3
5c4edb1
1c0c373
329f345
4e7b2e8
e0af1ed
83508c1
57144b0
91c389c
add2640
a564069
98705a6
34c1d8d
ff45787
6513a61
1fbfa41
61ad645
c3f3390
2b81712
01d0059
a4be402
4ac653c
c8dfae0
cb255a3
0f72b2c
142ee90
01aacc4
e316560
2ec6c8c
b96f4a0
5e2c779
cf5037d
db0805e
40fb58e
19c4ff6
e231ff4
cb5ca01
8dd874a
b5f60be
8f34eec
addac8c
a1e0604
0932310
6dc0619
602b651
a037ee9
45fa917
46cbc86
62b50ef
69e564e
1b26975
835fb78
5f30b60
109cee3
cfc3c55
dfa225d
270b3dd
fc4be81
4e77cfd
79e26d2
3378ad3
be84ee6
6b55d34
0b62fdc
6c6c69c
3ccfe26
41048a7
99aafcb
77dff79
31dfa89
baa040d
df193e8
c83afcc
29ea4af
d346111
4820e55
b7e65b1
c292030
9066b13
d1a0a9d
5317d5e
cc0ce30
c3f4938
a408a49
105b8c5
436f0ae
a0fe5e4
5555236
1af127b
08bd755
dd193ee
2a568ae
50c8d39
f197f03
58cb103
096c4b6
d24eb72
15ff641
3626f8d
06a1d4f
0dfe022
c10faf2
a4dc212
e1d0d5b
32d2a83
41eeebf
fb06b73
b888b6a
afa019b
d016585
a2d9be1
6add51d
52eac04
fe2fe6e
935e3b2
19b6994
fa8b081
4209c2d
eac8ccb
d3cacc6
5c0a89a
6aad4ae
6283e43
2b4db82
479d459
a35c391
7d15ba6
293911f
34546be
f7e72c5
f3e03e1
a325c36
ed34979
e3eb8fa
3a22fee
3703fca
debbdbe
b11be2d
a31abe0
59d3abc
2e08d94
97e4e9c
bada67b
6ea9296
559d676
d9d6be6
d39ece7
07b2516
8019727
fe9a2ba
0fac873
954a27c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
<?php | ||
/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */ | ||
|
||
/** | ||
* Class ilFileStandardDropzoneInputGUI | ||
* | ||
* A ilFileStandardDropzoneInputGUI is used in a (legacy) Form to upload Files using the Dropzone | ||
* of the UI-Framework introduced with ILIAS 5.3. In some cases this can be used as a | ||
* Drop-In-Replacement of the ilFileInputGUI, but check your usecase after. If you need an example | ||
* how to use it, see e.g. in UI/examples/Dropzone/File/Standard/with_usage_in_legacy_form.php | ||
* | ||
* Why make it a Drop-In-Replacement and not just replace ilFileInputGUI? | ||
* - There are a lot of different ways a form is handled in ILIAS, sometimes only checkInput is | ||
* called, sometime developers send their own error-messages and so on. The | ||
* ilFileStandardDropzoneInputGUI excepts some standard-behavior and would fail in some cases when | ||
* just replacing the ilFileInputGUI | ||
* - There are a lot of options in ilFileInputGUI which would be difficult to reimplement in | ||
* ilFileStandardDropzoneInputGUI without discussing them with all devs. | ||
* - Beside ilFileInputGUI there are many other File-InputGUIs with different functionality. We | ||
* should consolidate their use-cases first. | ||
* | ||
* Attention: This ilFileStandardDropzoneInputGUI changes the behaviour of your form when used: The | ||
* Form will be sent asynchronously due to limitations of dropped files (see | ||
* https://stackoverflow.com/questions/1017224/dynamically-set-value-of-a-file-input ) | ||
* | ||
* @author Fabian Schmid <[email protected]> | ||
*/ | ||
class ilFileStandardDropzoneInputGUI extends ilFileInputGUI implements ilToolbarItem { | ||
|
||
const ASYNC_FILEUPLOAD = "async_fileupload"; | ||
/** | ||
* @var int if there are more than one ilFileStandardDropzoneInputGUI in the same Form, this | ||
* value will be incremented during rendering to make sure all Inputs will be handled | ||
* correctly | ||
*/ | ||
protected static $count = 0; | ||
/** | ||
* @var string Set it to the URL (using ilCtrl->getFormAction() ) to override the Endpoint the | ||
* Form will be sent to. If not set, the ilFileStandardDropzoneInputGUI will get the | ||
* Form-Action of it's nearest form | ||
*/ | ||
protected $upload_url = ''; | ||
/** | ||
* @var int The amount of files which can be uploaded. Standard is 1 since the old | ||
* ilFileInputGUI in most cases allows one. | ||
*/ | ||
protected $max_files = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Describe what this variable does here. |
||
/** | ||
* @var \ILIAS\Data\DataSize only files beneath this size will be accepted to upload. Currently | ||
* this uses the defined valued of the php.ini | ||
*/ | ||
protected $max_file_size; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Describe what this variable does here. |
||
/** | ||
* @var string The message which will be rendered within the dropzone. | ||
*/ | ||
protected $dropzone_message = ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Describe what this variable does here. |
||
|
||
|
||
/** | ||
* @return string the URL where the form will be sent to. | ||
*/ | ||
public function getUploadUrl() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Describe this public function here. |
||
return $this->upload_url; | ||
} | ||
|
||
|
||
/** | ||
* Set the URL (using ilCtrl->getFormAction() ) to override the Endpoint the | ||
* Form will be sent to. If not set, the ilFileStandardDropzoneInputGUI will get the | ||
* Form-Action of it's nearest form | ||
* | ||
* @param string $upload_url | ||
* | ||
* @return $this | ||
*/ | ||
public function setUploadUrl($upload_url) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Describe this public function here. |
||
$this->upload_url = $upload_url; | ||
|
||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* @return int Amount of allowed files in this input | ||
*/ | ||
public function getMaxFiles() { | ||
return $this->max_files; | ||
} | ||
|
||
|
||
/** | ||
* @param int $max_files The amount of files which can be uploaded. Standard is 1 since the old | ||
* ilFileInputGUI in most cases allows one. | ||
*/ | ||
public function setMaxFiles($max_files) { | ||
$this->max_files = $max_files; | ||
} | ||
|
||
|
||
/** | ||
* @return \ILIAS\Data\DataSize allowed size of files which can be uploaded | ||
*/ | ||
public function getMaxFilesize() { | ||
return $this->max_file_size; | ||
} | ||
|
||
|
||
/** | ||
* @param \ILIAS\Data\DataSize $max_file_size only files beneath this size will be accepted to | ||
* upload. Currently this uses the defined valued of | ||
* the php.ini | ||
*/ | ||
public function setMaxFilesize(\ILIAS\Data\DataSize $max_file_size) { | ||
$this->max_file_size = $max_file_size; | ||
} | ||
|
||
|
||
/** | ||
* @return string The message which will be rendered within the dropzone. | ||
*/ | ||
public function getDropzoneMessage() { | ||
return $this->dropzone_message; | ||
} | ||
|
||
|
||
/** | ||
* @param string $dropzone_message The message which will be rendered within the dropzone. | ||
*/ | ||
public function setDropzoneMessage($dropzone_message) { | ||
$this->dropzone_message = $dropzone_message; | ||
} | ||
|
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function render($a_mode = "") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most probably you should use: |
||
global $DIC; | ||
|
||
$this->handleUploadURL(); | ||
$this->handleSuffixes(); | ||
|
||
$f = $DIC->ui()->factory(); | ||
$r = $DIC->ui()->renderer(); | ||
|
||
$dropzone = $f->dropzone() | ||
->file() | ||
->standard($this->getUploadUrl()) | ||
->withParameterName($this->getPostVar()) | ||
->withMaxFiles($this->getMaxFiles()) | ||
->withMessage($this->getDropzoneMessage()) | ||
->withAllowedFileTypes($this->getSuffixes()); | ||
$dropzone = $this->handleMaxFileSize($dropzone); | ||
if ($this->isFileNameSelectionEnabled()) { | ||
$dropzone = $dropzone->withUserDefinedFileNamesEnabled(true); | ||
} | ||
|
||
$render = $r->render($dropzone); | ||
|
||
$n = ++ self::$count; | ||
$out = "<div id='ilFileStandardDropzoneInputGUIWrapper{$n}'>" . $render . '</div>'; | ||
// We need some javascript magic | ||
/** @var ilTemplate $tpl */ | ||
$tpl = $DIC['tpl']; | ||
$tpl->addJavaScript('./Services/Form/js/ilFileStandardDropzoneInputGUI.js'); | ||
$tpl->addOnLoadCode("ilFileStandardDropzoneInputGUI.init('ilFileStandardDropzoneInputGUIWrapper{$n}');"); | ||
|
||
return $out; | ||
} | ||
|
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function checkInput() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most probably you should use: |
||
global $DIC; | ||
|
||
$hasUploads = $DIC->upload()->hasUploads(); | ||
if ($this->getRequired() && !$hasUploads) { | ||
return false; // No file uploaded but is was required | ||
} | ||
|
||
if ($hasUploads) { | ||
try { | ||
$_POST[$this->getPostVar()] = $_FILES[$this->getPostVar()]; | ||
} catch (Exception $e) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
protected function handleUploadURL() { | ||
if (!$this->getUploadUrl()) { | ||
$parentWrapper = $this; | ||
while (!$parentWrapper instanceof ilPropertyFormGUI && $parentWrapper !== null) { | ||
$parentWrapper = $parentWrapper->getParent(); | ||
} | ||
|
||
$str_replace = str_replace("&", "&", $parentWrapper->getFormAction()); | ||
$this->setUploadUrl($str_replace . "&" . self::ASYNC_FILEUPLOAD . "=true"); | ||
} | ||
} | ||
|
||
|
||
protected function handleSuffixes() { | ||
if (!is_array($this->getSuffixes())) { | ||
$this->setSuffixes(array()); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @param ILIAS\UI\Component\Dropzone\File\Standard $dropzone | ||
* | ||
* @return ILIAS\UI\Component\Dropzone\File\Standard | ||
*/ | ||
protected function handleMaxFileSize($dropzone) { | ||
if ($this->getMaxFilesize()) { | ||
$dropzone = $dropzone->withFileSizeLimit($this->getMaxFilesize()); | ||
} | ||
|
||
return $dropzone; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var ilFileStandardDropzoneInputGUI = (function ($) { | ||
|
||
var init = function (wrapper_id) { | ||
var wrapper = $('#' + wrapper_id); | ||
var uploadId = wrapper.find('.il-dropzone-base').attr('id'); | ||
var form = wrapper.closest('form'); | ||
var handledUpload = false; | ||
var buttonName = form.find('input[type=submit]:first').attr('name'); | ||
form.find('input[type=submit]').on("click", function () { | ||
buttonName = $(this).attr('name'); | ||
}); | ||
|
||
form.on('submit', function (event) { | ||
if (handledUpload) { | ||
return; | ||
} | ||
if ($(this)[0].checkValidity()) { | ||
// If we have any files to upload, start uploading process prior to submitting form | ||
if (il.UI.uploader.getUploads(uploadId).length) { | ||
event.preventDefault(); | ||
|
||
var params = {}; | ||
|
||
$.each($(this).serializeArray(), function (_, kv) { | ||
if (params.hasOwnProperty(kv.name)) { | ||
params[kv.name] = $.makeArray(params[kv.name]); | ||
params[kv.name].push(kv.value); | ||
} else { | ||
params[kv.name] = kv.value; | ||
} | ||
}); | ||
|
||
params[buttonName] = true; | ||
|
||
il.UI.uploader.setUploadParams(uploadId, params); | ||
il.UI.uploader.onError(uploadId, function (xmlHttpRequest) { | ||
handledUpload = true; | ||
return false; | ||
}); | ||
il.UI.uploader.onAllUploadCompleted(uploadId, function () { | ||
handledUpload = true; | ||
return true; | ||
}, function () { | ||
handledUpload = true; | ||
return false; | ||
}); | ||
|
||
il.UI.uploader.upload(uploadId); | ||
} | ||
else { | ||
handledUpload = true; | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
return { | ||
init: init | ||
} | ||
|
||
})($); |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# Dependency Management in ILIAS | ||
|
||
The are currently three type of external libraries: | ||
- PHP dependencies installed using composer in /libs/composer, see [composer-readme](libs/composer/README.md). | ||
- PHP dependencies installed using composer in /libs/composer, see [composer-readme](composer/README.md). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes can be ignored in this PR since the dependency-management using bower will be part of another PR |
||
- PHP dependencies installad manually, located in some /Services/\*/libs and /Modules/\*/libs | ||
- JS- and client-side Frameworks, currently also located in /Services\/* and /Modules\/* | ||
- JS- and client-side Frameworks installed using bower, see [bower-readme](bower/README.md) | ||
- JS- and client-side Frameworks installed manually, currently also located in /Services\/* and /Modules\/* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# How to add dependencies with bower in ILIAS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes can be ignored in this PR since the dependency-management using bower will be part of another PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #603 |
||
|
||
**New dependencies need to be approved by the Jour Fixe of the ILIAS society.** | ||
|
||
## Dependencies for production | ||
- Comment all lines in libs/.gitignore which begin with bower/ | ||
- Add a new library using bower, e.g. "bower install [email protected] --save" | ||
- Add a section in "extra" with the following metadata": | ||
```json | ||
"jquery": { | ||
"introduction": "03.08.2017", | ||
"introduced-by": "fschmid" | ||
}, | ||
``` | ||
- Run "bower install --no-dev" | ||
- Add all files to ILIAS git-repository and commit | ||
|
||
## Dependencies for development | ||
- Add a new library using bower, e.g. "bower install mocha --save-dev" | ||
- Ignore all directories which are added by installation (uncomment existing) | ||
- commit changes in gitignore and bower.json. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Describe what this variable does here.