ask/app/models/ask_setting.rb

96 lines
3.6 KiB
Ruby
Raw Normal View History

class AskSetting
include Mongoid::Document
include Mongoid::Timestamps
All_default_fields = ['ask_category_id','name','sex','mail','phone','appointment','recaptcha','usage_rule','agree_show','agree_usage']
after_initialize do
2020-07-04 13:36:32 +00:00
if !self.new_record?
if self.default_setting.class != Hash && self.default_setting.class != BSON::Document
self.default_setting = {}
end
if self.default_setting[:agree_show].nil?
self.default_setting[:agree_show] = true
end
if self.default_setting[:agree_usage].nil?
self.default_setting[:agree_usage] = false
end
if self.custom_fields.class != Hash && self.custom_fields.class != BSON::Document
self.custom_fields = {}
end
self.save
end
end
def custom_fields
tmp = super
tmp.select{|k,v| v['delete'] != true}
end
def field_name(k,locale=I18n.locale.to_s,use_checkbox_trans=false)
tmp = self.default_setting_field_name[k]
(tmp&&tmp[locale]) ? tmp[locale] : self.default_field_name(k,locale)
2022-03-09 17:00:05 +00:00
end
def default_field_name(k,locale=I18n.locale.to_s,use_checkbox_trans=false)
I18n.with_locale(locale){ (use_checkbox_trans && ['agree_show','agree_usage'].include?(k.to_s)) ? I18n.t("ask.#{k}_checkbox") : I18n.t("mongoid.attributes.ask_question.#{k}")}
2022-03-09 17:00:05 +00:00
end
def field_name_translations(k)
I18n.available_locales.map{|v| [v.to_s,self.field_name(k,v.to_s)]}.to_h
end
field :sort_number, type: Hash, default: {}
2020-07-04 13:36:32 +00:00
field :default_setting, type: Hash,default: {ask_category_id: true,name: true,sex: false,mail: true,phone: false,appointment: false,recaptcha: false,agree_show: true,agree_usage: false}
2022-03-09 17:00:05 +00:00
field :default_setting_field_name, type: Hash,default: {}
field :custom_fields, type: Hash,default: {}
2020-07-04 13:36:32 +00:00
field :usage_rule
def self.get_disp_fields_infos
ask_setting = self.first
ask_setting = self.create() if ask_setting.nil?
disp_fields = self::All_default_fields
disp_fields_infos = {}
locale = I18n.locale.to_s
disp_fields[0...-4].each do |f|
disp_fields_infos[f] = {"trans"=>ask_setting.field_name(f,locale),"sort_number"=>ask_setting.sort_number[f]}
end
tmp_locales = I18n.available_locales.map{|l| l.to_s}
tmp_locales = [locale] + (tmp_locales - [locale])
ask_setting.custom_fields.each do |k,v|
trans = v["field"]
tmp_locales.each do |l|
trans = trans[l] if trans[l].present?
end
next if trans.class != String
tmp_k = "default@#{k}"
disp_fields_infos[tmp_k] = {"trans"=>trans,"sort_number"=>ask_setting.sort_number[tmp_k]}
end
AskCategorySetting.all.each do |c|
cat = Category.find(c.category_id)
c.custom_fields.each do |k,v|
trans = v["field"]
tmp_locales.each do |l|
trans = trans[l] if trans[l].present?
end
next if trans.class != String
tmp_k = "custom@#{c.id}@#{k}"
cat_title = cat.title rescue ""
disp_fields_infos[tmp_k] = {"trans"=>"#{cat_title}-#{trans}","sort_number"=>ask_setting.sort_number[tmp_k]}
end
end
disp_fields[-4..-1].each do |f|
disp_fields_infos[f] = {"trans"=>ask_setting.field_name(f,locale),"sort_number"=>ask_setting.sort_number[f]}
end
max_sort_number = disp_fields_infos.values.map{|h| h["sort_number"].to_i}.max
if max_sort_number
max_sort_number = max_sort_number + 1
disp_fields_infos.each do |k,h|
if h["sort_number"].blank?
h["sort_number"] = max_sort_number
else
h["sort_number"] = h["sort_number"].to_i
end
end
disp_fields_infos = disp_fields_infos.sort_by {|_key, value| value["sort_number"]}
end
disp_fields_infos
end
end