Skip to content

Commit

Permalink
Merge pull request #2916 from sparc-request/sry-multiselect_documents…
Browse files Browse the repository at this point in the history
…_and_download_into_zip

SRY - Multi select documents and download into zip
  • Loading branch information
Stuart-Johnson committed Jun 24, 2022
2 parents 540b2a9 + 4f335f0 commit 4c3edc0
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 2 deletions.
23 changes: 22 additions & 1 deletion app/assets/javascripts/documents.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $(document).ready ->
else
$('#doc-type-other-field').addClass('d-none')

$(document).on 'change', '#document_document', ->
$(document).on 'change', '#document_document', ->
fileName = $(this).val().split('\\').pop()
$(this).next('.custom-file-label').addClass("selected").html(fileName)

Expand All @@ -36,3 +36,24 @@ $(document).ready ->
else
$('#org_ids').parents('.form-group').removeClass('d-none').addClass('d-flex')
$('#org_ids').prop('disabled', false).selectpicker('refresh')

$(document).on 'change', '#documentsTable input[type="checkbox"]', ->

if $('#documentsTable input[type="checkbox"]:checked').length == 0
$('.download-documents').addClass('disabled')
else
$('.download-documents').removeClass('disabled')

$(document).on 'click', '.download-documents', ->

selections = $('#documentsTable input[type="checkbox"]:checked') # get all selected checkboxes
document_ids = $.map(selections, (c) -> return c.value) # get ids of all selected documents

protocol_id = $(this).data( 'protocol-id')
href = '/documents/bulk_download.zip?protocol_id=' + protocol_id #build the paramaters to the url

for id in document_ids
href += "&document_ids[]=" + id

$('.download-documents').attr("href", href)

41 changes: 41 additions & 0 deletions app/controllers/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,50 @@
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

require('zip')

class DocumentsController < ApplicationController
before_action :initialize_service_request
before_action :authorize_identity

include DocumentsControllerShared

def bulk_download

if(params[:protocol_id] && params[:document_ids])
@protocol_id = params[:protocol_id]
@document_ids = params[:document_ids]

@documents = Protocol.find(@protocol_id).documents.where(id: @document_ids) #filter the checked documents

puts @documents

file_name = "bulk_download_#{@protocol_id}.zip"
temp_file = Tempfile.new(file_name)

respond_to do |format|
format.zip do
Zip::OutputStream.open(temp_file) { |zos| } #initialize the temp file as a zip file

Zip::File.open(temp_file.path, Zip::File::CREATE) do |zip| #add files to zip file
@documents.each do |doc|
doc_path = File.expand_path('../../../public' + doc.document.url, __FILE__)
zip.add("#{doc.document_file_name}", doc_path)
end
end

zip_data = File.read(temp_file.path) #read binary data from the temp file
send_data(zip_data, type: 'application/zip', disposition: 'attachment', filename: file_name) #send the data to the browser as an attachment

temp_file.close
temp_file.unlink

end
end

end



end
end
5 changes: 5 additions & 0 deletions app/helpers/documents_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ def delete_document_button(document, opts={})
def document_file_types_as_string
Document::SUPPORTED_FILE_TYPES.map(&:source).map{ |d| d.gsub('\\', '').gsub('$', '').gsub('?', '') }.join(' ')
end

def display_check_box(document)
check_box_tag "select-document-#{document.id}", "#{document.id}"
end

end
1 change: 1 addition & 0 deletions app/views/dashboard/documents/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
json.(@documents) do |doc|
permission = current_user.catalog_overlord? || @permission_to_edit || (@admin_orgs & doc.all_organizations).any?

json.select display_check_box(doc)
json.document display_document_title(doc, permission: permission)
json.type doc.display_document_type
json.uploaded format_datetime(doc.document_updated_at)
Expand Down
4 changes: 4 additions & 0 deletions app/views/documents/_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
#documentsTableToolbar
- if in_dashboard?
= new_document_button(protocol_id: protocol.id, permission: permission_to_edit)
= link_to icon('fas', 'download mr-2') + t('documents.download'), bulk_download_documents_path(protocol_id: protocol.id, format: :zip), target: :_blank, class: 'btn btn-success download-documents disabled', title: t('documents.tooltips.new'), data: { protocol_id: protocol.id, toggle: 'tooltip' }

- elsif !in_review?
= new_document_button(srid: service_request.id)
- url = in_dashboard? ? dashboard_documents_path(format: :json, protocol_id: protocol.id) : documents_path(format: :json, srid: service_request.id)
%table#documentsTable{ data: { toggle: 'table', search: 'true', 'show-columns' => 'true', 'show-refresh' => 'true', url: url, toolbar: "#documentsTableToolbar" } }
%thead.bg-light
%tr
- if in_dashboard?
%th{ data: { field: 'select', align: "left" } }
%th{ data: { field: 'document', align: "left", sortable: "true" } }
= Document.human_attribute_name(:document)
%th{ data: { field: 'type', align: "left", sortable: "true" } }
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ en:
created: "Document Uploaded!"
updated: "Document Updated!"
destroyed: "Document Destroyed!"
download: "Download Selected Documents"
form:
supported_types: "Supported File Types:<br>%{file_types}"
select_type: "Please Select Type"
Expand Down
8 changes: 7 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@
resources :visit_groups, only: [:new, :create, :edit, :update, :destroy]
resources :visits, only: [:edit, :update, :destroy]

resources :documents, only: [:index, :new, :create, :edit, :update, :destroy]
# resources :documents, only: [:index, :new, :create, :edit, :update, :destroy]
resources :documents, only: [:index, :new, :create, :edit, :update, :destroy] do
collection do
get :bulk_download
end
end


resources :notes, only: [:index, :create, :edit, :update, :destroy]

Expand Down

0 comments on commit 4c3edc0

Please sign in to comment.