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.
This commit is contained in:
Sam Minnee 2013-01-11 17:34:27 +13:00 committed by Ingo Schommer
parent cc7318fde4
commit 5b450f7fea
2 changed files with 12 additions and 0 deletions

View File

@ -6,6 +6,7 @@ UploadField:
allowedMaxFileNumber: allowedMaxFileNumber:
canUpload: true canUpload: true
canAttachExisting: 'CMS_ACCESS_AssetAdmin' canAttachExisting: 'CMS_ACCESS_AssetAdmin'
replaceExistingFile: false
previewMaxWidth: 80 previewMaxWidth: 80
previewMaxHeight: 60 previewMaxHeight: 60
uploadTemplateName: 'ss-uploadfield-uploadtemplate' uploadTemplateName: 'ss-uploadfield-uploadtemplate'

View File

@ -93,6 +93,13 @@ class UploadField extends FileField {
* String values are interpreted as permission codes. * String values are interpreted as permission codes.
*/ */
'canAttachExisting' => "CMS_ACCESS_AssetAdmin", '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 * @var int
*/ */
'previewMaxWidth' => 80, 'previewMaxWidth' => 80,
@ -487,6 +494,10 @@ class UploadField extends FileField {
$tooManyFiles = $record->{$name}()->count() >= $this->getConfig('allowedMaxFileNumber'); $tooManyFiles = $record->{$name}()->count() >= $this->getConfig('allowedMaxFileNumber');
// has_one only allows one file at any given time. // has_one only allows one file at any given time.
} elseif($record->has_one($name)) { } 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(); $tooManyFiles = $record->{$name}() && $record->{$name}()->exists();
} }