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