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