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