diff --git a/app/assets/stylesheets/ask/ask.css b/app/assets/stylesheets/ask/ask.css index dd0d2b5..6512d30 100644 --- a/app/assets/stylesheets/ask/ask.css +++ b/app/assets/stylesheets/ask/ask.css @@ -50,9 +50,13 @@ border-top: 1px solid #d7dadb; /*padding: 1em 0 0 20em;*/ } -.ask-question-form .form-actions input, .ask-question-form .form-actions a{ +.ask-question-form .form-actions input, .ask-question-form .form-actions a { float: right; } +.ask-question-form .form-actions [type="reset"] { + float: right; + margin-right: 1em; +} /*驗證碼*/ .text-error { color: #b94a48; diff --git a/app/controllers/admin/asks_controller.rb b/app/controllers/admin/asks_controller.rb index d0101eb..6a4f6cc 100644 --- a/app/controllers/admin/asks_controller.rb +++ b/app/controllers/admin/asks_controller.rb @@ -26,7 +26,6 @@ class Admin::AsksController < OrbitAdminController @set = AskCategoryPrintSetting.where(:category_id=>@category_id).first @set = AskPrintSetting.first if @set.nil? end - @set = AskPrintSetting.create if @set.nil? @url = print_setting_save_admin_asks_path end def print_setting_save @@ -145,7 +144,6 @@ class Admin::AsksController < OrbitAdminController end end def index - AskPrintSetting.create() if AskPrintSetting.first.nil? @ask_setting = AskSetting.first prepare_category_select category_id = @category_id.present? ? @category_id : nil @@ -486,8 +484,9 @@ class Admin::AsksController < OrbitAdminController 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 params[:category].present? if ask_setting.nil? + use_default = true default_setting = AskSetting.first ask_setting = AskCategorySetting.create(category_id: params[:category]) p1 = default_setting.get_attrs @@ -496,50 +495,58 @@ class Admin::AsksController < OrbitAdminController 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) + ask_setting.update(:use_default => use_default) 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 - 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 - 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) + if use_default + ask_setting.update(:default_sort_number=> sort_number) + else + sort_number_was = AskSetting.get_sort_number_only(false, ask_setting, true).merge(ask_setting.sort_number.to_h) + ask_setting.update(:sort_number=> sort_number) + default_ask_setting = AskSetting.first + last_fields = ["recaptcha", "usage_rule", "agree_show", "agree_usage"] + default_sort_number = AskSetting.get_sort_number_only(false, default_ask_setting, true).select{|k,v| k.exclude?('@') && last_fields.exclude?(k)}.map.with_index{|(k,v), i| [k, i]}.to_h.merge(default_ask_setting.sort_number) + ask_setting_id = ask_setting.id.to_s + sort_number_changed = (sort_number.map.with_index{|(k,v), i| [k,i]} - sort_number_was.map.with_index{|(k,v), i| [k,i]}).to_h + part_sort_number = sort_number_changed.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 = part_sort_number.keys & default_sort_number.keys + non_intersect_keys = (part_sort_number.keys - default_sort_number.keys).select{|k| k.exclude?('default@')} + if intersect_keys.count != 0 + 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) 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) + original_sort_info = AskSetting.get_sort_number_only(false, ask_setting, true) + sort_number_was = original_sort_info.merge(ask_setting.sort_number.to_h) + sort_number_changed = (sort_number.map.with_index{|(k,v), i| [k,i]} - sort_number_was.map.with_index{|(k,v), i| [k,i]}).to_h + ask_setting.update(:sort_number=>sort_number) unless use_default - default_customs_sort_number = sort_number.select{|k, v| k.start_with?('default@')}.map{|k,v| ["default@#{k}",v]} + default_customs_sort_number = sort_number_changed.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 + last_fields = ["recaptcha", "usage_rule", "agree_show", "agree_usage"] 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 + sort_number = AskSetting.get_sort_number_only(false, ask_setting, true).select{|k,v| last_fields.exclude?(k)}.map.with_index{|(k,v), i| [k, i]}.to_h.merge(ask_setting.sort_number) 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 + intersect_keys = part_sort_number.keys & sort_number.keys non_intersect_keys = part_sort_number.keys - sort_number.keys if intersect_keys.count != 0 org_order = intersect_keys.map{|k| [k, sort_number[k]]}.sort_by{|k,v| v}.to_h diff --git a/app/controllers/asks_controller.rb b/app/controllers/asks_controller.rb index 6a59827..b7348cd 100644 --- a/app/controllers/asks_controller.rb +++ b/app/controllers/asks_controller.rb @@ -38,19 +38,18 @@ class AsksController < ApplicationController end if ask_setting.nil? ask_setting = AskSetting.first - ask_setting = AskSetting.create() if ask_setting.nil? @default_ask_setting = ask_setting else @default_ask_setting = AskSetting.first end - defalt_fields = cal_form_from_setting(ask_setting,categories) + all_fields = cal_form_from_setting(ask_setting,categories) request = OrbitHelper.request csrf_value = OrbitHelper.request.session[:_csrf_token] || SecureRandom.base64(32) token_tag = hidden_field_tag('authenticity_token',csrf_value) switch_form = cal_switch_form(ask_settings,categories,token_tag,referer_url) email_regex = @default_ask_setting.get_email_regex(true) { - 'fields' => defalt_fields, + 'fields' => all_fields, 'extras'=>{ 'switch_form' => switch_form, 'email_regex' => email_regex @@ -230,7 +229,7 @@ class AsksController < ApplicationController end if tmp.present? if is_date && v["range_flag"] == 'true' - if tmp.class != Array + if (tmp.class != Array) || (tmp.select{|v| v.blank?}.count != 0) flag = false break end @@ -265,7 +264,7 @@ class AsksController < ApplicationController end end if (email_regex ? ::Regexp.new(email_regex).match(temp_params["mail"].to_s) : true) && flag - @disp_fields_infos = AskSetting.get_disp_fields_infos(true, ask_setting.class == AskCategorySetting ? ask_setting.category_id : nil) + @disp_fields_infos = AskSetting.get_disp_fields_infos(true, ask_setting, true, nil, true) @usage_rule = ask_setting.usage_rule @ask_question.update_attributes(temp_params) build_email(@ask_question) @@ -332,14 +331,28 @@ class AsksController < ApplicationController end private def cal_form_from_setting(ask_setting,categories,show_categories=false,filter_fields=nil) - disp_fields_infos = AskSetting.get_disp_fields_infos(true, ask_setting.class == AskCategorySetting ? ask_setting.category_id : nil) + is_cat_record = (ask_setting.class == AskCategorySetting) + override_sort_number = nil + if is_cat_record + category_id = ask_setting.category_id + if ask_setting.use_default + override_sort_number = ask_setting.default_sort_number + ask_setting = @default_ask_setting + if @default_fields + return @default_fields.sort_by{|h| override_sort_number[h['field']].to_i} + end + is_cat_record = false + end + else + category_id = (categories[0].id.to_s rescue '') + end + tmp_ask_cat_id = (is_cat_record ? ask_setting.id.to_s : '') + disp_fields_infos = AskSetting.get_disp_fields_infos(true, ask_setting, true, override_sort_number, true) if filter_fields disp_fields_infos = disp_fields_infos.select{|field, info| filter_fields.include?(field)} end # tmp = 'mongoid.attributes.ask_question' - defalt_fields = [] - tmp_type = (ask_setting.class == AskSetting) ? 0 : 1 - tmp_ask_cat_id = (tmp_type == 1) ? ask_setting.id.to_s : "" + all_fields = [] set_input_name_for_ask("ask_question") has_ask_category_id = false disp_fields_infos.each do |field, info| @@ -349,17 +362,17 @@ class AsksController < ApplicationController if v field_name = info['trans'] if field_name.present? - defalt_fields << {'label'=>ask_label(field_name, v['required']),'content'=>show_on_front(k,v)} + all_fields << {'field'=>field,'label'=>ask_label(field_name, v['required']),'content'=>show_on_front(k,v)} end end - elsif field.start_with?("custom@") && tmp_ask_cat_id != "" - if tmp_type == 1 && field.include?(tmp_ask_cat_id) + elsif field.start_with?("custom@") && is_cat_record + if field.include?(tmp_ask_cat_id) k = field[8+tmp_ask_cat_id.length..-1] v = ask_setting.custom_fields[k] if v field_name = info['trans'] if field_name.present? - defalt_fields << {'label'=>ask_label(v['field'][I18n.locale],v['required']),'content'=>show_on_front(k,v)} + all_fields << {'field'=>field,'label'=>ask_label(v['field'][I18n.locale],v['required']),'content'=>show_on_front(k,v)} end end end @@ -368,12 +381,12 @@ class AsksController < ApplicationController has_ask_category_id = true if show_categories || ask_setting.default_setting[field] if categories.count > 1 - defalt_fields << {'style_html'=>'','label'=>ask_label(@default_ask_setting.field_name("ask_category_id"),'true'),'content'=>select_tag('ask_question[category_id]', options_for_select(categories.collect{|t| [ t.title, t.id ]}))} + all_fields << {'field'=>field,'style_html'=>'','label'=>ask_label(@default_ask_setting.field_name("ask_category_id"),'true'),'content'=>select_tag('ask_question[category_id]', options_for_select(categories.collect{|t| [ t.title, t.id ]}))} else - defalt_fields << {'style_html'=>'','label'=>ask_label(@default_ask_setting.field_name("ask_category_id"),'true'),'content'=>"#{(categories[0].title rescue '')}"+hidden_field_tag('ask_question[category_id]', (categories[0].id.to_s rescue ''))} + all_fields << {'field'=>field,'style_html'=>'','label'=>ask_label(@default_ask_setting.field_name("ask_category_id"),'true'),'content'=>"#{(categories[0].title rescue '')}"+hidden_field_tag('ask_question[category_id]', (categories[0].id.to_s rescue ''))} end else - defalt_fields << {'style_html'=>'display: none;','label'=>'','content'=>hidden_field_tag('ask_question[category_id]', (categories[0].id.to_s rescue ''))} + all_fields << {'field'=>field,'style_html'=>'display: none;','label'=>'','content'=>hidden_field_tag('ask_question[category_id]', (categories[0].id.to_s rescue ''))} end else if ask_setting.default_setting[field] || field == "usage_rule" @@ -414,15 +427,19 @@ class AsksController < ApplicationController when 'agree_usage' f1['content'] = check_box_tag('ask_question[agree_usage]', "1", false, {required: required}) end - defalt_fields << f1 + f1['field'] = field + all_fields << f1 end end end end unless has_ask_category_id - defalt_fields << {'style_html'=>'display: none;','label'=>'','content'=>hidden_field_tag('ask_question[category_id]', (tmp_type == 1 ? ask_setting.category_id : (categories[0].id.to_s rescue '')))} + all_fields << {'field'=>'ask_category_id','style_html'=>'display: none;','label'=>'','content'=>hidden_field_tag('ask_question[category_id]', category_id)} end - defalt_fields + unless is_cat_record + @default_fields = all_fields + end + all_fields end def cal_html(fields,token_tag,form_id,referer_url,is_hidden=true,ask_setting=None) form_url = "/#{I18n.locale.to_s}/asks" diff --git a/app/helpers/admin/asks_helper.rb b/app/helpers/admin/asks_helper.rb index 97555ba..358c6fb 100644 --- a/app/helpers/admin/asks_helper.rb +++ b/app/helpers/admin/asks_helper.rb @@ -50,7 +50,7 @@ module Admin::AsksHelper end.join.html_safe end end - def multiple_lang_show_tag_for_ask(field_name, index1,field,value=nil,combine_element='',exteral_options={},sortable=false) + def multiple_lang_show_tag_for_ask(field_name, index1,field,value=nil,combine_element='',exteral_options={},sortable=false, extra_tr_class="",only_contents=false) if !index1.nil? all_field = (get_input_name_for_ask + "[#{index1}][#{field}][parant]").gsub(/[\[@]/,'_').gsub(/\]/,'') else @@ -85,7 +85,11 @@ module Admin::AsksHelper tmp end end - tmp = "