annotate app/controllers/messages_controller.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children cbb26bc654de
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 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@0 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@0 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@0 25 verify :method => :post, :only => [ :reply, :destroy ], :redirect_to => { :action => :show }
Chris@0 26 verify :xhr => true, :only => :quote
Chris@0 27
Chris@0 28 helper :watchers
Chris@0 29 helper :attachments
Chris@0 30 include AttachmentsHelper
Chris@0 31
Chris@0 32 REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
Chris@0 33
Chris@0 34 # Show a topic and its replies
Chris@0 35 def show
Chris@0 36 page = params[:page]
Chris@0 37 # Find the page of the requested reply
Chris@0 38 if params[:r] && page.nil?
Chris@0 39 offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i])
Chris@0 40 page = 1 + offset / REPLIES_PER_PAGE
Chris@0 41 end
Chris@0 42
Chris@0 43 @reply_count = @topic.children.count
Chris@0 44 @reply_pages = Paginator.new self, @reply_count, REPLIES_PER_PAGE, page
Chris@0 45 @replies = @topic.children.find(:all, :include => [:author, :attachments, {:board => :project}],
Chris@0 46 :order => "#{Message.table_name}.created_on ASC",
Chris@0 47 :limit => @reply_pages.items_per_page,
Chris@0 48 :offset => @reply_pages.current.offset)
Chris@0 49
Chris@0 50 @reply = Message.new(:subject => "RE: #{@message.subject}")
Chris@0 51 render :action => "show", :layout => false if request.xhr?
Chris@0 52 end
Chris@0 53
Chris@0 54 # Create a new topic
Chris@0 55 def new
Chris@0 56 @message = Message.new(params[:message])
Chris@0 57 @message.author = User.current
Chris@0 58 @message.board = @board
Chris@0 59 if params[:message] && User.current.allowed_to?(:edit_messages, @project)
Chris@0 60 @message.locked = params[:message]['locked']
Chris@0 61 @message.sticky = params[:message]['sticky']
Chris@0 62 end
Chris@0 63 if request.post? && @message.save
Chris@0 64 call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
Chris@0 65 attachments = Attachment.attach_files(@message, params[:attachments])
Chris@0 66 render_attachment_warning_if_needed(@message)
Chris@0 67 redirect_to :action => 'show', :id => @message
Chris@0 68 end
Chris@0 69 end
Chris@0 70
Chris@0 71 # Reply to a topic
Chris@0 72 def reply
Chris@0 73 @reply = Message.new(params[:reply])
Chris@0 74 @reply.author = User.current
Chris@0 75 @reply.board = @board
Chris@0 76 @topic.children << @reply
Chris@0 77 if !@reply.new_record?
Chris@0 78 call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
Chris@0 79 attachments = Attachment.attach_files(@reply, params[:attachments])
Chris@0 80 render_attachment_warning_if_needed(@reply)
Chris@0 81 end
Chris@0 82 redirect_to :action => 'show', :id => @topic, :r => @reply
Chris@0 83 end
Chris@0 84
Chris@0 85 # Edit a message
Chris@0 86 def edit
Chris@0 87 (render_403; return false) unless @message.editable_by?(User.current)
Chris@0 88 if params[:message]
Chris@0 89 @message.locked = params[:message]['locked']
Chris@0 90 @message.sticky = params[:message]['sticky']
Chris@0 91 end
Chris@0 92 if request.post? && @message.update_attributes(params[:message])
Chris@0 93 attachments = Attachment.attach_files(@message, params[:attachments])
Chris@0 94 render_attachment_warning_if_needed(@message)
Chris@0 95 flash[:notice] = l(:notice_successful_update)
Chris@0 96 @message.reload
Chris@0 97 redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id)
Chris@0 98 end
Chris@0 99 end
Chris@0 100
Chris@0 101 # Delete a messages
Chris@0 102 def destroy
Chris@0 103 (render_403; return false) unless @message.destroyable_by?(User.current)
Chris@0 104 @message.destroy
Chris@0 105 redirect_to @message.parent.nil? ?
Chris@0 106 { :controller => 'boards', :action => 'show', :project_id => @project, :id => @board } :
Chris@0 107 { :action => 'show', :id => @message.parent, :r => @message }
Chris@0 108 end
Chris@0 109
Chris@0 110 def quote
Chris@0 111 user = @message.author
Chris@0 112 text = @message.content
Chris@0 113 subject = @message.subject.gsub('"', '\"')
Chris@0 114 subject = "RE: #{subject}" unless subject.starts_with?('RE:')
Chris@0 115 content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> "
Chris@0 116 content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n"
Chris@0 117 render(:update) { |page|
Chris@0 118 page << "$('reply_subject').value = \"#{subject}\";"
Chris@0 119 page.<< "$('message_content').value = \"#{content}\";"
Chris@0 120 page.show 'reply'
Chris@0 121 page << "Form.Element.focus('message_content');"
Chris@0 122 page << "Element.scrollTo('reply');"
Chris@0 123 page << "$('message_content').scrollTop = $('message_content').scrollHeight - $('message_content').clientHeight;"
Chris@0 124 }
Chris@0 125 end
Chris@0 126
Chris@0 127 def preview
Chris@0 128 message = @board.messages.find_by_id(params[:id])
Chris@0 129 @attachements = message.attachments if message
Chris@0 130 @text = (params[:message] || params[:reply])[:content]
Chris@0 131 render :partial => 'common/preview'
Chris@0 132 end
Chris@0 133
Chris@0 134 private
Chris@0 135 def find_message
Chris@0 136 find_board
Chris@0 137 @message = @board.messages.find(params[:id], :include => :parent)
Chris@0 138 @topic = @message.root
Chris@0 139 rescue ActiveRecord::RecordNotFound
Chris@0 140 render_404
Chris@0 141 end
Chris@0 142
Chris@0 143 def find_board
Chris@0 144 @board = Board.find(params[:board_id], :include => :project)
Chris@0 145 @project = @board.project
Chris@0 146 rescue ActiveRecord::RecordNotFound
Chris@0 147 render_404
Chris@0 148 end
Chris@0 149 end