Skip to content

Commit

Permalink
API UploadField: move replaceFile to the front end config
Browse files Browse the repository at this point in the history
  • Loading branch information
JayDevlin committed May 21, 2014
1 parent ec578e5 commit 5f7ebd3
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 11 deletions.
24 changes: 24 additions & 0 deletions docs/en/changelogs/3.2.0.md
Expand Up @@ -5,6 +5,7 @@
* Minimum PHP version raised to 5.3.3
* DataObject::validate() method visibility changed to public
* UploadField "Select from files" shows files in all folders by default
* UploadField won't display an overwrite warning unless Upload:replaceFile is true
* HtmlEditorField no longer substitutes `<blockquote />` for indented text

## Changelog
Expand Down Expand Up @@ -32,6 +33,29 @@ use `setDisplayFolderName()` with a folder path relative to `assets/`:

UploadField::create('MyField')->setDisplayFolderName('Uploads');

### UploadField won't display an overwrite warning unless Upload:replaceFile is true

The configuration setting `UploadField:overwriteWarning` is dependent on `Upload:replaceFile`
which is set to false by default.

To display a warning before overwriting a file:

Via config:

::yaml
Upload:
# Replace an existing file rather than renaming the new one.
replaceFile: true
UploadField:
# Warning before overwriting existing file (only relevant when Upload: replaceFile is true)
overwriteWarning: true

Or per instance:

::php
$uploadField->getUpload()->setReplaceFile(true);
$uploadField->setOverwriteWarning(true);

### File.allowed_extensions restrictions

Certain file types such as swf, html, htm, xhtml and xml have been removed from the list
Expand Down
20 changes: 20 additions & 0 deletions docs/en/reference/uploadfield.md
Expand Up @@ -165,6 +165,26 @@ NOTE: this only sets the configuration for your UploadField, this does NOT chang
$size = $sizeMB * 1024 * 1024; // 2 MB in bytes
$this->getValidator()->setAllowedMaxFileSize($size);

### Overwrite warning

In order to display a warning before overwriting an existing file, `Upload:replaceFile` must be set to true.

Via config:

:::yaml
Upload:
# Replace an existing file rather than renaming the new one.
replaceFile: true
UploadField:
# Warning before overwriting existing file (only relevant when Upload: replaceFile is true)
overwriteWarning: true

Or per instance:

:::php
$uploadField->getUpload()->setReplaceFile(true);
$uploadField->setOverwriteWarning(true);

### Preview dimensions

Set the dimensions of the image preview. By default the max width is set to 80
Expand Down
14 changes: 4 additions & 10 deletions forms/UploadField.php
Expand Up @@ -154,8 +154,7 @@ class UploadField extends FileField {
/**
* Show a warning when overwriting a file.
* This requires Upload->replaceFile config to be set to true, otherwise
* files will be renamed instead of overwritten (although the warning will
* still be displayed)
* files will be renamed instead of overwritten
*
* @see Upload
* @var boolean
Expand Down Expand Up @@ -984,7 +983,8 @@ public function Field($properties = array()) {
'urlFileExists' => $this->link('fileexists'),
'acceptFileTypes' => '.+$',
// Fileupload treats maxNumberOfFiles as the max number of _additional_ items allowed
'maxNumberOfFiles' => $allowedMaxFileNumber ? ($allowedMaxFileNumber - count($this->getItemIDs())) : null
'maxNumberOfFiles' => $allowedMaxFileNumber ? ($allowedMaxFileNumber - count($this->getItemIDs())) : null,
'replaceFile' => $this->getUpload()->getReplaceFile(),
);

// Validation: File extensions
Expand Down Expand Up @@ -1022,9 +1022,8 @@ public function Field($properties = array()) {
}
}

//get all the existing files in the current folder
// add overwrite warning error message to the config object sent to Javascript
if ($this->getOverwriteWarning()) {
//add overwrite warning error message to the config object sent to Javascript
$config['errorMessages']['overwriteWarning'] =
_t('UploadField.OVERWRITEWARNING', 'File with the same name already exists');
}
Expand Down Expand Up @@ -1178,11 +1177,6 @@ protected function saveTemporaryFile($tmpFile, &$error = null) {
$fileObject = Object::create($relationClass);
}

// Allow replacing files (rather than renaming a duplicate) when warning about overwrites
if($this->getConfig('overwriteWarning')) {
$this->upload->setReplaceFile(true);
}

// Get the uploaded file into a new file object.
try {
$this->upload->loadIntoFile($tmpFile, $fileObject, $this->getFolderName());
Expand Down
2 changes: 1 addition & 1 deletion javascript/UploadField.js
Expand Up @@ -60,7 +60,7 @@
//check the array of existing files to see if we are trying to upload a file that already exists
var that = this;
var config = this.options;
if (config.overwriteWarning) {
if (config.overwriteWarning && config.replaceFile) {
$.get(
config['urlFileExists'],
{'filename': data.files[0].name},
Expand Down
39 changes: 39 additions & 0 deletions tests/forms/uploadfield/UploadFieldTest.php
Expand Up @@ -693,6 +693,45 @@ public function testSelectWithDisplayFolderName() {
$this->assertNotContains($fileSubfolder->ID, $itemIDs, 'Does not contain file in subfolder');
}

/**
* Test that UploadField:overwriteWarning cannot overwrite Upload:replaceFile
*/
public function testConfigOverwriteWarningCannotRelaceFiles() {
$this->loginWithPermission('ADMIN');

Upload::config()->replaceFile = false;
UploadField::config()->defaultConfig = array_merge(
UploadField::config()->defaultConfig, array('overwriteWarning' => true)
);

$tmpFileName = 'testUploadBasic.txt';
$response = $this->mockFileUpload('NoRelationField', $tmpFileName);
$this->assertFalse($response->isError());
$responseData = Convert::json2array($response->getBody());
$this->assertFileExists(ASSETS_PATH . '/UploadFieldTest/' . $responseData[0]['name']);
$uploadedFile = DataObject::get_by_id('File', (int) $responseData[0]['id']);
$this->assertTrue(is_object($uploadedFile), 'The file object is created');

$tmpFileName = 'testUploadBasic.txt';
$response = $this->mockFileUpload('NoRelationField', $tmpFileName);
$this->assertFalse($response->isError());
$responseData = Convert::json2array($response->getBody());
$this->assertFileExists(ASSETS_PATH . '/UploadFieldTest/' . $responseData[0]['name']);
$uploadedFile2 = DataObject::get_by_id('File', (int) $responseData[0]['id']);
$this->assertTrue(is_object($uploadedFile2), 'The file object is created');
$this->assertTrue(
$uploadedFile->Filename !== $uploadedFile2->Filename,
'Filename is not the same'
);
$this->assertTrue(
$uploadedFile->ID !== $uploadedFile2->ID,
'File database record is not the same'
);

$uploadedFile->delete();
$uploadedFile2->delete();
}

/**
* Tests that UploadField::fileexist works
*/
Expand Down

0 comments on commit 5f7ebd3

Please sign in to comment.