2013-11-24 20:02:53 +08:00
|
|
|
# added by fq
|
2013-11-22 10:24:15 +08:00
|
|
|
class ForumsController < ApplicationController
|
2014-07-25 10:42:56 +08:00
|
|
|
layout "users_base"
|
|
|
|
|
2013-11-22 10:24:15 +08:00
|
|
|
# GET /forums
|
|
|
|
# GET /forums.json
|
2013-12-11 08:52:55 +08:00
|
|
|
before_filter :find_forum_if_available
|
2013-12-07 10:15:55 +08:00
|
|
|
before_filter :authenticate_user_edit, :only => [:edit, :update]
|
|
|
|
before_filter :authenticate_user_destroy, :only => [:destroy]
|
2014-03-08 11:08:48 +08:00
|
|
|
before_filter :require_login, :only => [:new, :create]
|
2013-12-09 18:31:08 +08:00
|
|
|
|
2013-12-11 08:52:55 +08:00
|
|
|
helper :sort
|
|
|
|
include SortHelper
|
|
|
|
|
2013-12-09 18:31:08 +08:00
|
|
|
PageLimit = 20
|
2014-04-14 10:15:18 +08:00
|
|
|
|
|
|
|
def create_memo
|
|
|
|
@memo = Memo.new(params[:memo])
|
|
|
|
@memo.forum_id = @forum.id
|
|
|
|
@memo.author_id = User.current.id
|
|
|
|
|
|
|
|
@memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads]))
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @memo.save
|
|
|
|
format.html { redirect_to (forum_memo_path(@forum, (@memo.parent_id.nil? ? @memo : @memo.parent_id))), notice: "#{l :label_memo_create_succ}" }
|
|
|
|
format.json { render json: @memo, status: :created, location: @memo }
|
|
|
|
else
|
|
|
|
sort_init 'updated_at', 'desc'
|
|
|
|
sort_update 'created_at' => "#{Memo.table_name}.created_at",
|
|
|
|
'replies' => "#{Memo.table_name}.replies_count",
|
|
|
|
'updated_at' => "COALESCE (last_replies_memos.created_at, #{Memo.table_name}.created_at)"
|
|
|
|
|
|
|
|
@topic_count = @forum.topics.count
|
|
|
|
@topic_pages = Paginator.new @topic_count, per_page_option, params['page']
|
|
|
|
@memos = @forum.topics.
|
|
|
|
reorder("#{Memo.table_name}.sticky DESC").
|
|
|
|
includes(:last_reply).
|
|
|
|
limit(@topic_pages.per_page).
|
|
|
|
offset(@topic_pages.offset).
|
|
|
|
order(sort_clause).
|
|
|
|
preload(:author, {:last_reply => :author}).
|
|
|
|
all
|
|
|
|
|
|
|
|
flash.now[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}"
|
|
|
|
# back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id)
|
|
|
|
format.html { render action: :show, layout: 'base_forums' }#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" }
|
|
|
|
format.json { render json: @memo.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-11-24 20:02:53 +08:00
|
|
|
|
2013-11-22 10:24:15 +08:00
|
|
|
def index
|
2013-11-24 20:02:53 +08:00
|
|
|
@offset, @limit = api_offset_and_limit({:limit => 10})
|
2014-07-16 15:16:10 +08:00
|
|
|
@forums_all = Forum.where('1=1')
|
2013-11-24 20:02:53 +08:00
|
|
|
@forums_count = @forums_all.count
|
|
|
|
@forums_pages = Paginator.new @forums_count, @limit, params['page']
|
|
|
|
|
2013-11-22 10:24:15 +08:00
|
|
|
|
2013-11-24 20:02:53 +08:00
|
|
|
@offset ||= @forums_pages.offset
|
2014-07-16 15:16:10 +08:00
|
|
|
@forums = @forums_all.offset(@offset).limit(@limit).all
|
|
|
|
#@forums = Forum.all
|
2013-11-22 10:24:15 +08:00
|
|
|
respond_to do |format|
|
2013-11-22 20:03:56 +08:00
|
|
|
format.html # index.html.erb
|
|
|
|
format.json { render json: @forums }
|
2013-11-22 10:24:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /forums/1
|
|
|
|
# GET /forums/1.json
|
|
|
|
def show
|
2013-12-11 08:52:55 +08:00
|
|
|
sort_init 'updated_at', 'desc'
|
|
|
|
sort_update 'created_at' => "#{Memo.table_name}.created_at",
|
|
|
|
'replies' => "#{Memo.table_name}.replies_count",
|
|
|
|
'updated_at' => "COALESCE (last_replies_memos.created_at, #{Memo.table_name}.created_at)"
|
|
|
|
|
|
|
|
@memo = Memo.new(:forum => @forum)
|
|
|
|
@topic_count = @forum.topics.count
|
|
|
|
@topic_pages = Paginator.new @topic_count, per_page_option, params['page']
|
|
|
|
@memos = @forum.topics.
|
|
|
|
reorder("#{Memo.table_name}.sticky DESC").
|
|
|
|
includes(:last_reply).
|
|
|
|
limit(@topic_pages.per_page).
|
|
|
|
offset(@topic_pages.offset).
|
|
|
|
order(sort_clause).
|
|
|
|
preload(:author, {:last_reply => :author}).
|
|
|
|
all
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# @offset, @limit = api_offset_and_limit({:limit => 10})
|
|
|
|
# @forum = Forum.find(params[:id])
|
|
|
|
# @memos_all = @forum.topics
|
|
|
|
# @topic_count = @memos_all.count
|
|
|
|
# @topic_pages = Paginator.new @topic_count, @limit, params['page']
|
2013-11-24 20:02:53 +08:00
|
|
|
|
2013-12-11 08:52:55 +08:00
|
|
|
# @offset ||= @topic_pages.offset
|
|
|
|
# @memos = @memos_all.offset(@offset).limit(@limit).all
|
2013-11-22 10:24:15 +08:00
|
|
|
respond_to do |format|
|
2013-11-23 20:31:48 +08:00
|
|
|
format.html {
|
|
|
|
render :layout => 'base_forums'
|
|
|
|
}# show.html.erb
|
2013-11-22 20:03:56 +08:00
|
|
|
format.json { render json: @forum }
|
2013-11-22 10:24:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /forums/new
|
|
|
|
# GET /forums/new.json
|
|
|
|
def new
|
|
|
|
@forum = Forum.new
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html # new.html.erb
|
|
|
|
format.json { render json: @forum }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /forums/1/edit
|
|
|
|
def edit
|
|
|
|
@forum = Forum.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST /forums
|
|
|
|
# POST /forums.json
|
|
|
|
def create
|
2013-11-22 20:03:56 +08:00
|
|
|
@forum = Forum.new(params[:forum])
|
2013-11-22 21:55:21 +08:00
|
|
|
@forum.creator_id = User.current.id
|
2013-11-22 11:19:08 +08:00
|
|
|
|
2013-11-22 20:03:56 +08:00
|
|
|
respond_to do |format|
|
|
|
|
if @forum.save
|
2013-12-04 15:31:18 +08:00
|
|
|
format.html { redirect_to @forum, notice: l(:label_forum_create_succ) }
|
2013-11-22 20:03:56 +08:00
|
|
|
format.json { render json: @forum, status: :created, location: @forum }
|
|
|
|
else
|
2014-07-05 11:44:05 +08:00
|
|
|
flash.now[:error] = "#{l :label_forum_create_fail}: #{@forum.errors.full_messages[0]}"
|
2013-11-22 20:03:56 +08:00
|
|
|
format.html { render action: "new" }
|
|
|
|
format.json { render json: @forum.errors, status: :unprocessable_entity }
|
2013-11-22 10:24:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# PUT /forums/1
|
|
|
|
# PUT /forums/1.json
|
|
|
|
def update
|
|
|
|
@forum = Forum.find(params[:id])
|
|
|
|
|
2013-11-22 20:03:56 +08:00
|
|
|
respond_to do |format|
|
|
|
|
if @forum.update_attributes(params[:forum])
|
2014-07-25 10:42:56 +08:00
|
|
|
format.html { redirect_to @forum, notice: l(:label_forum_update_succ) }
|
2013-11-22 20:03:56 +08:00
|
|
|
format.json { head :no_content }
|
|
|
|
else
|
2014-07-25 10:42:56 +08:00
|
|
|
flash.now[:error] = "#{l :label_forum_update_fail}: #{@forum.errors.full_messages[0]}"
|
2013-11-22 20:03:56 +08:00
|
|
|
format.html { render action: "edit" }
|
|
|
|
format.json { render json: @forum.errors, status: :unprocessable_entity }
|
2013-11-22 10:24:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# DELETE /forums/1
|
|
|
|
# DELETE /forums/1.json
|
|
|
|
def destroy
|
|
|
|
@forum = Forum.find(params[:id])
|
2013-11-22 20:03:56 +08:00
|
|
|
@forum.destroy
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to forums_url }
|
|
|
|
format.json { head :no_content }
|
2013-11-22 10:24:15 +08:00
|
|
|
end
|
|
|
|
end
|
2013-12-09 18:31:08 +08:00
|
|
|
|
|
|
|
def search_forum
|
|
|
|
# @forums = paginateHelper Forum.where("name LIKE '%#{params[:name]}%'")
|
2014-07-10 09:01:48 +08:00
|
|
|
q = "%#{params[:name].strip}%"
|
|
|
|
(redirect_to forums_path, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
|
2013-12-09 18:31:08 +08:00
|
|
|
@offset, @limit = api_offset_and_limit({:limit => 10})
|
2014-07-10 09:01:48 +08:00
|
|
|
@forums_all = Forum.where("name LIKE ?", q)
|
2013-12-09 18:31:08 +08:00
|
|
|
@forums_count = @forums_all.count
|
|
|
|
@forums_pages = Paginator.new @forums_count, @limit, params['page']
|
|
|
|
|
|
|
|
|
|
|
|
@offset ||= @forums_pages.offset
|
|
|
|
@forums = @forums_all.offset(@offset).limit(@limit).all
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
render 'index'
|
|
|
|
}
|
|
|
|
format.json { render json: @forums }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_memo
|
2014-07-10 09:01:48 +08:00
|
|
|
q = "%#{params[:name].strip}%"
|
|
|
|
|
2013-12-09 18:31:08 +08:00
|
|
|
limit = PageLimit
|
|
|
|
@memo = Memo.new
|
|
|
|
@offset, @limit = api_offset_and_limit({:limit => limit})
|
|
|
|
@forum = Forum.find(params[:id])
|
2014-07-10 09:01:48 +08:00
|
|
|
@memos_all = @forum.topics.where("subject LIKE ?", q)
|
2013-12-09 18:31:08 +08:00
|
|
|
@topic_count = @memos_all.count
|
|
|
|
@topic_pages = Paginator.new @topic_count, @limit, params['page']
|
|
|
|
|
|
|
|
@offset ||= @topic_pages.offset
|
|
|
|
@memos = @memos_all.offset(@offset).limit(@limit).all
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
render 'show', :layout => 'base_forums'
|
|
|
|
}
|
|
|
|
format.json { render json: @forum }
|
|
|
|
end
|
|
|
|
end
|
2013-12-07 10:15:55 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
2013-12-11 08:52:55 +08:00
|
|
|
def find_forum_if_available
|
|
|
|
@forum = Forum.find(params[:id]) if params[:id]
|
2013-12-07 10:15:55 +08:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_user_edit
|
2013-12-11 08:52:55 +08:00
|
|
|
find_forum_if_available
|
2013-12-07 10:15:55 +08:00
|
|
|
render_403 unless @forum.editable_by? User.current
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_user_destroy
|
2013-12-11 08:52:55 +08:00
|
|
|
find_forum_if_available
|
2013-12-07 10:15:55 +08:00
|
|
|
render_403 unless @forum.destroyable_by? User.current
|
2013-12-09 18:31:08 +08:00
|
|
|
end
|
|
|
|
end
|