2011-10-29 16:19:11 +04:00
|
|
|
#-- encoding: UTF-8
|
2011-05-30 00:11:52 +04:00
|
|
|
#-- copyright
|
|
|
|
# ChiliProject is a project management system.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2013-01-07 02:20:49 +04:00
|
|
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +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-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# See doc/COPYRIGHT.rdoc for more details.
|
|
|
|
#++
|
|
|
|
|
2007-05-13 21:09:56 +04:00
|
|
|
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]
|
|
|
|
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
|
|
|
|
|
|
|
verify :method => :post, :only => [ :reply, :destroy ], :redirect_to => { :action => :show }
|
2008-08-28 22:56:47 +04:00
|
|
|
verify :xhr => true, :only => :quote
|
2007-05-13 21:09:56 +04:00
|
|
|
|
2011-05-30 22:52: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-05-30 22:52: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?
|
|
|
|
offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i])
|
|
|
|
page = 1 + offset / REPLIES_PER_PAGE
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-02-06 15:54:13 +03:00
|
|
|
@reply_count = @topic.children.count
|
|
|
|
@reply_pages = Paginator.new self, @reply_count, REPLIES_PER_PAGE, page
|
|
|
|
@replies = @topic.children.find(:all, :include => [:author, :attachments, {:board => :project}],
|
|
|
|
:order => "#{Message.table_name}.created_on ASC",
|
|
|
|
:limit => @reply_pages.items_per_page,
|
|
|
|
:offset => @reply_pages.current.offset)
|
2011-05-30 22:52: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-05-30 22:52: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]
|
|
|
|
if request.post?
|
|
|
|
if @message.save
|
|
|
|
call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
|
|
|
|
attachments = Attachment.attach_files(@message, params[:attachments])
|
|
|
|
render_attachment_warning_if_needed(@message)
|
|
|
|
redirect_to :action => 'show', :id => @message
|
|
|
|
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
|
2010-02-06 15:54:13 +03:00
|
|
|
redirect_to :action => 'show', :id => @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
|
2010-02-06 15:54:13 +03:00
|
|
|
redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id)
|
2007-11-24 15:25:07 +03:00
|
|
|
end
|
|
|
|
end
|
2011-05-30 22:52: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)
|
2007-11-24 15:25:07 +03:00
|
|
|
@message.destroy
|
|
|
|
redirect_to @message.parent.nil? ?
|
|
|
|
{ :controller => 'boards', :action => 'show', :project_id => @project, :id => @board } :
|
2010-02-06 15:54:13 +03:00
|
|
|
{ :action => 'show', :id => @message.parent, :r => @message }
|
2007-05-13 21:09:56 +04:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-08-28 22:56:47 +04:00
|
|
|
def quote
|
|
|
|
user = @message.author
|
|
|
|
text = @message.content
|
2009-11-13 20:46:38 +03:00
|
|
|
subject = @message.subject.gsub('"', '\"')
|
2009-11-13 22:14:18 +03:00
|
|
|
subject = "RE: #{subject}" unless subject.starts_with?('RE:')
|
2008-08-28 22:56:47 +04:00
|
|
|
content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> "
|
|
|
|
content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n"
|
|
|
|
render(:update) { |page|
|
2011-12-18 15:03:38 +04:00
|
|
|
page << "$('message_subject').value = \"#{subject}\";"
|
2008-08-28 22:56:47 +04:00
|
|
|
page.<< "$('message_content').value = \"#{content}\";"
|
|
|
|
page.show 'reply'
|
|
|
|
page << "Form.Element.focus('message_content');"
|
|
|
|
page << "Element.scrollTo('reply');"
|
|
|
|
page << "$('message_content').scrollTop = $('message_content').scrollHeight - $('message_content').clientHeight;"
|
|
|
|
}
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-03-07 20:49:44 +03:00
|
|
|
def preview
|
|
|
|
message = @board.messages.find_by_id(params[:id])
|
|
|
|
@attachements = message.attachments if message
|
|
|
|
@text = (params[:message] || params[:reply])[:content]
|
|
|
|
render :partial => 'common/preview'
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2007-05-13 21:09:56 +04:00
|
|
|
private
|
2007-11-24 15:25:07 +03:00
|
|
|
def find_message
|
|
|
|
find_board
|
|
|
|
@message = @board.messages.find(params[:id], :include => :parent)
|
|
|
|
@topic = @message.root
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2007-11-24 15:25:07 +03:00
|
|
|
def find_board
|
2007-05-13 21:09:56 +04:00
|
|
|
@board = Board.find(params[:board_id], :include => :project)
|
|
|
|
@project = @board.project
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|