diff --git a/app/controllers/admin/asks_controller.rb b/app/controllers/admin/asks_controller.rb index c8274c9..a3f5eec 100644 --- a/app/controllers/admin/asks_controller.rb +++ b/app/controllers/admin/asks_controller.rb @@ -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 diff --git a/app/models/ask_category_setting.rb b/app/models/ask_category_setting.rb index 3872a46..364e933 100644 --- a/app/models/ask_category_setting.rb +++ b/app/models/ask_category_setting.rb @@ -127,7 +127,7 @@ class AskCategorySetting field :use_default, type: Boolean, default: false field :custom_fields, type: Hash,default: {} field :usage_rule, type: String, default: '' - field :category_id + field :category_id, type: String field :title_layout, type: Integer, default: 0 field :email_regex_enable, type: Boolean, default: false field :email_regex, type: String, default: '\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z' diff --git a/app/views/admin/asks/_index.html.erb b/app/views/admin/asks/_index.html.erb index 3e2e762..a5fa639 100644 --- a/app/views/admin/asks/_index.html.erb +++ b/app/views/admin/asks/_index.html.erb @@ -244,7 +244,7 @@ <%= 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' end %> diff --git a/app/views/admin/asks/do_export.xlsx.axlsx b/app/views/admin/asks/do_export.xlsx.axlsx new file mode 100644 index 0000000..13ca01f --- /dev/null +++ b/app/views/admin/asks/do_export.xlsx.axlsx @@ -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