From 694a7dfb7425e781f21bd16bc390218b39d45c46 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 31 Jan 2015 15:31:46 +1100 Subject: [PATCH] Uploader now supports defining acceptable file types --- modules/backend/formwidgets/FileUpload.php | 59 +++++++++++++++++-- .../fileupload/assets/js/fileupload.js | 8 ++- .../fileupload/partials/_file_multi.htm | 1 + .../fileupload/partials/_file_single.htm | 1 + .../fileupload/partials/_image_multi.htm | 1 + .../fileupload/partials/_image_single.htm | 1 + 6 files changed, 66 insertions(+), 5 deletions(-) diff --git a/modules/backend/formwidgets/FileUpload.php b/modules/backend/formwidgets/FileUpload.php index 816f95144..074040174 100644 --- a/modules/backend/formwidgets/FileUpload.php +++ b/modules/backend/formwidgets/FileUpload.php @@ -29,10 +29,26 @@ class FileUpload extends FormWidgetBase */ public $defaultAlias = 'fileupload'; + /** + * @var int Preview image width + */ public $imageWidth; + + /** + * @var int Preview image height + */ public $imageHeight; + + /** + * @var string Text to display when no file is associated + */ public $previewNoFilesMessage; + /** + * @var mixed Collection of acceptable file types. + */ + public $acceptedFileTypes; + /** * {@inheritDoc} */ @@ -40,6 +56,7 @@ class FileUpload extends FormWidgetBase { $this->imageHeight = $this->getConfig('imageHeight', 100); $this->imageWidth = $this->getConfig('imageWidth', 100); + $this->acceptedFileTypes = $this->getConfig('fileTypes'); $this->previewNoFilesMessage = $this->getConfig( 'previewNoFilesMessage', 'backend::lang.form.preview_no_files_message' @@ -68,6 +85,7 @@ class FileUpload extends FormWidgetBase $this->vars['emptyIcon'] = $this->getConfig('emptyIcon', 'icon-plus'); $this->vars['imageHeight'] = $this->imageHeight; $this->vars['imageWidth'] = $this->imageWidth; + $this->vars['acceptedFileTypes'] = $this->getAcceptedFileTypes(true); } protected function getFileList() @@ -102,6 +120,41 @@ class FileUpload extends FormWidgetBase return $mode; } + /** + * Returns the specified accepted file types, or the default + * based on the mode. Image mode will return: + * - jpg,jpeg,bmp,png,gif,svg + * @return string + */ + public function getAcceptedFileTypes($includeDot = false) + { + if (!$types = $this->acceptedFileTypes) { + $types = starts_with($this->getDisplayMode(), 'image') + ? 'jpg,jpeg,bmp,png,gif,svg' + : null; + } + + if (!is_array($types)) { + $types = explode(',', $types); + } + + $types = array_map(function($value) use ($includeDot) { + $value = trim($value); + + if (substr($value, 0, 1) == '.') { + $value = substr($value, 1); + } + + if ($includeDot) { + $value = '.'.$value; + } + + return $value; + }, $types); + + return implode(',', $types); + } + /** * Returns the value as a relation object from the model, * supports nesting via HTML array. @@ -213,11 +266,9 @@ class FileUpload extends FormWidgetBase try { $uploadedFile = Input::file('file_data'); - $isImage = starts_with($this->getDisplayMode(), 'image'); - $validationRules = ['max:'.File::getMaxFilesize()]; - if ($isImage) { - $validationRules[] = 'mimes:jpg,jpeg,bmp,png,gif,svg'; + if ($fileTypes = $this->getAcceptedFileTypes()) { + $validationRules[] = 'mimes:'.$fileTypes; } $validation = Validator::make( diff --git a/modules/backend/formwidgets/fileupload/assets/js/fileupload.js b/modules/backend/formwidgets/fileupload/assets/js/fileupload.js index 51206c93b..c6dd4ec98 100644 --- a/modules/backend/formwidgets/fileupload/assets/js/fileupload.js +++ b/modules/backend/formwidgets/fileupload/assets/js/fileupload.js @@ -127,6 +127,10 @@ method: 'POST' } + if (this.options.fileTypes) { + uploaderOptions.acceptedFiles = this.options.fileTypes + } + /* * Bind uploader */ @@ -225,7 +229,8 @@ } FileUpload.prototype.onUploadThumbnail = function(file, thumbUrl) { - // @todo + // Provides a thumbnail preview, could be useful + // but not used at this point in time. } FileUpload.prototype.onUploadComplete = function(file, data) { @@ -290,6 +295,7 @@ onFail: null, imageWidth: 100, imageHeight: 100, + fileTypes: null, itemTemplate: null, paramName: 'file_data' } diff --git a/modules/backend/formwidgets/fileupload/partials/_file_multi.htm b/modules/backend/formwidgets/fileupload/partials/_file_multi.htm index da55cb043..76cb3a37a 100644 --- a/modules/backend/formwidgets/fileupload/partials/_file_multi.htm +++ b/modules/backend/formwidgets/fileupload/partials/_file_multi.htm @@ -4,6 +4,7 @@ data-control="fileupload" data-unique-id="getId() ?>" data-sort-handler="getEventHandler('onSortAttachments') ?>" + data-file-types="" data-item-template="getId('template') ?>">
diff --git a/modules/backend/formwidgets/fileupload/partials/_file_single.htm b/modules/backend/formwidgets/fileupload/partials/_file_single.htm index 2f9100534..9548af1b7 100644 --- a/modules/backend/formwidgets/fileupload/partials/_file_single.htm +++ b/modules/backend/formwidgets/fileupload/partials/_file_single.htm @@ -3,6 +3,7 @@ class="field-fileupload " data-control="fileupload" data-unique-id="getId() ?>" + data-file-types="" data-item-template="getId('template') ?>">
diff --git a/modules/backend/formwidgets/fileupload/partials/_image_multi.htm b/modules/backend/formwidgets/fileupload/partials/_image_multi.htm index 0fae7aa7b..5a9b55f48 100644 --- a/modules/backend/formwidgets/fileupload/partials/_image_multi.htm +++ b/modules/backend/formwidgets/fileupload/partials/_image_multi.htm @@ -6,6 +6,7 @@ data-sort-handler="getEventHandler('onSortAttachments') ?>" data-image-width="" data-image-height="" + data-file-types="" data-item-template="getId('template') ?>">
diff --git a/modules/backend/formwidgets/fileupload/partials/_image_single.htm b/modules/backend/formwidgets/fileupload/partials/_image_single.htm index c8809867c..f260643a4 100644 --- a/modules/backend/formwidgets/fileupload/partials/_image_single.htm +++ b/modules/backend/formwidgets/fileupload/partials/_image_single.htm @@ -5,6 +5,7 @@ data-unique-id="getId() ?>" data-image-width="" data-image-height="" + data-file-types="" data-item-template="getId('template') ?>">