From c06beb7b91ef784a60014660a0255077fd30d995 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 10 Aug 2015 11:22:56 -0400
Subject: [PATCH] add config option to enable direct download for bulk
 operations

---
 config/vufind/config.ini                      |  4 ++++
 config/vufind/export.ini                      |  6 ++++++
 .../src/VuFind/Controller/AjaxController.php  |  1 +
 module/VuFind/src/VuFind/Export.php           | 20 +++++++++++++++++++
 themes/blueprint/js/lightbox.js               |  5 +++++
 themes/bootstrap3/js/common.js                |  4 +++-
 6 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index a38ca1006bd..a7ad4a8ab1b 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -995,6 +995,10 @@ RIS = false
 [BulkExport]
 enabled = true
 options = MARC:MARCXML:EndNote:EndNoteWeb:RefWorks:BibTeX:RIS
+; Export behavior to use when no bulkExportType setting is found in the matching
+; format section of export.ini; default is 'link' if not overridden below. See
+; export.ini for more details on available options.
+;defaultType = download
 
 ;AddThis is optional. It uses the Add This tool available from www.addthis.com
 ; and requires the username generated when an analytics account is registered.
diff --git a/config/vufind/export.ini b/config/vufind/export.ini
index 6866fa07358..04a4702aac5 100644
--- a/config/vufind/export.ini
+++ b/config/vufind/export.ini
@@ -25,6 +25,12 @@
 ;             {config...} setting above, except result will be URL-encoded.
 ; requiredMethods[] - a repeatable field indicating methods which must be available
 ;     on the record driver object in order to allow export in this format.
+; bulkExportType - [ "link" | "download" ] - link renders a download link in the UI,
+;     download offers to save the export-file directly; overrides the defaultType
+;     setting found in the [BulkExport] section of config.ini. This distinction
+;     currently only affects users with Javascript enabled; the 'download' option
+;     cannot be implemented in a user-friendly way when Javascript is disabled.
+
 [RefWorks]
 requiredMethods[] = getTitle
 redirectUrl = "{config|RefWorks|url|http://www.refworks.com}/express/expressimport.asp?vendor={encodedConfig|RefWorks|vendor|VuFind}&filter=RefWorks%20Tagged%20Format&url={encodedCallback}"
diff --git a/module/VuFind/src/VuFind/Controller/AjaxController.php b/module/VuFind/src/VuFind/Controller/AjaxController.php
index 7089d8e119c..e62040caa03 100644
--- a/module/VuFind/src/VuFind/Controller/AjaxController.php
+++ b/module/VuFind/src/VuFind/Controller/AjaxController.php
@@ -1351,6 +1351,7 @@ class AjaxController extends AbstractBase
                 'result' => $this->translate('Done'),
                 'result_additional' => $html,
                 'needs_redirect' => $export->needsRedirect($format),
+                'export_type' => $export->getBulkExportType($format),
                 'result_url' => $url
             ], self::STATUS_OK
         );
diff --git a/module/VuFind/src/VuFind/Export.php b/module/VuFind/src/VuFind/Export.php
index a1a6d6e79ce..8e073e67b48 100644
--- a/module/VuFind/src/VuFind/Export.php
+++ b/module/VuFind/src/VuFind/Export.php
@@ -338,4 +338,24 @@ class Export
         return isset($this->exportConfig->$format->label)
             ? $this->exportConfig->$format->label : $format;
     }
+
+    /**
+     * Get the bulk export type for the specified export format.
+     *
+     * @param string $format Format identifier
+     *
+     * @return string
+     */
+    public function getBulkExportType($format)
+    {
+        // if exportType is set on per-format basis in export.ini then use it
+        if (isset($this->exportConfig->$format->bulkExportType)) {
+            return $this->exportConfig->$format->bulkExportType;
+        }
+
+        // else check if export type is set in config.ini
+        return isset($this->mainConfig->BulkExport->defaultType)
+            ? $this->mainConfig->BulkExport->defaultType : 'link';
+    }
+
 }
diff --git a/themes/blueprint/js/lightbox.js b/themes/blueprint/js/lightbox.js
index 4ba745d8c9d..467fd925e43 100644
--- a/themes/blueprint/js/lightbox.js
+++ b/themes/blueprint/js/lightbox.js
@@ -519,6 +519,11 @@ function registerAjaxCartExport() {
             url: url,
             dataType: 'json',
             success: function(response, statusText, xhr, $form) {
+               if(response.data.export_type == 'download') {
+                   document.location.href = response.data.result_url;
+                   hideLightbox();
+                   return false;
+                } 
                 if (response.status == 'OK') {
                     $form.parent().empty().append(response.data.result_additional);
                 } else {
diff --git a/themes/bootstrap3/js/common.js b/themes/bootstrap3/js/common.js
index 9f0dc0306f7..2bab168d847 100644
--- a/themes/bootstrap3/js/common.js
+++ b/themes/bootstrap3/js/common.js
@@ -472,8 +472,10 @@ $(document).ready(function() {
       dataType:'json',
       data:Lightbox.getFormData($(evt.target)),
       success:function(data) {
-        if(data.data.needs_redirect) {
+        if(data.data.export_type == 'download' || data.data.needs_redirect) {
           document.location.href = data.data.result_url;
+          Lightbox.close();
+          return false;
         } else {
           Lightbox.changeContent(data.data.result_additional);
         }
-- 
GitLab