mirror of
https://github.com/silverstripe/silverstripe-dms
synced 2024-10-22 14:05:56 +02:00
FEATURE Replace file (incl. name validation)
This commit is contained in:
parent
c65f3340eb
commit
c44434d498
@ -447,8 +447,10 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
|
|||||||
$fields->add(new TextareaField('Description','Description'));
|
$fields->add(new TextareaField('Description','Description'));
|
||||||
|
|
||||||
//create upload field to replace document
|
//create upload field to replace document
|
||||||
$UploadField = new DMSUploadField('ReplaceFile', 'Replace file');
|
$uploadField = new DMSUploadField('ReplaceFile', 'Replace file');
|
||||||
$UploadField->setConfig('allowedMaxFileNumber', 1);
|
$uploadField->setConfig('allowedMaxFileNumber', 1);
|
||||||
|
$uploadField->setRecord($this);
|
||||||
|
|
||||||
|
|
||||||
$gridFieldConfig = GridFieldConfig::create()->addComponents(
|
$gridFieldConfig = GridFieldConfig::create()->addComponents(
|
||||||
new GridFieldToolbarHeader(),
|
new GridFieldToolbarHeader(),
|
||||||
@ -478,7 +480,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
|
|||||||
);
|
);
|
||||||
|
|
||||||
$fields->add(new LiteralField('BottomTaskSelection',"<div id=\"Actions\" class=\"field actions\"><label class=\"left\">Actions</label><ul><li class=\"ss-ui-button\">Replace</li><li class=\"ss-ui-button\">Embargo</li></ul></div>"));
|
$fields->add(new LiteralField('BottomTaskSelection',"<div id=\"Actions\" class=\"field actions\"><label class=\"left\">Actions</label><ul><li class=\"ss-ui-button\">Replace</li><li class=\"ss-ui-button\">Embargo</li></ul></div>"));
|
||||||
$fields->add($UploadField);
|
$fields->add($uploadField);
|
||||||
$fields->add($pagesGrid);
|
$fields->add($pagesGrid);
|
||||||
|
|
||||||
return $fields;
|
return $fields;
|
||||||
|
@ -2,12 +2,13 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Field for uploading files into a DMSDocument. Replacing the existing file.
|
* Field for uploading files into a DMSDocument. Replacing the existing file.
|
||||||
|
* Not ideally suited for the purpose, as the base implementation
|
||||||
|
* assumes to operate on a {@link File} record. We only use this as
|
||||||
|
* a temporary container, which gets deleted as soon as the actual
|
||||||
|
* {@link DMSDocument} is created.
|
||||||
*
|
*
|
||||||
* <b>NOTE: this Field will call write() on the supplied record</b>
|
* <b>NOTE: this Field will call write() on the supplied record</b>
|
||||||
*
|
*
|
||||||
* <b>Features (some might not be available to old browsers):</b>
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @author Julian Seidenberg
|
* @author Julian Seidenberg
|
||||||
* @package dms
|
* @package dms
|
||||||
*/
|
*/
|
||||||
@ -21,14 +22,28 @@ class DMSUploadField extends UploadField {
|
|||||||
* @param File
|
* @param File
|
||||||
*/
|
*/
|
||||||
protected function attachFile($file) {
|
protected function attachFile($file) {
|
||||||
$page = $this->getRecord();
|
|
||||||
|
|
||||||
$dms = DMS::getDMSInstance();
|
$dms = DMS::getDMSInstance();
|
||||||
$document = $dms->storeDocument($file);
|
$record = $this->getRecord();
|
||||||
$file->delete();
|
|
||||||
$document->addPage($page);
|
|
||||||
|
|
||||||
return $document;
|
if($record instanceof DMSDocument) {
|
||||||
|
// If the edited record is a document,
|
||||||
|
// assume we're replacing an existing file
|
||||||
|
$doc = $record;
|
||||||
|
$doc->ingestFile($file);
|
||||||
|
} else {
|
||||||
|
// Otherwise create it
|
||||||
|
$doc = $dms->storeDocument($file);
|
||||||
|
$file->delete();
|
||||||
|
// Relate to the underlying page being edited.
|
||||||
|
// Not applicable when editing the document itself and replacing it.
|
||||||
|
$doc->addPage($record);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validate($validator) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,6 +55,26 @@ class DMSUploadField extends UploadField {
|
|||||||
public function upload(SS_HTTPRequest $request) {
|
public function upload(SS_HTTPRequest $request) {
|
||||||
if($this->isDisabled() || $this->isReadonly()) return $this->httpError(403);
|
if($this->isDisabled() || $this->isReadonly()) return $this->httpError(403);
|
||||||
|
|
||||||
|
// CUSTOM Validate that the replaced file is of the same name,
|
||||||
|
// as we don't support altering the file attributes after initial upload
|
||||||
|
$record = $this->getRecord();
|
||||||
|
if($record instanceof DMSDocument) {
|
||||||
|
$tmpfile = $request->postVar($this->getName());
|
||||||
|
$suggestedFileName = basename(FileNameFilter::create()->filter($tmpfile['name']));
|
||||||
|
if($suggestedFileName != $record->getFilenameWithoutID()) {
|
||||||
|
$return = array(
|
||||||
|
'error' => _t(
|
||||||
|
'DMSUploadField.WRONGNAME',
|
||||||
|
'The new file name needs to match the one being replaced'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$response = new SS_HTTPResponse(Convert::raw2json(array($return)));
|
||||||
|
$response->addHeader('Content-Type', 'text/plain');
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// CUSTOM END
|
||||||
|
|
||||||
// Protect against CSRF on destructive action
|
// Protect against CSRF on destructive action
|
||||||
$token = $this->getForm()->getSecurityToken();
|
$token = $this->getForm()->getSecurityToken();
|
||||||
if(!$token->checkRequest($request)) return $this->httpError(400);
|
if(!$token->checkRequest($request)) return $this->httpError(400);
|
||||||
@ -109,7 +144,7 @@ class DMSUploadField extends UploadField {
|
|||||||
} else {
|
} else {
|
||||||
$file = $this->upload->getFile();
|
$file = $this->upload->getFile();
|
||||||
|
|
||||||
// Attach the file to the related record.
|
// CUSTOM Attach the file to the related record.
|
||||||
$document = $this->attachFile($file);
|
$document = $this->attachFile($file);
|
||||||
|
|
||||||
// TODO: both $document->UploadFieldThumbnailURL and $document->UploadFieldFileButtons are null,
|
// TODO: both $document->UploadFieldThumbnailURL and $document->UploadFieldFileButtons are null,
|
||||||
@ -126,6 +161,8 @@ class DMSUploadField extends UploadField {
|
|||||||
'buttons' => $document->renderWith($this->getTemplateFileButtons()),
|
'buttons' => $document->renderWith($this->getTemplateFileButtons()),
|
||||||
'showeditform' => true
|
'showeditform' => true
|
||||||
));
|
));
|
||||||
|
|
||||||
|
// CUSTOM END
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user