Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class MessagesController < ApplicationController Chris@0: menu_item :boards Chris@0: default_search_scope :messages Chris@0: before_filter :find_board, :only => [:new, :preview] Chris@0: before_filter :find_message, :except => [:new, :preview] Chris@0: before_filter :authorize, :except => [:preview, :edit, :destroy] Chris@0: Chris@0: verify :method => :post, :only => [ :reply, :destroy ], :redirect_to => { :action => :show } Chris@0: verify :xhr => true, :only => :quote Chris@0: Chris@0: helper :watchers Chris@0: helper :attachments Chris@909: include AttachmentsHelper Chris@0: Chris@0: REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE) Chris@909: Chris@0: # Show a topic and its replies Chris@0: def show Chris@0: page = params[:page] Chris@0: # Find the page of the requested reply Chris@0: if params[:r] && page.nil? Chris@0: offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i]) Chris@0: page = 1 + offset / REPLIES_PER_PAGE Chris@0: end Chris@909: Chris@0: @reply_count = @topic.children.count Chris@0: @reply_pages = Paginator.new self, @reply_count, REPLIES_PER_PAGE, page Chris@0: @replies = @topic.children.find(:all, :include => [:author, :attachments, {:board => :project}], Chris@0: :order => "#{Message.table_name}.created_on ASC", Chris@0: :limit => @reply_pages.items_per_page, Chris@0: :offset => @reply_pages.current.offset) Chris@909: Chris@0: @reply = Message.new(:subject => "RE: #{@message.subject}") Chris@0: render :action => "show", :layout => false if request.xhr? Chris@0: end Chris@909: Chris@0: # Create a new topic Chris@0: def new Chris@929: @message = Message.new Chris@0: @message.author = User.current Chris@0: @message.board = @board Chris@929: @message.safe_attributes = params[:message] Chris@0: if request.post? && @message.save Chris@0: call_hook(:controller_messages_new_after_save, { :params => params, :message => @message}) Chris@0: attachments = Attachment.attach_files(@message, params[:attachments]) Chris@0: render_attachment_warning_if_needed(@message) Chris@0: redirect_to :action => 'show', :id => @message Chris@0: end Chris@0: end Chris@0: Chris@0: # Reply to a topic Chris@0: def reply Chris@929: @reply = Message.new Chris@0: @reply.author = User.current Chris@0: @reply.board = @board Chris@929: @reply.safe_attributes = params[:reply] Chris@0: @topic.children << @reply Chris@0: if !@reply.new_record? Chris@0: call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply}) Chris@0: attachments = Attachment.attach_files(@reply, params[:attachments]) Chris@0: render_attachment_warning_if_needed(@reply) Chris@0: end Chris@0: redirect_to :action => 'show', :id => @topic, :r => @reply Chris@0: end Chris@0: Chris@0: # Edit a message Chris@0: def edit Chris@0: (render_403; return false) unless @message.editable_by?(User.current) Chris@929: @message.safe_attributes = params[:message] Chris@929: if request.post? && @message.save Chris@0: attachments = Attachment.attach_files(@message, params[:attachments]) Chris@0: render_attachment_warning_if_needed(@message) Chris@0: flash[:notice] = l(:notice_successful_update) Chris@0: @message.reload Chris@0: redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id) Chris@0: end Chris@0: end Chris@909: Chris@0: # Delete a messages Chris@0: def destroy Chris@0: (render_403; return false) unless @message.destroyable_by?(User.current) Chris@0: @message.destroy Chris@0: redirect_to @message.parent.nil? ? Chris@0: { :controller => 'boards', :action => 'show', :project_id => @project, :id => @board } : Chris@0: { :action => 'show', :id => @message.parent, :r => @message } Chris@0: end Chris@909: Chris@0: def quote Chris@0: user = @message.author Chris@0: text = @message.content Chris@0: subject = @message.subject.gsub('"', '\"') Chris@0: subject = "RE: #{subject}" unless subject.starts_with?('RE:') Chris@0: content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> " Chris@0: content << text.to_s.strip.gsub(%r{
((.|\s)*?)
}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n" Chris@0: render(:update) { |page| Chris@909: page << "$('message_subject').value = \"#{subject}\";" Chris@0: page.<< "$('message_content').value = \"#{content}\";" Chris@0: page.show 'reply' Chris@0: page << "Form.Element.focus('message_content');" Chris@0: page << "Element.scrollTo('reply');" Chris@0: page << "$('message_content').scrollTop = $('message_content').scrollHeight - $('message_content').clientHeight;" Chris@0: } Chris@0: end Chris@909: Chris@0: def preview Chris@0: message = @board.messages.find_by_id(params[:id]) Chris@0: @attachements = message.attachments if message Chris@0: @text = (params[:message] || params[:reply])[:content] Chris@0: render :partial => 'common/preview' Chris@0: end Chris@909: Chris@0: private Chris@0: def find_message Chris@0: find_board Chris@0: @message = @board.messages.find(params[:id], :include => :parent) Chris@0: @topic = @message.root Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@909: Chris@0: def find_board Chris@0: @board = Board.find(params[:board_id], :include => :project) Chris@0: @project = @board.project Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@0: end