From 5f7ebd3c235d663b5ad5c0fbcd3d393d2e893306 Mon Sep 17 00:00:00 2001 From: Devlin Date: Wed, 16 Apr 2014 11:08:25 +0200 Subject: [PATCH] API UploadField: move replaceFile to the front end config --- docs/en/changelogs/3.2.0.md | 24 +++++++++++++ docs/en/reference/uploadfield.md | 20 +++++++++++ forms/UploadField.php | 14 +++----- javascript/UploadField.js | 2 +- tests/forms/uploadfield/UploadFieldTest.php | 39 +++++++++++++++++++++ 5 files changed, 88 insertions(+), 11 deletions(-) diff --git a/docs/en/changelogs/3.2.0.md b/docs/en/changelogs/3.2.0.md index 0f45949ac93..4a1aad6ffe1 100644 --- a/docs/en/changelogs/3.2.0.md +++ b/docs/en/changelogs/3.2.0.md @@ -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 `
` for indented text ## Changelog @@ -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 diff --git a/docs/en/reference/uploadfield.md b/docs/en/reference/uploadfield.md index 52e038034d3..28142a44420 100644 --- a/docs/en/reference/uploadfield.md +++ b/docs/en/reference/uploadfield.md @@ -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 diff --git a/forms/UploadField.php b/forms/UploadField.php index f6c6a251373..f5e66adbea6 100644 --- a/forms/UploadField.php +++ b/forms/UploadField.php @@ -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 @@ -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 @@ -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'); } @@ -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()); diff --git a/javascript/UploadField.js b/javascript/UploadField.js index 37bf858ed55..48b8fc0009f 100644 --- a/javascript/UploadField.js +++ b/javascript/UploadField.js @@ -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}, diff --git a/tests/forms/uploadfield/UploadFieldTest.php b/tests/forms/uploadfield/UploadFieldTest.php index f0c59b77bd3..028201c5ec1 100644 --- a/tests/forms/uploadfield/UploadFieldTest.php +++ b/tests/forms/uploadfield/UploadFieldTest.php @@ -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 */