To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / messages_controller.rb @ 1535:e2c122809c5c

History | View | Annotate | Download (4.73 KB)

1 909:cbb26bc654de Chris
# Redmine - project management software
2 1494:e248c7af89ec Chris
# Copyright (C) 2006-2014  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 909:cbb26bc654de Chris
#
9 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
#
14 0:513646585e45 Chris
# 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
18
class MessagesController < ApplicationController
19
  menu_item :boards
20
  default_search_scope :messages
21
  before_filter :find_board, :only => [:new, :preview]
22 1464:261b3d9a4903 Chris
  before_filter :find_attachments, :only => [:preview]
23 0:513646585e45 Chris
  before_filter :find_message, :except => [:new, :preview]
24
  before_filter :authorize, :except => [:preview, :edit, :destroy]
25
26 1115:433d4f72a19b Chris
  helper :boards
27 0:513646585e45 Chris
  helper :watchers
28
  helper :attachments
29 909:cbb26bc654de Chris
  include AttachmentsHelper
30 0:513646585e45 Chris
31
  REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
32 909:cbb26bc654de Chris
33 0:513646585e45 Chris
  # Show a topic and its replies
34
  def show
35
    page = params[:page]
36
    # Find the page of the requested reply
37
    if params[:r] && page.nil?
38 1464:261b3d9a4903 Chris
      offset = @topic.children.where("#{Message.table_name}.id < ?", params[:r].to_i).count
39 0:513646585e45 Chris
      page = 1 + offset / REPLIES_PER_PAGE
40
    end
41 909:cbb26bc654de Chris
42 0:513646585e45 Chris
    @reply_count = @topic.children.count
43 1464:261b3d9a4903 Chris
    @reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page
44
    @replies =  @topic.children.
45
      includes(:author, :attachments, {:board => :project}).
46
      reorder("#{Message.table_name}.created_on ASC").
47
      limit(@reply_pages.per_page).
48
      offset(@reply_pages.offset).
49
      all
50 909:cbb26bc654de Chris
51 0:513646585e45 Chris
    @reply = Message.new(:subject => "RE: #{@message.subject}")
52
    render :action => "show", :layout => false if request.xhr?
53
  end
54 909:cbb26bc654de Chris
55 0:513646585e45 Chris
  # Create a new topic
56
  def new
57 929:5f33065ddc4b Chris
    @message = Message.new
58 0:513646585e45 Chris
    @message.author = User.current
59
    @message.board = @board
60 929:5f33065ddc4b Chris
    @message.safe_attributes = params[:message]
61 1115:433d4f72a19b Chris
    if request.post?
62
      @message.save_attachments(params[:attachments])
63
      if @message.save
64
        call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
65
        render_attachment_warning_if_needed(@message)
66
        redirect_to board_message_path(@board, @message)
67
      end
68 0:513646585e45 Chris
    end
69
  end
70
71
  # Reply to a topic
72
  def reply
73 929:5f33065ddc4b Chris
    @reply = Message.new
74 0:513646585e45 Chris
    @reply.author = User.current
75
    @reply.board = @board
76 929:5f33065ddc4b Chris
    @reply.safe_attributes = params[:reply]
77 0:513646585e45 Chris
    @topic.children << @reply
78
    if !@reply.new_record?
79
      call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
80
      attachments = Attachment.attach_files(@reply, params[:attachments])
81
      render_attachment_warning_if_needed(@reply)
82
    end
83 1115:433d4f72a19b Chris
    redirect_to board_message_path(@board, @topic, :r => @reply)
84 0:513646585e45 Chris
  end
85
86
  # Edit a message
87
  def edit
88
    (render_403; return false) unless @message.editable_by?(User.current)
89 929:5f33065ddc4b Chris
    @message.safe_attributes = params[:message]
90
    if request.post? && @message.save
91 0:513646585e45 Chris
      attachments = Attachment.attach_files(@message, params[:attachments])
92
      render_attachment_warning_if_needed(@message)
93
      flash[:notice] = l(:notice_successful_update)
94
      @message.reload
95 1115:433d4f72a19b Chris
      redirect_to board_message_path(@message.board, @message.root, :r => (@message.parent_id && @message.id))
96 0:513646585e45 Chris
    end
97
  end
98 909:cbb26bc654de Chris
99 0:513646585e45 Chris
  # Delete a messages
100
  def destroy
101
    (render_403; return false) unless @message.destroyable_by?(User.current)
102 1115:433d4f72a19b Chris
    r = @message.to_param
103 0:513646585e45 Chris
    @message.destroy
104 1115:433d4f72a19b Chris
    if @message.parent
105
      redirect_to board_message_path(@board, @message.parent, :r => r)
106
    else
107
      redirect_to project_board_path(@project, @board)
108
    end
109 0:513646585e45 Chris
  end
110 909:cbb26bc654de Chris
111 0:513646585e45 Chris
  def quote
112 1115:433d4f72a19b Chris
    @subject = @message.subject
113
    @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')
114
115
    @content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> "
116 1517:dffacf8a6908 Chris
    @content << @message.content.to_s.strip.gsub(%r{<pre>(.*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
117 0:513646585e45 Chris
  end
118 909:cbb26bc654de Chris
119 0:513646585e45 Chris
  def preview
120
    message = @board.messages.find_by_id(params[:id])
121
    @text = (params[:message] || params[:reply])[:content]
122 1115:433d4f72a19b Chris
    @previewed = message
123 0:513646585e45 Chris
    render :partial => 'common/preview'
124
  end
125 909:cbb26bc654de Chris
126 0:513646585e45 Chris
private
127
  def find_message
128 1294:3e4c3460b6ca Chris
    return unless find_board
129 1517:dffacf8a6908 Chris
    @message = @board.messages.includes(:parent).find(params[:id])
130 0:513646585e45 Chris
    @topic = @message.root
131
  rescue ActiveRecord::RecordNotFound
132
    render_404
133
  end
134 909:cbb26bc654de Chris
135 0:513646585e45 Chris
  def find_board
136 1517:dffacf8a6908 Chris
    @board = Board.includes(:project).find(params[:board_id])
137 0:513646585e45 Chris
    @project = @board.project
138
  rescue ActiveRecord::RecordNotFound
139
    render_404
140 1294:3e4c3460b6ca Chris
    nil
141 0:513646585e45 Chris
  end
142
end