merged initial codebase
[smr.git] / gui / app / controllers / documents_controller.rb
blob487935a190bd0731af6392e07698ec1772c6dfdf
2 # This file is part of SMR.
4 # SMR is free software: you can redistribute it and/or modify it under the
5 # terms of the GNU General Public License as published by the Free Software
6 # Foundation, either version 3 of the License, or (at your option) any later
7 # version.
9 # SMR is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # SMR.  If not, see <http://www.gnu.org/licenses/>.
16 require 'smr_document'
19 # Controller for all sorts of SmrDocument things.
21 # This shows all documents available for the user, no matter where they've been
22 # attached to, when or if.
23 class DocumentsController < ApplicationController
24     public
26     def index
27         @documents = SmrDocuments.new(current_user.id)
28         @portfolios = Portfolio.where(:id_user=>current_user.id).order(order: :asc)
29         @new_document = Document.new
30     end
32     ##
33     # save broser-supplied file content as new Document
34     def create
35         uploaded_file = params[:new_document][:content]
36                 
37         if uploaded_file then
38             d = store_new_document(current_user.id, uploaded_file, params[:new_document][:comment])
39             n = 'saved file %s' % uploaded_file.original_filename
40         else
41             n = 'please select a file first'
42         end
43         redirect_to :back, :notice=>n
44     end
46     ##
47     # serve DocumentData file content with correct mimetype to the browser
48     def download
49         if params[:id] then 
50             d = SmrDocument.new(params[:id], current_user.id)
51             if not d.empty? then
52                 send_data(d.data, :type=>d.mimetype, :filename=>d.filename, :disposition=>'attachment', :length=>d.size)
53                 return
54             else
55                 n = 'requested document #%i does not exist' % params[:id]
56             end
57         else
58             n = 'please specify a document id to download'
59         end
60         redirect_to :back, :notice=>n
61     end
63     ##
64     # delete Document with DocumentData and DocumentAssign associations
65     #
66     # WORKAROUND:
67     # - the standard way in Rails to delete a record requires JavaScript, but
68     #   SMr is ment to work without it
69     # - therefore deletion is implemented as  non-standard action with seperate
70     #   route
71     def delete_document
72         d = Document.where(:id_user=>current_user.id, :id=>params[:id]).first
73         d.destroy!
74         redirect_to :back, :notice=>'deleted document and its associations'
75     end
76 end