You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

messages_controller.rb 5.3KB

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