documents_controller.rb
94 lines
| 3.2 KiB
| text/x-ruby
|
RubyLexer
|
r6683 | # Redmine - project management software | ||
|
r9453 | # Copyright (C) 2006-2012 Jean-Philippe Lang | ||
|
r330 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r6683 | # | ||
|
r330 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r6683 | # | ||
|
r330 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
|
r22 | |||
|
r330 | class DocumentsController < ApplicationController | ||
|
r2829 | default_search_scope :documents | ||
|
r3483 | model_object Document | ||
|
r7890 | before_filter :find_project_by_project_id, :only => [:index, :new, :create] | ||
before_filter :find_model_object, :except => [:index, :new, :create] | ||||
before_filter :find_project_from_association, :except => [:index, :new, :create] | ||||
|
r998 | before_filter :authorize | ||
|
r6683 | |||
|
r1166 | helper :attachments | ||
|
r6683 | |||
|
r998 | def index | ||
@sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category' | ||||
documents = @project.documents.find :all, :include => [:attachments, :category] | ||||
case @sort_by | ||||
when 'date' | ||||
|
r2981 | @grouped = documents.group_by {|d| d.updated_on.to_date } | ||
|
r998 | when 'title' | ||
@grouped = documents.group_by {|d| d.title.first.upcase} | ||||
when 'author' | ||||
@grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author} | ||||
else | ||||
@grouped = documents.group_by(&:category) | ||||
end | ||||
|
r2122 | @document = @project.documents.build | ||
|
r998 | render :layout => false if request.xhr? | ||
end | ||||
|
r6683 | |||
|
r330 | def show | ||
|
r10687 | @attachments = @document.attachments.all | ||
|
r2 | end | ||
|
r998 | def new | ||
|
r9010 | @document = @project.documents.build | ||
@document.safe_attributes = params[:document] | ||||
|
r7890 | end | ||
def create | ||||
|
r9010 | @document = @project.documents.build | ||
@document.safe_attributes = params[:document] | ||||
|
r8824 | @document.save_attachments(params[:attachments]) | ||
if @document.save | ||||
|
r3414 | render_attachment_warning_if_needed(@document) | ||
|
r998 | flash[:notice] = l(:notice_successful_create) | ||
redirect_to :action => 'index', :project_id => @project | ||||
|
r7890 | else | ||
render :action => 'new' | ||||
|
r998 | end | ||
end | ||||
|
r6683 | |||
|
r330 | def edit | ||
|
r7890 | end | ||
def update | ||||
|
r9010 | @document.safe_attributes = params[:document] | ||
if request.put? and @document.save | ||||
|
r15 | flash[:notice] = l(:notice_successful_update) | ||
|
r2 | redirect_to :action => 'show', :id => @document | ||
|
r7890 | else | ||
render :action => 'edit' | ||||
|
r2 | end | ||
|
r6683 | end | ||
|
r2 | |||
def destroy | ||||
|
r7890 | @document.destroy if request.delete? | ||
|
r998 | redirect_to :controller => 'documents', :action => 'index', :project_id => @project | ||
|
r2 | end | ||
|
r6683 | |||
|
r330 | def add_attachment | ||
|
r3409 | attachments = Attachment.attach_files(@document, params[:attachments]) | ||
|
r3414 | render_attachment_warning_if_needed(@document) | ||
|
r3409 | |||
|
r9455 | if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added') | ||
Mailer.attachments_added(attachments[:files]).deliver | ||||
end | ||||
|
r330 | redirect_to :action => 'show', :id => @document | ||
end | ||||
|
r2 | end | ||