mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #2009 from sminnee/uploadfield-choose-new-file
NEW 'Choose another file' button on upload field AAS-917
This commit is contained in:
commit
6f2ba3fcac
@ -286,7 +286,8 @@ class UploadField extends FileField {
|
||||
'UploadFieldThumbnailURL' => $this->getThumbnailURLForFile($file),
|
||||
'UploadFieldRemoveLink' => $this->getItemHandler($file->ID)->RemoveLink(),
|
||||
'UploadFieldDeleteLink' => $this->getItemHandler($file->ID)->DeleteLink(),
|
||||
'UploadFieldEditLink' => $this->getItemHandler($file->ID)->EditLink()
|
||||
'UploadFieldEditLink' => $this->getItemHandler($file->ID)->EditLink(),
|
||||
'UploadField' => $this
|
||||
));
|
||||
// we do this in a second customise to have the access to the previous customisations
|
||||
return $file->customise(array(
|
||||
@ -617,11 +618,15 @@ class UploadField extends FileField {
|
||||
* @param File
|
||||
*/
|
||||
protected function attachFile($file) {
|
||||
$replaceFileID = $this->getRequest()->requestVar('ReplaceFileID');
|
||||
$record = $this->getRecord();
|
||||
$name = $this->getName();
|
||||
if ($record && $record->exists()) {
|
||||
if ($record->has_many($name) || $record->many_many($name)) {
|
||||
if (($record->has_many($name) || $record->many_many($name))) {
|
||||
if(!$record->isInDB()) $record->write();
|
||||
if ($replaceFileID){
|
||||
$record->{$name}()->removebyId($replaceFileID);
|
||||
}
|
||||
$record->{$name}()->add($file);
|
||||
} elseif($record->has_one($name)) {
|
||||
$record->{$name . 'ID'} = $file->ID;
|
||||
@ -1004,6 +1009,8 @@ class UploadField_SelectHandler extends RequestHandler {
|
||||
$folderField = new TreeDropdownField('ParentID', _t('HtmlEditorField.FOLDER', 'Folder'), 'Folder');
|
||||
$folderField->setValue($folderID);
|
||||
|
||||
|
||||
|
||||
// Generate the file list field.
|
||||
$config = GridFieldConfig::create();
|
||||
$config->addComponent(new GridFieldSortableHeader());
|
||||
@ -1032,6 +1039,11 @@ class UploadField_SelectHandler extends RequestHandler {
|
||||
$fileField
|
||||
);
|
||||
|
||||
//Existing file to replace
|
||||
if ($replaceFileID = $this->parent->getRequest()->requestVar('ReplaceFileID')) {
|
||||
$selectComposite->push(new HiddenField('ReplaceFileID','ReplaceFileID', $replaceFileID));
|
||||
}
|
||||
|
||||
return $selectComposite;
|
||||
}
|
||||
|
||||
|
@ -202,13 +202,21 @@
|
||||
onunmatch: function() {
|
||||
this._super();
|
||||
},
|
||||
openSelectDialog: function() {
|
||||
openSelectDialog: function(uploadedFile) {
|
||||
// Create dialog and load iframe
|
||||
var self = this, config = this.getConfig(), dialogId = 'ss-uploadfield-dialog-' + this.attr('id'), dialog = jQuery('#' + dialogId);
|
||||
if(!dialog.length) dialog = jQuery('<div class="ss-uploadfield-dialog" id="' + dialogId + '" />');
|
||||
|
||||
// If user selected 'Choose another file', we need the ID of the file to replace
|
||||
var iframeUrl = config['urlSelectDialog'];
|
||||
var uploadedFileId = null;
|
||||
if (uploadedFile && uploadedFile.attr('data-fileid') > 0){
|
||||
uploadedFileId = uploadedFile.attr('data-fileid');
|
||||
iframeUrl = iframeUrl + '?ReplaceFileID=' + uploadedFileId;
|
||||
}
|
||||
|
||||
// Show dialog
|
||||
dialog.ssdialog({iframeUrl: config['urlSelectDialog'], height: 550});
|
||||
dialog.ssdialog({iframeUrl: iframeUrl, height: 550});
|
||||
|
||||
// TODO Allow single-select
|
||||
dialog.find('iframe').bind('load', function(e) {
|
||||
@ -224,7 +232,7 @@
|
||||
contents.find('input[name=action_doAttach]').unbind('click.openSelectDialog').bind('click.openSelectDialog', function() {
|
||||
// TODO Fix entwine method calls across iframe/document boundaries
|
||||
var ids = $.map(gridField.find('.ss-gridfield-item.ui-selected'), function(el) {return $(el).data('id');});
|
||||
if(ids && ids.length) self.attachFiles(ids);
|
||||
if(ids && ids.length) self.attachFiles(ids, uploadedFileId);
|
||||
|
||||
dialog.ssdialog('close');
|
||||
return false;
|
||||
@ -232,14 +240,18 @@
|
||||
});
|
||||
dialog.ssdialog('open');
|
||||
},
|
||||
attachFiles: function(ids) {
|
||||
attachFiles: function(ids, uploadedFileId) {
|
||||
var self = this, config = this.getConfig();
|
||||
$.post(
|
||||
config['urlAttach'],
|
||||
{'ids': ids},
|
||||
{'ids': ids, 'ReplaceFileID': uploadedFileId},
|
||||
function(data, status, xhr) {
|
||||
var fn = self.fileupload('option', 'downloadTemplate');
|
||||
self.find('.ss-uploadfield-files').append(fn({
|
||||
var container = self.find('.ss-uploadfield-files');
|
||||
if (config['allowedMaxFileNumber'] == 1){
|
||||
container.empty();
|
||||
}
|
||||
container.append(fn({
|
||||
files: data,
|
||||
formatFileSize: function (bytes) {
|
||||
if (typeof bytes !== 'number') return '';
|
||||
@ -436,7 +448,7 @@
|
||||
$('div.ss-upload .ss-uploadfield-fromfiles').entwine({
|
||||
onclick: function(e) {
|
||||
e.preventDefault();
|
||||
this.getUploadField().openSelectDialog();
|
||||
this.getUploadField().openSelectDialog(this.closest('.ss-uploadfield-item'));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -13,4 +13,8 @@
|
||||
<% if canDelete %>
|
||||
<button data-href="$UploadFieldDeleteLink" class="ss-uploadfield-item-delete ss-ui-button ui-corner-all" title="<% _t('UploadField.DELETEINFO', 'Permanently delete this file from the file store') %>" data-icon="minus-circle"><% _t('UploadField.DELETE', 'Delete from files') %></button>
|
||||
<% end_if %>
|
||||
<% if UploadFieldHasRelation && UploadField.canAttachExisting %>
|
||||
<button class="ss-uploadfield-item-choose-another ss-uploadfield-fromfiles ss-ui-button ui-corner-all" title="<% _t('UploadField.CHOOSEANOTHERINFO', 'Replace this file with another one from the file store') %>" data-icon="network-cloud">
|
||||
<% _t('UploadField.CHOOSEANOTHERFILE', 'Choose another file') %></button>
|
||||
<% end_if %>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user