"
end
end
def ask_label(id, value, required='false')
label_tag(id,value,{:class=>"control-label#{required=='true' ? ' required' : ''}"})
end
def format_checkbox(options,value,multple_choose=false)
options.select{|index1,option| option['disabled'] != 'true'}.collect do |index1,option|
if multple_choose
if value && value[index1]==index1
"●#{option[I18n.locale]}"
else
"○#{option[I18n.locale]}"
end
else
if index1 == value
"●#{option[I18n.locale]}"
else
"○#{option[I18n.locale]}"
end
end
end.join(' ')
end
def show_on_front(k,v,object=nil,readonly=false,lock=false)
value = nil
if !object.nil?
value = object.custom_values[k]
if value.nil?
if k.start_with?("default@")
value = object.custom_values[k.sub(/.*@/,'')]
elsif k.start_with?("custom@")
category_id, k = k.scan(/custom@((?:(?!@).)*)@\w+/)[0][0]
if category_id==object.category_id.to_s
value = object.custom_values[k]
end
end
end
end
field_name = "#{get_input_name_for_ask}[custom_values][#{k}]"
begin
case v['type']
when 'text_field'
readonly ? value : text_field_tag(field_name,value,{:required => v['required']=='true',placeholder: v['prompt_word'][I18n.locale],title: v['prompt_word'][I18n.locale]})
when 'instructions'
(v['instructions'][I18n.locale].html_safe rescue "")
when 'select'
prompt_hash = v['prompt_word'][I18n.locale].blank? ? {} : {prompt: v['prompt_word'][I18n.locale]}
prompt_hash.merge!(required: v['required']=='true')
options_hash = Hash(v['options'])
if lock
format_checkbox(options_hash,value)
else
if v['default_option'].present?
default_idx = v['default_option'].to_i
default_key = options_hash.keys[default_idx]
value ||= default_key
end
readonly ? (v['options'][value.to_s][I18n.locale] rescue '') : select_tag(field_name,options_for_select(options_hash.select{|index1,option| option['disabled'] != 'true'}.collect{|index1,option| [option[I18n.locale],index1]},selected: value),prompt_hash)
end
when 'date'
if value.nil?
value = {}
end
if v['range_flag']=='true'
if readonly
format_str = "%Y/%m/%d %H:%M"
case v['format']
when 'format1'
format_str = "%Y/%m/%d %H:%M"
when 'format2'
format_str = "%Y/%m/%d"
when 'format3'
format_str = "%Y/%m"
when 'format4'
format_str = "%Y"
end
tmp = value['datetime']['date'] rescue {}
"#{DateTime.parse(tmp['0']).strftime(format_str) rescue ''}~#{DateTime.parse(tmp['1']).strftime(format_str) rescue ''}"
else
"
"
end
end
def create_delete_button_for_ask(func_name)
""
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
def process_format_text(text,custom_fields,show_recommendation=false)
customs = text.scan(/
/m).uniq
if customs.length != 0
custom_exist_nums = []
custom_exists = text.scan(/{custom\d+}/)
custom_exists.each do |custom_exist|
k = custom_exist[6...-1].to_i - 1
custom_exist_nums << k
end
customs.each do |custom|
tmp = custom_fields.keys.collect.with_index do |k,i|
if custom_exist_nums.exclude?(i)
tp = custom.gsub(/{custom}/,"{custom#{i+1}}")
tp = tp.gsub(/{custom_title}/,"{custom#{i+1}_title}")
tp
else
nil
end
end.compact.join
text = text.gsub(custom,tmp)
end
end
format_texts = text.scan(/{\w+\|*\w+}/).uniq
format_texts.each do |format_text|
if format_text[-7...-1] == '_title'
case format_text[1...-7]
when /custom\d+/
i = format_text.scan(/\d+/)[0].to_i - 1
k = custom_fields.keys[i] rescue nil
if !k.blank?
text_tp = custom_fields[k]['field'][I18n.locale] rescue ''
text = text.gsub(format_text,text_tp)
end
when 'title'
text = text.gsub(format_text,I18n.t('title'))
when 'ask_time'
text = text.gsub(format_text,I18n.t('ask.ask_time'))
when 'category'
text = text.gsub(format_text,I18n.t('mongoid.attributes.ask_question.ask_category_id'))
when 'name','sex','mail','phone','appointment','agree_show'
text = text.gsub(format_text,I18n.t("mongoid.attributes.ask_question.#{format_text[1...-7]}"))
end
else
case format_text[1...-1]
when 'ask_time'
text = text.gsub(format_text,@ask_question.created_at.strftime('%Y/%m/%d %H:%M'))
when 'title','name','mail','phone','appointment'
text = text.gsub(format_text,@ask_question.send(format_text[1...-1]).to_s)
when 'category'
cat = Category.find(@ask_question.category_id).title rescue ''
text = text.gsub(format_text,cat)
when 'name|mask'
name_tp = @ask_question.name.to_s
if name_tp.length>1
name_tp = name_tp[0] + '*'*(name_tp.length-1)
end
text = text.gsub(format_text,name_tp)
when 'sex'
sex = @ask_question.sex
if ['male','female'].include? sex.to_s
sex = I18n.t("mongoid.attributes.ask_question.#{sex}")
else
sex = ''
end
text = text.gsub(format_text,sex)
when 'agree_show'
if @ask_question.agree_show
text = text.gsub(format_text,I18n.t('ask.yes'))
else
text = text.gsub(format_text,I18n.t('ask.no'))
end
when 'recommendation'
if show_recommendation
recommendation_tp = "
".html_safe
text = text.gsub(format_text,recommendation_tp)
else
text = text.gsub(format_text,"")
end
when /custom\d+\|format/
i = format_text.scan(/\d+/)[0].to_i - 1
k = custom_fields.keys[i] rescue nil
if !k.blank?
v = custom_fields[k] rescue {}
text_tp = Admin::AsksHelper.show_on_front(k,v,@ask_question,true,true)
text = text.gsub(format_text,text_tp.to_s)
end
when /custom\d+/
i = format_text.scan(/\d+/)[0].to_i - 1
k = custom_fields.keys[i] rescue nil
if !k.blank?
v = custom_fields[k] rescue {}
text_tp = Admin::AsksHelper.show_on_front(k,v,@ask_question,true)
text = text.gsub(format_text,text_tp.to_s)
end
end
end
end
text
end
# Adding this helper method for PDF generation
def generate_pdf(ask_question, show_recommendation=false)
@ask_question = ask_question # Set instance variable for process_format_text
# Get settings similar to print method
print_setting = AskCategoryPrintSetting.where(category_id: ask_question.category_id.to_s).first
print_setting = AskPrintSetting.first if print_setting.nil?
# Get custom fields
ask_setting = AskCategorySetting.where(category_id: ask_question.category_id.to_s, :use_default.ne=>true).first
ask_setting = AskSetting.first if ask_setting.nil?
custom_fields = ask_setting.custom_fields
print_text = print_setting.print_format rescue ''
print_text = ""+ print_text
print_text = process_format_text(print_text,custom_fields, show_recommendation)
# Render the print format explanation partial
html_content = <<-HTML
#{print_text}
HTML
# Generate PDF using WickedPDF
WickedPdf.new.pdf_from_string(
html_content,
encoding: 'UTF-8',
page_size: 'A4',
margin: {
top: 20,
bottom: 20,
left: 20,
right: 20
},
print_media_type: true,
zoom: 1,
dpi: 300,
footer: {
font_size: 8,
right: '[page] of [topage]',
spacing: 5
}
)
end
end