Fix import and export.

This commit is contained in:
BoHung Chiu 2023-09-24 13:45:40 +08:00
parent d39a4461b3
commit 1879d976d4
5 changed files with 115 additions and 85 deletions

View File

@ -158,6 +158,7 @@ class Admin::AnnouncementsController < OrbitAdminController
end end
def import def import
@thread = Multithread.where(:id=>params[:thread_id]).first if params[:thread_id].present?
end end
def excel_format def excel_format
@ -186,7 +187,7 @@ class Admin::AnnouncementsController < OrbitAdminController
end end
Thread.new do Thread.new do
begin 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 last_updated = [Bulletin.max(:updated_at).to_i, AnnouncementSetting.max(:updated_at).to_i].max
filename = "public/announcement_export_#{last_updated}.xlsx" filename = "public/announcement_export_#{last_updated}.xlsx"
if File.exist?(filename) if File.exist?(filename)
@ -227,11 +228,6 @@ class Admin::AnnouncementsController < OrbitAdminController
redirect_to admin_announcements_path redirect_to admin_announcements_path
end end
def import
@thread = Multithread.where(:id=>params[:thread_id]).first if params[:thread_id].present?
end
def import_from_wp def import_from_wp
import_from_wordpress params["import_xml"].tempfile import_from_wordpress params["import_xml"].tempfile
redirect_to admin_announcements_path redirect_to admin_announcements_path
@ -239,11 +235,59 @@ class Admin::AnnouncementsController < OrbitAdminController
def importanns def importanns
workbook = RubyXL::Parser.parse(params["import_file"].tempfile) workbook = RubyXL::Parser.parse(params["import_file"].tempfile)
categories = @module_app.categories.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
tags = @module_app.tags.asc(:created_at).to_a 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] sheet = workbook[0]
if sheet.count <= 503 if sheet.count <= 503
sheet.each_with_index do |row, i| 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 next if i < 3
v = row.cells.first.value rescue nil v = row.cells.first.value rescue nil
next if v.blank? next if v.blank?

View File

@ -38,13 +38,11 @@ module Admin::AnnouncementsHelper
anns = Bulletin.new anns = Bulletin.new
row.cells.each_with_index do |cell,index| row.cells.each_with_index do |cell,index|
val = cell.nil? ? nil : cell.value rescue nil 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 case index
when 0 when 0
anns.category = categories[val.to_i] anns.category = categories[val.to_s.strip]
when 1 when 1
val = val.to_s
new_tags = [] new_tags = []
if (val.include?(",") rescue false) if (val.include?(",") rescue false)
ts = val.split(",") ts = val.split(",")
@ -68,31 +66,31 @@ module Admin::AnnouncementsHelper
when 7 when 7
anns.remote_image_url = val anns.remote_image_url = val
when 8 when 8
value["en"] = val value["en"] = val if val.present?
anns.image_description_translations = value.clone anns.image_description_translations = value.clone
when 9 when 9
value["zh_tw"] = val value["zh_tw"] = val if val.present?
anns.image_description_translations = value.clone anns.image_description_translations = value.clone
value = {} value = {}
when 10 when 10
value["en"] = val value["en"] = val if val.present?
anns.title_translations = value.clone anns.title_translations = value.clone
when 11 when 11
value["zh_tw"] = val value["zh_tw"] = val if val.present?
anns.title_translations = value.clone anns.title_translations = value.clone
value = {} value = {}
when 12 when 12
value["en"] = val value["en"] = val if val.present?
anns.subtitle_translations = value.clone anns.subtitle_translations = value.clone
when 13 when 13
value["zh_tw"] = val value["zh_tw"] = val if val.present?
anns.subtitle_translations = value.clone anns.subtitle_translations = value.clone
value = {} value = {}
when 14 when 14
value["en"] = val value["en"] = val if val.present?
anns.text_translations = value.clone anns.text_translations = value.clone
when 15 when 15
value["zh_tw"] = val value["zh_tw"] = val if val.present?
anns.text_translations = value.clone anns.text_translations = value.clone
value = {} value = {}
when 16 when 16

View File

@ -1,5 +1,5 @@
<% content_for :page_specific_javascript do %> <% content_for :page_specific_javascript do %>
<script type="text/javascript" src="/assets/validator.js"></script> <script type="text/javascript" src="/assets/validator.js"></script>
<% end %> <% end %>
<% if @thread %> <% if @thread %>
<div id="threadModal" class="modal hide fade in" tabindex="-1" role="dialog" aria-labelledby="threadModal" aria-hidden="false"> <div id="threadModal" class="modal hide fade in" tabindex="-1" role="dialog" aria-labelledby="threadModal" aria-hidden="false">
@ -31,41 +31,30 @@
</div> </div>
<% end %> <% end %>
<form action="<%= admin_announcement_importanns_path %>" method="post" class="form-horizontal main-forms" id="import-anns-xls" enctype="multipart/form-data"> <form action="<%= admin_announcement_importanns_path %>" method="post" class="form-horizontal main-forms" id="import-anns-xls" enctype="multipart/form-data">
<h3 style="padding-left: 30px;"><%= t("announcement.export_to_excel") %></h3> <h3 style="padding-left: 30px;"><%= t("announcement.export_to_excel") %></h3>
<div class="control-group"> <div class="control-group">
<div class="controls"> <div class="controls">
<a href="<%= admin_announcement_export_excel_path %>"><%= t("announcement.export_all_anns") %></a> <a href="<%= admin_announcement_export_excel_path %>"><%= t("announcement.export_all_anns") %></a>
</div> </div>
</div> </div>
<h3 style="padding-left: 30px;"><%= t("announcement.import_from_excel") %></h3> <h3 style="padding-left: 30px;"><%= t("announcement.import_from_excel") %></h3>
<%= hidden_field_tag :authenticity_token, form_authenticity_token %> <%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<div class="input-area"> <div class="input-area">
<% if @module_app.categories.count > 0 %> <div class="control-group">
<div class="control-group"> <div class="controls">
<div class="controls"> <a href="<%= admin_announcement_excel_format_path(:format => "xlsx") %>"><%= t("announcement.download_example_sheet_here") %></a>
<a href="<%= admin_announcement_excel_format_path(:format => "xlsx") %>"><%= t("announcement.download_example_sheet_here") %></a> </div>
</div> </div>
</div> <div class="control-group">
<div class="control-group"> <label for="import-anns" class="control-label muted"><%= t("upload") %></label>
<label for="import-anns" class="control-label muted"><%= t("upload") %></label> <div class="controls">
<div class="controls"> <input type="file" id="import-anns" name="import_file" data-fv-validation="required;mustbexls;" data-fv-messages="Cannot be empty; Must be an excel file.;" />
<input type="file" id="import-anns" name="import_file" data-fv-validation="required;mustbexls;" data-fv-messages="Cannot be empty; Must be an excel file.;" /> </div>
<span class="help-block"><%= t("announcement.please_create_tags_cats") %></span> </div>
</div> </div>
</div> <div class="form-actions">
<% else %> <input type="submit" value="<%= t("restful_actions.import") %>" class="btn btn-primary">
<div class="control-group"> </div>
<div class="controls">
<h4><%= t("announcement.create_atleast_one_cat") %></h4>
</div>
</div>
<% end %>
</div>
<% if @module_app.categories.count > 0 %>
<div class="form-actions">
<input type="submit" value="<%= t("restful_actions.import") %>" class="btn btn-primary">
</div>
<% end %>
</form> </form>
@ -86,33 +75,33 @@
</div> </div>
</form> </form>
<!-- <form action="<%#= admin_announcement_import_from_xml_path %>" method="post" class="form-horizontal main-forms" id="import-anns-xml" enctype="multipart/form-data"> <!-- <form action="<%#= admin_announcement_import_from_xml_path %>" method="post" class="form-horizontal main-forms" id="import-anns-xml" enctype="multipart/form-data">
<h3 style="padding-left: 30px;">Import from XML</h3> <h3 style="padding-left: 30px;">Import from XML</h3>
<%#= hidden_field_tag :authenticity_token, form_authenticity_token %> <%#= hidden_field_tag :authenticity_token, form_authenticity_token %>
<div class="input-area"> <div class="input-area">
<div class="control-group"> <div class="control-group">
<label for="import-anns" class="control-label muted">URL :</label> <label for="import-anns" class="control-label muted">URL :</label>
<div class="controls"> <div class="controls">
<input type="text" id="import-anns" name="import_xml" data-fv-validation="required;url;" data-fv-messages="Cannot be empty; Must be an URL.;" /> <input type="text" id="import-anns" name="import_xml" data-fv-validation="required;url;" data-fv-messages="Cannot be empty; Must be an URL.;" />
</div> </div>
</div> </div>
</div> </div>
<div class="form-actions"> <div class="form-actions">
<input type="submit" value="Import" class="btn btn-primary"> <input type="submit" value="Import" class="btn btn-primary">
</div> </div>
</form> --> </form> -->
<script type="text/javascript"> <script type="text/javascript">
var form = new FormValidator($("#import-anns-xls")); var form = new FormValidator($("#import-anns-xls"));
form.validate_functions.mustbexls = function(val){ form.validate_functions.mustbexls = function(val){
var t = val.split("."), var t = val.split("."),
ext = t[t.length - 1]; ext = t[t.length - 1];
return (ext == "xls" || ext == "xlsx") return (ext == "xls" || ext == "xlsx")
} }
var form = new FormValidator($("#import-anns-wp-xml")); var form = new FormValidator($("#import-anns-wp-xml"));
form.validate_functions.mustbexml = function(val){ form.validate_functions.mustbexml = function(val){
var t = val.split("."), var t = val.split("."),
ext = t[t.length - 1]; ext = t[t.length - 1];
return (ext == "xml") return (ext == "xml")
} }
$(document).ready(function(){ $(document).ready(function(){
function update_thread(){ function update_thread(){
$.post("<%=admin_threads_get_status_path%>",{"id": "<%=params[:thread_id]%>"}).done(function(data){ $.post("<%=admin_threads_get_status_path%>",{"id": "<%=params[:thread_id]%>"}).done(function(data){

View File

@ -94,12 +94,11 @@ zh_tw:
view_count: 瀏覽人次 view_count: 瀏覽人次
department: 單位 department: 單位
add_new: 新建 add_new: 新建
import: 匯入
export_to_excel: 匯出至Excel檔 export_to_excel: 匯出至Excel檔
export_all_anns: 匯出所有公告 export_all_anns: 匯出所有公告
import_from_excel: 從Excel檔匯入 import_from_excel: 從Excel檔匯入
download_example_sheet_here: 在此下載範例 download_example_sheet_here: 在此下載範例
please_create_tags_cats: 甲、 請事先建立所有標籤及分類。 僅限Excel檔。 please_create_tags_cats: 請事先建立所有標籤及分類。 僅限Excel檔。
create_atleast_one_cat: 匯入前, 請先建立至少一個類別 create_atleast_one_cat: 匯入前, 請先建立至少一個類別
import_from_wp_xml: 從WordPress XML檔匯入 import_from_wp_xml: 從WordPress XML檔匯入
top_limit: 最高設限 top_limit: 最高設限
@ -111,7 +110,7 @@ zh_tw:
approve: 通過 approve: 通過
feed_name: Feed 標題 feed_name: Feed 標題
settings: 設定 settings: 設定
import: 匯入 / 匯出 import: 匯入 / 匯出
rssfeed: RSS 供給連結 rssfeed: RSS 供給連結
jsonfeed: JSON 供給連結 jsonfeed: JSON 供給連結
feed_list: 訂閱清單 feed_list: 訂閱清單

View File

@ -147,7 +147,7 @@ module Announcement
:priority=>6, :priority=>6,
:active_for_action=>{'admin/announcements'=>'feed'}, :active_for_action=>{'admin/announcements'=>'feed'},
:available_for => 'managers' :available_for => 'managers'
context_link 'announcement.import', context_link 'announcement.import',
:link_path=>"admin_announcement_import_path" , :link_path=>"admin_announcement_import_path" ,
:priority=>7, :priority=>7,
:active_for_action=>{'admin/announcements'=>'import'}, :active_for_action=>{'admin/announcements'=>'import'},