added recommendation field

This commit is contained in:
rulingcom 2025-10-23 23:05:16 +08:00
parent 7442993e35
commit 66a5a5597b
8 changed files with 184 additions and 166 deletions

View File

@ -235,98 +235,7 @@ class Admin::AsksController < OrbitAdminController
locale = I18n.locale locale = I18n.locale
redirect_to "/#{locale}/admin/asks/category_setting" redirect_to "/#{locale}/admin/asks/category_setting"
end end
def process_format_text(text,custom_fields)
customs = text.scan(/<tr(?:(?!<tr).)+\{custom\}(?:(?!<tr).)+<\/tr>/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 /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
def print def print
@print_setting = AskCategoryPrintSetting.where(category_id: @ask_question.category_id.to_s).first @print_setting = AskCategoryPrintSetting.where(category_id: @ask_question.category_id.to_s).first
@print_setting = AskPrintSetting.first if @print_setting.nil? @print_setting = AskPrintSetting.first if @print_setting.nil?
@ -336,13 +245,13 @@ class Admin::AsksController < OrbitAdminController
ask_setting = AskCategorySetting.where(category_id: @ask_question.category_id.to_s, :use_default.ne=>true).first 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? ask_setting = AskSetting.first if ask_setting.nil?
custom_fields = ask_setting.custom_fields custom_fields = ask_setting.custom_fields
@print_text = process_format_text(@print_text,custom_fields) @print_text = process_format_text(@print_text,custom_fields, true)
@save_name = process_format_text(@save_name,custom_fields) @save_name = process_format_text(@save_name,custom_fields)
end end
def print_pdf def print_pdf
@ask_question = AskQuestion.find(params[:id]) @ask_question = AskQuestion.find(params[:id])
pdf_content = generate_pdf(@ask_question) pdf_content = generate_pdf(@ask_question, true)
send_data pdf_content, send_data pdf_content,
filename: "ask_question_#{@ask_question.id}.pdf", filename: "ask_question_#{@ask_question.id}.pdf",
@ -422,76 +331,6 @@ class Admin::AsksController < OrbitAdminController
redirect_to admin_asks_path(:locale=>locale), notice: t('ask.reply_success') redirect_to admin_asks_path(:locale=>locale), notice: t('ask.reply_success')
end end
# Adding this helper method for PDF generation
def generate_pdf(ask_question)
@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 = "<style>img{width: 100%;}</style>"+ print_text
print_text = process_format_text(print_text,custom_fields)
# Render the print format explanation partial
html_content = <<-HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
font-size: 12px;
line-height: 1.4;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
#{print_text}
</body>
</html>
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
# Modify the build_email method to include PDF attachment # Modify the build_email method to include PDF attachment
def build_email(email_er) def build_email(email_er)

View File

@ -591,7 +591,7 @@ class AsksController < ApplicationController
:mail_subject=>mail_subject, :mail_subject=>mail_subject,
:template=>'asks/email', :template=>'asks/email',
:template_data=>{ :template_data=>{
# "title" => email_er.title, "title" => email_er.title,
"name" => email_er.name, "name" => email_er.name,
"identity" => email_er[:identity], "identity" => email_er[:identity],
"mail" => email_er.mail, "mail" => email_er.mail,

View File

@ -490,4 +490,174 @@ module Admin::AsksHelper
ann_page = pages.first if ann_page.nil? ann_page = pages.first if ann_page.nil?
request.protocol+(request.host_with_port+ann_page.url+'/'+askquestion.to_param).gsub('//','/') rescue "/" request.protocol+(request.host_with_port+ann_page.url+'/'+askquestion.to_param).gsub('//','/') rescue "/"
end end
def process_format_text(text,custom_fields,show_recommendation=false)
customs = text.scan(/<tr(?:(?!<tr).)+\{custom\}(?:(?!<tr).)+<\/tr>/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 = "<h3>" + I18n.t('ask.recommendation') + "</h3><p>" + @ask_question.recommendation.to_s.gsub("\n", "<br/>").gsub("\r", "") + "</p>".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 = "<style>img{width: 100%;}</style>"+ print_text
print_text = process_format_text(print_text,custom_fields, show_recommendation)
# Render the print format explanation partial
html_content = <<-HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
font-size: 12px;
line-height: 1.4;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
#{print_text}
</body>
</html>
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 end

View File

@ -20,6 +20,7 @@ class AskQuestion
field :reply, type: String field :reply, type: String
field :comment, type: String field :comment, type: String
field :recommendation, type: String
field :bcc_emails, type: String field :bcc_emails, type: String
field :situation, type: String, default: "is_email_not_confirmed" # 預設email未驗證 field :situation, type: String, default: "is_email_not_confirmed" # 預設email未驗證
field :send_email, type: Integer, default: 0 field :send_email, type: Integer, default: 0

View File

@ -176,6 +176,12 @@
<%= f.text_area :release_comment, :id => "#{f.object_name}_comment" %> <%= f.text_area :release_comment, :id => "#{f.object_name}_comment" %>
</div> </div>
</div> </div>
<div class="control-group">
<label class="control-label muted"><%= f.label :recommendation %></label>
<div class="controls">
<%= f.text_area :recommendation, rows: 5, style: 'max-width: 500px; width: 100%;'%>
</div>
</div>
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t(:file_) %></label> <label class="control-label muted"><%= t(:file_) %></label>
<div class="controls"> <div class="controls">

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head> </head>
<body> <body>
<!-- <h1><%= @data['title'] %></h1> --> <!-- <h1><%= @data['title'] %></h1> -->

View File

@ -146,6 +146,7 @@ en:
email_id: Email email_id: Email
attach_pdf_to_email: Attach PDF template to email replies attach_pdf_to_email: Attach PDF template to email replies
pdf_attachment_notice: "A PDF document has been attached to this email" pdf_attachment_notice: "A PDF document has been attached to this email"
recommendation: Recommendation
mongoid: mongoid:
attributes: attributes:

View File

@ -156,6 +156,7 @@ zh_tw:
email_id: 電子郵件 email_id: 電子郵件
attach_pdf_to_email: 回覆信附加PDF範本 attach_pdf_to_email: 回覆信附加PDF範本
pdf_attachment_notice: "此郵件已附加PDF文件" pdf_attachment_notice: "此郵件已附加PDF文件"
recommendation: 擬辦/註解
mongoid: mongoid:
attributes: attributes: