From 98b79adca72dfd095617a0e7666fcee4fec8405b Mon Sep 17 00:00:00 2001 From: James Barnsley Date: Thu, 24 Nov 2016 16:28:25 +1300 Subject: [PATCH] Optional field that allows users to force PDF downloads, otherwise allow browsers to open in new tab --- README.md | 9 ++++++++ _config.php | 2 -- _config/dmsdocument.yml | 4 +++- code/model/DMSDocument.php | 43 ++++++++++++++++++++++++++++++++++---- 4 files changed, 51 insertions(+), 7 deletions(-) mode change 100644 => 100755 README.md mode change 100644 => 100755 _config.php mode change 100644 => 100755 _config/dmsdocument.yml mode change 100644 => 100755 code/model/DMSDocument.php diff --git a/README.md b/README.md old mode 100644 new mode 100755 index b8d2a5a..1847344 --- a/README.md +++ b/README.md @@ -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 diff --git a/_config.php b/_config.php old mode 100644 new mode 100755 index a1eaa70..b56df74 --- a/_config.php +++ b/_config.php @@ -1,7 +1,6 @@ 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')); } - diff --git a/_config/dmsdocument.yml b/_config/dmsdocument.yml old mode 100644 new mode 100755 index 47acaf0..9aa977f --- a/_config/dmsdocument.yml +++ b/_config/dmsdocument.yml @@ -10,4 +10,6 @@ SiteTree: - DMSSiteTreeExtension HtmlEditorField_Toolbar: extensions: - - DocumentHtmlEditorFieldToolbar \ No newline at end of file + - DocumentHtmlEditorFieldToolbar +DMSDocument_versions: + enable_versions: true diff --git a/code/model/DMSDocument.php b/code/model/DMSDocument.php old mode 100644 new mode 100755 index f4a3989..37e757d --- a/code/model/DMSDocument.php +++ b/code/model/DMSDocument.php @@ -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. Open in browser 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');