Chris@909
|
1 # Redmine - project management software
|
Chris@909
|
2 # Copyright (C) 2006-2011 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@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@909
|
30 include AttachmentsHelper
|
Chris@0
|
31
|
Chris@0
|
32 REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
|
Chris@909
|
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@909
|
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@909
|
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@909
|
53
|
Chris@0
|
54 # Create a new topic
|
Chris@0
|
55 def new
|
Chris@929
|
56 @message = Message.new
|
Chris@0
|
57 @message.author = User.current
|
Chris@0
|
58 @message.board = @board
|
Chris@929
|
59 @message.safe_attributes = params[:message]
|
Chris@0
|
60 if request.post? && @message.save
|
Chris@0
|
61 call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
|
Chris@0
|
62 attachments = Attachment.attach_files(@message, params[:attachments])
|
Chris@0
|
63 render_attachment_warning_if_needed(@message)
|
Chris@0
|
64 redirect_to :action => 'show', :id => @message
|
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@0
|
80 redirect_to :action => 'show', :id => @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@0
|
92 redirect_to :action => 'show', :board_id => @message.board, :id => @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@0
|
99 @message.destroy
|
Chris@0
|
100 redirect_to @message.parent.nil? ?
|
Chris@0
|
101 { :controller => 'boards', :action => 'show', :project_id => @project, :id => @board } :
|
Chris@0
|
102 { :action => 'show', :id => @message.parent, :r => @message }
|
Chris@0
|
103 end
|
Chris@909
|
104
|
Chris@0
|
105 def quote
|
Chris@0
|
106 user = @message.author
|
Chris@0
|
107 text = @message.content
|
Chris@0
|
108 subject = @message.subject.gsub('"', '\"')
|
Chris@0
|
109 subject = "RE: #{subject}" unless subject.starts_with?('RE:')
|
Chris@0
|
110 content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> "
|
Chris@0
|
111 content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n"
|
Chris@0
|
112 render(:update) { |page|
|
Chris@909
|
113 page << "$('message_subject').value = \"#{subject}\";"
|
Chris@0
|
114 page.<< "$('message_content').value = \"#{content}\";"
|
Chris@0
|
115 page.show 'reply'
|
Chris@0
|
116 page << "Form.Element.focus('message_content');"
|
Chris@0
|
117 page << "Element.scrollTo('reply');"
|
Chris@0
|
118 page << "$('message_content').scrollTop = $('message_content').scrollHeight - $('message_content').clientHeight;"
|
Chris@0
|
119 }
|
Chris@0
|
120 end
|
Chris@909
|
121
|
Chris@0
|
122 def preview
|
Chris@0
|
123 message = @board.messages.find_by_id(params[:id])
|
Chris@0
|
124 @attachements = message.attachments if message
|
Chris@0
|
125 @text = (params[:message] || params[:reply])[:content]
|
Chris@0
|
126 render :partial => 'common/preview'
|
Chris@0
|
127 end
|
Chris@909
|
128
|
Chris@0
|
129 private
|
Chris@0
|
130 def find_message
|
Chris@0
|
131 find_board
|
Chris@0
|
132 @message = @board.messages.find(params[:id], :include => :parent)
|
Chris@0
|
133 @topic = @message.root
|
Chris@0
|
134 rescue ActiveRecord::RecordNotFound
|
Chris@0
|
135 render_404
|
Chris@0
|
136 end
|
Chris@909
|
137
|
Chris@0
|
138 def find_board
|
Chris@0
|
139 @board = Board.find(params[:board_id], :include => :project)
|
Chris@0
|
140 @project = @board.project
|
Chris@0
|
141 rescue ActiveRecord::RecordNotFound
|
Chris@0
|
142 render_404
|
Chris@0
|
143 end
|
Chris@0
|
144 end
|