Optional field that allows users to force PDF downloads, otherwise allow browsers to open in new tab

This commit is contained in:
James Barnsley 2016-11-24 16:28:25 +13:00 committed by Daniel Hensby
parent 2b1e1c7afb
commit 98b79adca7
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
4 changed files with 51 additions and 7 deletions

9
README.md Normal file → Executable file
View File

@ -90,6 +90,15 @@ Note: Both operations copy the existing file.
$docs = $dms->getByTag('priority', 'important')->First();
$link = $doc->getLink();
// Set default download behavior ('open' or 'download'). 'download' is the system default
// Attempt to open the file in the browser
Config::inst()->update('DMSDocument', 'default_download_behaviour', 'open');
Or in you config.yml:
DMSDocument:
default_download_behaviour: open
#### Manage Page Relations
// Find documents by page

2
_config.php Normal file → Executable file
View File

@ -1,7 +1,6 @@
<?php
$config = Config::inst();
$config->update('DMSDocument_versions', 'enable_versions', true);
DMSSiteTreeExtension::show_documents_tab(); //show the Documents tab on all pages
DMSSiteTreeExtension::no_documents_tab(); //and don't exclude it from any pages
@ -21,4 +20,3 @@ if ($config->get('DMSDocument_versions', 'enable_versions')) {
//using the same db relations for the versioned documents, as for the actual documents
$config->update('DMSDocument_versions', 'db', $config->get('DMSDocument', 'db'));
}

2
_config/dmsdocument.yml Normal file → Executable file
View File

@ -11,3 +11,5 @@ SiteTree:
HtmlEditorField_Toolbar:
extensions:
- DocumentHtmlEditorFieldToolbar
DMSDocument_versions:
enable_versions: true

43
code/model/DMSDocument.php Normal file → Executable file
View File

@ -17,7 +17,8 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
"EmbargoedIndefinitely" => 'Boolean(false)',
"EmbargoedUntilPublished" => 'Boolean(false)',
"EmbargoedUntilDate" => 'SS_DateTime',
"ExpireAtDate" => 'SS_DateTime'
"ExpireAtDate" => 'SS_DateTime',
"DownloadBehavior" => 'Enum(array("open","download"), "download")',
);
private static $many_many = array(
@ -52,6 +53,12 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
'LastChanged'
);
/**
* @var string download|open
* @config
*/
private static $default_download_behaviour = 'download';
/**
* @param Member $member
*
@ -147,7 +154,6 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
/**
* Associates this document with a Page. This method does nothing if the
* association already exists.
@ -902,7 +908,6 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
$fields = new FieldList(); //don't use the automatic scaffolding, it is slow and unnecessary here
$extraTasks = ''; //additional text to inject into the list of tasks at the bottom of a DMSDocument CMSfield
$extraFields = FormField::create('Empty');
//get list of shortcode page relations
$relationFinder = new ShortCodeRelationFinder();
@ -914,6 +919,28 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
$fields->add(new TextField('Title', 'Title'));
$fields->add(new TextareaField('Description', 'Description'));
$downloadBehaviorSource = array(
'open' => _t('DMSDocument.OPENINBROWSER', 'Open in browser'),
'download' => _t('DMSDocument.FORCEDOWNLOAD', 'Force download'),
);
$defaultDownloadBehaviour = Config::inst()->get('DMSDocument', 'default_download_behaviour');
if (!isset($downloadBehaviorSource[$defaultDownloadBehaviour])) {
user_error('Default download behaviour "' . $defaultDownloadBehaviour . '" not supported.', E_USER_WARNING);
}
else {
$downloadBehaviorSource[$defaultDownloadBehaviour] .= ' (' . _t('DMSDocument.DEFAULT', 'default') . ')';
}
$fields->add(
OptionsetField::create(
'DownloadBehavior',
_t('DMSDocument.DOWNLOADBEHAVIOUR', 'Download behavior'),
$downloadBehaviorSource,
$defaultDownloadBehaviour
)
->setDescription('How the visitor will view this file. <strong>Open in browser</strong> allows files to be opened in a new tab.')
);
//create upload field to replace document
$uploadField = new DMSUploadField('ReplaceFile', 'Replace file');
$uploadField->setConfig('allowedMaxFileNumber', 1);
@ -1341,6 +1368,14 @@ class DMSDocument_Controller extends Controller
return $path;
}
// set fallback if no config nor file-specific value
$disposition = 'attachment';
// file-specific setting
if ($doc->DownloadBehavior == 'open') {
$disposition = 'inline';
}
//if a DMSDocument can be downloaded and all the permissions/privileges has passed,
//its ViewCount should be increased by 1 just before the browser sending the file to front.
$doc->trackView();
@ -1348,7 +1383,7 @@ class DMSDocument_Controller extends Controller
header('Content-Type: ' . $mime);
header('Content-Length: ' . filesize($path), null);
if (!empty($mime) && $mime != "text/html") {
header('Content-Disposition: attachment; filename="'.$doc->getFilenameWithoutID().'"');
header('Content-Disposition: '.$disposition.'; filename="'.$doc->getFilenameWithoutID().'"');
}
header('Content-transfer-encoding: 8bit');
header('Expires: 0');