Permalink
Browse files

NEW: Added replaceExistingFile setting for UploadField.

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...
1 parent cc7318f commit 5b450f7feaf4d2ca90dee5fda504479e288789cd @sminnee sminnee committed with chillu Jan 11, 2013
Showing with 12 additions and 0 deletions.
  1. +1 −0 _config/uploadfield.yml
  2. +11 −0 forms/UploadField.php
View
@@ -6,6 +6,7 @@ UploadField:
allowedMaxFileNumber:
canUpload: true
canAttachExisting: 'CMS_ACCESS_AssetAdmin'
+ replaceExistingFile: false
previewMaxWidth: 80
previewMaxHeight: 60
uploadTemplateName: 'ss-uploadfield-uploadtemplate'
View
@@ -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,
@@ -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();
}

0 comments on commit 5b450f7

Please sign in to comment.