messages_controller.rb 5.31 KB
Newer Older
1 2
# Redmine - project management software
# Copyright (C) 2006-2011  Jean-Philippe Lang
jplang's avatar
jplang committed
3 4 5 6 7
#
# 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.
8
#
jplang's avatar
jplang committed
9 10 11 12
# 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.
13
#
jplang's avatar
jplang committed
14 15 16 17 18
# 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.

class MessagesController < ApplicationController
19
  menu_item :boards
20
  default_search_scope :messages
21 22
  before_filter :find_board, :only => [:new, :preview]
  before_filter :find_message, :except => [:new, :preview]
23
  before_filter :authorize, :except => [:preview, :edit, :destroy]
jplang's avatar
jplang committed
24

25
  helper :watchers
26
  helper :attachments
27
  include AttachmentsHelper
28

29
  REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
30

jplang's avatar
jplang committed
31
  # Show a topic and its replies
jplang's avatar
jplang committed
32
  def show
33 34 35 36 37 38
    page = params[:page]
    # Find the page of the requested reply
    if params[:r] && page.nil?
      offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i])
      page = 1 + offset / REPLIES_PER_PAGE
    end
39

40 41 42 43 44 45
    @reply_count = @topic.children.count
    @reply_pages = Paginator.new self, @reply_count, REPLIES_PER_PAGE, page
    @replies =  @topic.children.find(:all, :include => [:author, :attachments, {:board => :project}],
                                           :order => "#{Message.table_name}.created_on ASC",
                                           :limit => @reply_pages.items_per_page,
                                           :offset => @reply_pages.current.offset)
46

jplang's avatar
jplang committed
47 48 49
    @reply = Message.new(:subject => "RE: #{@message.subject}")
    render :action => "show", :layout => false if request.xhr?
  end
50

jplang's avatar
jplang committed
51
  # Create a new topic
jplang's avatar
jplang committed
52
  def new
53
    @message = Message.new
54
    @message.author = User.current
jplang's avatar
jplang committed
55
    @message.board = @board
56
    @message.safe_attributes = params[:message]
57 58 59 60 61 62 63
    if request.post?
      @message.save_attachments(params[:attachments])
      if @message.save
        call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
        render_attachment_warning_if_needed(@message)
        redirect_to :action => 'show', :id => @message
      end
jplang's avatar
jplang committed
64 65 66
    end
  end

jplang's avatar
jplang committed
67
  # Reply to a topic
jplang's avatar
jplang committed
68
  def reply
69
    @reply = Message.new
70
    @reply.author = User.current
jplang's avatar
jplang committed
71
    @reply.board = @board
72
    @reply.safe_attributes = params[:reply]
jplang's avatar
jplang committed
73
    @topic.children << @reply
74
    if !@reply.new_record?
edavis10's avatar
edavis10 committed
75
      call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
76
      attachments = Attachment.attach_files(@reply, params[:attachments])
77
      render_attachment_warning_if_needed(@reply)
78
    end
79
    redirect_to :action => 'show', :id => @topic, :r => @reply
jplang's avatar
jplang committed
80 81 82 83
  end

  # Edit a message
  def edit
84
    (render_403; return false) unless @message.editable_by?(User.current)
85 86
    @message.safe_attributes = params[:message]
    if request.post? && @message.save
87
      attachments = Attachment.attach_files(@message, params[:attachments])
88
      render_attachment_warning_if_needed(@message)
jplang's avatar
jplang committed
89
      flash[:notice] = l(:notice_successful_update)
90
      @message.reload
91
      redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id)
jplang's avatar
jplang committed
92 93
    end
  end
94

jplang's avatar
jplang committed
95 96
  # Delete a messages
  def destroy
97
    (render_403; return false) unless @message.destroyable_by?(User.current)
jplang's avatar
jplang committed
98
    r = @message.to_param
jplang's avatar
jplang committed
99 100 101
    @message.destroy
    redirect_to @message.parent.nil? ?
      { :controller => 'boards', :action => 'show', :project_id => @project, :id => @board } :
jplang's avatar
jplang committed
102
      { :action => 'show', :id => @message.parent, :r => r }
jplang's avatar
jplang committed
103
  end
104

105 106 107
  def quote
    user = @message.author
    text = @message.content
108
    subject = @message.subject.gsub('"', '\"')
109
    subject = "RE: #{subject}" unless subject.starts_with?('RE:')
110 111 112
    content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> "
    content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n"
    render(:update) { |page|
113
      page << "$('message_subject').value = \"#{subject}\";"
114 115 116 117 118 119 120
      page.<< "$('message_content').value = \"#{content}\";"
      page.show 'reply'
      page << "Form.Element.focus('message_content');"
      page << "Element.scrollTo('reply');"
      page << "$('message_content').scrollTop = $('message_content').scrollHeight - $('message_content').clientHeight;"
    }
  end
121

122 123 124 125 126 127
  def preview
    message = @board.messages.find_by_id(params[:id])
    @attachements = message.attachments if message
    @text = (params[:message] || params[:reply])[:content]
    render :partial => 'common/preview'
  end
128

jplang's avatar
jplang committed
129
private
jplang's avatar
jplang committed
130 131 132 133 134 135 136
  def find_message
    find_board
    @message = @board.messages.find(params[:id], :include => :parent)
    @topic = @message.root
  rescue ActiveRecord::RecordNotFound
    render_404
  end
137

jplang's avatar
jplang committed
138
  def find_board
jplang's avatar
jplang committed
139 140 141 142 143 144
    @board = Board.find(params[:board_id], :include => :project)
    @project = @board.project
  rescue ActiveRecord::RecordNotFound
    render_404
  end
end