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 @ 1297:0a574315af3e

History | View | Annotate | Download (4.88 KB)

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