171 lines
5.1 KiB
Ruby
171 lines
5.1 KiB
Ruby
# encoding: utf-8
|
||
class Admin::AsksController < OrbitAdminController
|
||
include Admin::AsksHelper
|
||
before_action ->(module_app = @app_title) { set_variables module_app }
|
||
before_action :set_askquestion, only: [:edit, :destroy]
|
||
|
||
def initialize
|
||
super
|
||
@app_title = "ask"
|
||
end
|
||
|
||
# 重新定義排序
|
||
def sort
|
||
unless params[:sort].blank?
|
||
case params[:sort]
|
||
when "status"
|
||
@sort = {:status=>params[:order]}
|
||
when "category"
|
||
@sort = {:category_id=>params[:order]}
|
||
when "start_date"
|
||
@sort = {:postdate=>params[:order]}
|
||
when "end_date"
|
||
@sort = {:deadline=>params[:order]}
|
||
when "last_modified"
|
||
@sort = {:update_user_id=>params[:order]}
|
||
when "banner"
|
||
@sort = {'banner_id'=>params[:order]}
|
||
when "banner_name"
|
||
@sort = {:title=>params[:order]}
|
||
when "effect"
|
||
@sort = {:ad_fx=>params[:order]}
|
||
when "transition_interval"
|
||
@sort = {:timeout=>params[:order]}
|
||
when "transition_speed"
|
||
@sort = {:speed=>params[:order]}
|
||
when "size"
|
||
@sort = {:height=>params[:order]}
|
||
when "link"
|
||
@sort = {:out_link=>params[:order]}
|
||
else
|
||
s = Sanitize.clean(params[:sort]).to_sym
|
||
@sort = {s=>params[:order]}
|
||
end
|
||
else
|
||
@sort = {:created_at=>'desc'}
|
||
end
|
||
@sort
|
||
end
|
||
|
||
# 重新定義分類搜尋
|
||
def filter_fields(categories)
|
||
{
|
||
:status=>[{:title=>"待處理",:id=>"is_hot"},{:title=>"已處理",:id=>"is_top"},{:title=>"轉介其他單位",:id=>"is_hidden"}],
|
||
:category=>categories.map{|c| {:title=>(c.title.blank? ? " " : c.title), :id=>c.id}}
|
||
}
|
||
end
|
||
|
||
# 抓取網址的狀態參數
|
||
def filter2(type)
|
||
case type
|
||
when "status"
|
||
params[:filters][:status].blank? ? [] : params[:filters][:status] rescue []
|
||
end
|
||
end
|
||
|
||
def index
|
||
@categories = @module_app.categories
|
||
@filter_fields = filter_fields(@categories)
|
||
# 列表欄位
|
||
@table_fields = [:status, :category, :title, 'ask.name', 'ask.created_at']
|
||
# 列表排序
|
||
if filter2("status").blank?
|
||
@askquestions = AskQuestion.order_by(sort) .with_categories(filters("category"))
|
||
else
|
||
@askquestions = AskQuestion.order_by(sort) .with_categories(filters("category")) .where(:status.in => filter2("status"))
|
||
end
|
||
|
||
# 分頁
|
||
@askquestions = search_data(@askquestions,[:title]).page(params[:page]).per(10)
|
||
if request.xhr?
|
||
render :partial => "index"
|
||
end
|
||
end
|
||
|
||
def edit
|
||
@ask_question = AskQuestion.find(params[:id])
|
||
@url = admin_ask_path(@ask_question)
|
||
end
|
||
|
||
def destroy
|
||
@askquestions.destroy
|
||
redirect_to "/admin/asks"
|
||
end
|
||
|
||
def update
|
||
@ask_question = AskQuestion.find(params[:id])
|
||
@ask_question.update_attributes(params.require(:ask_question).permit!)
|
||
if @ask_question.send_email?
|
||
build_email(@ask_question)
|
||
end
|
||
|
||
redirect_to admin_asks_path, notice: t('ask.reply_success')
|
||
end
|
||
|
||
def build_email(email_er)
|
||
email = Email.new
|
||
email.save
|
||
email_er.email_id = email.id
|
||
email_er.save
|
||
|
||
@group_mail = email_er.mail
|
||
@mail_sentdate = DateTime.now
|
||
|
||
email_er.email.update_attributes(
|
||
:create_user=>current_user,
|
||
:mail_sentdate=>@mail_sentdate,
|
||
:module_app=>@module_app,
|
||
:mail_to=>@group_mail,
|
||
:mail_subject=>Site.first.title_translations["zh_tw"]+" #{t('ask.reply')}:"+email_er.title,
|
||
:template=>'admin/asks/email',
|
||
:template_data=>{
|
||
"reply" => email_er.reply
|
||
}
|
||
)
|
||
|
||
OrbitMailer.set_mail(email_er.email).deliver
|
||
end
|
||
|
||
def export
|
||
|
||
end
|
||
|
||
def do_export
|
||
|
||
Rails.application.config.mongoid.use_activesupport_time_zone = true
|
||
date_start = "#{params[:export]['start(1i)']}-#{params[:export]['start(2i)']}-#{params[:export]['start(3i)']}"
|
||
date_end = "#{params[:export]['end(1i)']}-#{params[:export]['end(2i)']}-#{params[:export]['end(3i)']}"
|
||
@ask_questions = AskQuestion.where(:created_at.gte => date_start.to_datetime, :created_at.lte => date_end.to_datetime+1)
|
||
|
||
csv = CSV.generate do |csv|
|
||
csv << [ t('category'),
|
||
AskQuestion.human_attribute_name(:name),
|
||
AskQuestion.human_attribute_name(:identity),
|
||
AskQuestion.human_attribute_name(:mail),
|
||
AskQuestion.human_attribute_name(:phone),
|
||
AskQuestion.human_attribute_name(:fax),
|
||
AskQuestion.human_attribute_name(:title),
|
||
AskQuestion.human_attribute_name(:content),
|
||
AskQuestion.human_attribute_name(:reply),
|
||
AskQuestion.human_attribute_name(:comment)]
|
||
@ask_questions.each do |ask_question|
|
||
csv << [ ask_question.category.title,
|
||
ask_question.name,
|
||
ask_question[:identity],
|
||
ask_question.mail,
|
||
ask_question.phone,
|
||
ask_question.fax,
|
||
ask_question.title,
|
||
ask_question.content,
|
||
ask_question.reply,
|
||
ask_question.comment ]
|
||
end
|
||
end
|
||
send_data csv.encode('Big5'), type: 'text/csv', filename: "Questions-#{date_start}-#{date_end}.csv"
|
||
end
|
||
|
||
def set_askquestion
|
||
@askquestions = AskQuestion.find(params[:id])
|
||
end
|
||
end
|