Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

documents_controller.rb 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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. redirect_to project_documents_path(@project)
  77. end
  78. def add_attachment
  79. attachments = Attachment.attach_files(@document, params[:attachments])
  80. render_attachment_warning_if_needed(@document)
  81. if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
  82. Mailer.deliver_attachments_added(attachments[:files])
  83. end
  84. redirect_to document_path(@document)
  85. end
  86. end