To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / db / dbe1bda8432254ab1c9b544c6336106917863bf8.svn-base @ 1297:0a574315af3e
History | View | Annotate | Download (4.86 KB)
| 1 | 1296:038ba2d95de8 | Chris | # Redmine - project management software |
|---|---|---|---|
| 2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
||
| 3 | # |
||
| 4 | # This program is free software; you can redistribute it and/or |
||
| 5 | # modify it under the terms of the GNU General Public License |
||
| 6 | # as published by the Free Software Foundation; either version 2 |
||
| 7 | # of the License, or (at your option) any later version. |
||
| 8 | # |
||
| 9 | # This program is distributed in the hope that it will be useful, |
||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 12 | # GNU General Public License for more details. |
||
| 13 | # |
||
| 14 | # You should have received a copy of the GNU General Public License |
||
| 15 | # along with this program; if not, write to the Free Software |
||
| 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||
| 17 | |||
| 18 | class MessagesController < ApplicationController |
||
| 19 | menu_item :boards |
||
| 20 | default_search_scope :messages |
||
| 21 | before_filter :find_board, :only => [:new, :preview] |
||
| 22 | before_filter :find_message, :except => [:new, :preview] |
||
| 23 | before_filter :authorize, :except => [:preview, :edit, :destroy] |
||
| 24 | |||
| 25 | helper :boards |
||
| 26 | helper :watchers |
||
| 27 | helper :attachments |
||
| 28 | include AttachmentsHelper |
||
| 29 | |||
| 30 | REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE) |
||
| 31 | |||
| 32 | # Show a topic and its replies |
||
| 33 | def show |
||
| 34 | page = params[:page] |
||
| 35 | # Find the page of the requested reply |
||
| 36 | if params[:r] && page.nil? |
||
| 37 | offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i])
|
||
| 38 | page = 1 + offset / REPLIES_PER_PAGE |
||
| 39 | end |
||
| 40 | |||
| 41 | @reply_count = @topic.children.count |
||
| 42 | @reply_pages = Paginator.new self, @reply_count, REPLIES_PER_PAGE, page |
||
| 43 | @replies = @topic.children.find(:all, :include => [:author, :attachments, {:board => :project}],
|
||
| 44 | :order => "#{Message.table_name}.created_on ASC",
|
||
| 45 | :limit => @reply_pages.items_per_page, |
||
| 46 | :offset => @reply_pages.current.offset) |
||
| 47 | |||
| 48 | @reply = Message.new(:subject => "RE: #{@message.subject}")
|
||
| 49 | render :action => "show", :layout => false if request.xhr? |
||
| 50 | end |
||
| 51 | |||
| 52 | # Create a new topic |
||
| 53 | def new |
||
| 54 | @message = Message.new |
||
| 55 | @message.author = User.current |
||
| 56 | @message.board = @board |
||
| 57 | @message.safe_attributes = params[:message] |
||
| 58 | if request.post? |
||
| 59 | @message.save_attachments(params[:attachments]) |
||
| 60 | if @message.save |
||
| 61 | call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
|
||
| 62 | render_attachment_warning_if_needed(@message) |
||
| 63 | redirect_to board_message_path(@board, @message) |
||
| 64 | end |
||
| 65 | end |
||
| 66 | end |
||
| 67 | |||
| 68 | # Reply to a topic |
||
| 69 | def reply |
||
| 70 | @reply = Message.new |
||
| 71 | @reply.author = User.current |
||
| 72 | @reply.board = @board |
||
| 73 | @reply.safe_attributes = params[:reply] |
||
| 74 | @topic.children << @reply |
||
| 75 | if !@reply.new_record? |
||
| 76 | call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
|
||
| 77 | attachments = Attachment.attach_files(@reply, params[:attachments]) |
||
| 78 | render_attachment_warning_if_needed(@reply) |
||
| 79 | end |
||
| 80 | redirect_to board_message_path(@board, @topic, :r => @reply) |
||
| 81 | end |
||
| 82 | |||
| 83 | # Edit a message |
||
| 84 | def edit |
||
| 85 | (render_403; return false) unless @message.editable_by?(User.current) |
||
| 86 | @message.safe_attributes = params[:message] |
||
| 87 | if request.post? && @message.save |
||
| 88 | attachments = Attachment.attach_files(@message, params[:attachments]) |
||
| 89 | render_attachment_warning_if_needed(@message) |
||
| 90 | flash[:notice] = l(:notice_successful_update) |
||
| 91 | @message.reload |
||
| 92 | redirect_to board_message_path(@message.board, @message.root, :r => (@message.parent_id && @message.id)) |
||
| 93 | end |
||
| 94 | end |
||
| 95 | |||
| 96 | # Delete a messages |
||
| 97 | def destroy |
||
| 98 | (render_403; return false) unless @message.destroyable_by?(User.current) |
||
| 99 | r = @message.to_param |
||
| 100 | @message.destroy |
||
| 101 | if @message.parent |
||
| 102 | redirect_to board_message_path(@board, @message.parent, :r => r) |
||
| 103 | else |
||
| 104 | redirect_to project_board_path(@project, @board) |
||
| 105 | end |
||
| 106 | end |
||
| 107 | |||
| 108 | def quote |
||
| 109 | @subject = @message.subject |
||
| 110 | @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')
|
||
| 111 | |||
| 112 | @content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> "
|
||
| 113 | @content << @message.content.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
|
||
| 114 | end |
||
| 115 | |||
| 116 | def preview |
||
| 117 | message = @board.messages.find_by_id(params[:id]) |
||
| 118 | @attachements = message.attachments if message |
||
| 119 | @text = (params[:message] || params[:reply])[:content] |
||
| 120 | @previewed = message |
||
| 121 | render :partial => 'common/preview' |
||
| 122 | end |
||
| 123 | |||
| 124 | private |
||
| 125 | def find_message |
||
| 126 | find_board |
||
| 127 | @message = @board.messages.find(params[:id], :include => :parent) |
||
| 128 | @topic = @message.root |
||
| 129 | rescue ActiveRecord::RecordNotFound |
||
| 130 | render_404 |
||
| 131 | end |
||
| 132 | |||
| 133 | def find_board |
||
| 134 | @board = Board.find(params[:board_id], :include => :project) |
||
| 135 | @project = @board.project |
||
| 136 | rescue ActiveRecord::RecordNotFound |
||
| 137 | render_404 |
||
| 138 | end |
||
| 139 | end |