Commit a9c38aa3 authored by Henning Leutz's avatar Henning Leutz 🥋

feat: #955

parent a1b17af9
<?php
/**
* Upload callback if a file is finished uploaded
*/
QUI::$Ajax->registerFunction(
'ajax_uploads_callback',
function ($File, $callable) {
if (!isset($callable)) {
return;
}
if (!\class_exists($callable)) {
return;
}
$Callable = new $callable();
if ($Callable instanceof QUI\Upload\Form) {
/* @var $File \QUI\QDOM */
$Callable->onFileFinish(
$File->getAttribute('filepath'),
$File->getAttributes()
);
}
},
['File', 'callable']
);
<div class="form-upload-{$id}"></div>
<script>
(function () {
require(['qui/QUI', 'controls/upload/Form'], function (QUI, UploadForm) {
var maxFileSize = {$maxFileSize};
var allowedFileTypes = {$allowedFileTypes};
var Form = new UploadForm({
contextMenu: {$contextMenu},
multiple : {$multiple},
sendbutton : {$sendbutton},
uploads : {$uploads},
Drops : [],
hasFile : {$hasFile},
deleteFile : {$deleteFile},
events : {
onFinished: function (Form) {
console.log('done', arguments);
},
onError: function (Form, Message) {
QUI.getMessageHandler().then(function (MH) {
MH.addError(Message.getMessage());
});
},
onAdd: function (Control, File) {
if (typeof FileReader === 'undefined') {
return;
}
if (!maxFileSize === false && File.size > maxFileSize) {
return false;
}
var isAllowed = function (type) {
if (!allowedFileTypes.length) {
return true;
}
for (var i = 0; i < allowedFileTypes.length; i++) {
if (type === allowedFileTypes[i]) {
return true;
}
}
return false;
};
if (!isAllowed(File.type)) {
var files = Control.getElm().getElements(
'.controls-upload-form-fileinfo'
);
files = files.filter(function (F) {
return F.get('text').trim() === File.name;
});
files[0].getParent().getElement('button').click();
}
}
}
});
Form.setParam('onfinish', 'ajax_uploads_callback');
Form.setParam('package', 'quiqqer/quiqqer');
Form.setParam('callable', '{$callable}');
Form.inject(document.getElement('.form-upload-{$id}'));
});
})();
</script>
<?php
namespace QUI\Upload;
use QUI;
/**
* Class Form
*
* @package QUI\Upload
*/
class Form extends QUI\QDOM
{
/**
* Form constructor.
*
* @param array $params
*/
public function __construct($params = [])
{
// defaults
$this->setAttributes([
'contextMenu' => true,
'multiple' => true,
'sendbutton' => true,
'uploads' => 1,
'hasFile' => false,
'deleteFile' => true,
'allowedFileTypes' => false, // eq: ['image/jpeg', 'image/png']
'maxFileSize' => false // eq: 20000000 = 20mb
]);
parent::setAttributes($params);
}
/**
* Return the generated JS control
*/
public function create()
{
$Engine = QUI::getTemplateManager()->getEngine();
$Engine->assign([
'this' => $this,
'id' => QUI\Utils\Uuid::get(),
'uploads' => \intval($this->getAttribute('uploads')),
'contextMenu' => $this->phpBool2JsBool(\boolval($this->getAttribute('contextMenu'))),
'multiple' => $this->phpBool2JsBool(\boolval($this->getAttribute('multiple'))),
'sendbutton' => $this->phpBool2JsBool(\boolval($this->getAttribute('sendbutton'))),
'hasFile' => $this->phpBool2JsBool(\boolval($this->getAttribute('hasFile'))),
'deleteFile' => $this->phpBool2JsBool(\boolval($this->getAttribute('deleteFile'))),
'callable' => \str_replace('\\', '\\\\', $this->getType())
]);
$maxFileSize = $this->getAttribute('maxFileSize');
$allowedFileTypes = $this->getAttribute('allowedFileTypes');
if (!$maxFileSize) {
$Engine->assign('maxFileSize', $this->phpBool2JsBool($maxFileSize));
} else {
$Engine->assign('maxFileSize', (int)$maxFileSize);
}
if (!$allowedFileTypes) {
$Engine->assign('allowedFileTypes', '[]');
} else {
$Engine->assign('allowedFileTypes', \json_encode($allowedFileTypes));
}
return $Engine->fetch(\dirname(__FILE__).'/Form.html');
}
/**
* Return a php bool var for js bool
*
* @param $var
* @return string
*/
public function phpBool2JsBool($var)
{
return $var ? 'true' : 'false';
}
//region API Events
/**
* Can be overwritten - will be called if the upload is finished
*
* @param $file
* @param $params
*/
public function onFileFinish($file, $params)
{
}
//endregion
}
......@@ -558,10 +558,8 @@ class Manager
$this->checkUserPermissions($User);
$id = $User->getId();
// for nobody we use the session id
if (!$id) {
if (!$User->getId()) {
$Session = QUI::getSession();
$uuid = $Session->get('uuid');
......@@ -571,6 +569,8 @@ class Manager
}
$id = $uuid;
} else {
$id = $User->getUniqueId();
}
return $this->getDir().$id.'/';
......
......@@ -9,4 +9,7 @@
>
{$Site->getAttribute('content')}
</div>
{/if}
\ No newline at end of file
{/if}
{$UploadForm->create()}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment