From 1879d976d4cc596ed4c2a123ee8994a2921fb452 Mon Sep 17 00:00:00 2001 From: BoHung Chiu Date: Sun, 24 Sep 2023 13:45:40 +0800 Subject: [PATCH] Fix import and export. --- .../admin/announcements_controller.rb | 60 ++++++++-- app/helpers/admin/announcements_helper.rb | 22 ++-- app/views/admin/announcements/import.html.erb | 111 ++++++++---------- config/locales/zh_tw.yml | 5 +- lib/announcement/engine.rb | 2 +- 5 files changed, 115 insertions(+), 85 deletions(-) diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index ad62522..316818d 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -158,6 +158,7 @@ class Admin::AnnouncementsController < OrbitAdminController end def import + @thread = Multithread.where(:id=>params[:thread_id]).first if params[:thread_id].present? end def excel_format @@ -186,7 +187,7 @@ class Admin::AnnouncementsController < OrbitAdminController end Thread.new do begin - @announcements = Bulletin.all.desc(:id) + @announcements = Bulletin.where(:is_preview.ne=>true).desc(:id) last_updated = [Bulletin.max(:updated_at).to_i, AnnouncementSetting.max(:updated_at).to_i].max filename = "public/announcement_export_#{last_updated}.xlsx" if File.exist?(filename) @@ -227,11 +228,6 @@ class Admin::AnnouncementsController < OrbitAdminController redirect_to admin_announcements_path end - def import - @thread = Multithread.where(:id=>params[:thread_id]).first if params[:thread_id].present? - end - - def import_from_wp import_from_wordpress params["import_xml"].tempfile redirect_to admin_announcements_path @@ -239,11 +235,59 @@ class Admin::AnnouncementsController < OrbitAdminController def importanns workbook = RubyXL::Parser.parse(params["import_file"].tempfile) - categories = @module_app.categories.asc(:created_at).to_a - tags = @module_app.tags.asc(:created_at).to_a + raw_categories = @module_app.categories.asc(:created_at).to_a.map.with_index{|v, k| [k.to_s,v]}.to_h + raw_tags = @module_app.tags.asc(:created_at).to_a.map.with_index{|v, k| [k.to_s,v]}.to_h + categories = raw_categories.clone + tags = raw_tags.clone sheet = workbook[0] if sheet.count <= 503 sheet.each_with_index do |row, i| + if i == 2 + begin + cats_text = row.cells[0].value.to_s.sub(/(^|,)\s*Example\s*:.*$/,'') + cats_keys = cats_text.split('->').map{|s| s.split(',')[-1].strip}[0...-1] + cats_values = cats_text.split('->')[1..-1].to_a.map{|s| s.strip.sub(/,\s*\d+$/,'')} + categories_relations = cats_keys.zip(cats_values).to_h + rescue => e + categories_relations = {} + end + begin + tags_text = row.cells[1].value.to_s.sub(/(^|,)\s*Example\s*:.*$/,'') + tags_keys = tags_text.split('->').map{|s| s.split(',')[-1].strip}[0...-1] + tags_values = tags_text.split('->')[1..-1].to_a.map{|s| s.strip.sub(/,\s*\d+$/,'')} + tags_relations = tags_keys.zip(tags_values).to_h + rescue => e + tags_relations = {} + end + if categories_relations.present? + categories = categories_relations.map do |k, v| + tmp = raw_categories[k] + if tmp && tmp.title.strip == v + [k, tmp] + else + tmp = raw_categories.detect{|kk, vv| vv.title.strip == v} + if tmp.nil? + tmp = @module_app.categories.create(:title_translations=> localize_data(v)) + end + [k, tmp] + end + end.to_h + end + if tags_relations.present? + tags = tags_relations.map do |k, v| + tmp = raw_tags[k] + if tmp && tmp.name.strip == v + [k, tmp] + else + tmp = raw_tags.detect{|kk, vv| vv.name.strip == v} + if tmp.nil? + tmp = @module_app.tags.create(:name_translations=> localize_data(v)) + end + [k, tmp] + end + end.to_h + end + end next if i < 3 v = row.cells.first.value rescue nil next if v.blank? diff --git a/app/helpers/admin/announcements_helper.rb b/app/helpers/admin/announcements_helper.rb index c280c64..af3f1ca 100644 --- a/app/helpers/admin/announcements_helper.rb +++ b/app/helpers/admin/announcements_helper.rb @@ -38,13 +38,11 @@ module Admin::AnnouncementsHelper anns = Bulletin.new row.cells.each_with_index do |cell,index| val = cell.nil? ? nil : cell.value rescue nil - if [8,9,10,11,12,13,14,15].exclude?(index) - next if val.blank? - end case index when 0 - anns.category = categories[val.to_i] + anns.category = categories[val.to_s.strip] when 1 + val = val.to_s new_tags = [] if (val.include?(",") rescue false) ts = val.split(",") @@ -68,31 +66,31 @@ module Admin::AnnouncementsHelper when 7 anns.remote_image_url = val when 8 - value["en"] = val + value["en"] = val if val.present? anns.image_description_translations = value.clone when 9 - value["zh_tw"] = val + value["zh_tw"] = val if val.present? anns.image_description_translations = value.clone value = {} when 10 - value["en"] = val + value["en"] = val if val.present? anns.title_translations = value.clone when 11 - value["zh_tw"] = val + value["zh_tw"] = val if val.present? anns.title_translations = value.clone value = {} when 12 - value["en"] = val + value["en"] = val if val.present? anns.subtitle_translations = value.clone when 13 - value["zh_tw"] = val + value["zh_tw"] = val if val.present? anns.subtitle_translations = value.clone value = {} when 14 - value["en"] = val + value["en"] = val if val.present? anns.text_translations = value.clone when 15 - value["zh_tw"] = val + value["zh_tw"] = val if val.present? anns.text_translations = value.clone value = {} when 16 diff --git a/app/views/admin/announcements/import.html.erb b/app/views/admin/announcements/import.html.erb index 2271418..f67c2da 100644 --- a/app/views/admin/announcements/import.html.erb +++ b/app/views/admin/announcements/import.html.erb @@ -1,5 +1,5 @@ <% content_for :page_specific_javascript do %> - + <% end %> <% if @thread %> <% end %>
-

<%= t("announcement.export_to_excel") %>

- -

<%= t("announcement.import_from_excel") %>

- <%= hidden_field_tag :authenticity_token, form_authenticity_token %> -
- <% if @module_app.categories.count > 0 %> - -
- -
- - <%= t("announcement.please_create_tags_cats") %> -
-
- <% else %> -
-
-

<%= t("announcement.create_atleast_one_cat") %>

-
-
- <% end %> -
- <% if @module_app.categories.count > 0 %> -
- " class="btn btn-primary"> -
- <% end %> +

<%= t("announcement.export_to_excel") %>

+ +

<%= t("announcement.import_from_excel") %>

+ <%= hidden_field_tag :authenticity_token, form_authenticity_token %> +
+ +
+ +
+ +
+
+
+
+ " class="btn btn-primary"> +
@@ -86,33 +75,33 @@