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