| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							|  |  |  | # Copyright (C) 2006-2011  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # 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. | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  | # 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. | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  | # 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 | 
					
						
							| 
									
										
										
										
											2008-01-19 11:53:43 +00:00
										 |  |  |   menu_item :boards | 
					
						
							| 
									
										
										
										
											2009-10-21 17:07:18 +00:00
										 |  |  |   default_search_scope :messages | 
					
						
							| 
									
										
										
										
											2008-03-07 17:49:44 +00:00
										 |  |  |   before_filter :find_board, :only => [:new, :preview] | 
					
						
							|  |  |  |   before_filter :find_message, :except => [:new, :preview] | 
					
						
							| 
									
										
										
										
											2008-11-11 15:07:55 +00:00
										 |  |  |   before_filter :authorize, :except => [:preview, :edit, :destroy] | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-19 15:32:52 +00:00
										 |  |  |   helper :watchers | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  |   helper :attachments | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  |   include AttachmentsHelper | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-06 12:54:13 +00:00
										 |  |  |   REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE) | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   # Show a topic and its replies | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |   def show | 
					
						
							| 
									
										
										
										
											2010-02-06 12:54:13 +00:00
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-06 12:54:13 +00:00
										 |  |  |     @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) | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |     @reply = Message.new(:subject => "RE: #{@message.subject}") | 
					
						
							|  |  |  |     render :action => "show", :layout => false if request.xhr? | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   # Create a new topic | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |   def new | 
					
						
							| 
									
										
										
										
											2012-03-06 19:46:59 +00:00
										 |  |  |     @message = Message.new | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |     @message.author = User.current | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |     @message.board = @board | 
					
						
							| 
									
										
										
										
											2012-03-06 19:46:59 +00:00
										 |  |  |     @message.safe_attributes = params[:message] | 
					
						
							| 
									
										
										
										
											2012-02-23 13:02:35 +00:00
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   # Reply to a topic | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |   def reply | 
					
						
							| 
									
										
										
										
											2012-03-06 19:46:59 +00:00
										 |  |  |     @reply = Message.new | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |     @reply.author = User.current | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |     @reply.board = @board | 
					
						
							| 
									
										
										
										
											2012-03-06 19:46:59 +00:00
										 |  |  |     @reply.safe_attributes = params[:reply] | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |     @topic.children << @reply | 
					
						
							| 
									
										
										
										
											2007-11-22 19:19:47 +00:00
										 |  |  |     if !@reply.new_record? | 
					
						
							| 
									
										
										
										
											2009-05-30 22:43:01 +00:00
										 |  |  |       call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply}) | 
					
						
							| 
									
										
										
										
											2010-03-02 19:26:03 +00:00
										 |  |  |       attachments = Attachment.attach_files(@reply, params[:attachments]) | 
					
						
							| 
									
										
										
										
											2010-03-03 17:05:00 +00:00
										 |  |  |       render_attachment_warning_if_needed(@reply) | 
					
						
							| 
									
										
										
										
											2007-11-22 19:19:47 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-02-06 12:54:13 +00:00
										 |  |  |     redirect_to :action => 'show', :id => @topic, :r => @reply | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Edit a message | 
					
						
							|  |  |  |   def edit | 
					
						
							| 
									
										
										
										
											2009-12-18 14:22:18 +00:00
										 |  |  |     (render_403; return false) unless @message.editable_by?(User.current) | 
					
						
							| 
									
										
										
										
											2012-03-06 19:46:59 +00:00
										 |  |  |     @message.safe_attributes = params[:message] | 
					
						
							|  |  |  |     if request.post? && @message.save | 
					
						
							| 
									
										
										
										
											2010-03-02 19:26:03 +00:00
										 |  |  |       attachments = Attachment.attach_files(@message, params[:attachments]) | 
					
						
							| 
									
										
										
										
											2010-03-03 17:05:00 +00:00
										 |  |  |       render_attachment_warning_if_needed(@message) | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |       flash[:notice] = l(:notice_successful_update) | 
					
						
							| 
									
										
										
										
											2009-04-05 10:08:11 +00:00
										 |  |  |       @message.reload | 
					
						
							| 
									
										
										
										
											2010-02-06 12:54:13 +00:00
										 |  |  |       redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id) | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   # Delete a messages | 
					
						
							|  |  |  |   def destroy | 
					
						
							| 
									
										
										
										
											2009-12-18 14:22:18 +00:00
										 |  |  |     (render_403; return false) unless @message.destroyable_by?(User.current) | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |     @message.destroy | 
					
						
							|  |  |  |     redirect_to @message.parent.nil? ? | 
					
						
							|  |  |  |       { :controller => 'boards', :action => 'show', :project_id => @project, :id => @board } : | 
					
						
							| 
									
										
										
										
											2010-02-06 12:54:13 +00:00
										 |  |  |       { :action => 'show', :id => @message.parent, :r => @message } | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-28 18:56:47 +00:00
										 |  |  |   def quote | 
					
						
							|  |  |  |     user = @message.author | 
					
						
							|  |  |  |     text = @message.content | 
					
						
							| 
									
										
										
										
											2009-11-13 17:46:38 +00:00
										 |  |  |     subject = @message.subject.gsub('"', '\"') | 
					
						
							| 
									
										
										
										
											2009-11-13 19:14:18 +00:00
										 |  |  |     subject = "RE: #{subject}" unless subject.starts_with?('RE:') | 
					
						
							| 
									
										
										
										
											2008-08-28 18:56:47 +00:00
										 |  |  |     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| | 
					
						
							| 
									
										
										
										
											2011-11-27 21:30:50 +00:00
										 |  |  |       page << "$('message_subject').value = \"#{subject}\";" | 
					
						
							| 
									
										
										
										
											2008-08-28 18:56:47 +00:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-07 17:49:44 +00:00
										 |  |  |   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 | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  | private | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   def find_message | 
					
						
							|  |  |  |     find_board | 
					
						
							|  |  |  |     @message = @board.messages.find(params[:id], :include => :parent) | 
					
						
							|  |  |  |     @topic = @message.root | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   def find_board | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |     @board = Board.find(params[:board_id], :include => :project) | 
					
						
							|  |  |  |     @project = @board.project | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |