fix admin index error and implement export feature

This commit is contained in:
chiu committed 2022-12-31 14:52:54 +08:00
1 parent 7c1164842d
commit b8ba91e226
4 files changed
+90 -32

No files matched your search

+33 -30
View File
@@ -302,7 +302,7 @@ class Admin::AsksController < OrbitAdminController
if @ask_question.agree_show
text = text.gsub(format_text,I18n.t('ask.yes'))
else
text = text.gsub(format_text,I18n.t('ask,no'))
text = text.gsub(format_text,I18n.t('ask.no'))
end
when /custom\d+\|format/
i = format_text.scan(/\d+/)[0].to_i - 1
@@ -432,39 +432,42 @@ class Admin::AsksController < OrbitAdminController
end
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
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)']}"
@ask_questions = AskQuestion.where(:created_at.gte => date_start.to_datetime, :created_at.lte => date_end.to_datetime+1)
csv = CSV.generate do |csv|
csv << [ t('category'),
AskQuestion.human_attribute_name(:name),
AskQuestion.human_attribute_name(:identity),
AskQuestion.human_attribute_name(:mail),
AskQuestion.human_attribute_name(:phone),
AskQuestion.human_attribute_name(:fax),
AskQuestion.human_attribute_name(:title),
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
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
ask_default_setting = AskSetting.first()
ask_settings = AskCategorySetting.where(:category_id.in => categories.collect(&:id)).collect{|s| [BSON::ObjectId(s.category_id), s]}.to_h
ask_category_settings = {}
fields_with_key_group = {}
categories.each do |c|
s = ask_settings.fetch(c.id, ask_default_setting)
ask_category_settings[c.id] = s
fields_with_key_group[c.id] = get_fields_with_key(s)
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
def set_askquestion