fix some error

This commit is contained in:
邱博亞 2022-03-10 01:00:05 +08:00
parent ca55f99f53
commit dbb2ddd3f6
9 changed files with 87 additions and 35 deletions

View File

@ -124,6 +124,7 @@ class Admin::AsksController < OrbitAdminController
Tag.find(tag).name_translations{}
end
def category_setting
@default_ask_setting = AskSetting.first
@categories = @module_app.categories
@category_ids = @categories.pluck(:id)
@category_titles = @categories.pluck(:title).collect{|v| v[I18n.locale]}
@ -387,7 +388,7 @@ class Admin::AsksController < OrbitAdminController
def ask_setting_params(ask_setting,collection_name)
param = params.require(collection_name).permit!
param_clone = param.clone
param_clone['default_setting'].each { |k, v| param_clone['default_setting'][k] = (v == 'true'? true : false) }
param_clone['default_setting'].each { |k, v| param_clone['default_setting'][k] = (v == 'true'? true : false) if param_clone['default_setting'][k].class==String}
param_clone.delete('custom_fields')
custom_fields = ask_setting['custom_fields'].clone
params_custom_fields = (params.require(collection_name).require('custom_fields') rescue {})

View File

@ -25,6 +25,9 @@ class AsksController < ActionController::Base
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)
request = OrbitHelper.request
@ -125,12 +128,15 @@ class AsksController < ActionController::Base
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
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)
tags = module_app.tags
ask_question = AskQuestion.new
ask_question = AskQuestion.new(id: nil)
{
'layout_type' => 'index',
'ask_question' => ask_question,
@ -138,7 +144,8 @@ class AsksController < ActionController::Base
'tags' => tags,
'module_app' => module_app,
'switch_form' => switch_form,
'ask_setting' => ask_setting
'ask_setting' => ask_setting,
'default_ask_setting' => @default_ask_setting
}
elsif @layout_type == 'published_index'
page_number = OrbitHelper.page_number.to_i
@ -252,33 +259,33 @@ class AsksController < ActionController::Base
defalt_fields = [{'style_html'=>'','label'=>ask_label(t('title'),'true'),'content'=>text_field_tag('ask_question[title]',nil,placeholder: t('title'),required: true)}]
if show_categories || ask_setting.default_setting['ask_category_id']
if categories.count > 1
defalt_fields = defalt_fields + [{'style_html'=>'','label'=>ask_label(t("#{tmp}.ask_category_id"),'true'),'content'=>select_tag('ask_question[category_id]', options_for_select(categories.collect{|t| [ t.title, t.id ]}))}]
defalt_fields = 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 ]}))}]
else
defalt_fields = defalt_fields + [{'style_html'=>'','label'=>ask_label(t("#{tmp}.ask_category_id"),'true'),'content'=>"<span>#{(categories[0].title rescue '')}</span>"+hidden_field_tag('ask_question[category_id]', (categories[0].id.to_s rescue ''))}]
defalt_fields = defalt_fields + [{'style_html'=>'','label'=>ask_label(@default_ask_setting.field_name("ask_category_id"),'true'),'content'=>"<span>#{(categories[0].title rescue '')}</span>"+hidden_field_tag('ask_question[category_id]', (categories[0].id.to_s rescue ''))}]
end
else
defalt_fields = defalt_fields + [{'style_html'=>'display: none;','label'=>'','content'=>hidden_field_tag('ask_question[category_id]', (categories[0].id.to_s rescue ''))}]
end
defalt_fields = defalt_fields + ['name','sex','mail','phone','appointment'].select{|v| ask_setting.default_setting[v]}.collect do |field|
required = ['phone','appointment'].exclude?(field) ? 'true' : 'false'
f1 = {'style_html'=>'','label'=>ask_label(t("#{tmp}.#{field}"),required)}
f1 = {'style_html'=>'','label'=>ask_label(@default_ask_setting.field_name("#{field}"),required)}
field_name = "ask_question[#{field}]"
case field
when 'name'
f1['content'] = text_field_tag(field_name,nil,data: {"fv-validation" => "required;", "fv-messages" => "必填欄位;"},required: true,placeholder:t("#{tmp}.#{field}"))
f1['content'] = text_field_tag(field_name,nil,data: {"fv-validation" => "required;", "fv-messages" => "必填欄位;"},required: true,placeholder:@default_ask_setting.field_name("#{field}"))
when 'sex'
f1['content'] = "<label class=\"radio-inline\">
#{radio_button(field_name,'male')}#{I18n.t('users.male')}
#{radio_button_tag(field_name,'male')}#{I18n.t('users.male')}
</label>
<label class=\"radio-inline\">
#{radio_button(field_name,'female')}#{I18n.t('users.female')}
#{radio_button_tag(field_name,'female')}#{I18n.t('users.female')}
</label>"
when 'mail'
f1['content'] = text_field_tag(field_name,nil,data: {"fv-validation" => "required;check_email;", "fv-messages" => "必填欄位;Email不正確;"},required: true,placeholder:t("#{tmp}.#{field}"))
f1['content'] = text_field_tag(field_name,nil,data: {"fv-validation" => "required;check_email;", "fv-messages" => "必填欄位;Email不正確;"},required: true,placeholder:@default_ask_setting.field_name("#{field}"))
when 'phone'
f1['content'] = text_field_tag(field_name,nil,placeholder:t("#{tmp}.#{field}"))
f1['content'] = text_field_tag(field_name,nil,placeholder:@default_ask_setting.field_name("#{field}"))
when 'appointment'
f1['content'] = "<div class=\"default_picker\">#{text_field_tag(field_name,nil,placeholder:t("#{tmp}.#{field}"))}</div>"
f1['content'] = "<div class=\"default_picker\">#{text_field_tag(field_name,nil,placeholder:@default_ask_setting.field_name("#{field}"))}</div>"
end
f1
end
@ -337,18 +344,19 @@ class AsksController < ActionController::Base
if (id != $(this).parents('form').eq(0).attr('id')){
var self = $(this)
var old_controls = self.parents('form').eq(0).find('.control-group')
var old_inputs = old_controls.find('label').map(function(){
var old_inputs = old_controls.children('label').map(function(){
return $.trim($(this).text().replace(/\\\*/g,''));
}).get()
var new_controls = self.parents('.ask-question').eq(0).find('form#'+id).find('.control-group')
var new_inputs = new_controls.find('label').map(function(){
var new_inputs = new_controls.children('label').map(function(){
return $.trim($(this).text().replace(/\\\*/g,''));
}).get()
$(new_inputs).each(function(i,v){
var i0 = old_inputs.indexOf(v)
if (i0 != -1 && new_controls.eq(i).find('select#ask_question_category_id').length==0){
new_controls.eq(i).find('.controls').eq(0).before(old_controls.eq(i0).find('.controls').clone())
new_controls.eq(i).find('.controls').eq(-1).remove()
var new_ = new_controls.eq(i).find('.controls').eq(0),
old_ = old_controls.eq(i0).find('.controls').eq(0);
old_.clone().replaceAll(new_);
}
})
self.parents('.ask-question').eq(0).find('form').hide()

View File

@ -207,7 +207,7 @@ module Admin::AsksHelper
file_path = value[1] rescue nil
file_required = v['required']=='true' ? 'required="required"' : ''
readonly ? "<a href=\"#{file_path}\" title=\"file\">#{file_value}</a>" : "<div class=\"file-selector\"><label class=\"ui-button\">
<input data-src=\"#{file_path}\" style=\"position: absolute;width:0.00000001em;\" class=\"upload\" name=\"#{field_name}\" #{file_required} type=\"file\" value=\"#{file_value}\"/>
<input data-src=\"#{file_path}\" style=\"position: absolute;width:0.00000001em;opacity: 0;\" class=\"upload\" name=\"#{field_name}\" #{file_required} type=\"file\" value=\"#{file_value}\"/>
<i class=\"fa fa-file\"></i> #{t('ask.upload_file')}
</label><div class=\"filename\"></div></div>"
when 'image'

View File

@ -22,7 +22,21 @@ class AskCategorySetting
tmp = super
tmp.select{|k,v| v['delete'] != true}
end
def field_name(k,locale=I18n.locale.to_s)
(self.default_setting_field_name[k]&&self.default_setting_field_name[k][locale]) ? self.default_setting_field_name[k][locale] : self.default_field_name(k,locale)
end
def default_field_name(k,locale=I18n.locale.to_s)
I18n.with_locale(locale){(['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 :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}
field :default_setting_field_name, type: Hash,default: {}
field :custom_fields, type: Hash,default: {}
field :usage_rule
field :category_id

View File

@ -22,7 +22,21 @@ class AskSetting
tmp = super
tmp.select{|k,v| v['delete'] != true}
end
def field_name(k,locale=I18n.locale.to_s)
(self.default_setting_field_name[k]&&self.default_setting_field_name[k][locale]) ? self.default_setting_field_name[k][locale] : self.default_field_name(k,locale)
end
def default_field_name(k,locale=I18n.locale.to_s)
I18n.with_locale(locale){(['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 :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}
field :default_setting_field_name, type: Hash,default: {}
field :custom_fields, type: Hash,default: {}
field :usage_rule
end

View File

@ -90,7 +90,10 @@
border-top: 0.1em solid #099700;
}
</style>
<% tmp_get_input_name = get_input_name
form_type = tmp_get_input_name.split('[')[0]
set_input_name("#{form_type}")
%>
<fieldset>
<table>
<thead>
@ -101,19 +104,30 @@
<td>
<%= t('ask.whether_open') %>
</td>
<% if form_type=='ask_setting' %>
<td>
<%= t('ask.field_name') %>
</td>
<% end %>
</tr>
</thead>
<tbody>
<% ask_setting.default_setting.each do |k,v| %>
<tr>
<td>
<%= ['agree_show','agree_usage'].include?(k.to_s) ? t("ask.#{k}_checkbox") : t("mongoid.attributes.ask_question.#{k}") %>
<%= tmp = (form_type=='ask_setting' ? ask_setting.default_field_name(k) : @default_ask_setting.field_name(k)) %>
</td>
<td>
<%= f.select "default_setting[#{k}]",[[t('yes'),'true'],[t('no'),false]] ,selected: v %>
<%= select_tag "#{get_input_name}[default_setting][#{k}]",options_for_select([[t('yes'),'true'],[t('no'),false]],v) %>
</td>
<% if form_type=='ask_setting' %>
<td>
<%= multiple_lang_tag(nil,'text_field',"default_setting_field_name][#{k}",ask_setting.field_name_translations(k),{placeholder: tmp}) %>
</td>
<% end %>
</tr>
<% end %>
<% set_input_name(tmp_get_input_name) %>
<tr>
<td>
<%= t("ask.usage_rule") %>

View File

@ -6,6 +6,7 @@
@switch_form = data["switch_form"]
ask_setting = data["ask_setting"]
set_input_name('ask_question')
@default_ask_setting = data['default_ask_setting']
%>
<% if data['layout_type'] != 'index' %>
<%= render template: "asks/#{data['layout_type']}",:locals => { :data => data, :ask_setting => ask_setting } %>
@ -64,7 +65,7 @@
<%= stylesheet_link_tag "custom_field" %>
<%# stylesheet_link_tag "jquery-ui-timepicker-addon" %>
<script type="text/javascript">
/*
$(document).ready(function(){
$('select[name="ask_question[category_id]"]').change(function(){
var category_id = $(this).val()
@ -81,7 +82,7 @@
$(this).parents('.ask-question').eq(0).find('form#'+id).show()
}
})
})
})*/
var datepicker_fn = $.fn.datepicker
@ -108,7 +109,7 @@
<!-- Category -->
<% if ask_setting.default_setting['ask_category_id'] %>
<div class="control-group">
<%= f.label :ask_category_id, class: 'control-label required' %>
<%= f.label :ask_category_id, @default_ask_setting.field_name("ask_category_id"), class: 'control-label required' %>
<div class="controls">
<% if @categories.count > 1 %>
<%= f.select :category_id, @categories.collect{|t| [ t.title, t.id ]} %>
@ -126,7 +127,7 @@
<!-- 姓名 -->
<% if ask_setting.default_setting['name'] %>
<div class="control-group">
<%= f.label :name, class: 'control-label required' %>
<%= f.label :name, @default_ask_setting.field_name("name"), class: 'control-label required' %>
<div class="controls">
<%= f.text_field :name, data: {"fv-validation" => "required;", "fv-messages" => "必填欄位;"} %>
</div>
@ -135,7 +136,7 @@
<!-- 性別 -->
<% if ask_setting.default_setting['sex'] %>
<div class="control-group">
<%= f.label :sex, class: 'control-label required' %>
<%= f.label :sex,@default_ask_setting.field_name("sex"), class: 'control-label required' %>
<div class="controls">
<label class="radio-inline">
<%= f.radio_button :sex, "male" %><%= t('users.male')%>
@ -155,7 +156,7 @@
</div> -->
<!-- Email -->
<div class="control-group">
<%= f.label :mail, class: 'control-label required' %>
<%= f.label :mail,@default_ask_setting.field_name("mail"), class: 'control-label required' %>
<div class="controls">
<%= f.text_field :mail, data: {"fv-validation" => "required;check_email;", "fv-messages" => "必填欄位;Email不正確;"} %>
</div>
@ -163,7 +164,7 @@
<!-- 聯絡電話 -->
<% if ask_setting.default_setting['phone'] %>
<div class="control-group">
<%= f.label :phone, class: 'control-label' %>
<%= f.label :phone,@default_ask_setting.field_name("phone"), class: 'control-label' %>
<div class="controls">
<%= f.text_field :phone %>
</div>
@ -172,7 +173,7 @@
<!-- 預約日期 -->
<% if ask_setting.default_setting['appointment'] %>
<div class="control-group">
<%= f.label :appointment, class: 'control-label' %>
<%= f.label :appointment,@default_ask_setting.field_name("appointment"), class: 'control-label' %>
<div class="controls">
<%= f.datetime_picker :appointment %>
</div>
@ -211,7 +212,7 @@
end.join.html_safe %>
<% if ask_setting.default_setting['recaptcha'] %>
<div class="control-group">
<%= f.label :recaptcha, class: 'control-label' %>
<%= f.label :recaptcha,@default_ask_setting.field_name("recaptcha"), class: 'control-label' %>
<div class="controls">
<%= gotcha_error %>
<%= gotcha %>
@ -228,7 +229,7 @@
<% end %>
<% if ask_setting.default_setting['agree_usage'] %>
<div class="control-group">
<%= f.label :agree_usage, class: 'control-label' %>
<%= f.label :agree_usage,@default_ask_setting.field_name("agree_usage"), class: 'control-label' %>
<div class="controls">
<%= f.check_box :agree_usage %>
</div>
@ -236,7 +237,7 @@
<% end %>
<% if ask_setting.default_setting['agree_show'] %>
<div class="control-group">
<%= f.label :agree_show, class: 'control-label' %>
<%= f.label :agree_show,@default_ask_setting.field_name("agree_show"), class: 'control-label' %>
<div class="controls">
<%= f.check_box :agree_show %>
</div>

View File

@ -2,6 +2,7 @@ en:
situation: Situation
sex: Sex
ask:
field_name: Field Name
more_title: See more ask history
save_name: Save FileName
usage_rule: Usage rule
@ -54,7 +55,7 @@ en:
option_name: Option Name
field_name: Field Name
whether_open: Whether to open it
setting: Setting
setting: Default fields
ask: Ask
all_articles: All
exports: Export

View File

@ -2,12 +2,11 @@ zh_tw:
situation: 狀況
identity: 身份
module_name:
ask: 發問
recaptcha:
errors:
verification_failed: 驗證碼錯誤
ask:
field_show_name: 欄位名稱
more_title: 查看更多發問紀錄
save_name: 檔案儲存名稱
usage_rule: 使用規範
@ -59,7 +58,7 @@ zh_tw:
option_name: 選項名稱
field_name: 欄位名稱
whether_open: 是否開啟
setting: 設定
setting: 基本欄位設定
name: 發問者
appointment: 預約時間
created_at: 發問時間