faq/app/controllers/admin/faqs_controller.rb

110 lines
3.0 KiB
Ruby
Raw Normal View History

2014-05-08 07:33:39 +00:00
class Admin::FaqsController < OrbitAdminController
before_filter :setup_vars
2014-05-05 07:57:25 +00:00
def index
2014-08-07 10:47:37 +00:00
@table_fields = [:status,:category,:title,:tags]
2014-05-08 07:33:39 +00:00
@categories = @module_app.categories
@tags = @module_app.tags
2014-08-07 10:47:37 +00:00
@filter_fields = filter_fields(@categories, @tags)
2014-05-08 07:33:39 +00:00
2014-08-07 10:47:37 +00:00
@qas = Qa.order_by(sort)
.with_categories(filters("category"))
.with_tags(filters("tag"))
.with_status(filters("status"))
2014-05-08 07:33:39 +00:00
2014-08-07 10:47:37 +00:00
@qas = search_data(@qas,[:title]).page(params[:page]).per(10)
@faq_page_url = Page.where(:module=>'faq').first.url rescue '#'
2014-08-07 10:47:37 +00:00
render :partial => "index" if request.xhr?
2014-05-08 07:33:39 +00:00
end
def new
2014-08-07 10:47:37 +00:00
@qa = Qa.new
2014-05-08 07:33:39 +00:00
@tags = @module_app.tags
@categories = @module_app.categories
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @qa }
end
end
def create
@qa = Qa.new(create_params)
@qa.create_user_id = current_user.id
@qa.update_user_id = current_user.id
@qa.save
2021-03-24 04:32:22 +00:00
redirect_to admin_faqs_path#params['referer_url']
2014-05-08 07:33:39 +00:00
end
def edit
@qa = Qa.find(params[:id])
2014-07-31 12:41:24 +00:00
if can_edit_or_delete?(@qa)
@tags = @module_app.tags
@categories = @module_app.categories
else
render_401
end
2014-05-08 07:33:39 +00:00
end
def update
@qa = Qa.find(params[:id])
@qa.update_attributes(create_params)
@qa.update_user_id = current_user.id
@qa.save
2021-03-24 04:32:22 +00:00
redirect_to admin_faqs_path#params['referer_url']
2014-05-08 07:33:39 +00:00
end
def destroy
@qa = Qa.find(params[:id])
@qa.destroy
redirect_to admin_faqs_path
end
2016-05-09 17:12:43 +00:00
def order
empty_position_qas = Qa.where(:order_position => nil)
if empty_position_qas.count > 0
max_position = Qa.max(:order_position)
max_position = 0 if max_position.nil?
empty_position_qas.each_with_index do |epl,i|
epl.order_position = i + max_position
epl.save
end
end
@qas = Qa.all.asc(:order_position)
end
def updateorder
ids_with_order = params[:order]
ids_with_order.each_with_index do |id,index|
qa = Qa.find(id) rescue nil
if !qa.nil?
qa.order_position = index
qa.save
end
end
render :json => {"success" => true}.to_json
end
2014-05-08 07:33:39 +00:00
private
def setup_vars
if ModuleApp.where(:key => "faq").count > 1
module_apps = ModuleApp.where(:key => "faq").to_a
categories_count = module_apps.map{|m| m.categories.count}
module_apps = module_apps.map.with_index{|m,i| {'id'=>m.id,'count'=>categories_count[i]}}.sort_by{|m| -m['count']}
module_apps[1..-1].each do |m|
module_app = ModuleApp.find(m['id'])
module_app.categories.each{|c| c.update(:module_app_id=>module_apps.first['id'])}
module_app.destroy
end
end
2014-08-07 10:47:37 +00:00
@module_app = ModuleApp.where(:key => "faq").first
2014-05-08 07:33:39 +00:00
end
def create_params
qa_params = params.require(:qa).permit!
qa_params['title_translations'] = {}
qa_params['rich_title_translations'].each{|locale,value| temp_title = Nokogiri::HTML(value);qa_params['title_translations'][locale] = temp_title.inner_text;}
return qa_params
2014-05-05 07:57:25 +00:00
end
end