@@ -1,142 +1,142 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 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_attachments, :only => [:preview] |
|
23 | 23 | before_filter :find_message, :except => [:new, :preview] |
|
24 | 24 | before_filter :authorize, :except => [:preview, :edit, :destroy] |
|
25 | 25 | |
|
26 | 26 | helper :boards |
|
27 | 27 | helper :watchers |
|
28 | 28 | helper :attachments |
|
29 | 29 | include AttachmentsHelper |
|
30 | 30 | |
|
31 | 31 | REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE) |
|
32 | 32 | |
|
33 | 33 | # Show a topic and its replies |
|
34 | 34 | def show |
|
35 | 35 | page = params[:page] |
|
36 | 36 | # Find the page of the requested reply |
|
37 | 37 | if params[:r] && page.nil? |
|
38 | 38 | offset = @topic.children.where("#{Message.table_name}.id < ?", params[:r].to_i).count |
|
39 | 39 | page = 1 + offset / REPLIES_PER_PAGE |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | @reply_count = @topic.children.count |
|
43 | 43 | @reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page |
|
44 | 44 | @replies = @topic.children. |
|
45 | 45 | includes(:author, :attachments, {:board => :project}). |
|
46 | 46 | reorder("#{Message.table_name}.created_on ASC"). |
|
47 | 47 | limit(@reply_pages.per_page). |
|
48 | 48 | offset(@reply_pages.offset). |
|
49 | 49 | all |
|
50 | 50 | |
|
51 | 51 | @reply = Message.new(:subject => "RE: #{@message.subject}") |
|
52 | 52 | render :action => "show", :layout => false if request.xhr? |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | # Create a new topic |
|
56 | 56 | def new |
|
57 | 57 | @message = Message.new |
|
58 | 58 | @message.author = User.current |
|
59 | 59 | @message.board = @board |
|
60 | 60 | @message.safe_attributes = params[:message] |
|
61 | 61 | if request.post? |
|
62 | 62 | @message.save_attachments(params[:attachments]) |
|
63 | 63 | if @message.save |
|
64 | 64 | call_hook(:controller_messages_new_after_save, { :params => params, :message => @message}) |
|
65 | 65 | render_attachment_warning_if_needed(@message) |
|
66 | 66 | redirect_to board_message_path(@board, @message) |
|
67 | 67 | end |
|
68 | 68 | end |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | # Reply to a topic |
|
72 | 72 | def reply |
|
73 | 73 | @reply = Message.new |
|
74 | 74 | @reply.author = User.current |
|
75 | 75 | @reply.board = @board |
|
76 | 76 | @reply.safe_attributes = params[:reply] |
|
77 | 77 | @topic.children << @reply |
|
78 | 78 | if !@reply.new_record? |
|
79 | 79 | call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply}) |
|
80 | 80 | attachments = Attachment.attach_files(@reply, params[:attachments]) |
|
81 | 81 | render_attachment_warning_if_needed(@reply) |
|
82 | 82 | end |
|
83 | 83 | redirect_to board_message_path(@board, @topic, :r => @reply) |
|
84 | 84 | end |
|
85 | 85 | |
|
86 | 86 | # Edit a message |
|
87 | 87 | def edit |
|
88 | 88 | (render_403; return false) unless @message.editable_by?(User.current) |
|
89 | 89 | @message.safe_attributes = params[:message] |
|
90 | 90 | if request.post? && @message.save |
|
91 | 91 | attachments = Attachment.attach_files(@message, params[:attachments]) |
|
92 | 92 | render_attachment_warning_if_needed(@message) |
|
93 | 93 | flash[:notice] = l(:notice_successful_update) |
|
94 | 94 | @message.reload |
|
95 | 95 | redirect_to board_message_path(@message.board, @message.root, :r => (@message.parent_id && @message.id)) |
|
96 | 96 | end |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | # Delete a messages |
|
100 | 100 | def destroy |
|
101 | 101 | (render_403; return false) unless @message.destroyable_by?(User.current) |
|
102 | 102 | r = @message.to_param |
|
103 | 103 | @message.destroy |
|
104 | 104 | if @message.parent |
|
105 | 105 | redirect_to board_message_path(@board, @message.parent, :r => r) |
|
106 | 106 | else |
|
107 | 107 | redirect_to project_board_path(@project, @board) |
|
108 | 108 | end |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | def quote |
|
112 | 112 | @subject = @message.subject |
|
113 | 113 | @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:') |
|
114 | 114 | |
|
115 | 115 | @content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> " |
|
116 |
@content << @message.content.to_s.strip.gsub(%r{<pre>( |
|
|
116 | @content << @message.content.to_s.strip.gsub(%r{<pre>(.*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" | |
|
117 | 117 | end |
|
118 | 118 | |
|
119 | 119 | def preview |
|
120 | 120 | message = @board.messages.find_by_id(params[:id]) |
|
121 | 121 | @text = (params[:message] || params[:reply])[:content] |
|
122 | 122 | @previewed = message |
|
123 | 123 | render :partial => 'common/preview' |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | private |
|
127 | 127 | def find_message |
|
128 | 128 | return unless find_board |
|
129 | 129 | @message = @board.messages.includes(:parent).find(params[:id]) |
|
130 | 130 | @topic = @message.root |
|
131 | 131 | rescue ActiveRecord::RecordNotFound |
|
132 | 132 | render_404 |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | def find_board |
|
136 | 136 | @board = Board.includes(:project).find(params[:board_id]) |
|
137 | 137 | @project = @board.project |
|
138 | 138 | rescue ActiveRecord::RecordNotFound |
|
139 | 139 | render_404 |
|
140 | 140 | nil |
|
141 | 141 | end |
|
142 | 142 | end |
General Comments 0
You need to be logged in to leave comments.
Login now