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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class DocumentsController < ApplicationController
  19. default_search_scope :documents
  20. model_object Document
  21. before_action :find_project_by_project_id, :only => [:index, :new, :create]
  22. before_action :find_model_object, :except => [:index, :new, :create]
  23. before_action :find_project_from_association, :except => [:index, :new, :create]
  24. before_action :authorize
  25. helper :attachments
  26. helper :custom_fields
  27. def index
  28. @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
  29. documents = @project.documents.includes(:attachments, :category).to_a
  30. case @sort_by
  31. when 'date'
  32. documents.sort!{|a, b| b.updated_on <=> a.updated_on}
  33. @grouped = documents.group_by {|d| d.updated_on.to_date}
  34. when 'title'
  35. @grouped = documents.group_by {|d| d.title.first.upcase}
  36. when 'author'
  37. @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
  38. else
  39. @grouped = documents.group_by(&:category)
  40. end
  41. @document = @project.documents.build
  42. render :layout => false if request.xhr?
  43. end
  44. def show
  45. @attachments = @document.attachments.to_a
  46. end
  47. def new
  48. @document = @project.documents.build
  49. @document.safe_attributes = params[:document]
  50. end
  51. def create
  52. @document = @project.documents.build
  53. @document.safe_attributes = params[:document]
  54. @document.save_attachments(params[:attachments])
  55. if @document.save
  56. render_attachment_warning_if_needed(@document)
  57. flash[:notice] = l(:notice_successful_create)
  58. redirect_to project_documents_path(@project)
  59. else
  60. render :action => 'new'
  61. end
  62. end
  63. def edit
  64. end
  65. def update
  66. @document.safe_attributes = params[:document]
  67. if @document.save
  68. flash[:notice] = l(:notice_successful_update)
  69. redirect_to document_path(@document)
  70. else
  71. render :action => 'edit'
  72. end
  73. end
  74. def destroy
  75. @document.destroy if request.delete?
  76. flash[:notice] = l(:notice_successful_delete)
  77. redirect_to project_documents_path(@project)
  78. end
  79. def add_attachment
  80. attachments = Attachment.attach_files(@document, params[:attachments])
  81. render_attachment_warning_if_needed(@document)
  82. if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
  83. Mailer.deliver_attachments_added(attachments[:files])
  84. end
  85. redirect_to document_path(@document)
  86. end
  87. end