Fix import and export.
This commit is contained in:
parent
d39a4461b3
commit
1879d976d4
|
@ -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?
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<% 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 %>
|
||||
<% if @thread %>
|
||||
<div id="threadModal" class="modal hide fade in" tabindex="-1" role="dialog" aria-labelledby="threadModal" aria-hidden="false">
|
||||
|
@ -31,41 +31,30 @@
|
|||
</div>
|
||||
<% end %>
|
||||
<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>
|
||||
<div class="control-group">
|
||||
<div class="controls">
|
||||
<a href="<%= admin_announcement_export_excel_path %>"><%= t("announcement.export_all_anns") %></a>
|
||||
</div>
|
||||
</div>
|
||||
<h3 style="padding-left: 30px;"><%= t("announcement.import_from_excel") %></h3>
|
||||
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
|
||||
<div class="input-area">
|
||||
<% if @module_app.categories.count > 0 %>
|
||||
<div class="control-group">
|
||||
<div class="controls">
|
||||
<a href="<%= admin_announcement_excel_format_path(:format => "xlsx") %>"><%= t("announcement.download_example_sheet_here") %></a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label for="import-anns" class="control-label muted"><%= t("upload") %></label>
|
||||
<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.;" />
|
||||
<span class="help-block"><%= t("announcement.please_create_tags_cats") %></span>
|
||||
</div>
|
||||
</div>
|
||||
<% else %>
|
||||
<div class="control-group">
|
||||
<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 %>
|
||||
<h3 style="padding-left: 30px;"><%= t("announcement.export_to_excel") %></h3>
|
||||
<div class="control-group">
|
||||
<div class="controls">
|
||||
<a href="<%= admin_announcement_export_excel_path %>"><%= t("announcement.export_all_anns") %></a>
|
||||
</div>
|
||||
</div>
|
||||
<h3 style="padding-left: 30px;"><%= t("announcement.import_from_excel") %></h3>
|
||||
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
|
||||
<div class="input-area">
|
||||
<div class="control-group">
|
||||
<div class="controls">
|
||||
<a href="<%= admin_announcement_excel_format_path(:format => "xlsx") %>"><%= t("announcement.download_example_sheet_here") %></a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label for="import-anns" class="control-label muted"><%= t("upload") %></label>
|
||||
<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.;" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<input type="submit" value="<%= t("restful_actions.import") %>" class="btn btn-primary">
|
||||
</div>
|
||||
</form>
|
||||
|
||||
|
||||
|
@ -86,33 +75,33 @@
|
|||
</div>
|
||||
</form>
|
||||
<!-- <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>
|
||||
<%#= hidden_field_tag :authenticity_token, form_authenticity_token %>
|
||||
<div class="input-area">
|
||||
<div class="control-group">
|
||||
<label for="import-anns" class="control-label muted">URL :</label>
|
||||
<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.;" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<input type="submit" value="Import" class="btn btn-primary">
|
||||
</div>
|
||||
<h3 style="padding-left: 30px;">Import from XML</h3>
|
||||
<%#= hidden_field_tag :authenticity_token, form_authenticity_token %>
|
||||
<div class="input-area">
|
||||
<div class="control-group">
|
||||
<label for="import-anns" class="control-label muted">URL :</label>
|
||||
<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.;" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<input type="submit" value="Import" class="btn btn-primary">
|
||||
</div>
|
||||
</form> -->
|
||||
<script type="text/javascript">
|
||||
var form = new FormValidator($("#import-anns-xls"));
|
||||
form.validate_functions.mustbexls = function(val){
|
||||
var t = val.split("."),
|
||||
ext = t[t.length - 1];
|
||||
return (ext == "xls" || ext == "xlsx")
|
||||
}
|
||||
var form = new FormValidator($("#import-anns-wp-xml"));
|
||||
form.validate_functions.mustbexml = function(val){
|
||||
var t = val.split("."),
|
||||
ext = t[t.length - 1];
|
||||
return (ext == "xml")
|
||||
}
|
||||
var form = new FormValidator($("#import-anns-xls"));
|
||||
form.validate_functions.mustbexls = function(val){
|
||||
var t = val.split("."),
|
||||
ext = t[t.length - 1];
|
||||
return (ext == "xls" || ext == "xlsx")
|
||||
}
|
||||
var form = new FormValidator($("#import-anns-wp-xml"));
|
||||
form.validate_functions.mustbexml = function(val){
|
||||
var t = val.split("."),
|
||||
ext = t[t.length - 1];
|
||||
return (ext == "xml")
|
||||
}
|
||||
$(document).ready(function(){
|
||||
function update_thread(){
|
||||
$.post("<%=admin_threads_get_status_path%>",{"id": "<%=params[:thread_id]%>"}).done(function(data){
|
||||
|
|
|
@ -94,12 +94,11 @@ zh_tw:
|
|||
view_count: 瀏覽人次
|
||||
department: 單位
|
||||
add_new: 新建
|
||||
import: 匯入
|
||||
export_to_excel: 匯出至Excel檔
|
||||
export_all_anns: 匯出所有公告
|
||||
import_from_excel: 從Excel檔匯入
|
||||
download_example_sheet_here: 在此下載範例
|
||||
please_create_tags_cats: 甲、 請事先建立所有標籤及分類。 僅限Excel檔。
|
||||
please_create_tags_cats: 請事先建立所有標籤及分類。 僅限Excel檔。
|
||||
create_atleast_one_cat: 匯入前, 請先建立至少一個類別
|
||||
import_from_wp_xml: 從WordPress XML檔匯入
|
||||
top_limit: 最高設限
|
||||
|
|
|
@ -147,7 +147,7 @@ module Announcement
|
|||
:priority=>6,
|
||||
:active_for_action=>{'admin/announcements'=>'feed'},
|
||||
:available_for => 'managers'
|
||||
context_link 'announcement.import',
|
||||
context_link 'announcement.import',
|
||||
:link_path=>"admin_announcement_import_path" ,
|
||||
:priority=>7,
|
||||
:active_for_action=>{'admin/announcements'=>'import'},
|
||||
|
|
Loading…
Reference in New Issue