ask/app/models/ask_setting.rb

134 lines
5.5 KiB
Ruby

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
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
after_save do
if self.email_regex_enable_changed? || self.email_regex_changed?
AskCategorySetting.all.update_all(email_regex_enable: self.email_regex_enable,email_regex: self.email_regex)
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)
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}")}
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 :email_regex_enable, type: Boolean, default: false
field :email_regex, type: String, default: '\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z'
field :sort_number, type: Hash, default: {}
field :default_setting, type: Hash,default: {title:true,ask_category_id: true,name: true,sex: true,mail: true,phone: true,appointment: true,recaptcha: true,agree_show: true,agree_usage: true}
field :default_setting_required, type: Hash,default: {title:true,ask_category_id: true,name: true,sex: true,mail: true,phone: false,appointment: false,recaptcha: true,agree_show: true,agree_usage: true}
field :default_setting_field_name, type: Hash,default: {}
field :custom_fields, type: Hash,default: {}
field :usage_rule
field :title_layout, type: Integer
field :last_ticket_key, type: Integer, default: 0
def get_attrs
attrs = self.attributes.clone
self.fields.each do |k, v|
if (v.options[:localize] rescue false)
attrs["#{k}_translations"] = attrs[k]
attrs.delete(k)
end
end
attrs.except("_type","_id","uid","updated_at", "created_at", "email_regex", "last_ticket_key")
end
def self.get_disp_fields_infos(only_display=true, cat=nil)
ask_setting = nil
if cat.present?
ask_setting = AskCategorySetting.enabled.where(:category_id=>cat).first
end
ask_setting = self.first if ask_setting.nil?
ask_setting = self.create() if ask_setting.nil?
default_ask_setting = ask_setting.class == self ? ask_setting : self.first
disp_fields = self::All_default_fields
disp_fields_infos = {}
locale = I18n.locale.to_s
tmp = disp_fields[0...-4]
if only_display
tmp = tmp.select{|f| ask_setting.default_setting[f]}
end
tmp.each do |f|
disp_fields_infos[f] = {"trans"=>default_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],"key"=>k,"type"=>v["type"],"options"=>v["options"],"instructions"=>v["instructions"]}
end
ask_category_settings = []
if ask_setting.class == AskCategorySetting || cat.present?
ask_category_settings = []
else
ask_category_settings = AskCategorySetting.enabled.to_a
end
ask_category_settings.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],"key"=>k,"type"=>v["type"],"options"=>v["options"],"instructions"=>v["instructions"]}
end
end
tmp = disp_fields[-4..-1]
if only_display
tmp = tmp.select{|f| ask_setting.default_setting[f] || f == 'usage_rule'}
end
tmp.each do |f|
disp_fields_infos[f] = {"trans"=>default_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"]}.compact.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 + h["key"].to_i
else
h["sort_number"] = h["sort_number"]
end
end
disp_fields_infos = disp_fields_infos.sort_by {|_key, value| value["sort_number"]}
end
disp_fields_infos
end
end