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