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