200 lines
10 KiB
Ruby
200 lines
10 KiB
Ruby
module Admin::AsksHelper
|
|
def set_input_name(input_name)
|
|
@input_name = input_name
|
|
end
|
|
def get_input_name
|
|
@input_name
|
|
end
|
|
def create_lang_panel(field)
|
|
tmp2 = content_tag(:div,:class => 'btn-group', :data=>{:toggle=>"buttons-radio"}) do
|
|
I18n.available_locales.collect do |key|
|
|
link_entry_ary = ["##{field}","_#{key}"]
|
|
link_entry = link_entry_ary.join
|
|
link_to(I18n.t(key),link_entry,:data=>{:toggle=>"tab"},:class=>"btn #{(key == I18n.locale ? "active" : nil)}",:for=>key)
|
|
end.join.html_safe
|
|
end
|
|
end
|
|
def multiple_lang_tag(index1,type_of_tag,field,value=nil,custom_options={},combine_element='',exteral_options={})
|
|
content_tag(:div,{:class => "tab-panel"}.merge(exteral_options)) do
|
|
all_field = (get_input_name + "[#{index1}][#{field}][parant]").gsub(/\[/,'_').gsub(/\]/,'')
|
|
(I18n.available_locales.collect do |locale|
|
|
active_flag = ((locale == I18n.locale) ? ' active' : '')
|
|
content_tag(:div,:class => "tab-content#{active_flag}",:id=>"#{all_field}_#{locale}") do
|
|
value_locale = value[locale.to_s] rescue nil
|
|
self.__send__("#{type_of_tag}_tag","#{get_input_name}[#{index1}][#{field}][#{locale}]",value_locale,custom_options)
|
|
end
|
|
end.join + create_lang_panel(all_field)).html_safe + combine_element
|
|
end
|
|
end
|
|
def time_setting_block(key,value={})
|
|
class_block = (value['type'] != 'date') ? "time_setting_block" : "time_setting_block active"
|
|
format_selected = (value['type'] != 'date') ? nil : value['format']
|
|
range_selected = (value['type'] != 'date') ? nil : value['range_flag']
|
|
options1 = [['YYYY / MM / DD HH:mm','format1'],['YYYY / MM / DD','format2'],['YYYY / MM','format3'],['YYYY','format4']]
|
|
options2 = [[t('yes'),'true'],[t('no'),'false']]
|
|
format_setting_tag = field_select_tag(key,'format',options1,format_selected)
|
|
range_setting_tag = field_select_tag(key,'range_flag',options2,range_selected)
|
|
"<div class=\"#{class_block}\">
|
|
<table>
|
|
<tbody>
|
|
<tr>
|
|
<td>#{t('ask.format')}:</td><td>#{format_setting_tag}</td>
|
|
</tr>
|
|
<tr>
|
|
<td>#{t('ask.enable_range_setting')}:</td><td>#{range_setting_tag}</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>"
|
|
end
|
|
def field_select_tag(index1,field,options,selected=nil,custom_options={})
|
|
select_tag("#{get_input_name}[#{index1}][#{field}]",options_for_select(options,selected: selected),custom_options)
|
|
end
|
|
def field_radio_button_tag(index1,field,show_value,value,custom_options={})
|
|
radio_button_tag("#{get_input_name}[#{index1}][#{field}]",show_value,value)
|
|
end
|
|
def render_date_block(field_name,v,i,value)
|
|
case v['format']
|
|
when 'format1'
|
|
t1 = text_field_tag("#{field_name}[datetime][date][#{i}]",(value['datetime']['date']["#{i}"] rescue nil),{:required => v['required']=='true',autocomplete: 'off',placeholder: t('ask.datepicker'),style:'width:100%;'})
|
|
#t2_value = value['datetime']['h']["#{i}"] rescue nil
|
|
#t3_value = value['datetime']['m']["#{i}"] rescue nil
|
|
#t2 = select_tag("#{field_name}[datetime][h][#{i}]",options_for_select((1..24).collect{|v1| v1.to_s.rjust(2, "0")},selected: t2_value))
|
|
#t3 = select_tag("#{field_name}[datetime][m][#{i}]",options_for_select((0..59).collect{|v1| v1.to_s.rjust(2, "0")},selected: t3_value))
|
|
#t4 = "#{t2}<b style=\"padding: 0 0.1em;\">:</b>#{t3}"
|
|
"<div class=\"datetime_selector\">#{t1}</div>
|
|
<script>
|
|
jQuery(document).ready(function( $ ) {
|
|
$(\"input[name='#{field_name}[datetime][date][#{i}]']\").ui_datetimepicker({controlType: 'select',timeInput: true,oneLine: true,timeFormat: 'HH:mm'});
|
|
})
|
|
</script>".html_safe
|
|
when 'format2'
|
|
t1 = text_field_tag("#{field_name}[datetime][date][#{i}]",(value['datetime']['date']["#{i}"] rescue nil),{:required => v['required']=='true',autocomplete: 'off',placeholder: t('ask.datepicker')})
|
|
"<div class=\"datetime_selector\">#{t1}</div>
|
|
<script>
|
|
jQuery(document).ready(function( $ ) {
|
|
$(\"input[name='#{field_name}[datetime][date][#{i}]']\").ui_datepicker();
|
|
})
|
|
</script>".html_safe
|
|
when 'format3'
|
|
t1 = text_field_tag("#{field_name}[datetime][date][#{i}]",(value['datetime']['date']["#{i}"] rescue nil),{:required => v['required']=='true',autocomplete: 'off',placeholder: t('ask.datepicker'),:class => 'yearpicker'})
|
|
"#{t1}
|
|
<script>
|
|
jQuery(document).ready(function( $ ) {
|
|
$(\"input[name='#{field_name}[datetime][date][#{i}]']\").ui_datepicker({dateFormat: 'yy-mm',show_view: 'month'});
|
|
})
|
|
</script>
|
|
"
|
|
when 'format4'
|
|
t1 = text_field_tag("#{field_name}[datetime][date][#{i}]",(value['datetime']['date']["#{i}"] rescue nil),{:required => v['required']=='true',autocomplete: 'off',placeholder: t('ask.datepicker'),:class => 'yearpicker'})
|
|
"#{t1}
|
|
<script>
|
|
jQuery(document).ready(function( $ ) {
|
|
$(\"input[name='#{field_name}[datetime][date][#{i}]']\").ui_datepicker({dateFormat: 'yy',show_view: 'year'});
|
|
})
|
|
</script>
|
|
"
|
|
end
|
|
end
|
|
def show_on_front(k,v,value=nil)
|
|
field_name = "#{get_input_name}[custom_values][#{k}]"
|
|
begin
|
|
case v['type']
|
|
when 'text_field'
|
|
text_field_tag(field_name,value,{:required => v['required']=='true',placeholder: v['prompt_word'][I18n.locale]})
|
|
when 'select'
|
|
prompt_hash = v['prompt_word'][I18n.locale].blank? ? {} : {prompt: v['prompt_word'][I18n.locale]}
|
|
prompt_hash.merge(:required => v['required']=='true')
|
|
select_tag(field_name,options_for_select(Array(v['options']).select{|index1,option| option['disabled'] != 'true'}.collect{|index1,option| [option[I18n.locale],index1]},selected: value),prompt_hash)
|
|
when 'date'
|
|
if v['range_flag']=='true'
|
|
"<div style=\"display:inline-flex;flex-direction:column;\">
|
|
#{render_date_block(field_name,v,0,value)}
|
|
<div style=\"display: inline-flex;align-items: center;writing-mode: tb-rl;padding: 0.3em;\">~</div>
|
|
#{render_date_block(field_name,v,1,value)}
|
|
</div>".html_safe
|
|
else
|
|
render_date_block(field_name,v,0,value)
|
|
end
|
|
when 'text_area'
|
|
text_area_tag(field_name,value,{:required => v['required']=='true',:placeholder=> v['prompt_word'][I18n.locale],:class=>'ckeditor'})
|
|
when 'radio_button'
|
|
if value.nil?
|
|
value = {}
|
|
end
|
|
Array(v['options']).select{|index1,option| option['disabled'] != 'true'}.collect do |index1,option|
|
|
"#{radio_button_tag(field_name,index1,value==index1,{:required => v['required']=='true'})}#{option[I18n.locale]}"
|
|
end.join
|
|
when 'checkbox'
|
|
if value.nil?
|
|
value = {}
|
|
end
|
|
Array(v['options']).select{|index1,option| option['disabled'] != 'true'}.collect do |index1,option|
|
|
"#{check_box_tag("#{field_name}[#{index1}]",index1,value[index1]==index1)}#{option[I18n.locale]}"
|
|
end.join
|
|
end
|
|
rescue => e
|
|
debug e.inspect
|
|
end
|
|
end
|
|
def custom_field_block(k,v={})
|
|
set_input_name('ask_setting[custom_fields]')
|
|
markups = LIST[:markups].select{|k,v| k != 'member_relationship' && k != 'address'}.collect{|key,val| [t("lists.markups."+key),key]}
|
|
multi_lang_tag = multiple_lang_tag(k,'text_field','field',v['field'],{placeholder: t('ask.field_name')})
|
|
field_select_tag = field_select_tag(k,'type',markups,v['type'],{:onchange=>'check_available_setting_block(this)'})
|
|
key = hidden_field_tag "ask_setting[custom_fields][#{k}][key]",k,{'class' => 'key'}
|
|
all_new_options = Array(v['options']).collect do |key,value|
|
|
tmp = create_delete_button('delete_added_select_option').html_safe+hidden_field_tag("ask_setting[custom_fields][#{k}][options][#{key}][disabled]",value['disabled'],{'class' => 'disabled_field'})
|
|
hidden_style = value['disabled'] == 'true' ? {style: 'display:none;'} : {}
|
|
"#{multiple_lang_tag(k,'text_field',"options][#{key}",value,{placeholder: t('ask.option_name')},tmp,hidden_style)}"
|
|
end.join
|
|
active_class = Array(v['options']).count !=0 ? ' active' : ''
|
|
active_prompt_class = ['date','radio_button','checkbox'].exclude?(v['type']) ? ' active' : ''
|
|
multi_lang_prompt_tag = "<div class=\"prompt_word#{active_prompt_class}\">#{multiple_lang_tag(k,'text_field','prompt_word',v['prompt_word'],{placeholder: t('ask.prompt_word')})}</div>"
|
|
require_ask_tag = "<div>#{t('ask.required')}: #{field_radio_button_tag(k,'required','true',v['required']=='true')}#{t('ask.yes')}#{field_radio_button_tag(k,'required','false',v['required']!='true')}#{t('ask.no')}</div>"
|
|
tmp = "<div class=\"add_new_options#{active_class}\">#{all_new_options}<input type=\"button\" value=\"#{t('ask.add_new_options')}\" onclick=\"add_new_options_for_field(this,#{k})\"></div>"
|
|
field_html = "<tr class=\"custom_field\">
|
|
<td>
|
|
#{multi_lang_tag}
|
|
#{require_ask_tag}
|
|
#{field_select_tag}<br>
|
|
#{multi_lang_prompt_tag}
|
|
#{time_setting_block(k,v)}
|
|
#{tmp}
|
|
#{key}
|
|
</td>
|
|
<td>
|
|
#{create_delete_button('delete_field_func')}
|
|
</td>
|
|
</tr>"
|
|
end
|
|
def create_delete_button(func_name)
|
|
"<input class=\"btn btn-danger\" type=\"button\" value=\"#{t('delete_')}\" onclick=\"#{func_name}(this)\">"
|
|
end
|
|
def page_for_askquestion(askquestion)
|
|
ann_page = nil
|
|
pages = Page.where(:module=>'ask')
|
|
|
|
pages.each do |page|
|
|
if page.categories.count ==1
|
|
if page.categories.include?(askquestion.category.id.to_s)
|
|
ann_page = page
|
|
end
|
|
end
|
|
break if !ann_page.nil?
|
|
end
|
|
|
|
if ann_page.nil?
|
|
pages.each do |page|
|
|
if page.categories.include?(askquestion.category.id.to_s)
|
|
ann_page = page
|
|
end
|
|
break if !ann_page.nil?
|
|
end
|
|
end
|
|
|
|
ann_page = pages.first if ann_page.nil?
|
|
request.protocol+(request.host_with_port+ann_page.url+'/'+askquestion.to_param).gsub('//','/') rescue "/"
|
|
end
|
|
end
|