messages_controller.rb
141 lines
| 4.7 KiB
| text/x-ruby
|
RubyLexer
|
r6775 | # Redmine - project management software | ||
|
r9453 | # Copyright (C) 2006-2012 Jean-Philippe Lang | ||
|
r526 | # | ||
# 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. | ||||
|
r6775 | # | ||
|
r526 | # 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. | ||||
|
r6775 | # | ||
|
r526 | # 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 | ||||
|
r1062 | menu_item :boards | ||
|
r2829 | default_search_scope :messages | ||
|
r1191 | before_filter :find_board, :only => [:new, :preview] | ||
before_filter :find_message, :except => [:new, :preview] | ||||
|
r2017 | before_filter :authorize, :except => [:preview, :edit, :destroy] | ||
|
r526 | |||
|
r9959 | helper :boards | ||
|
r1876 | helper :watchers | ||
|
r538 | helper :attachments | ||
|
r6775 | include AttachmentsHelper | ||
|
r538 | |||
|
r3259 | REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE) | ||
|
r6775 | |||
|
r913 | # Show a topic and its replies | ||
|
r526 | def show | ||
|
r3259 | 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 | ||||
|
r6775 | |||
|
r3259 | @reply_count = @topic.children.count | ||
@reply_pages = Paginator.new self, @reply_count, REPLIES_PER_PAGE, page | ||||
|
r10687 | @replies = @topic.children. | ||
includes(:author, :attachments, {:board => :project}). | ||||
reorder("#{Message.table_name}.created_on ASC"). | ||||
limit(@reply_pages.items_per_page). | ||||
offset(@reply_pages.current.offset). | ||||
all | ||||
|
r6775 | |||
|
r526 | @reply = Message.new(:subject => "RE: #{@message.subject}") | ||
render :action => "show", :layout => false if request.xhr? | ||||
end | ||||
|
r6775 | |||
|
r913 | # Create a new topic | ||
|
r526 | def new | ||
|
r9013 | @message = Message.new | ||
|
r906 | @message.author = User.current | ||
|
r913 | @message.board = @board | ||
|
r9013 | @message.safe_attributes = params[:message] | ||
|
r8820 | 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) | ||||
|
r9820 | redirect_to board_message_path(@board, @message) | ||
|
r8820 | end | ||
|
r526 | end | ||
end | ||||
|
r913 | # Reply to a topic | ||
|
r526 | def reply | ||
|
r9013 | @reply = Message.new | ||
|
r906 | @reply.author = User.current | ||
|
r526 | @reply.board = @board | ||
|
r9013 | @reply.safe_attributes = params[:reply] | ||
|
r913 | @topic.children << @reply | ||
|
r910 | if !@reply.new_record? | ||
|
r2674 | call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply}) | ||
|
r3409 | attachments = Attachment.attach_files(@reply, params[:attachments]) | ||
|
r3414 | render_attachment_warning_if_needed(@reply) | ||
|
r910 | end | ||
|
r9820 | redirect_to board_message_path(@board, @topic, :r => @reply) | ||
|
r913 | end | ||
# Edit a message | ||||
def edit | ||||
|
r3071 | (render_403; return false) unless @message.editable_by?(User.current) | ||
|
r9013 | @message.safe_attributes = params[:message] | ||
if request.post? && @message.save | ||||
|
r3409 | attachments = Attachment.attach_files(@message, params[:attachments]) | ||
|
r3414 | render_attachment_warning_if_needed(@message) | ||
|
r913 | flash[:notice] = l(:notice_successful_update) | ||
|
r2560 | @message.reload | ||
|
r9820 | redirect_to board_message_path(@message.board, @message.root, :r => (@message.parent_id && @message.id)) | ||
|
r913 | end | ||
end | ||||
|
r6775 | |||
|
r913 | # Delete a messages | ||
def destroy | ||||
|
r3071 | (render_403; return false) unless @message.destroyable_by?(User.current) | ||
|
r9346 | r = @message.to_param | ||
|
r913 | @message.destroy | ||
|
r9820 | if @message.parent | ||
redirect_to board_message_path(@board, @message.parent, :r => r) | ||||
else | ||||
redirect_to project_board_path(@project, @board) | ||||
end | ||||
|
r526 | end | ||
|
r6775 | |||
|
r1771 | def quote | ||
|
r9870 | @subject = @message.subject | ||
@subject = "RE: #{@subject}" unless @subject.starts_with?('RE:') | ||||
@content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> " | ||||
@content << @message.content.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" | ||||
|
r1771 | end | ||
|
r6775 | |||
|
r1191 | def preview | ||
message = @board.messages.find_by_id(params[:id]) | ||||
@attachements = message.attachments if message | ||||
@text = (params[:message] || params[:reply])[:content] | ||||
|
r9962 | @previewed = message | ||
|
r1191 | render :partial => 'common/preview' | ||
end | ||||
|
r6775 | |||
|
r526 | private | ||
|
r913 | def find_message | ||
find_board | ||||
@message = @board.messages.find(params[:id], :include => :parent) | ||||
@topic = @message.root | ||||
rescue ActiveRecord::RecordNotFound | ||||
render_404 | ||||
end | ||||
|
r6775 | |||
|
r913 | def find_board | ||
|
r526 | @board = Board.find(params[:board_id], :include => :project) | ||
@project = @board.project | ||||
rescue ActiveRecord::RecordNotFound | ||||
render_404 | ||||
end | ||||
end | ||||