Skip to content

Commit

Permalink
NEW: Added replaceExistingFile setting for UploadField.
Browse files Browse the repository at this point in the history
Sometimes has-one UploadFields can get confused about whether or not there is an existing file that needs deleting.  This setting lets you make a more robust has-one UploadField, where any existing file will be replaced.  It more closely mimics simple single-file upload fields.
  • Loading branch information
Sam Minnee authored and chillu committed Jan 11, 2013
1 parent cc7318f commit 5b450f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions _config/uploadfield.yml
Expand Up @@ -6,6 +6,7 @@ UploadField:
allowedMaxFileNumber:
canUpload: true
canAttachExisting: 'CMS_ACCESS_AssetAdmin'
replaceExistingFile: false
previewMaxWidth: 80
previewMaxHeight: 60
uploadTemplateName: 'ss-uploadfield-uploadtemplate'
Expand Down
11 changes: 11 additions & 0 deletions forms/UploadField.php
Expand Up @@ -93,6 +93,13 @@ class UploadField extends FileField {
* String values are interpreted as permission codes.
*/
'canAttachExisting' => "CMS_ACCESS_AssetAdmin",
/**
* @var boolean If a second file is uploaded, should it replace the existing one rather than throwing an errror?
* This only applies for has_one relationships, and only replaces the association
* rather than the actual file database record or filesystem entry.
*/
'replaceExistingFile' => false,
/**
* @var int
*/
'previewMaxWidth' => 80,
Expand Down Expand Up @@ -487,6 +494,10 @@ public function upload(SS_HTTPRequest $request) {
$tooManyFiles = $record->{$name}()->count() >= $this->getConfig('allowedMaxFileNumber');
// has_one only allows one file at any given time.
} elseif($record->has_one($name)) {
// If we're allowed to replace an existing file, clear out the old one
if($record->$name && $this->getConfig('replaceExistingFile')) {
$record->$name = null;
}
$tooManyFiles = $record->{$name}() && $record->{$name}()->exists();
}

Expand Down

0 comments on commit 5b450f7

Please sign in to comment.