2014-10-23 11:30:34 +08:00
|
|
|
|
class MemosController < ApplicationController
|
|
|
|
|
default_search_scope :memos
|
|
|
|
|
before_filter :find_forum, :only => [:new, :create, :preview]
|
|
|
|
|
before_filter :find_attachments, :only => [:preview]
|
|
|
|
|
before_filter :find_memo, :except => [:new, :create, :preview]
|
|
|
|
|
before_filter :authenticate_user_edit, :only => [:edit, :update]
|
|
|
|
|
before_filter :authenticate_user_destroy, :only => [:destroy]
|
|
|
|
|
before_filter :require_login, :only => [:new, :create]
|
|
|
|
|
|
|
|
|
|
helper :attachments
|
|
|
|
|
include AttachmentsHelper
|
|
|
|
|
include ApplicationHelper
|
|
|
|
|
|
2015-09-18 17:44:16 +08:00
|
|
|
|
# layout 'base_memos'
|
2014-10-23 11:30:34 +08:00
|
|
|
|
|
|
|
|
|
def quote
|
|
|
|
|
@subject = @memo.subject
|
|
|
|
|
@subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')
|
|
|
|
|
|
|
|
|
|
@content = "#{ll(Setting.default_language, :text_user_wrote, @memo.author)} <br/> "
|
|
|
|
|
@content << @memo.content.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n") + "</blockquote>\n\n<br/>"
|
2014-11-20 17:10:05 +08:00
|
|
|
|
@content = "<blockquote style='word-break: break-all;word-wrap: break-word;'>" << @content
|
2014-10-23 11:30:34 +08:00
|
|
|
|
#@content = "> #{ll(Setting.default_language, :text_user_wrote, @memo.author)}\n> "
|
|
|
|
|
#@content << @memo.content.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
|
|
|
|
|
#@content_html = textilizable(@content)
|
|
|
|
|
@temp = Memo.new
|
|
|
|
|
@temp.content = @content
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def new
|
|
|
|
|
@memo = Memo.new
|
|
|
|
|
@memo.forum_id = @forum.id
|
|
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html {
|
|
|
|
|
render action: :new ,layout: 'base'
|
|
|
|
|
}
|
|
|
|
|
format.json { render json: @memo }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
|
|
|
|
|
if params[:quote].nil?
|
|
|
|
|
@quote = ""
|
|
|
|
|
else
|
|
|
|
|
@quote = params[:quote]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#unless params[:quote].nil?
|
|
|
|
|
# @quote = params[:quote][:quote]
|
|
|
|
|
#end
|
|
|
|
|
|
|
|
|
|
@memo = Memo.new(params[:memo])
|
|
|
|
|
@memo.forum_id = params[:forum_id]
|
|
|
|
|
@memo.author_id = User.current.id
|
|
|
|
|
|
|
|
|
|
@memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads]))
|
|
|
|
|
@memo.content = @quote + @memo.content
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
if @memo.save
|
2015-03-26 16:08:31 +08:00
|
|
|
|
# Time 2015-03-24 14:47:05
|
|
|
|
|
# Author lizanle
|
|
|
|
|
# Description after save后需要进行资源记录的更新
|
|
|
|
|
# owner_type = 1 对应的是 memo
|
|
|
|
|
if !params[:asset_id].nil?
|
|
|
|
|
ids = params[:asset_id].split(',')
|
|
|
|
|
ids.each do |id|
|
|
|
|
|
asset = Kindeditor::Asset.find(id.to_i)
|
|
|
|
|
asset.owner_id = @memo.id
|
|
|
|
|
asset.owner_type = 1
|
|
|
|
|
asset.save
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
#end
|
2015-09-21 15:31:00 +08:00
|
|
|
|
format.html { redirect_to back_memo_url, notice: "#{l :label_memo_create_succ}" }
|
|
|
|
|
format.json { render json: @memo, status: :created, location: @memo }
|
2014-10-23 11:30:34 +08:00
|
|
|
|
else
|
|
|
|
|
flash.now[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}"
|
2015-09-21 15:31:00 +08:00
|
|
|
|
format.js
|
|
|
|
|
format.html { redirect_to( forum_path(Forum.find(params[:forum_id]),:errors=>@memo.errors.full_messages[0])) }
|
2014-10-23 11:30:34 +08:00
|
|
|
|
format.json { render json: @memo.errors, status: :unprocessable_entity }
|
2015-09-21 15:31:00 +08:00
|
|
|
|
#end
|
2014-10-23 11:30:34 +08:00
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
REPLIES_PER_PAGE = 20 unless const_defined?(:REPLIES_PER_PAGE)
|
|
|
|
|
def show
|
2015-09-18 17:44:16 +08:00
|
|
|
|
|
2015-09-02 10:23:22 +08:00
|
|
|
|
# 更新贴吧帖子留言对应的memo_messages的viewed字段
|
2015-09-02 11:00:32 +08:00
|
|
|
|
unless @memo.children.blank?
|
|
|
|
|
@memo.children.each do |child|
|
|
|
|
|
child.memo_messages.each do |memo_message|
|
|
|
|
|
if User.current.id == memo_message.user_id
|
|
|
|
|
memo_message.update_attributes(:viewed => true)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-09-02 11:46:49 +08:00
|
|
|
|
end
|
|
|
|
|
query_memo_messages = @memo.memo_messages
|
|
|
|
|
query_memo_messages.each do |query_memo_message|
|
|
|
|
|
if User.current.id == query_memo_message.user_id
|
|
|
|
|
query_memo_message.update_attributes(:viewed => true)
|
2015-09-02 11:00:32 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2014-10-23 11:30:34 +08:00
|
|
|
|
|
2015-09-02 11:00:32 +08:00
|
|
|
|
pre_count = REPLIES_PER_PAGE
|
2014-10-23 11:30:34 +08:00
|
|
|
|
@memo = @memo.root # 取出楼主,防止输入帖子id让回复作为主贴显示
|
|
|
|
|
@memo.update_column(:viewed_count, (@memo.viewed_count.to_i + 1))
|
|
|
|
|
|
|
|
|
|
page = params[:page]
|
|
|
|
|
if params[:r] && page.nil?
|
|
|
|
|
offset = @memo.children.where("#{Memo.table_name}.id < ?", params[:r].to_i).count
|
|
|
|
|
page = 1 + offset / pre_count
|
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
@reply_count = @memo.children.count
|
|
|
|
|
@reply_pages = Paginator.new @reply_count, pre_count, page
|
|
|
|
|
@replies = @memo.children.
|
|
|
|
|
includes(:author, :attachments).
|
|
|
|
|
reorder("#{Memo.table_name}.created_at DESC").
|
|
|
|
|
limit(@reply_pages.per_page).
|
|
|
|
|
offset(@reply_pages.offset).
|
|
|
|
|
all
|
|
|
|
|
|
|
|
|
|
@memo_new = Memo.new
|
|
|
|
|
|
2015-09-18 17:44:16 +08:00
|
|
|
|
@my_topic_count = Memo.where("forum_id = #{@memo.forum_id} and author_id = #{User.current.id} and parent_id is null").count
|
|
|
|
|
@my_replies_count = Memo.where("forum_id = #{@memo.forum_id} and author_id = #{User.current.id} and parent_id is not null").count
|
2014-10-23 11:30:34 +08:00
|
|
|
|
# @memo = Memo.find_by_id(params[:id])
|
|
|
|
|
# @forum = Forum.find(params[:forum_id])
|
|
|
|
|
# @replies = @memo.replies
|
|
|
|
|
# @mome_new = Memo.new
|
|
|
|
|
|
|
|
|
|
respond_to do |format|
|
2015-09-18 17:44:16 +08:00
|
|
|
|
format.html {render :layout=> 'base_forums'}#:layout=> 'base_forums',
|
2014-10-23 11:30:34 +08:00
|
|
|
|
format.json { render json: @memo }
|
|
|
|
|
format.xml { render xml: @memo }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def edit
|
2015-09-24 09:40:53 +08:00
|
|
|
|
@my_topic_count = Memo.where("forum_id = #{@memo.forum_id} and author_id = #{User.current.id} and parent_id is null").count
|
|
|
|
|
@my_replies_count = Memo.where("forum_id = #{@memo.forum_id} and author_id = #{User.current.id} and parent_id is not null").count
|
2014-10-23 11:30:34 +08:00
|
|
|
|
@replying = false
|
2015-09-24 09:40:53 +08:00
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html {render :layout=>'base_forums'}
|
|
|
|
|
end
|
2014-10-23 11:30:34 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
|
|
|
|
respond_to do |format|
|
2015-06-19 11:42:07 +08:00
|
|
|
|
if( #@memo.update_column(:subject, params[:memo][:subject]) &&
|
2014-10-23 11:30:34 +08:00
|
|
|
|
@memo.update_column(:content, params[:memo][:content]) &&
|
|
|
|
|
@memo.update_column(:sticky, params[:memo][:sticky]) &&
|
2015-09-24 13:38:40 +08:00
|
|
|
|
@memo.update_column(:lock, params[:memo][:lock]) &&
|
|
|
|
|
@memo.update_column(:subject,params[:memo][:subject]))
|
2014-10-23 11:30:34 +08:00
|
|
|
|
@memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads]))
|
|
|
|
|
@memo.save
|
|
|
|
|
# @memo.root.update_attribute(:updated_at, @memo.updated_at)
|
|
|
|
|
format.html {redirect_to back_memo_url, notice: "#{l :label_memo_create_succ}"}
|
|
|
|
|
else
|
|
|
|
|
format.html { render action: "edit" }
|
|
|
|
|
format.json { render json: @person.errors, status: :unprocessable_entity }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
|
@memo.destroy
|
|
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
# format.html { redirect_to @back_url }
|
|
|
|
|
format.html { redirect_to back_memo_or_forum_url }
|
|
|
|
|
format.json { head :no_content }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def find_memo
|
|
|
|
|
return unless find_forum
|
|
|
|
|
#@memo = @forum.memos.find(params[:id])
|
|
|
|
|
@memo = Memo.find(params[:id])
|
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
|
render_404
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def find_forum
|
|
|
|
|
@forum = Forum.find(params[:forum_id])
|
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
|
render_404
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def authenticate_user_edit
|
|
|
|
|
find_memo
|
|
|
|
|
render_403 unless @memo.editable_by? User.current
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def authenticate_user_destroy
|
|
|
|
|
find_memo
|
|
|
|
|
render_403 unless @memo.destroyable_by? User.current
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def back_memo_url
|
2015-09-19 10:00:46 +08:00
|
|
|
|
forum_memo_path(@forum, (@memo.root.nil? ? @memo : @memo.root))
|
2014-10-23 11:30:34 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def back_memo_or_forum_url
|
|
|
|
|
@memo.parent_id.nil? ? forum_url(@forum) : forum_memo_url(@forum, @memo.parent_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|