2011-08-31 16:04:25 +04:00
|
|
|
# Redmine - project management software
|
2014-01-30 02:45:39 +04:00
|
|
|
# Copyright (C) 2006-2014 Jean-Philippe Lang
|
2007-05-13 21:09:56 +04:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-31 16:04:25 +04:00
|
|
|
#
|
2007-05-13 21:09:56 +04:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-08-31 16:04:25 +04:00
|
|
|
#
|
2007-05-13 21:09:56 +04:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
class MessagesController < ApplicationController
|
2008-01-19 14:53:43 +03:00
|
|
|
menu_item :boards
|
2009-10-21 21:07:18 +04:00
|
|
|
default_search_scope :messages
|
2008-03-07 20:49:44 +03:00
|
|
|
before_filter :find_board, :only => [:new, :preview]
|
2012-12-11 00:09:41 +04:00
|
|
|
before_filter :find_attachments, :only => [:preview]
|
2008-03-07 20:49:44 +03:00
|
|
|
before_filter :find_message, :except => [:new, :preview]
|
2008-11-11 18:07:55 +03:00
|
|
|
before_filter :authorize, :except => [:preview, :edit, :destroy]
|
2007-05-13 21:09:56 +04:00
|
|
|
|
2012-07-31 21:17:52 +04:00
|
|
|
helper :boards
|
2008-09-19 19:32:52 +04:00
|
|
|
helper :watchers
|
2007-05-26 19:42:37 +04:00
|
|
|
helper :attachments
|
2011-08-31 16:04:25 +04:00
|
|
|
include AttachmentsHelper
|
2007-05-26 19:42:37 +04:00
|
|
|
|
2010-02-06 15:54:13 +03:00
|
|
|
REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
|
2011-08-31 16:04:25 +04:00
|
|
|
|
2007-11-24 15:25:07 +03:00
|
|
|
# Show a topic and its replies
|
2007-05-13 21:09:56 +04:00
|
|
|
def show
|
2010-02-06 15:54:13 +03:00
|
|
|
page = params[:page]
|
|
|
|
# Find the page of the requested reply
|
|
|
|
if params[:r] && page.nil?
|
2013-06-12 23:13:25 +04:00
|
|
|
offset = @topic.children.where("#{Message.table_name}.id < ?", params[:r].to_i).count
|
2010-02-06 15:54:13 +03:00
|
|
|
page = 1 + offset / REPLIES_PER_PAGE
|
|
|
|
end
|
2011-08-31 16:04:25 +04:00
|
|
|
|
2010-02-06 15:54:13 +03:00
|
|
|
@reply_count = @topic.children.count
|
2013-01-09 00:18:12 +04:00
|
|
|
@reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page
|
2012-12-02 23:09:42 +04:00
|
|
|
@replies = @topic.children.
|
|
|
|
includes(:author, :attachments, {:board => :project}).
|
|
|
|
reorder("#{Message.table_name}.created_on ASC").
|
2013-02-17 15:35:42 +04:00
|
|
|
limit(@reply_pages.per_page).
|
2013-01-09 00:18:12 +04:00
|
|
|
offset(@reply_pages.offset).
|
2012-12-02 23:09:42 +04:00
|
|
|
all
|
2011-08-31 16:04:25 +04:00
|
|
|
|
2007-05-13 21:09:56 +04:00
|
|
|
@reply = Message.new(:subject => "RE: #{@message.subject}")
|
|
|
|
render :action => "show", :layout => false if request.xhr?
|
|
|
|
end
|
2011-08-31 16:04:25 +04:00
|
|
|
|
2007-11-24 15:25:07 +03:00
|
|
|
# Create a new topic
|
2007-05-13 21:09:56 +04:00
|
|
|
def new
|
2012-03-06 23:46:59 +04:00
|
|
|
@message = Message.new
|
2007-11-20 18:40:16 +03:00
|
|
|
@message.author = User.current
|
2007-11-24 15:25:07 +03:00
|
|
|
@message.board = @board
|
2012-03-06 23:46:59 +04:00
|
|
|
@message.safe_attributes = params[:message]
|
2012-02-23 17:02:35 +04:00
|
|
|
if request.post?
|
|
|
|
@message.save_attachments(params[:attachments])
|
|
|
|
if @message.save
|
|
|
|
call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
|
|
|
|
render_attachment_warning_if_needed(@message)
|
2012-07-15 23:14:23 +04:00
|
|
|
redirect_to board_message_path(@board, @message)
|
2012-02-23 17:02:35 +04:00
|
|
|
end
|
2007-05-13 21:09:56 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-24 15:25:07 +03:00
|
|
|
# Reply to a topic
|
2007-05-13 21:09:56 +04:00
|
|
|
def reply
|
2012-03-06 23:46:59 +04:00
|
|
|
@reply = Message.new
|
2007-11-20 18:40:16 +03:00
|
|
|
@reply.author = User.current
|
2007-05-13 21:09:56 +04:00
|
|
|
@reply.board = @board
|
2012-03-06 23:46:59 +04:00
|
|
|
@reply.safe_attributes = params[:reply]
|
2007-11-24 15:25:07 +03:00
|
|
|
@topic.children << @reply
|
2007-11-22 22:19:47 +03:00
|
|
|
if !@reply.new_record?
|
2009-05-31 02:43:01 +04:00
|
|
|
call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
|
2010-03-02 22:26:03 +03:00
|
|
|
attachments = Attachment.attach_files(@reply, params[:attachments])
|
2010-03-03 20:05:00 +03:00
|
|
|
render_attachment_warning_if_needed(@reply)
|
2007-11-22 22:19:47 +03:00
|
|
|
end
|
2012-07-15 23:14:23 +04:00
|
|
|
redirect_to board_message_path(@board, @topic, :r => @reply)
|
2007-11-24 15:25:07 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# Edit a message
|
|
|
|
def edit
|
2009-12-18 17:22:18 +03:00
|
|
|
(render_403; return false) unless @message.editable_by?(User.current)
|
2012-03-06 23:46:59 +04:00
|
|
|
@message.safe_attributes = params[:message]
|
|
|
|
if request.post? && @message.save
|
2010-03-02 22:26:03 +03:00
|
|
|
attachments = Attachment.attach_files(@message, params[:attachments])
|
2010-03-03 20:05:00 +03:00
|
|
|
render_attachment_warning_if_needed(@message)
|
2007-11-24 15:25:07 +03:00
|
|
|
flash[:notice] = l(:notice_successful_update)
|
2009-04-05 14:08:11 +04:00
|
|
|
@message.reload
|
2012-07-15 23:14:23 +04:00
|
|
|
redirect_to board_message_path(@message.board, @message.root, :r => (@message.parent_id && @message.id))
|
2007-11-24 15:25:07 +03:00
|
|
|
end
|
|
|
|
end
|
2011-08-31 16:04:25 +04:00
|
|
|
|
2007-11-24 15:25:07 +03:00
|
|
|
# Delete a messages
|
|
|
|
def destroy
|
2009-12-18 17:22:18 +03:00
|
|
|
(render_403; return false) unless @message.destroyable_by?(User.current)
|
2012-04-25 21:17:49 +04:00
|
|
|
r = @message.to_param
|
2007-11-24 15:25:07 +03:00
|
|
|
@message.destroy
|
2012-07-15 23:14:23 +04:00
|
|
|
if @message.parent
|
|
|
|
redirect_to board_message_path(@board, @message.parent, :r => r)
|
|
|
|
else
|
|
|
|
redirect_to project_board_path(@project, @board)
|
|
|
|
end
|
2007-05-13 21:09:56 +04:00
|
|
|
end
|
2011-08-31 16:04:25 +04:00
|
|
|
|
2008-08-28 22:56:47 +04:00
|
|
|
def quote
|
2012-07-19 22:23:45 +04:00
|
|
|
@subject = @message.subject
|
|
|
|
@subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')
|
|
|
|
|
|
|
|
@content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> "
|
2014-03-17 11:49:14 +04:00
|
|
|
@content << @message.content.to_s.strip.gsub(%r{<pre>(.*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
|
2008-08-28 22:56:47 +04:00
|
|
|
end
|
2011-08-31 16:04:25 +04:00
|
|
|
|
2008-03-07 20:49:44 +03:00
|
|
|
def preview
|
|
|
|
message = @board.messages.find_by_id(params[:id])
|
|
|
|
@text = (params[:message] || params[:reply])[:content]
|
2012-07-31 21:22:17 +04:00
|
|
|
@previewed = message
|
2008-03-07 20:49:44 +03:00
|
|
|
render :partial => 'common/preview'
|
|
|
|
end
|
2011-08-31 16:04:25 +04:00
|
|
|
|
2007-05-13 21:09:56 +04:00
|
|
|
private
|
2007-11-24 15:25:07 +03:00
|
|
|
def find_message
|
2013-02-21 00:59:52 +04:00
|
|
|
return unless find_board
|
2014-01-08 10:11:46 +04:00
|
|
|
@message = @board.messages.includes(:parent).find(params[:id])
|
2007-11-24 15:25:07 +03:00
|
|
|
@topic = @message.root
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
|
|
|
end
|
2011-08-31 16:04:25 +04:00
|
|
|
|
2007-11-24 15:25:07 +03:00
|
|
|
def find_board
|
2014-01-08 10:11:46 +04:00
|
|
|
@board = Board.includes(:project).find(params[:board_id])
|
2007-05-13 21:09:56 +04:00
|
|
|
@project = @board.project
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
2013-02-21 00:59:52 +04:00
|
|
|
nil
|
2007-05-13 21:09:56 +04:00
|
|
|
end
|
|
|
|
end
|