2014-09-15 10:58:14 +08:00
|
|
|
# Redmine - project management software
|
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
include ApplicationHelper
|
2015-02-06 18:15:08 +08:00
|
|
|
before_filter :authorize1, :only => [:show]
|
2014-09-15 10:58:14 +08:00
|
|
|
menu_item :boards
|
|
|
|
default_search_scope :messages
|
|
|
|
before_filter :find_board, :only => [:new, :preview,:edit]
|
|
|
|
before_filter :find_attachments, :only => [:preview]
|
|
|
|
before_filter :find_message, :except => [:new, :preview]
|
|
|
|
before_filter :authorize, :except => [:preview, :edit, :destroy, :new]
|
|
|
|
|
|
|
|
helper :boards
|
|
|
|
helper :watchers
|
|
|
|
helper :attachments
|
|
|
|
include AttachmentsHelper
|
|
|
|
helper :project_score
|
|
|
|
|
2015-06-04 15:55:41 +08:00
|
|
|
include CoursesHelper
|
|
|
|
|
2014-09-15 10:58:14 +08:00
|
|
|
REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
|
|
|
|
|
|
|
|
# Show a topic and its replies
|
|
|
|
def show
|
2015-06-23 16:05:08 +08:00
|
|
|
if @course
|
|
|
|
topic_id = params[:r]?params[:r]:params[:id]
|
|
|
|
parent_id = params[:id]
|
|
|
|
url = course_boards_path(@course,:topic_id => topic_id,:parent_id=>parent_id);
|
|
|
|
redirect_to url
|
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
2014-09-15 10:58:14 +08:00
|
|
|
@isReply = true
|
|
|
|
page = params[:page]
|
|
|
|
# Find the page of the requested reply
|
|
|
|
if params[:r] && page.nil?
|
|
|
|
offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i])
|
|
|
|
page = 1 + offset / REPLIES_PER_PAGE
|
|
|
|
end
|
|
|
|
|
|
|
|
@reply_count = @topic.children.count
|
2015-03-26 16:17:33 +08:00
|
|
|
# @reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page
|
|
|
|
# @replies = @topic.children.
|
|
|
|
# includes(:author, :attachments, {:board => :project}).
|
|
|
|
# reorder("#{Message.table_name}.created_on DESC").
|
|
|
|
# limit(@reply_pages.per_page).
|
|
|
|
# offset(@reply_pages.offset).
|
|
|
|
# all
|
2014-09-15 10:58:14 +08:00
|
|
|
|
|
|
|
@reply = Message.new(:subject => "RE: #{@message.subject}")
|
|
|
|
if @course
|
2015-03-26 16:17:33 +08:00
|
|
|
messages_replies = @topic.children.
|
|
|
|
includes(:author, :attachments, {:board => :project}).
|
|
|
|
reorder("#{Message.table_name}.created_on DESC").
|
|
|
|
#limit(@reply_pages.per_page).
|
|
|
|
#offset(@reply_pages.offset).
|
|
|
|
all
|
|
|
|
@replies = paginateHelper messages_replies,10
|
|
|
|
@reply = Message.new(:subject => "RE: #{@message.subject}")
|
2014-09-15 10:58:14 +08:00
|
|
|
render :action => "show", :layout => "base_courses"#by young
|
|
|
|
else
|
2015-03-26 16:17:33 +08:00
|
|
|
@reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page
|
|
|
|
@replies = @topic.children.
|
|
|
|
includes(:author, :attachments, {:board => :project}).
|
|
|
|
reorder("#{Message.table_name}.created_on DESC").
|
|
|
|
limit(@reply_pages.per_page).
|
|
|
|
offset(@reply_pages.offset).
|
|
|
|
all
|
|
|
|
|
|
|
|
@reply = Message.new(:subject => "RE: #{@message.subject}")
|
2014-09-15 10:58:14 +08:00
|
|
|
render :action => "show", :layout => "base_projects"#by young
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new topic
|
|
|
|
def new
|
2015-09-06 17:27:20 +08:00
|
|
|
if User.current.logged?
|
|
|
|
@message = Message.new
|
|
|
|
@message.author = User.current
|
|
|
|
@message.board = @board
|
|
|
|
@message.safe_attributes = params[:message]
|
|
|
|
if request.post?
|
|
|
|
@message.save_attachments(params[:attachments])
|
|
|
|
if @message.save
|
|
|
|
# 更新kindeditor上传的图片资源所有者
|
|
|
|
if params[:asset_id]
|
|
|
|
ids = params[:asset_id].split(',')
|
|
|
|
update_kindeditor_assets_owner ids,@message.id,OwnerTypeHelper::MESSAGE
|
|
|
|
end
|
2015-06-04 15:55:41 +08:00
|
|
|
|
2015-09-06 17:27:20 +08:00
|
|
|
call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
|
|
|
|
render_attachment_warning_if_needed(@message)
|
|
|
|
if params[:is_board]
|
|
|
|
if @project
|
|
|
|
redirect_to project_boards_path(@project)
|
|
|
|
elsif @course
|
|
|
|
redirect_to course_boards_path(@course)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
redirect_to board_message_url(@board, @message)
|
2015-05-11 17:23:53 +08:00
|
|
|
end
|
|
|
|
else
|
2015-09-06 17:27:20 +08:00
|
|
|
if params[:is_board]
|
|
|
|
if @project
|
|
|
|
redirect_to project_boards_path(@project, :flag => true)
|
|
|
|
elsif @course
|
|
|
|
redirect_to course_boards_path(@course, :flag => true)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
layout_file = @project ? 'base_projects' : 'base_courses'
|
|
|
|
render :action => 'new', :layout => layout_file
|
|
|
|
end
|
|
|
|
|
2015-05-11 17:23:53 +08:00
|
|
|
end
|
2014-09-15 10:58:14 +08:00
|
|
|
else
|
2015-09-06 17:27:20 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
layout_file = @project ? 'base_projects' : 'base_courses'
|
|
|
|
render :layout => layout_file
|
|
|
|
}
|
2015-05-11 17:35:36 +08:00
|
|
|
end
|
2014-09-15 10:58:14 +08:00
|
|
|
end
|
2015-03-24 17:12:51 +08:00
|
|
|
else
|
2015-09-06 17:27:20 +08:00
|
|
|
redirect_to signin_path
|
2014-09-15 10:58:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reply to a topic
|
|
|
|
def reply
|
|
|
|
if params[:reply][:content] == ""
|
2015-05-11 17:35:36 +08:00
|
|
|
if params[:is_board]
|
|
|
|
if @project
|
|
|
|
(redirect_to project_boards_path(@project), :notice => l(:label_reply_empty);return)
|
|
|
|
elsif @course
|
|
|
|
(redirect_to course_boards_path(@course), :notice => l(:label_reply_empty);return)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
(redirect_to board_message_url(@board, @topic, :r => @reply), :notice => l(:label_reply_empty);return)
|
|
|
|
end
|
2014-09-15 10:58:14 +08:00
|
|
|
end
|
|
|
|
@quote = params[:quote][:quote]
|
|
|
|
@reply = Message.new
|
|
|
|
@reply.author = User.current
|
|
|
|
@reply.board = @board
|
|
|
|
@reply.safe_attributes = params[:reply]
|
|
|
|
@reply.content = @quote + @reply.content
|
2015-09-01 14:18:44 +08:00
|
|
|
@reply.subject = "RE: #{@topic.subject}" unless params[:reply][:subject]
|
2014-09-15 10:58:14 +08:00
|
|
|
@topic.children << @reply
|
2015-09-17 15:50:33 +08:00
|
|
|
user_activity = UserActivity.where("act_type='Message' and act_id =#{@topic.id}").first
|
2015-09-19 13:45:32 +08:00
|
|
|
user_activity.updated_at = Time.now
|
2015-09-17 15:50:33 +08:00
|
|
|
user_activity.save
|
2014-09-15 10:58:14 +08:00
|
|
|
#@topic.update_attribute(:updated_on, Time.now)
|
|
|
|
if !@reply.new_record?
|
2015-04-03 16:35:46 +08:00
|
|
|
if params[:asset_id]
|
2015-05-08 15:02:04 +08:00
|
|
|
ids = params[:asset_id].split(',')
|
|
|
|
update_kindeditor_assets_owner ids,@reply.id,OwnerTypeHelper::MESSAGE
|
2015-04-03 16:35:46 +08:00
|
|
|
end
|
2015-06-04 15:55:41 +08:00
|
|
|
|
2014-09-15 10:58:14 +08:00
|
|
|
call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
|
|
|
|
attachments = Attachment.attach_files(@reply, params[:attachments])
|
|
|
|
render_attachment_warning_if_needed(@reply)
|
2015-05-05 14:32:40 +08:00
|
|
|
else
|
2015-05-08 15:02:04 +08:00
|
|
|
#render file: 'messages#show', layout: 'base_courses'
|
2014-09-15 10:58:14 +08:00
|
|
|
end
|
2015-09-01 14:18:44 +08:00
|
|
|
if params[:user_activity_id]
|
|
|
|
@user_activity_id = params[:user_activity_id]
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
end
|
|
|
|
elsif params[:is_board]
|
2015-05-11 17:23:53 +08:00
|
|
|
if @project
|
|
|
|
redirect_to project_boards_path(@project)
|
|
|
|
elsif @course
|
|
|
|
redirect_to course_boards_path(@course)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
redirect_to board_message_url(@board, @topic, :r => @reply)
|
|
|
|
end
|
2015-05-08 15:02:04 +08:00
|
|
|
|
2014-09-15 10:58:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Edit a message
|
|
|
|
def edit
|
|
|
|
@isReply = false
|
2015-05-12 17:14:07 +08:00
|
|
|
if @project
|
|
|
|
(render_403; return false) unless @message.editable_by?(User.current)
|
2014-09-15 10:58:14 +08:00
|
|
|
else
|
2015-05-12 17:14:07 +08:00
|
|
|
(render_403; return false) unless @message.course_editable_by?(User.current)
|
2014-09-15 10:58:14 +08:00
|
|
|
end
|
2015-05-11 17:35:36 +08:00
|
|
|
|
2014-09-15 10:58:14 +08:00
|
|
|
@message.safe_attributes = params[:message]
|
|
|
|
if request.post? && @message.save
|
|
|
|
attachments = Attachment.attach_files(@message, params[:attachments])
|
|
|
|
render_attachment_warning_if_needed(@message)
|
|
|
|
flash[:notice] = l(:notice_successful_update)
|
|
|
|
@message.reload
|
2015-05-11 17:23:53 +08:00
|
|
|
if params[:is_board]
|
|
|
|
if @project
|
|
|
|
redirect_to project_boards_path(@project)
|
|
|
|
elsif @course
|
|
|
|
redirect_to course_boards_path(@course)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
redirect_to board_message_url(@message.board, @message.root, :r => (@message.parent_id && @message.id))
|
|
|
|
end
|
2015-05-22 11:09:43 +08:00
|
|
|
elsif request.get? || request.post?
|
2015-05-13 14:31:54 +08:00
|
|
|
if params[:is_board]
|
|
|
|
if @project
|
|
|
|
redirect_to project_boards_path(@project)
|
|
|
|
elsif @course
|
|
|
|
redirect_to course_boards_path(@course)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
layout_file = @project ? 'base_projects' : 'base_courses'
|
|
|
|
render :layout => layout_file
|
|
|
|
}
|
|
|
|
end
|
2014-09-15 10:58:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Delete a messages
|
|
|
|
def destroy
|
|
|
|
if @project
|
|
|
|
(render_403; return false) unless @message.destroyable_by?(User.current)
|
|
|
|
else
|
|
|
|
(render_403; return false) unless @message.course_destroyable_by?(User.current)
|
|
|
|
end
|
|
|
|
r = @message.to_param
|
|
|
|
@message.destroy
|
|
|
|
# modify by nwb
|
|
|
|
if @project
|
2015-05-11 17:23:53 +08:00
|
|
|
if params[:is_board]
|
2015-05-08 15:02:04 +08:00
|
|
|
redirect_to project_boards_url(@project)
|
2015-05-11 17:23:53 +08:00
|
|
|
else
|
|
|
|
redirect_to board_message_url(@board, @topic, :r => @reply)
|
2014-09-15 10:58:14 +08:00
|
|
|
end
|
|
|
|
elsif @course
|
2015-05-11 17:23:53 +08:00
|
|
|
if params[:is_board]
|
|
|
|
redirect_to course_boards_url(@course)
|
2014-09-15 10:58:14 +08:00
|
|
|
else
|
2015-05-11 17:23:53 +08:00
|
|
|
if @message.parent
|
|
|
|
redirect_to board_message_url(@board, @message.parent, :r => r)
|
|
|
|
else
|
|
|
|
redirect_to course_board_url(@course, @board)
|
|
|
|
end
|
2014-09-15 10:58:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def quote
|
|
|
|
@subject = @message.subject
|
|
|
|
@subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')
|
|
|
|
|
|
|
|
@content = "> #{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> "
|
|
|
|
@temp = Message.new
|
2015-04-08 17:25:52 +08:00
|
|
|
@temp.content = "<blockquote>#{ll(Setting.default_language, :text_user_wrote, @message.author)} <br/>#{@message.content.html_safe}</blockquote>".html_safe
|
2014-09-15 10:58:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def preview
|
|
|
|
message = @board.messages.find_by_id(params[:id])
|
|
|
|
@text = (params[:message] || params[:reply])[:content]
|
|
|
|
@previewed = message
|
|
|
|
render :partial => 'common/preview'
|
|
|
|
end
|
|
|
|
|
2015-05-08 15:02:04 +08:00
|
|
|
private
|
2014-09-15 10:58:14 +08:00
|
|
|
def find_message
|
|
|
|
return unless find_board
|
|
|
|
@message = @board.messages.find(params[:id], :include => :parent)
|
|
|
|
@topic = @message.root
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_board
|
|
|
|
#modify by nwb
|
|
|
|
@board = Board.find(params[:board_id])
|
|
|
|
if @board.project_id != -1 && @board.project_id != nil
|
|
|
|
@project = @board.project
|
|
|
|
elsif @board.course_id
|
|
|
|
@course = @board.course
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|