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