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'] No_prompt_word = ['ask_category_id', 'sex', 'appointment', 'recaptcha'] No_required = ['agree_show'] 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 before_save do if self.sort_number_changed? self.sort_number = self.sort_number.map{|k,v| [k,v.to_i]}.sort_by{|k,v| v}.to_h end @email_regex_enable_changed = self.email_regex_enable_changed? @email_regex_changed = self.email_regex_changed? @default_setting_prompt_word_changed = self.default_setting_prompt_word_changed? @default_setting_field_name_changed = self.default_setting_field_name_changed? @custom_fields_changed = self.custom_fields_changed? if @custom_fields_changed @delete_custom_fields = self.custom_fields.select{|k,v| v['delete'] == true}.keys @delete_custom_fields.each{|f| self.sort_number.delete(f)} self.custom_fields = self.custom_fields.select{|k,v| v['delete'] != true} end true end after_save do update_all_hash = {} if @email_regex_enable_changed || @email_regex_changed update_all_hash[:email_regex_enable] = self.email_regex_enable update_all_hash[:email_regex] = self.email_regex end if @default_setting_prompt_word_changed update_all_hash[:default_setting_prompt_word] = self.default_setting_prompt_word end if @default_setting_field_name_changed update_all_hash[:default_setting_field_name] = self.default_setting_field_name end if update_all_hash.present? AskCategorySetting.all.update_all(update_all_hash) end if @custom_fields_changed AskCategorySetting.all.each do |a| a.delete_customs_func(@delete_custom_fields) a.update_need_check_customs(self.custom_fields) end end 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 is_required(k) self.default_setting_required[k] 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 def prompt_word(k,locale=I18n.locale.to_s,use_checkbox_trans=false) tmp = self.default_setting_prompt_word[k] (tmp&&tmp[locale]) ? tmp[locale] : self.field_name(k,locale) end def prompt_word_translations(k) I18n.available_locales.map{|v| [v.to_s,self.prompt_word(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: false,agree_usage: true} field :default_setting_field_name, type: Hash,default: {} field :default_setting_prompt_word, type: Hash,default: {} field :custom_fields, type: Hash, default: {} field :usage_rule, type: String, default: '' field :title_layout, type: Integer, default: 0 field :last_ticket_key, type: Integer, default: 0 field :last_serial_number, 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", "default_setting_required", "last_serial_number") end def self.update_last_serial_number(last_serial_number=nil) can_update_shared_hash = (defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash) if last_serial_number.nil? last_serial_number = (can_update_shared_hash ? OrbitHelper::SharedHash["ask"][:last_serial_number] : AskSetting.pluck(:last_serial_number)[0].to_i) AskSetting.all.inc({'last_serial_number'=>1}) last_serial_number += 1 else AskSetting.all.update_all(:last_serial_number=>last_serial_number) end if can_update_shared_hash OrbitHelper::SharedHash["ask"][:last_serial_number] = last_serial_number end last_serial_number 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 if !only_display || ask_setting.default_setting['title'] != false disp_fields_infos['title'] = {"trans"=>ask_setting.field_name('title'), "sort_number"=>ask_setting.sort_number['title'].to_i} end 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| if trans[l].present? trans = trans[l] break end 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) rescue nil if cat.nil? c.destroy next end c.custom_fields.each do |k,v| trans = v["field"] tmp_locales.each do |l| if trans[l].present? trans = trans[l] break end end next if trans.class != String next if k.start_with?('default@') 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_with_index do |(k,h), i| if h["sort_number"].blank? h["sort_number"] = max_sort_number + 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 def get_email_regex(frontend=false) tmp = self.email_regex if tmp tmp = tmp.gsub("\\A","^").gsub("\\z","$").gsub("\\","\\\\\\\\") end tmp = nil if !(self.email_regex_enable) || (self.application_form_signup_field_sets.where(:field_name=>'email').pluck(:disabled)[0] rescue false) tmp end end