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
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
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(
33 :date=>smr_browse_date,
34 :ransack_query=>session[:documents_query]
37 @documents, @total_pages = smr_paginate(@page = smr_page, files)
41 # save broser-supplied file content as new Document
43 uploaded_file = params[:document][:content]
46 d = Smr::UploadedFile.store(current_user.id, uploaded_file, params[:document][:comment])
47 n = 'saved file %s' % uploaded_file.original_filename
49 n = 'please select a file first'
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'
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'
70 da.id_order = params[:id_order].to_i
71 n += ', assigned to Order #%i' % params[:id_order]
78 redirect_to :back, :notice=>n
82 # serve DocumentData file content with correct mimetype to the browser
85 d = Smr::UploadedFile.new(params[:id], current_user.id)
87 send_data(d.data, :type=>d.mimetype, :filename=>d.filename, :disposition=>'attachment', :length=>d.size)
90 n = 'requested document #%i does not exist' % params[:id]
93 n = 'please specify a document id to download'
95 redirect_to :back, :notice=>n
99 # delete Document with DocumentData and DocumentAssign associations
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
107 d = Document.where(:id_user=>current_user.id, :id=>params[:id]).first
109 redirect_to :back, :notice=>'deleted document and its associations'