ask/app/controllers/admin/asks_controller.rb

629 lines
27 KiB
Ruby
Raw Normal View History

2014-10-02 06:00:35 +00:00
# encoding: utf-8
class Admin::AsksController < OrbitAdminController
helper Admin::AsksHelper
include Admin::AsksHelper
2014-10-02 06:00:35 +00:00
before_action ->(module_app = @app_title) { set_variables module_app }
2020-07-04 13:36:32 +00:00
before_action :set_askquestion, only: [:edit, :destroy, :update, :print]
layout :compute_layout
helper_method :ask_thead
def compute_layout
2020-07-04 13:36:32 +00:00
if action_name=='print'
false
else
'back_end_with_jquery_first'
end
end
2014-10-02 06:00:35 +00:00
def initialize
super
@app_title = "ask"
end
2020-07-04 13:36:32 +00:00
def print_setting
@set = nil
prepare_category_select
if @category_id.blank?
@set = AskPrintSetting.first
else
@set = AskCategoryPrintSetting.where(:category_id=>@category_id).first
@set = AskPrintSetting.first if @set.nil?
end
2020-07-04 13:36:32 +00:00
@set = AskPrintSetting.create if @set.nil?
@url = print_setting_save_admin_asks_path
end
def print_setting_save
locale = I18n.locale
if params[:ask_print_setting][:category_id].present?
set = AskCategoryPrintSetting.where(:category_id=>params[:ask_print_setting][:category_id]).first
set = AskCategoryPrintSetting.create(:category_id=>params[:ask_print_setting][:category_id]) if set.nil?
else
set = AskPrintSetting.first
end
2020-07-04 13:36:32 +00:00
set.update_attributes(print_format: params[:ask_print_setting][:print_format],save_name: params[:ask_print_setting][:save_name])
redirect_to admin_asks_path(:locale=>locale)
2020-07-04 13:36:32 +00:00
end
def category_print_setting_save
locale = I18n.locale
set = AskCategoryPrintSetting.where(category_id: params['category_id']).first
set = AskCategoryPrintSetting.create if set.nil?
set.update_attributes(print_format: params['print_format'],save_name: params['save_name'],category_id: params['category_id'])
redirect_to "/#{locale}/admin/asks/category_setting"
end
def filter_fields(categories, tags=[])
2014-10-02 06:00:35 +00:00
{
:situation=> AskTicketStatus.all.map{|s| {:title=> s.title, :id=>s.key}}
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
2020-07-04 13:36:32 +00:00
locale = I18n.locale
ask_setting = AskSetting.first
2020-07-04 13:36:32 +00:00
p1 = ask_setting_params(ask_setting,'ask_setting')
ask_setting.update_attributes(p1)
2022-08-18 03:07:45 +00:00
redirect_to "/#{locale}/admin/asks/setting"
2020-07-04 13:36:32 +00:00
end
def switch_to_default_setting
if AskCategorySetting.where(:id=>params[:id]).update_all(:use_default=>params[:use_default] == "true").documents[0]["nModified"] != 0
render :json => {success: true}
else
render :json => {success: false}
end
end
2020-07-04 13:36:32 +00:00
def category_setting_save
# render :html => params and return
2020-07-04 13:36:32 +00:00
locale = I18n.locale
ask_setting = AskCategorySetting.where(category_id: params['category_id']).first
ask_setting = AskCategorySetting.create(category_id: params['category_id']) if ask_setting.nil?
2022-08-18 03:07:45 +00:00
p1 = ask_setting_params(ask_setting,'ask_category_setting', ask_setting.agree_customs.map{|k| "default@#{k}"})
ask_setting.update_attributes(p1)
2020-07-04 13:36:32 +00:00
redirect_to "/#{locale}/admin/asks/category_setting"
end
def setting
@ask_setting = AskSetting.first
@url = setting_save_admin_asks_path
end
def backend_table_setting
category_id = params[:category_id].present? ? params[:category_id] : nil
@ask_list_setting = AskListSetting.where(:category_id=>category_id).first || AskListSetting.create(:category_id=>category_id)
@url = ask_list_setting_save_admin_asks_path
end
def ask_list_setting_save
@ask_list_setting = AskListSetting.find(params[:id])
ask_list_setting_params = params.require('ask_list_setting').permit!
@ask_list_setting.update_attributes(ask_list_setting_params)
redirect_to admin_asks_path(:locale=>locale)
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
2020-07-04 13:36:32 +00:00
def get_new_category_setting_index
ask_setting_index = AskCategorySettingIndex.where(category_id: params['category_id']).first
ask_setting_index = AskCategorySettingIndex.create(category_id: params['category_id']) 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
2022-08-18 03:07:45 +00:00
def apply_default_custom_fields
ask_setting = AskCategorySetting.where(:category_id=>params[:category]).first
if ask_setting
if params[:apply_all].to_s == 'true'
apply_fields = ask_setting.need_check_customs
if params[:include_reject].to_s == 'true'
apply_fields += ask_setting.reject_customs
apply_fields = apply_fields.sort_by{|k| k.to_i}
end
else
apply_fields = params[:apply_fields].to_a
end
is_agree = params[:agree].to_s == 'true'
if is_agree
ask_setting.agree_customs_func(apply_fields)
custom_fields = AskSetting.first.custom_fields
2022-09-21 10:07:01 +00:00
set_input_name_for_ask('ask_category_setting[custom_fields]')
view_context.set_input_name_for_ask('ask_category_setting[custom_fields]') #prevent not setting variables
2022-08-18 03:07:45 +00:00
html = apply_fields.map do |k|
2022-09-22 08:06:12 +00:00
view_context.custom_field_block_for_ask("default@#{k}", custom_fields[k])
2022-08-18 03:07:45 +00:00
end.join("\n")
else
ask_setting.delete_customs_func(apply_fields, true)
html = ""
end
render :json => {:success=>true, :html=>html}
else
render :json => {:success=>false}
end
end
2014-10-02 06:00:35 +00:00
def index
2020-07-04 13:36:32 +00:00
AskPrintSetting.create() if AskPrintSetting.first.nil?
2020-04-30 05:18:02 +00:00
@ask_setting = AskSetting.first
prepare_category_select
category_id = @category_id.present? ? @category_id : nil
@ask_list_setting = AskListSetting.where(:category_id=>category_id).first || AskListSetting.create(:category_id=>category_id)
# @tags = @module_app.tags
2022-05-12 07:35:53 +00:00
@categories = @module_app.categories.enabled.sort_by{|category| (((@module_app.asc rescue true) ? category.sort_number.to_i : -category.sort_number.to_i) rescue category.id)}
@filter_fields = filter_fields(@categories)
2014-10-02 06:00:35 +00:00
# 列表欄位
2022-10-14 14:33:04 +00:00
@table_fields = @ask_list_setting.default_fields.select{|f| @ask_setting.default_setting[f].nil? || @ask_setting.default_setting[f] }
2022-10-15 04:32:29 +00:00
if !category_id.nil?
2022-10-15 03:22:41 +00:00
ask_setting = AskCategorySetting.where(:category_id=>@category_id, :use_default.ne=>true).first
2022-10-15 03:45:37 +00:00
if ask_setting.nil?
ask_setting = AskSetting.first
2022-10-15 03:47:52 +00:00
tmp = ask_setting.custom_fields.map{|k,v| ["default@#{k}", v]}.to_h
2022-10-15 03:45:37 +00:00
else
2022-10-15 04:32:29 +00:00
tmp = ask_setting.custom_fields.map{|k,v| ["custom@#{category_id}@#{k}", v]}.to_h
2022-10-15 03:45:37 +00:00
end
2022-10-12 00:30:06 +00:00
list_setting_custom_fields = @ask_list_setting.custom_fields
else
tmp = []
default_setting = AskSetting.first
if default_setting
tmp += default_setting.custom_fields.map{|k,v| ["default@#{k}", v]}
end
AskCategorySetting.all.each do |ask_setting|
tmp += ask_setting.custom_fields.map{|k,v| ["custom@#{ask_setting.id}@#{k}", v]}
end
tmp = tmp.to_h
list_setting_custom_fields = (tmp.keys & @ask_list_setting.custom_fields)
end
@table_ext_fields = list_setting_custom_fields.map{|k| [k,tmp[k]]}.select{|k,v| v}
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
2022-10-16 11:04:40 +00:00
@askquestions = @askquestions.where(:category_id=>category_id) if category_id
2014-10-02 06:00:35 +00:00
# 分頁
@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
2020-07-04 13:36:32 +00:00
def category_setting
2022-03-09 17:00:05 +00:00
@default_ask_setting = AskSetting.first
2022-05-12 07:35:53 +00:00
@categories = current_user.approved_categories_for_module(@module_app).sort_by{|category| (((@module_app.asc rescue true) ? category.sort_number.to_i : -category.sort_number.to_i) rescue category.id)}
@category_ids = @categories.collect(&:id)
@category_titles = @categories.collect{|v| v.title}
@category_added = AskCategorySetting.all.asc(:created_at).map{|a| [a.category_id, a]}.to_h
@category_added_ids = @category_added.map{|k,v| k}
@url = category_setting_save_admin_asks_path
2020-07-04 13:36:32 +00:00
end
def get_category_setting_field
@default_ask_setting = AskSetting.first
2020-07-04 13:36:32 +00:00
ask_setting = AskCategorySetting.find(params['id'])
render partial: 'category_setting_field',locals: {ask_setting: ask_setting}
end
def category_print_setting_delete
print_setting = AskCategoryPrintSetting.find(params['id'])
print_setting.destroy
locale = I18n.locale
redirect_to "/#{locale}/admin/asks/category_setting"
end
def category_setting_delete
ask_setting = AskCategorySetting.find(params['id'])
ask_setting.destroy
locale = I18n.locale
redirect_to "/#{locale}/admin/asks/category_setting"
end
def process_format_text(text,custom_fields)
2021-02-23 08:53:40 +00:00
customs = text.scan(/<tr(?:(?!<tr).)+\{custom\}(?:(?!<tr).)+<\/tr>/m).uniq
2020-07-04 13:36:32 +00:00
if customs.length != 0
custom_exist_nums = []
custom_exists = text.scan(/{custom\d+}/)
custom_exists.each do |custom_exist|
2021-02-23 15:55:50 +00:00
k = custom_exist[6...-1].to_i - 1
2020-07-04 13:36:32 +00:00
custom_exist_nums << k
end
customs.each do |custom|
2021-02-23 15:55:50 +00:00
tmp = custom_fields.keys.collect.with_index do |k,i|
if custom_exist_nums.exclude?(i)
tp = custom.gsub(/{custom}/,"{custom#{i+1}}")
tp = tp.gsub(/{custom_title}/,"{custom#{i+1}_title}")
tp
else
nil
end
end.compact.join
2020-07-04 13:36:32 +00:00
text = text.gsub(custom,tmp)
end
end
format_texts = text.scan(/{\w+\|*\w+}/).uniq
format_texts.each do |format_text|
if format_text[-7...-1] == '_title'
case format_text[1...-7]
when /custom\d+/
2021-02-23 15:55:50 +00:00
i = format_text.scan(/\d+/)[0].to_i - 1
k = custom_fields.keys[i] rescue nil
2020-07-04 13:36:32 +00:00
if !k.blank?
2021-02-23 15:55:50 +00:00
text_tp = custom_fields[k]['field'][I18n.locale] rescue ''
2020-07-04 13:36:32 +00:00
text = text.gsub(format_text,text_tp)
end
when 'title'
text = text.gsub(format_text,I18n.t('title'))
when 'ask_time'
text = text.gsub(format_text,I18n.t('ask.ask_time'))
when 'category'
text = text.gsub(format_text,I18n.t('mongoid.attributes.ask_question.ask_category_id'))
when 'name','sex','mail','phone','appointment','agree_show'
text = text.gsub(format_text,I18n.t("mongoid.attributes.ask_question.#{format_text[1...-7]}"))
end
else
case format_text[1...-1]
when 'ask_time'
text = text.gsub(format_text,@ask_question.created_at.strftime('%Y/%m/%d %H:%M'))
when 'title','name','mail','phone','appointment'
text = text.gsub(format_text,@ask_question.send(format_text[1...-1]).to_s)
when 'category'
2022-12-31 07:27:56 +00:00
cat = Category.find(@ask_question.category_id).title rescue ''
2020-07-04 13:36:32 +00:00
text = text.gsub(format_text,cat)
when 'name|mask'
name_tp = @ask_question.name.to_s
if name_tp.length>1
name_tp = name_tp[0] + '*'*(name_tp.length-1)
end
text = text.gsub(format_text,name_tp)
when 'sex'
sex = @ask_question.sex
if ['male','female'].include? sex.to_s
sex = I18n.t("mongoid.attributes.ask_question.#{sex}")
else
sex = ''
end
text = text.gsub(format_text,sex)
when 'agree_show'
if @ask_question.agree_show
text = text.gsub(format_text,I18n.t('ask.yes'))
else
text = text.gsub(format_text,I18n.t('ask.no'))
2020-07-04 13:36:32 +00:00
end
when /custom\d+\|format/
2021-02-23 15:55:50 +00:00
i = format_text.scan(/\d+/)[0].to_i - 1
k = custom_fields.keys[i] rescue nil
2020-07-04 13:36:32 +00:00
if !k.blank?
2021-02-23 15:55:50 +00:00
v = custom_fields[k] rescue {}
2022-10-15 04:32:29 +00:00
text_tp = Admin::AsksHelper.show_on_front(k,v,@ask_question,true,true)
2020-07-04 13:36:32 +00:00
text = text.gsub(format_text,text_tp.to_s)
end
when /custom\d+/
2021-02-23 15:55:50 +00:00
i = format_text.scan(/\d+/)[0].to_i - 1
k = custom_fields.keys[i] rescue nil
2020-07-04 13:36:32 +00:00
if !k.blank?
2021-02-23 15:55:50 +00:00
v = custom_fields[k] rescue {}
2022-10-15 04:32:29 +00:00
text_tp = Admin::AsksHelper.show_on_front(k,v,@ask_question,true)
2020-07-04 13:36:32 +00:00
text = text.gsub(format_text,text_tp.to_s)
end
end
end
end
text
end
def print
@print_setting = AskCategoryPrintSetting.where(category_id: @ask_question.category_id.to_s).first
@print_setting = AskPrintSetting.first if @print_setting.nil?
@save_name = @print_setting.save_name rescue ''
@print_text = @print_setting.print_format rescue ''
2022-12-31 07:27:56 +00:00
@print_text = "<style>img{width: 100%;}</style>"+ @print_text
2022-10-15 03:22:41 +00:00
ask_setting = AskCategorySetting.where(category_id: @ask_question.category_id.to_s, :use_default.ne=>true).first
2020-07-04 13:36:32 +00:00
ask_setting = AskSetting.first if ask_setting.nil?
custom_fields = ask_setting.custom_fields
@print_text = process_format_text(@print_text,custom_fields)
@save_name = process_format_text(@save_name,custom_fields)
end
2014-10-02 06:00:35 +00:00
def edit
2022-10-15 03:22:41 +00:00
@ask_setting = AskCategorySetting.where(category_id: @ask_question.category_id.to_s, :use_default.ne=>true).first
2020-07-04 13:36:32 +00:00
@ask_setting = AskSetting.first if @ask_setting.nil?
2014-10-02 06:00:35 +00:00
@url = admin_ask_path(@ask_question)
end
def batch_modify_status
AskQuestion.where(:id.in=>params[:ids]).update_all(:situation=>params[:status_key])
render :body => nil, :status => 204
end
def delete
AskQuestion.where(:id.in=>params[:ids]).destroy
render :body => nil, :status => 204
end
2014-10-02 06:00:35 +00:00
def destroy
2020-05-13 09:25:08 +00:00
locale = I18n.locale
2016-11-07 07:40:22 +00:00
@ask_question.destroy
redirect_to admin_asks_path(:locale=>locale)
2014-10-02 06:00:35 +00:00
end
def update
2020-05-13 09:25:08 +00:00
locale = I18n.locale
temp_params = params.require(:ask_question).permit!
all_to_save = []
2020-07-04 13:36:32 +00:00
ask_setting = AskCategorySetting.where(category_id: params['ask_question']['category_id']).first
if ask_setting.nil?
ask_setting = AskSetting.first
end
2020-08-21 10:19:28 +00:00
if temp_params['custom_values'].nil?
2020-08-21 11:06:13 +00:00
temp_params['custom_values'] = {}
2020-08-21 10:19:28 +00:00
end
ask_setting.custom_fields.select{|k,v| v['type']=='file' || v['type']=='image'}.each do |k,v|
2020-08-21 10:15:35 +00:00
file = temp_params['custom_values'][k] rescue nil
if !file.blank?
if v['type']=='image'
all_to_save += [[k,AskImage.new(file: file,ask_question_id: @ask_question.id)]]
else
all_to_save += [[k,AskFile.new(file: file,ask_question_id: @ask_question.id)]]
end
else
2020-08-21 10:17:14 +00:00
temp_params['custom_values'][k] = @ask_question.custom_values[k] rescue nil
end
end
2020-08-21 11:06:13 +00:00
temp_params['custom_values'] = @ask_question.custom_values.merge(temp_params['custom_values'])
flag = @ask_question.save
if flag
all_to_save.each do |to_save|
flag = flag && to_save[1].save
temp_params['custom_values'][to_save[0]] = [temp_params['custom_values'][to_save[0]].original_filename ,to_save[1].file.url]
end
end
@ask_question.update_attributes(temp_params)
2014-10-02 06:00:35 +00:00
if @ask_question.send_email?
build_email(@ask_question)
end
redirect_to admin_asks_path(:locale=>locale), notice: t('ask.reply_success')
2014-10-02 06:00:35 +00:00
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
2020-05-13 09:25:08 +00:00
site = Site.first
mail_from = site['title'][site['default_locale']]
2014-10-02 06:00:35 +00:00
email_er.email.update_attributes(
2020-05-13 09:34:42 +00:00
:mail_lang=> site['default_locale'],
2014-10-02 06:00:35 +00:00
:create_user=>current_user,
:mail_sentdate=>@mail_sentdate,
:module_app=>@module_app,
:mail_to=>@group_mail,
2020-05-13 09:34:42 +00:00
:mail_subject=>mail_from+" #{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
def process_trans(trans)
trans.collect{|k,v| v if !v.blank?}.compact.uniq.join('/')
end
def get_fields_with_key(s)
fields_with_key = {}
2022-12-31 07:08:43 +00:00
fields_with_key['default_values'] = s.default_setting.collect{|k,v| [k, process_trans(s.field_name_translations(k))] if v && !['ask_category_id','recaptcha'].include?(k)}.compact
2022-12-31 08:05:26 +00:00
fields_with_key['default_values'] = [['ask_category_id', process_trans(s.field_name_translations('ask_category_id'))]] + [['situation', process_trans(I18n.available_locales.map{|v| [v.to_s, I18n.with_locale(v){I18n.t('ask.situation')}]}.to_h)]] + fields_with_key['default_values']
fields_with_key['default_values'] = fields_with_key['default_values'].to_h
fields_with_key['custom_values'] = s.custom_fields.collect{|k, field_setting| [k, process_trans(field_setting['field'])]}.to_h
fields_with_key
end
2014-10-02 06:00:35 +00:00
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)
categories = Category.where(:id.in => ask_questions.collect(&:category_id).uniq).to_a
ask_default_setting = AskSetting.first()
ask_settings = AskCategorySetting.where(:category_id.in => categories.collect(&:id)).collect{|s| [BSON::ObjectId(s.category_id), s]}.to_h
ask_category_settings = {}
fields_with_key_group = {}
categories.each do |c|
s = ask_settings.fetch(c.id, ask_default_setting)
ask_category_settings[c.id] = s
fields_with_key_group[c.id] = get_fields_with_key(s)
2014-10-02 06:00:35 +00:00
end
ask_category_settings[nil] = ask_default_setting
fields_with_key_group[nil] = get_fields_with_key(ask_default_setting)
2022-12-31 08:06:53 +00:00
situations = AskTicketStatus.all.collect{|situation| [situation.key, situation.get_title_translations]}.to_h
render xlsx: 'do_export.xlsx',
handlers: ['axlsx'],
filename: "Questions-#{date_start}-#{date_end}.xlsx",
locals: {
:ask_category_settings => ask_category_settings,
:fields_with_key_group => fields_with_key_group,
2022-12-31 07:27:56 +00:00
:categories => categories.collect{|c| [c.id, c]}.to_h,
2022-12-31 08:05:26 +00:00
:ask_questions => ask_questions,
:situations => situations
}
2014-10-02 06:00:35 +00:00
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
def order_fields
prepare_category_select
@disp_fields_infos = AskSetting.get_disp_fields_infos(false,@category_id)
end
def prepare_category_select
2022-09-22 08:06:12 +00:00
@cats_relations = get_categories_info_for_ask
@category_id = (params[:category] || @cats_relations[0][0] rescue nil)
end
def update_order_fields
ask_setting = AskCategorySetting.where(:category_id=>params[:category]).first
use_default = ask_setting && ask_setting.use_default
sort_number = params[:sort_number].map{|k,v| [k,v.to_i]}.to_h
if params[:category].present? && !use_default
if ask_setting.nil?
default_setting = AskSetting.first
ask_setting = AskCategorySetting.create(category_id: params[:category])
p1 = default_setting.get_attrs
p1['need_check_customs'] = []
p1['agree_customs'] = p1['custom_fields'].keys rescue []
p1['reject_customs'] = []
p1['custom_fields'] = p1['custom_fields'].map{|k,v| ["default@#{k}", v]}.to_h rescue {}
ask_setting.update_attributes(p1)
ask_setting.update(:use_default => true)
2022-08-18 03:07:45 +00:00
ask_setting_index = AskCategorySettingIndex.where(category_id: params[:category]).first
ask_setting_index = AskCategorySettingIndex.create(category_id: params[:category]) if ask_setting_index.nil?
ask_setting_index.key = default_setting.custom_fields.keys[-1].to_i rescue 0
ask_setting_index.save
end
ask_setting.update(:sort_number=> sort_number)
default_ask_setting = AskSetting.first
default_sort_number = default_ask_setting.sort_number
ask_setting_id = ask_setting.id.to_s
2022-08-18 03:07:45 +00:00
part_sort_number = sort_number.select{|k,v| k.start_with?('default@')}.map{|k,v| tmp=k.split('@')[1..-1].join('@');[(tmp.start_with?('default@') ? tmp : "custom@#{ask_setting_id}@#{tmp}"), v]}.to_h
intersect_keys = default_sort_number.keys & part_sort_number.keys
non_intersect_keys = part_sort_number.keys - default_sort_number.keys
if intersect_keys.count != 0
2022-08-18 03:07:45 +00:00
org_order = intersect_keys.map{|k| [k, default_sort_number[k]]}.sort_by{|k,v| v}.to_h
default_sort_number = default_sort_number.merge(org_order.sort_by{|k, v| part_sort_number[k]}.map{|k,v| k}.zip(org_order.values).to_h)
end
preserve_count = AskSetting::All_default_fields.count
if non_intersect_keys.count != 0
max_sort_number = default_sort_number.map{|k,v| v.to_i}.max.to_i
max_sort_number += 1
max_sort_number += preserve_count
part_sort_number = non_intersect_keys.map.with_index{|(k,v), i| [k, max_sort_number + i]}
default_sort_number = default_sort_number.merge(part_sort_number.to_h)
end
default_ask_setting.update(:sort_number=>default_sort_number)
else
ask_setting = AskSetting.first
if use_default
sort_number = ask_setting.sort_number.merge(sort_number)
end
AskSetting.update_all(:sort_number=>sort_number)
unless use_default
2022-08-18 03:07:45 +00:00
default_customs_sort_number = sort_number.select{|k, v| k.start_with?('default@')}.map{|k,v| ["default@#{k}",v]}
custom_sort_number = sort_number.select{|k, v| k.start_with?('custom@')}
all_custom_sort_numbers = custom_sort_number.group_by{|k, v| k.split('@')[1]}
preserve_count = AskSetting::All_default_fields.count
all_custom_sort_numbers.each do |ask_category_setting_id, part_sort_number|
ask_setting = AskCategorySetting.find(ask_category_setting_id) rescue nil
if ask_setting
sort_number = ask_setting.sort_number
2022-08-18 03:07:45 +00:00
tmp_default_customs_sort_number = default_customs_sort_number.select{|k, v| ask_setting.agree_customs.include?(k.split('@')[-1])}
part_sort_number = (part_sort_number.map{|k,v| ["default@#{k.split('@')[-1]}", v]} + tmp_default_customs_sort_number).to_h
intersect_keys = sort_number.keys & part_sort_number.keys
non_intersect_keys = part_sort_number.keys - sort_number.keys
if intersect_keys.count != 0
2022-08-18 03:07:45 +00:00
org_order = intersect_keys.map{|k| [k, sort_number[k]]}.sort_by{|k,v| v}.to_h
sort_number = sort_number.merge(org_order.sort_by{|k, v| part_sort_number[k]}.map{|k,v| k}.zip(org_order.values).to_h)
end
if non_intersect_keys.count != 0
max_sort_number = sort_number.map{|k,v| v.to_i}.max.to_i
max_sort_number += 1
max_sort_number += preserve_count
part_sort_number = non_intersect_keys.map.with_index{|(k,v), i| [k, max_sort_number + i]}
sort_number = sort_number.merge(part_sort_number.to_h)
end
ask_setting.update(:sort_number=>sort_number)
end
end
end
end
render :json => {:success=>true}
end
def ticket_status
@ticket_statuses = AskTicketStatus.all
if request.xhr?
render 'ticket_status', :layout => false
end
end
def get_ticket_status
ticket_status = AskTicketStatus.where(:id=>params[:id]).first
render :json => {
"id"=>params[:id],
"title_translations"=> (ticket_status ? ticket_status.get_title_translations : {})
}
end
def update_ticket_status
if params[:id].blank?
ticket_status = AskTicketStatus.new
else
ticket_status = AskTicketStatus.where(:id=>params[:id]).first
end
ticket_status.update_attributes(params.require(:ask_ticket_status).permit!)
render :json => {:success=>true}
end
def delete_ticket_status
ticket_status = AskTicketStatus.where(:id=>params[:id]).first
unless ticket_status.is_default
ticket_status.destroy
end
render :json => {:success=>true}
end
private
2022-08-18 03:07:45 +00:00
def ask_setting_params(ask_setting,collection_name, except_customs=[])
param = params.require(collection_name).except("id").permit!
param_clone = param.clone
param_clone['default_setting'].each { |k, v| param_clone['default_setting'][k] = (v == 'true'? true : false) if param_clone['default_setting'][k].class==String}
param_clone['default_setting_required'].each { |k, v| param_clone['default_setting_required'][k] = (v == 'true'? true : false) if param_clone['default_setting_required'][k].class==String}
param_clone.delete('custom_fields')
2022-04-12 14:17:34 +00:00
param_clone.delete('email_regex')
2022-08-18 03:07:45 +00:00
ask_setting.custom_fields_will_change!
custom_fields = params['not_clone_attrs'] ? {} : ask_setting['custom_fields'].clone
2020-07-04 13:36:32 +00:00
params_custom_fields = (params.require(collection_name).require('custom_fields') rescue {})
params_custom_fields.each do |k,v|
2022-08-18 03:07:45 +00:00
next if except_customs.include?(k.to_s)
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
def ask_thead(field, trans,center=false)
sort = field.to_s.include?('.') ? field.to_s.split('.')[1] : field.to_s
active = params[:sort].eql? sort
order = active ? (["asc", "desc"]-[params[:order]]).first : "asc"
arrow = (order.eql? "desc") ? "<b class='icons-arrow-up-3'></b>" : "<b class='icons-arrow-down-4'></b>"
klass = field.eql?(:title) ? "span5" : "span2"
th_data = "<a href='?sort=#{sort}&order=#{order}'>#{trans} #{active ? arrow : ""}</a>"
"<th class='#{klass} #{active ? "active" : ""}' style='#{center ? "text-align:center" : ""}'>#{th_data}</th>".html_safe
end
2014-10-02 06:00:35 +00:00
end