annotate .svn/pristine/1b/1b1f0da73986061f36bbd7cd1895fc7ac1182885.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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