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'] 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 need_recalc_sort = true else need_recalc_sort = false 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} need_recalc_sort = true end if need_recalc_sort self.recalc_sort(false) 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 use_default false end def migrate_old unless 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.recalc_sort(true) 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") : ['serial_number','situation','ip'].include?(k) ? I18n.t("ask.#{k}") : 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 :tmp_sort_number, type: Hash, default: {} 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 recalc_sort(save_flag=true) self.tmp_sort_number = AskSetting.get_sort_number_only(true, self, true).map.with_index{|(k,v), i| [k,i]}.to_h self.save if save_flag end def get_cache_sort_number #For Frontend if self.tmp_sort_number.blank? self.recalc_sort end self.tmp_sort_number end 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] : self.pluck(:last_serial_number)[0].to_i) self.all.inc({'last_serial_number'=>1}) last_serial_number += 1 else self.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_sort_number_only(only_display=true, cat=nil, cat_is_record=false, override_sort_number=nil, use_cache=false) ask_setting = nil if cat_is_record ask_setting = cat is_cat_record = !(override_sort_number.nil?) || cat.class == AskCategorySetting else if cat.present? ask_setting = AskCategorySetting.enabled.where(:category_id=>cat).first is_cat_record = true end ask_setting = self.first if ask_setting.nil? ask_setting = self.create() if ask_setting.nil? end if only_display && use_cache if override_sort_number sort_number = override_sort_number else sort_number = ask_setting.get_cache_sort_number end return sort_number end is_default_setting = (ask_setting.class == self) if override_sort_number.present? sort_number = override_sort_number else sort_number = ask_setting.sort_number if ask_setting.use_default if ask_setting.default_sort_number.blank? sort_number = (is_default_setting ? sort_number : self.first.sort_number) else sort_number = ask_setting.default_sort_number end override_sort_number = sort_number ask_setting = self.first end end default_ask_setting = is_default_setting ? 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'] = 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] = 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| tmp_k = (k.start_with?('default@') ? k : "default@#{k}") disp_fields_infos[tmp_k] = sort_number[tmp_k] end ask_category_settings = [] if is_cat_record 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| next if k.start_with?('default@') tmp_k = "custom@#{c.id}@#{k}" cat_title = cat.title rescue "" disp_fields_infos[tmp_k] = sort_number[tmp_k] 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] = sort_number[f] end max_sort_number = disp_fields_infos.values.map{|v| v}.compact.max if max_sort_number if !use_cache || (is_default_setting && override_sort_number.nil?) max_sort_number = max_sort_number + 1 disp_fields_infos.each_with_index do |(k,v), i| if v.blank? disp_fields_infos[k] = max_sort_number + i end end end disp_fields_infos = disp_fields_infos.sort_by {|_key, value| value}.to_h end disp_fields_infos end def self.get_disp_fields_infos(only_display=true, cat=nil, cat_is_record=false, override_sort_number=nil, use_cache=false) ask_setting = nil if cat_is_record ask_setting = cat is_cat_record = !(override_sort_number.nil?) || cat.class == AskCategorySetting else if cat.present? ask_setting = AskCategorySetting.enabled.where(:category_id=>cat).first is_cat_record = true end ask_setting = self.first if ask_setting.nil? ask_setting = self.create() if ask_setting.nil? end is_default_setting = (ask_setting.class == self) sort_number = nil if only_display && use_cache if override_sort_number sort_number = override_sort_number else sort_number = ask_setting.get_cache_sort_number end else use_cache = false sort_number = ask_setting.sort_number if override_sort_number.present? sort_number = override_sort_number elsif ask_setting.use_default if ask_setting.default_sort_number.blank? sort_number = (is_default_setting ? sort_number : self.first.sort_number) else sort_number = ask_setting.default_sort_number end override_sort_number = sort_number ask_setting = self.first end end default_ask_setting = is_default_setting ? 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"=>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"=>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 = (k.start_with?('default@') ? k : "default@#{k}") disp_fields_infos[tmp_k] = {"trans"=>trans,"sort_number"=>sort_number[tmp_k],"key"=>k,"type"=>v["type"],"options"=>v["options"],"instructions"=>v["instructions"]} end ask_category_settings = [] if is_cat_record 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"=>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"=>sort_number[f]} end max_sort_number = disp_fields_infos.values.map{|h| h["sort_number"]}.compact.max if max_sort_number if !use_cache || (is_default_setting && override_sort_number.nil?) 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 end end end disp_fields_infos = disp_fields_infos.sort_by {|_key, value| value["sort_number"]}.to_h 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