fix admin index error and implement export feature

This commit is contained in:
邱博亞 2022-12-31 14:52:54 +08:00
parent 7c1164842d
commit b8ba91e226
4 changed files with 90 additions and 32 deletions

View File

@ -302,7 +302,7 @@ class Admin::AsksController < OrbitAdminController
if @ask_question.agree_show if @ask_question.agree_show
text = text.gsub(format_text,I18n.t('ask.yes')) text = text.gsub(format_text,I18n.t('ask.yes'))
else else
text = text.gsub(format_text,I18n.t('ask,no')) text = text.gsub(format_text,I18n.t('ask.no'))
end end
when /custom\d+\|format/ when /custom\d+\|format/
i = format_text.scan(/\d+/)[0].to_i - 1 i = format_text.scan(/\d+/)[0].to_i - 1
@ -432,39 +432,42 @@ class Admin::AsksController < OrbitAdminController
end end
def do_export def do_export
def process_trans(trans)
trans.collect{|k,v| v if !v.blank?}.compact.uniq.join('/')
end
def get_fields_with_key(s)
fields_with_key = {}
fields_with_key['default_values'] = s.default_setting.collect{|k,v| [k, process_trans(s.field_name_translations(k))] if v && k!='ask_category_id'}.compact
fields_with_key['default_values'] = [['ask_category_id', process_trans(s.field_name_translations('ask_category_id'))]] + fields_with_key['default_values']
fields_with_key['default_values'] = fields_with_key['default_values'].to_h
fields_with_key['custom_values'] = s.custom_fields.collect{|k, field_setting| [k, process_trans(field_setting['field'])]}.to_h
fields_with_key
end
Rails.application.config.mongoid.use_activesupport_time_zone = true Rails.application.config.mongoid.use_activesupport_time_zone = true
date_start = "#{params[:export]['start(1i)']}-#{params[:export]['start(2i)']}-#{params[:export]['start(3i)']}" date_start = "#{params[:export]['start(1i)']}-#{params[:export]['start(2i)']}-#{params[:export]['start(3i)']}"
date_end = "#{params[:export]['end(1i)']}-#{params[:export]['end(2i)']}-#{params[:export]['end(3i)']}" date_end = "#{params[:export]['end(1i)']}-#{params[:export]['end(2i)']}-#{params[:export]['end(3i)']}"
@ask_questions = AskQuestion.where(:created_at.gte => date_start.to_datetime, :created_at.lte => date_end.to_datetime+1) ask_questions = AskQuestion.where(:created_at.gte => date_start.to_datetime, :created_at.lte => date_end.to_datetime)
categories = Category.where(:id.in => ask_questions.collect(&:category_id).uniq).to_a
csv = CSV.generate do |csv| ask_default_setting = AskSetting.first()
csv << [ t('category'), ask_settings = AskCategorySetting.where(:category_id.in => categories.collect(&:id)).collect{|s| [BSON::ObjectId(s.category_id), s]}.to_h
AskQuestion.human_attribute_name(:name), ask_category_settings = {}
AskQuestion.human_attribute_name(:identity), fields_with_key_group = {}
AskQuestion.human_attribute_name(:mail), categories.each do |c|
AskQuestion.human_attribute_name(:phone), s = ask_settings.fetch(c.id, ask_default_setting)
AskQuestion.human_attribute_name(:fax), ask_category_settings[c.id] = s
AskQuestion.human_attribute_name(:title), fields_with_key_group[c.id] = get_fields_with_key(s)
AskQuestion.human_attribute_name(:content),
AskQuestion.human_attribute_name(:reply),
AskQuestion.human_attribute_name(:comment)]
@ask_questions.each do |ask_question|
Tag.where({:id => ask_question[:identity]}).each do |tag|
csv << [ ask_question.category.title,
ask_question.name,
tag.name,
ask_question.mail,
ask_question.phone,
ask_question.fax,
ask_question.title,
ask_question.content,
ask_question.reply,
ask_question.comment ]
end
end
end end
send_data csv.encode('Big5'), type: 'text/csv', filename: "Questions-#{date_start}-#{date_end}.csv" ask_category_settings[nil] = ask_default_setting
fields_with_key_group[nil] = get_fields_with_key(ask_default_setting)
render xlsx: 'do_export.xlsx',
handlers: ['axlsx'],
filename: "Questions-#{date_start}-#{date_end}.xlsx",
locals: {
:ask_category_settings => ask_category_settings,
:fields_with_key_group => fields_with_key_group,
:categories => categories,
:ask_questions => ask_questions
}
end end
def set_askquestion def set_askquestion

View File

@ -127,7 +127,7 @@ class AskCategorySetting
field :use_default, type: Boolean, default: false field :use_default, type: Boolean, default: false
field :custom_fields, type: Hash,default: {} field :custom_fields, type: Hash,default: {}
field :usage_rule, type: String, default: '' field :usage_rule, type: String, default: ''
field :category_id field :category_id, type: String
field :title_layout, type: Integer, default: 0 field :title_layout, type: Integer, default: 0
field :email_regex_enable, type: Boolean, default: false field :email_regex_enable, type: Boolean, default: false
field :email_regex, type: String, default: '\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z' field :email_regex, type: String, default: '\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z'

View File

@ -244,7 +244,7 @@
<%= <%=
content_tag :div, class: "bottomnav clearfix" do content_tag :div, class: "bottomnav clearfix" do
content_tag :div, paginate(@askquestions), class: "pagination pagination-centered" content_tag :div, paginate(@askquestions), class: "pagination pagination-centered" +
content_tag :div, link_to(I18n.t('ask.display_fields_setting'), backend_table_setting_admin_asks_path + (@category_id.present? ? "?category_id=#{@category_id}" : ''), :class=>'btn btn-primary' ) + link_to(t("ask.delete_selected"), "javascript:void(0)", :class=>"btn btn-danger delete_asks_btn hide", :style=>'margin-left: 0.5em;') + link_to(t("ask.batch_modify_status"), "javascript:void(0)", :class=>"btn btn-success batch_modify_status_btn hide", :style=>'margin-left: 0.5em;'), class: 'pull-right' content_tag :div, link_to(I18n.t('ask.display_fields_setting'), backend_table_setting_admin_asks_path + (@category_id.present? ? "?category_id=#{@category_id}" : ''), :class=>'btn btn-primary' ) + link_to(t("ask.delete_selected"), "javascript:void(0)", :class=>"btn btn-danger delete_asks_btn hide", :style=>'margin-left: 0.5em;') + link_to(t("ask.batch_modify_status"), "javascript:void(0)", :class=>"btn btn-success batch_modify_status_btn hide", :style=>'margin-left: 0.5em;'), class: 'pull-right'
end end
%> %>

View File

@ -0,0 +1,55 @@
# encoding: utf-8
wb = xlsx_package.workbook
wb.add_worksheet(name: "Ask Question") do |sheet|
heading = sheet.styles.add_style(:b => true, :locked => true)
heads = []
fields_with_key_group.each do |cat_id, fields_with_key|
fields_with_key.each do |type, fs|
fs.each do |key, field|
heads << field if !heads.include?(field)
end
end
end
sheet.add_row heads, :style => heading
ask_questions.each do |ask_question|
row = []
row_group = {}
s = ask_category_settings[ask_question.category_id]
fields_with_key_group[ask_question.category_id]['default_values'].each do |key, field|
text = ''
case key
when 'ask_time'
text = ask_question.created_at.strftime('%Y/%m/%d %H:%M')
when 'title','name','mail','phone','appointment'
text = ask_question.send(key).to_s
when 'ask_category_id'
text = categories[ask_question.ask_category_id].title rescue ''
when 'sex'
text = ask_question.sex
if ['male','female'].include? sex.to_s
text = I18n.t("mongoid.attributes.ask_question.#{sex}")
else
text = ''
end
when 'agree_show'
if ask_question.agree_show
text = I18n.t('ask.yes')
else
text = I18n.t('ask.no')
end
end
row_group[field] = text
end
fields_with_key_group[ask_question.category_id]['custom_values'].each do |key, field|
row_group[field] = Admin::AsksHelper.show_on_front(key, s.custom_fields[key], ask_question, true)
end
row = heads.collect do |head|
row_group[head]
end
sheet.add_row row
end
end