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