diff --git a/app/controllers/admin/asks_controller.rb b/app/controllers/admin/asks_controller.rb index 0cf5d17..6ebfac2 100644 --- a/app/controllers/admin/asks_controller.rb +++ b/app/controllers/admin/asks_controller.rb @@ -235,98 +235,7 @@ class Admin::AsksController < OrbitAdminController locale = I18n.locale redirect_to "/#{locale}/admin/asks/category_setting" end - def process_format_text(text,custom_fields) - 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 /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 @print_setting = AskCategoryPrintSetting.where(category_id: @ask_question.category_id.to_s).first @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 = AskSetting.first if ask_setting.nil? 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) end def print_pdf @ask_question = AskQuestion.find(params[:id]) - pdf_content = generate_pdf(@ask_question) + pdf_content = generate_pdf(@ask_question, true) send_data pdf_content, 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') 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 = ""+ print_text - print_text = process_format_text(print_text,custom_fields) - # 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 # Modify the build_email method to include PDF attachment def build_email(email_er) diff --git a/app/controllers/asks_controller.rb b/app/controllers/asks_controller.rb index 1c8e632..5a55b11 100644 --- a/app/controllers/asks_controller.rb +++ b/app/controllers/asks_controller.rb @@ -591,7 +591,7 @@ class AsksController < ApplicationController :mail_subject=>mail_subject, :template=>'asks/email', :template_data=>{ - # "title" => email_er.title, + "title" => email_er.title, "name" => email_er.name, "identity" => email_er[:identity], "mail" => email_er.mail, diff --git a/app/helpers/admin/asks_helper.rb b/app/helpers/admin/asks_helper.rb index d5c2e7e..4278c26 100644 --- a/app/helpers/admin/asks_helper.rb +++ b/app/helpers/admin/asks_helper.rb @@ -490,4 +490,174 @@ module Admin::AsksHelper 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 = "

" + I18n.t('ask.recommendation') + "

" + @ask_question.recommendation.to_s.gsub("\n", "
").gsub("\r", "") + "

".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 diff --git a/app/models/ask_question.rb b/app/models/ask_question.rb index 9c7ce38..0b2ed29 100644 --- a/app/models/ask_question.rb +++ b/app/models/ask_question.rb @@ -20,6 +20,7 @@ class AskQuestion field :reply, type: String field :comment, type: String + field :recommendation, type: String field :bcc_emails, type: String field :situation, type: String, default: "is_email_not_confirmed" # 預設email未驗證 field :send_email, type: Integer, default: 0 diff --git a/app/views/admin/asks/_form.html.erb b/app/views/admin/asks/_form.html.erb index 522992f..0d1f28b 100644 --- a/app/views/admin/asks/_form.html.erb +++ b/app/views/admin/asks/_form.html.erb @@ -176,6 +176,12 @@ <%= f.text_area :release_comment, :id => "#{f.object_name}_comment" %> +
+ +
+ <%= f.text_area :recommendation, rows: 5, style: 'max-width: 500px; width: 100%;'%> +
+
diff --git a/app/views/asks/email.html.erb b/app/views/asks/email.html.erb index 31778b6..32c9094 100644 --- a/app/views/asks/email.html.erb +++ b/app/views/asks/email.html.erb @@ -1,7 +1,7 @@ - + diff --git a/config/locales/en.yml b/config/locales/en.yml index a1b0b40..c51579d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -146,6 +146,7 @@ en: email_id: Email attach_pdf_to_email: Attach PDF template to email replies pdf_attachment_notice: "A PDF document has been attached to this email" + recommendation: Recommendation mongoid: attributes: diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index e4610d0..4239ff2 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -156,6 +156,7 @@ zh_tw: email_id: 電子郵件 attach_pdf_to_email: 回覆信附加PDF範本 pdf_attachment_notice: "此郵件已附加PDF文件" + recommendation: 擬辦/註解 mongoid: attributes: