import form wp xml now available

This commit is contained in:
tseng yen chi 2017-01-26 14:30:59 +08:00
parent e6852a91ad
commit 673afd2f5a
4 changed files with 78 additions and 0 deletions

View File

@ -67,6 +67,15 @@ class Admin::AnnouncementsController < OrbitAdminController
redirect_to admin_announcements_path redirect_to admin_announcements_path
end end
def import
end
def import_from_wp
import_from_wordpress params["import_xml"].tempfile
redirect_to admin_announcements_path
end
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 categories = @module_app.categories.asc(:created_at).to_a

View File

@ -210,6 +210,50 @@ module Admin::AnnouncementsHelper
File.delete(file) File.delete(file)
end end
def import_from_wordpress(xmlfile)
xml_file = File.read(xmlfile)
doc = Nokogiri::XML.parse(xml_file)
doc.xpath("//channel").each do|channel_data|
channel_data.xpath('//item').each do|itme|
bu = Bulletin.new
bu.approved = true
bu.title_translations = {"en" => itme.xpath('title').text, "zh_tw" => itme.xpath('title').text}
bu.text_translations = {"en" => itme.xpath('content:encoded').text, "zh_tw" => itme.xpath('content:encoded').text}
bu.postdate = itme.xpath('wp:post_date').text
itme.xpath('category').each do |i_cate|
if i_cate["domain"].to_s == "category"
cat = @module_app.categories.where(:title => i_cate.text.to_s).first rescue nil
if cat.nil?
cat = Category.new
cat.module_app = @module_app
cat.title_translations = {"en" => i_cate.text.to_s, "zh_tw" => i_cate.text.to_s}
cat.save
end
bu.category = cat
elsif i_cate["domain"].to_s == "post_tag"
tag = Tag.where(:name => i_cate.text.to_s )
if tag.nil? == false
tag = Tag.new
tag.name_translations = {"en" => i_cate.text.to_s, "zh_tw" => i_cate.text.to_s}
tag.module_app_ids << @module_app.id
tag.save
end
bu.tags=tag
end
end
bu.save
end
end
File.delete(xmlfile)
end
def load_access_level def load_access_level
if (current_user.is_admin? rescue false) if (current_user.is_admin? rescue false)
@access_level = "admin" @access_level = "admin"

View File

@ -38,6 +38,24 @@
</div> </div>
<% end %> <% end %>
</form> </form>
<!-- import from wp xml -->
<form action="<%= admin_announcement_import_from_wp_path %>" method="post" class="form-horizontal main-forms" id="import-anns-wp-xml" enctype="multipart/form-data">
<h3 style="padding-left: 30px;">Import from Word Press XML</h3>
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<div class="input-area">
<div class="control-group">
<label for="import-anns-wp-xml" class="control-label muted">Upload :</label>
<div class="controls">
<input type="file" id="import-anns-wp-xml" name="import_xml" data-fv-validation="required;mustbexml;" data-fv-messages="Cannot be empty; Must be an XML file.;" />
</div>
</div>
</div>
<div class="form-actions">
<input type="submit" value="Import" class="btn btn-primary">
</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"> <!-- <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 %>
@ -60,4 +78,10 @@
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"));
form.validate_functions.mustbexml = function(val){
var t = val.split("."),
ext = t[t.length - 1];
return (ext == "xml")
}
</script> </script>

View File

@ -20,6 +20,7 @@ Rails.application.routes.draw do
get 'announcement/import', to: 'announcements#import' get 'announcement/import', to: 'announcements#import'
post 'announcement/createsettings', to: 'announcements#createsettings' post 'announcement/createsettings', to: 'announcements#createsettings'
patch 'announcement/updatesettings', to: 'announcements#updatesettings' patch 'announcement/updatesettings', to: 'announcements#updatesettings'
post 'announcement/import_from_wp', to: 'announcements#import_from_wp'
resources :announcements resources :announcements
end end