manage bookmarks, bugfixes
[smr.git] / gui / app / controllers / documents_controller.rb
blob0ef0850be04790b96ea34e005fd346eba71b5d34
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 'uploaded_file'
19 # Controller for all sorts of Smr::UploadedFile 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
25     def index
26         session[:documents_query] = params[:q] if params[:q]
28         @portfolios = Portfolio.where(:id_user=>current_user.id).order(order: :asc)
29         @document = Document.new
31         files = Smr::UploadedFiles.new(
32             current_user.id,
33             :date=>smr_browse_date,
34             :ransack_query=>session[:documents_query]
35         )
36         @query = files.query
37         @documents, @total_pages = smr_paginate(@page = smr_page, files)
38     end
40     ##
41     # save broser-supplied file content as new Document
42     def create
43         uploaded_file = params[:document][:content]
44                 
45         if uploaded_file then
46             d = Smr::UploadedFile.store(current_user.id, uploaded_file, params[:document][:comment])
47             n = 'saved file %s' % uploaded_file.original_filename
48         else
49             n = 'please select a file first'
50         end
52         unless params[:assign].blank? and d.is_a?(Smr::UploadedFile) then
53             da = DocumentAssign.where(:id_document=>d.id).first || DocumentAssign.new(:id_document=>d.id)
55             if not params[:id_portfolio].blank?
56                 da.id_portfolio = params[:id_portfolio].to_i
57                 n += ', assigned to Portfolio #%i' % params[:id_portfolio]
58             elsif not params[:id_position].blank?
59                 if 1 > Position.joins(:Portfolio).where(:id=>params[:id_position].to_i, :portfolio=>{:id_user=>current_user.id}).count
60                     # FIXME: shouldnt the model check things like this?
61                     raise 'DocumentAssign: position does not belong to current_user'
62                 end
63                 da.id_position = params[:id_position].to_i
64                 n += ', assigned to Position #%i' % params[:id_position]
65             elsif not params[:id_order].blank?
66                 if 1 > Order.joins(:Portfolio).where(:id=>params[:id_order].to_i, :portfolio=>{:id_user=>current_user.id}).count
67                     # FIXME: shouldnt the model check things like this?
68                     raise 'DocumentAssign: order does not belong to position of current_user'
69                 end
70                 da.id_order = params[:id_order].to_i
71                 n += ', assigned to Order #%i' % params[:id_order]
72             end
74             da.is_assigned = true
75             da.save!
76         end
78         redirect_to :back, :notice=>n
79     end
81     ##
82     # serve DocumentData file content with correct mimetype to the browser
83     def download
84         if params[:id] then 
85             d = Smr::UploadedFile.new(params[:id], current_user.id)
86             if not d.empty? then
87                 send_data(d.data, :type=>d.mimetype, :filename=>d.filename, :disposition=>'attachment', :length=>d.size)
88                 return
89             else
90                 n = 'requested document #%i does not exist' % params[:id]
91             end
92         else
93             n = 'please specify a document id to download'
94         end
95         redirect_to :back, :notice=>n
96     end
98     ##
99     # delete Document with DocumentData and DocumentAssign associations
100     #
101     # WORKAROUND:
102     # - the standard way in Rails to delete a record requires JavaScript, but
103     #   SMr is ment to work without it
104     # - therefore deletion is implemented as  non-standard action with seperate
105     #   route
106     def delete_document
107         d = Document.where(:id_user=>current_user.id, :id=>params[:id]).first
108         d.destroy!
109         redirect_to :back, :notice=>'deleted document and its associations'
110     end