annotate app/controllers/messages_controller.rb @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 3e4c3460b6ca
children 622f24f53b42 261b3d9a4903
rev   line source
Chris@909 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class MessagesController < ApplicationController
Chris@0 19 menu_item :boards
Chris@0 20 default_search_scope :messages
Chris@0 21 before_filter :find_board, :only => [:new, :preview]
Chris@0 22 before_filter :find_message, :except => [:new, :preview]
Chris@0 23 before_filter :authorize, :except => [:preview, :edit, :destroy]
Chris@0 24
Chris@1115 25 helper :boards
Chris@0 26 helper :watchers
Chris@0 27 helper :attachments
Chris@909 28 include AttachmentsHelper
Chris@0 29
Chris@0 30 REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
Chris@909 31
Chris@0 32 # Show a topic and its replies
Chris@0 33 def show
Chris@0 34 page = params[:page]
Chris@0 35 # Find the page of the requested reply
Chris@0 36 if params[:r] && page.nil?
Chris@0 37 offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i])
Chris@0 38 page = 1 + offset / REPLIES_PER_PAGE
Chris@0 39 end
Chris@909 40
Chris@0 41 @reply_count = @topic.children.count
Chris@0 42 @reply_pages = Paginator.new self, @reply_count, REPLIES_PER_PAGE, page
Chris@0 43 @replies = @topic.children.find(:all, :include => [:author, :attachments, {:board => :project}],
Chris@0 44 :order => "#{Message.table_name}.created_on ASC",
Chris@0 45 :limit => @reply_pages.items_per_page,
Chris@0 46 :offset => @reply_pages.current.offset)
Chris@909 47
Chris@0 48 @reply = Message.new(:subject => "RE: #{@message.subject}")
Chris@0 49 render :action => "show", :layout => false if request.xhr?
Chris@0 50 end
Chris@909 51
Chris@0 52 # Create a new topic
Chris@0 53 def new
Chris@929 54 @message = Message.new
Chris@0 55 @message.author = User.current
Chris@0 56 @message.board = @board
Chris@929 57 @message.safe_attributes = params[:message]
Chris@1115 58 if request.post?
Chris@1115 59 @message.save_attachments(params[:attachments])
Chris@1115 60 if @message.save
Chris@1115 61 call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
Chris@1115 62 render_attachment_warning_if_needed(@message)
Chris@1115 63 redirect_to board_message_path(@board, @message)
Chris@1115 64 end
Chris@0 65 end
Chris@0 66 end
Chris@0 67
Chris@0 68 # Reply to a topic
Chris@0 69 def reply
Chris@929 70 @reply = Message.new
Chris@0 71 @reply.author = User.current
Chris@0 72 @reply.board = @board
Chris@929 73 @reply.safe_attributes = params[:reply]
Chris@0 74 @topic.children << @reply
Chris@0 75 if !@reply.new_record?
Chris@0 76 call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
Chris@0 77 attachments = Attachment.attach_files(@reply, params[:attachments])
Chris@0 78 render_attachment_warning_if_needed(@reply)
Chris@0 79 end
Chris@1115 80 redirect_to board_message_path(@board, @topic, :r => @reply)
Chris@0 81 end
Chris@0 82
Chris@0 83 # Edit a message
Chris@0 84 def edit
Chris@0 85 (render_403; return false) unless @message.editable_by?(User.current)
Chris@929 86 @message.safe_attributes = params[:message]
Chris@929 87 if request.post? && @message.save
Chris@0 88 attachments = Attachment.attach_files(@message, params[:attachments])
Chris@0 89 render_attachment_warning_if_needed(@message)
Chris@0 90 flash[:notice] = l(:notice_successful_update)
Chris@0 91 @message.reload
Chris@1115 92 redirect_to board_message_path(@message.board, @message.root, :r => (@message.parent_id && @message.id))
Chris@0 93 end
Chris@0 94 end
Chris@909 95
Chris@0 96 # Delete a messages
Chris@0 97 def destroy
Chris@0 98 (render_403; return false) unless @message.destroyable_by?(User.current)
Chris@1115 99 r = @message.to_param
Chris@0 100 @message.destroy
Chris@1115 101 if @message.parent
Chris@1115 102 redirect_to board_message_path(@board, @message.parent, :r => r)
Chris@1115 103 else
Chris@1115 104 redirect_to project_board_path(@project, @board)
Chris@1115 105 end
Chris@0 106 end
Chris@909 107
Chris@0 108 def quote
Chris@1115 109 @subject = @message.subject
Chris@1115 110 @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')
Chris@1115 111
Chris@1115 112 @content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> "
Chris@1115 113 @content << @message.content.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
Chris@0 114 end
Chris@909 115
Chris@0 116 def preview
Chris@0 117 message = @board.messages.find_by_id(params[:id])
Chris@0 118 @attachements = message.attachments if message
Chris@0 119 @text = (params[:message] || params[:reply])[:content]
Chris@1115 120 @previewed = message
Chris@0 121 render :partial => 'common/preview'
Chris@0 122 end
Chris@909 123
Chris@0 124 private
Chris@0 125 def find_message
Chris@1294 126 return unless find_board
Chris@0 127 @message = @board.messages.find(params[:id], :include => :parent)
Chris@0 128 @topic = @message.root
Chris@0 129 rescue ActiveRecord::RecordNotFound
Chris@0 130 render_404
Chris@0 131 end
Chris@909 132
Chris@0 133 def find_board
Chris@0 134 @board = Board.find(params[:board_id], :include => :project)
Chris@0 135 @project = @board.project
Chris@0 136 rescue ActiveRecord::RecordNotFound
Chris@0 137 render_404
Chris@1294 138 nil
Chris@0 139 end
Chris@0 140 end