annotate .svn/pristine/85/85316aa62f266023688bb5d8d7b7c31ca5b820f1.svn-base @ 931:ec1c49528f36 cannam_integration

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