ask/app/controllers/admin/asks_controller.rb

194 lines
6.5 KiB
Ruby
Raw Normal View History

2014-10-02 06:00:35 +00:00
# encoding: utf-8
class Admin::AsksController < OrbitAdminController
helper Admin::AsksHelper
2014-10-02 06:00:35 +00:00
before_action ->(module_app = @app_title) { set_variables module_app }
2016-11-07 07:40:22 +00:00
before_action :set_askquestion, only: [:edit, :destroy, :update]
layout :compute_layout
def compute_layout
'back_end_with_jquery_first'
end
2014-10-02 06:00:35 +00:00
def initialize
super
@app_title = "ask"
end
2014-10-23 07:03:21 +00:00
def filter_fields(categories, tags)
2014-10-02 06:00:35 +00:00
{
2014-10-23 07:03:21 +00:00
:situation=>[{:title=>"is_waiting",:id=>"is_waiting"},{:title=>"is_processed",:id=>"is_processed"},{:title=>"is_referral",:id=>"is_referral"}],
:category=>categories.map{|c| {:title=>(c.title.blank? ? " " : c.title), :id=>c.id}},
:identity=>tags.map{|tag| {:title=>(tag.name.blank? ? " " : tag.name), :id=>tag.id}}
2014-10-02 06:00:35 +00:00
}
end
# 抓取網址的狀態參數
def filter2(type)
case type
2014-10-23 07:03:21 +00:00
when "situation"
params[:filters][:situation].blank? ? [] : params[:filters][:situation] rescue []
when "identity"
params[:filters][:identity].blank? ? [] : params[:filters][:identity] rescue []
2014-10-02 06:00:35 +00:00
end
end
def setting_save
ask_setting = AskSetting.first
p1 = ask_setting_params
ask_setting.update_attributes(p1)
redirect_to '/admin/asks'
end
def setting
@ask_setting = AskSetting.first
@ask_setting = AskSetting.create() if @ask_setting.nil?
@url = setting_save_admin_asks_path
end
def get_new_setting_index
ask_setting_index = AskSettingIndex.first
ask_setting_index = AskSettingIndex.create() if ask_setting_index.nil?
ask_setting_index.key = ask_setting_index.key + 1
ask_setting_index.save
render :json => {key: ask_setting_index.key.to_i}
end
2014-10-02 06:00:35 +00:00
def index
2014-10-23 07:03:21 +00:00
@tags = @module_app.tags
2014-10-02 06:00:35 +00:00
@categories = @module_app.categories
2014-10-23 07:03:21 +00:00
@filter_fields = filter_fields(@categories, @tags)
2014-10-02 06:00:35 +00:00
# 列表欄位
2016-11-07 02:08:04 +00:00
@table_fields = [:situation, :category, 'ask.name', 'ask.phone', 'ask.appointment']
2014-10-02 06:00:35 +00:00
# 列表排序
2014-10-23 07:03:21 +00:00
# debugger
if filter2("situation").blank? and filter2("identity").blank?
@askquestions = AskQuestion.order_by(sort)
.with_categories(filters("category"))
elsif filter2("situation").blank?
@askquestions = AskQuestion.order_by(sort)
.with_categories(filters("category"))
.any_in(:identity => filter2("identity"))
elsif filter2("identity").blank?
@askquestions = AskQuestion.order_by(sort)
.with_categories(filters("category"))
.any_in(:situation => filter2("situation"))
2014-10-02 06:00:35 +00:00
else
2014-10-23 07:03:21 +00:00
@askquestions = AskQuestion.order_by(sort)
.with_categories(filters("category"))
.any_in(:identity => filter2("identity"))
.any_in(:situation => filter2("situation"))
2014-10-02 06:00:35 +00:00
end
# 分頁
@askquestions = search_data(@askquestions,[:title]).page(params[:page]).per(10)
if request.xhr?
render :partial => "index"
end
end
2014-10-23 07:03:21 +00:00
def search_tag(tag)
Tag.find(tag).name_translations{}
end
2014-10-02 06:00:35 +00:00
def edit
@url = admin_ask_path(@ask_question)
end
def destroy
2016-11-07 07:40:22 +00:00
@ask_question.destroy
2014-10-02 06:00:35 +00:00
redirect_to "/admin/asks"
end
def update
ask_question_param = params.require(:ask_question).permit!
@ask_question.update_attributes(ask_question_param)
2014-10-02 06:00:35 +00:00
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,
2016-11-07 02:08:04 +00:00
:mail_subject=>" #{t('ask.reply')}",
2014-10-02 06:00:35 +00:00
: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|
2014-10-23 07:03:21 +00:00
Tag.where({:id => ask_question[:identity]}).each do |tag|
csv << [ ask_question.category.title,
ask_question.name,
tag.name,
ask_question.mail,
ask_question.phone,
ask_question.fax,
ask_question.title,
ask_question.content,
ask_question.reply,
ask_question.comment ]
end
2014-10-02 06:00:35 +00:00
end
end
send_data csv.encode('Big5'), type: 'text/csv', filename: "Questions-#{date_start}-#{date_end}.csv"
end
def set_askquestion
2016-11-07 07:40:22 +00:00
@ask_question = AskQuestion.find(params[:id])
2014-10-02 06:00:35 +00:00
end
private
def ask_setting_params
param = params.require('ask_setting').permit!
param_clone = param.clone
param_clone['default_setting'].each { |k, v| param_clone['default_setting'][k] = (v == 'true'? true : false) }
param_clone.delete('custom_fields')
ask_setting = AskSetting.first
custom_fields = ask_setting['custom_fields'].clone
params_custom_fields = (params.require('ask_setting').require('custom_fields') rescue {})
params_custom_fields.each do |k,v|
custom_fields[k] = v
end
field_to_delete = params['delete_field'].to_s.split(',').select{|v| !v.empty?}
field_to_delete.each do |k|
if custom_fields.keys.include? k
custom_fields[k]['delete'] = true
end
end
param_clone.merge({custom_fields: custom_fields})
end
2014-10-02 06:00:35 +00:00
end