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:
Ingo Schommer 2013-05-25 01:55:26 -07:00
commit 6f2ba3fcac
3 changed files with 38 additions and 10 deletions

View File

@ -286,7 +286,8 @@ class UploadField extends FileField {
'UploadFieldThumbnailURL' => $this->getThumbnailURLForFile($file), 'UploadFieldThumbnailURL' => $this->getThumbnailURLForFile($file),
'UploadFieldRemoveLink' => $this->getItemHandler($file->ID)->RemoveLink(), 'UploadFieldRemoveLink' => $this->getItemHandler($file->ID)->RemoveLink(),
'UploadFieldDeleteLink' => $this->getItemHandler($file->ID)->DeleteLink(), '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 // we do this in a second customise to have the access to the previous customisations
return $file->customise(array( return $file->customise(array(
@ -617,11 +618,15 @@ class UploadField extends FileField {
* @param File * @param File
*/ */
protected function attachFile($file) { protected function attachFile($file) {
$replaceFileID = $this->getRequest()->requestVar('ReplaceFileID');
$record = $this->getRecord(); $record = $this->getRecord();
$name = $this->getName(); $name = $this->getName();
if ($record && $record->exists()) { 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(!$record->isInDB()) $record->write();
if ($replaceFileID){
$record->{$name}()->removebyId($replaceFileID);
}
$record->{$name}()->add($file); $record->{$name}()->add($file);
} elseif($record->has_one($name)) { } elseif($record->has_one($name)) {
$record->{$name . 'ID'} = $file->ID; $record->{$name . 'ID'} = $file->ID;
@ -1003,6 +1008,8 @@ class UploadField_SelectHandler extends RequestHandler {
// Generate the folder selection field. // Generate the folder selection field.
$folderField = new TreeDropdownField('ParentID', _t('HtmlEditorField.FOLDER', 'Folder'), 'Folder'); $folderField = new TreeDropdownField('ParentID', _t('HtmlEditorField.FOLDER', 'Folder'), 'Folder');
$folderField->setValue($folderID); $folderField->setValue($folderID);
// Generate the file list field. // Generate the file list field.
$config = GridFieldConfig::create(); $config = GridFieldConfig::create();
@ -1031,6 +1038,11 @@ class UploadField_SelectHandler extends RequestHandler {
$folderField, $folderField,
$fileField $fileField
); );
//Existing file to replace
if ($replaceFileID = $this->parent->getRequest()->requestVar('ReplaceFileID')) {
$selectComposite->push(new HiddenField('ReplaceFileID','ReplaceFileID', $replaceFileID));
}
return $selectComposite; return $selectComposite;
} }

View File

@ -202,13 +202,21 @@
onunmatch: function() { onunmatch: function() {
this._super(); this._super();
}, },
openSelectDialog: function() { openSelectDialog: function(uploadedFile) {
// Create dialog and load iframe // Create dialog and load iframe
var self = this, config = this.getConfig(), dialogId = 'ss-uploadfield-dialog-' + this.attr('id'), dialog = jQuery('#' + dialogId); 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(!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 // Show dialog
dialog.ssdialog({iframeUrl: config['urlSelectDialog'], height: 550}); dialog.ssdialog({iframeUrl: iframeUrl, height: 550});
// TODO Allow single-select // TODO Allow single-select
dialog.find('iframe').bind('load', function(e) { dialog.find('iframe').bind('load', function(e) {
@ -224,7 +232,7 @@
contents.find('input[name=action_doAttach]').unbind('click.openSelectDialog').bind('click.openSelectDialog', function() { contents.find('input[name=action_doAttach]').unbind('click.openSelectDialog').bind('click.openSelectDialog', function() {
// TODO Fix entwine method calls across iframe/document boundaries // 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');}); 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'); dialog.ssdialog('close');
return false; return false;
@ -232,14 +240,18 @@
}); });
dialog.ssdialog('open'); dialog.ssdialog('open');
}, },
attachFiles: function(ids) { attachFiles: function(ids, uploadedFileId) {
var self = this, config = this.getConfig(); var self = this, config = this.getConfig();
$.post( $.post(
config['urlAttach'], config['urlAttach'],
{'ids': ids}, {'ids': ids, 'ReplaceFileID': uploadedFileId},
function(data, status, xhr) { function(data, status, xhr) {
var fn = self.fileupload('option', 'downloadTemplate'); 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, files: data,
formatFileSize: function (bytes) { formatFileSize: function (bytes) {
if (typeof bytes !== 'number') return ''; if (typeof bytes !== 'number') return '';
@ -436,7 +448,7 @@
$('div.ss-upload .ss-uploadfield-fromfiles').entwine({ $('div.ss-upload .ss-uploadfield-fromfiles').entwine({
onclick: function(e) { onclick: function(e) {
e.preventDefault(); e.preventDefault();
this.getUploadField().openSelectDialog(); this.getUploadField().openSelectDialog(this.closest('.ss-uploadfield-item'));
} }
}); });
}); });

View File

@ -13,4 +13,8 @@
<% if canDelete %> <% 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> <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 %> <% 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 %>