You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

documents_controller.rb 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # redMine - project management software
  2. # Copyright (C) 2006-2007 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class DocumentsController < ApplicationController
  18. layout 'base'
  19. before_filter :find_project, :only => [:index, :new]
  20. before_filter :find_document, :except => [:index, :new]
  21. before_filter :authorize
  22. helper :attachments
  23. def index
  24. @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
  25. documents = @project.documents.find :all, :include => [:attachments, :category]
  26. case @sort_by
  27. when 'date'
  28. @grouped = documents.group_by {|d| d.created_on.to_date }
  29. when 'title'
  30. @grouped = documents.group_by {|d| d.title.first.upcase}
  31. when 'author'
  32. @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
  33. else
  34. @grouped = documents.group_by(&:category)
  35. end
  36. render :layout => false if request.xhr?
  37. end
  38. def show
  39. @attachments = @document.attachments.find(:all, :order => "created_on DESC")
  40. end
  41. def new
  42. @document = @project.documents.build(params[:document])
  43. if request.post? and @document.save
  44. attach_files(@document, params[:attachments])
  45. flash[:notice] = l(:notice_successful_create)
  46. Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added')
  47. redirect_to :action => 'index', :project_id => @project
  48. end
  49. end
  50. def edit
  51. @categories = Enumeration::get_values('DCAT')
  52. if request.post? and @document.update_attributes(params[:document])
  53. flash[:notice] = l(:notice_successful_update)
  54. redirect_to :action => 'show', :id => @document
  55. end
  56. end
  57. def destroy
  58. @document.destroy
  59. redirect_to :controller => 'documents', :action => 'index', :project_id => @project
  60. end
  61. def download
  62. @attachment = @document.attachments.find(params[:attachment_id])
  63. @attachment.increment_download
  64. send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
  65. :type => @attachment.content_type
  66. rescue
  67. render_404
  68. end
  69. def add_attachment
  70. attachments = attach_files(@document, params[:attachments])
  71. Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('document_added')
  72. redirect_to :action => 'show', :id => @document
  73. end
  74. def destroy_attachment
  75. @document.attachments.find(params[:attachment_id]).destroy
  76. redirect_to :action => 'show', :id => @document
  77. end
  78. private
  79. def find_project
  80. @project = Project.find(params[:project_id])
  81. rescue ActiveRecord::RecordNotFound
  82. render_404
  83. end
  84. def find_document
  85. @document = Document.find(params[:id])
  86. @project = @document.project
  87. rescue ActiveRecord::RecordNotFound
  88. render_404
  89. end
  90. end