- Enh: Added $maxAttachedFiles to content module to restrict the uploaded file count of comments and posts

This commit is contained in:
buddh4 2017-02-21 20:47:07 +01:00
parent 9ec29fad51
commit 1f9e55770c
7 changed files with 66 additions and 18 deletions

View File

@ -46,6 +46,7 @@ HumHub Change Log
- Fix #2280: Meta data (rotation) not respected for camera images (ImageMagick)
- Fix: Activity stream rendering issue on page unload.
- Enh: Optimized stream entry fade animation.
- Enh: Added $maxAttachedFiles to content module to restrict the uploaded file count of comments and posts

View File

@ -25,7 +25,8 @@ use yii\helpers\Url;
'model' => $comment,
'dropZone' => '#comment_'.$comment->id,
'preview' => '#comment_upload_preview_'.$comment->id,
'progress' => '#comment_upload_progress_'.$comment->id
'progress' => '#comment_upload_progress_'.$comment->id,
'max' => Yii::$app->getModule('content')->maxAttachedFiles
])?>

View File

@ -22,7 +22,8 @@ use yii\helpers\Url;
'id' => 'comment_create_upload_' . $id,
'progress' => '#comment_create_upload_progress_' . $id,
'preview' => '#comment_create_upload_preview_' . $id,
'dropZone' => '#comment_create_form_'.$id
'dropZone' => '#comment_create_form_'.$id,
'max' => Yii::$app->getModule('content')->maxAttachedFiles
]); ?>
<a href="#" class="btn btn-sm btn-default btn-comment-submit pull-left"

View File

@ -46,6 +46,12 @@ class Module extends \humhub\components\Module
* @var string Custom e-mail subject for daily update mails - default: Your daily summary
*/
public $emailSubjectDailyUpdate = null;
/**
* @since 1.2
* @var integer Maximum allowed file uploads for posts/comments
*/
public $maxAttachedFiles = 3;
/**
* @inheritdoc

View File

@ -13,6 +13,7 @@ $this->registerJsConfig('content.form', [
'makePublic' => Yii::t('ContentModule.widgets_views_contentForm', 'Make public'),
'info.archived' => Yii::t('ContentModule.widgets_views_contentForm', 'This space is archived.')
]]);
?>
<div class="panel panel-default clearfix">
@ -51,7 +52,8 @@ $this->registerJsConfig('content.form', [
'id' => 'contentFormFiles',
'progress' => '#contentFormFiles_progress',
'preview' => '#contentFormFiles_preview',
'dropZone' => '#contentFormBody'
'dropZone' => '#contentFormBody',
'max' => Yii::$app->getModule('content')->maxAttachedFiles
]);
?>
<?= humhub\modules\file\widgets\FileHandlerButtonDropdown::widget(['primaryButton' => $uploadButton, 'handlers' => $fileHandlers, 'cssButtonClass' => 'btn-default']); ?>

View File

@ -22,12 +22,16 @@ humhub.module('file', function (module, require, $) {
Upload.component = 'humhub-file-upload';
Upload.prototype.init = function () {
this.fileCount = 0;
this.fileCount = this.options.fileCount || 0;
this.options.name = this.options.name || 'fileList[]';
this.$form = (this.$.data('upload-form')) ? $(this.$.data('upload-form')) : this.$.closest('form');
this.initProgress();
this.initPreview();
this.initFileUpload();
if(!this.canUploadMore()) {
this.disable(this.$.data('max-number-of-files-message'));
}
var that = this;
this.on('upload', function () {
@ -53,8 +57,7 @@ humhub.module('file', function (module, require, $) {
autoUpload: false,
singleFileUploads: false,
add: function (e, data) {
if (that.options.maxNumberOfFiles
&& (that.fileCount + data.files.length > that.options.maxNumberOfFiles)) {
if (that.options.maxNumberOfFiles && (that.getFileCount() + data.files.length > that.options.maxNumberOfFiles)) {
that.handleMaxFileReached();
} else {
data.process().done(function () {
@ -70,23 +73,39 @@ humhub.module('file', function (module, require, $) {
module.log.warn(this.$.data('max-number-of-files-message'), true);
this.$ = $(this.getIdSelector());
if (!this.canUploadMore()) {
this.disable();
this.disable(this.$.data('max-number-of-files-message'));
}
};
Upload.prototype.disable = function () {
Upload.prototype.disable = function (message) {
var $trigger = this.getTrigger();
if ($trigger.length) {
$trigger.addClass('disabled')
.attr('title', this.$.data('max-number-of-files-message'))
.tooltip({
html: false,
container: 'body'
});
$trigger.addClass('disabled');
this.originalTriggerTitle = $trigger.data('original-title');
message = message || 'disabled';
if(message && $trigger.data('bs.tooltip')) {
$trigger.attr('data-original-title', message)
.tooltip('fixTitle');
}
}
this.$.prop('disabled', true);
};
Upload.prototype.enable = function () {
var $trigger = this.getTrigger();
if ($trigger.length) {
$trigger.removeClass('disabled');
}
debugger;
if($trigger.data('bs.tooltip')) {
$trigger.attr('data-original-title', this.originalTriggerTitle)
.tooltip('fixTitle');
}
this.$.prop('disabled', false);
};
Upload.prototype.getDropZone = function () {
var dropZone = $(this.$.data('upload-drop-zone'));
@ -116,10 +135,22 @@ humhub.module('file', function (module, require, $) {
if (this.preview.setSource) {
this.preview.setSource(this);
} else {
this.preview.source;
this.preview.source = this;
}
// Get current file count form preview component.
if(object.isFunction(this.preview.getFileCount)) {
this.fileCount = this.preview.getFileCount();
}
}
};
Upload.prototype.getFileCount = function () {
if(this.preview && object.isFunction(this.preview.getFileCount)) {
return this.preview.getFileCount();
}
return this.fileCount;
};
Upload.prototype.initProgress = function () {
this.progress = Progress.instance(this.$.data('upload-progress'));
@ -212,6 +243,7 @@ humhub.module('file', function (module, require, $) {
_delete(file).then(function (response) {
that.$form.find('[value="' + file.guid + '"]').remove();
module.log.success('success.delete', true);
that.enable();
resolve();
}).catch(function (err) {
module.log.error(err, true);
@ -241,12 +273,12 @@ humhub.module('file', function (module, require, $) {
this.$ = $(this.getIdSelector());
if (!this.canUploadMore()) {
this.disable();
this.disable(this.$.data('max-number-of-files-message'));
}
};
Upload.prototype.canUploadMore = function () {
return !this.options.maxNumberOfFiles || (this.fileCount < this.options.maxNumberOfFiles);
return !this.options.maxNumberOfFiles || (this.getFileCount() < this.options.maxNumberOfFiles);
};
var Preview = function (node, options) {
@ -273,6 +305,10 @@ humhub.module('file', function (module, require, $) {
that.add(file);
});
};
Preview.prototype.getFileCount = function () {
return this.$.find('.file-preview-item').length;
};
Preview.prototype.add = function (file) {
file.galleryId = this.$.attr('id') + '_file_preview_gallery';

View File

@ -21,7 +21,8 @@ use humhub\compat\CActiveForm;
'model' => $post,
'dropZone' => '#post_edit_' . $post->id . ':parent',
'preview' => '#post_upload_preview_' . $post->id,
'progress' => '#post_upload_progress_' . $post->id
'progress' => '#post_upload_progress_' . $post->id,
'max' => Yii::$app->getModule('content')->maxAttachedFiles
])
?>