From 673afd2f5ad839732c63c6c9ca6607874e0946bc Mon Sep 17 00:00:00 2001 From: tseng yen chi Date: Thu, 26 Jan 2017 14:30:59 +0800 Subject: [PATCH] import form wp xml now available --- .../admin/announcements_controller.rb | 9 ++++ app/helpers/admin/announcements_helper.rb | 44 +++++++++++++++++++ app/views/admin/announcements/import.html.erb | 24 ++++++++++ config/routes.rb | 1 + 4 files changed, 78 insertions(+) diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 21a9b5f..af62201 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -67,6 +67,15 @@ class Admin::AnnouncementsController < OrbitAdminController redirect_to admin_announcements_path end + def import + end + + + def import_from_wp + import_from_wordpress params["import_xml"].tempfile + redirect_to admin_announcements_path + end + def importanns workbook = RubyXL::Parser.parse(params["import_file"].tempfile) categories = @module_app.categories.asc(:created_at).to_a diff --git a/app/helpers/admin/announcements_helper.rb b/app/helpers/admin/announcements_helper.rb index 57aff77..e13688a 100644 --- a/app/helpers/admin/announcements_helper.rb +++ b/app/helpers/admin/announcements_helper.rb @@ -210,6 +210,50 @@ module Admin::AnnouncementsHelper File.delete(file) 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 if (current_user.is_admin? rescue false) @access_level = "admin" diff --git a/app/views/admin/announcements/import.html.erb b/app/views/admin/announcements/import.html.erb index 6bb67af..d361b8c 100644 --- a/app/views/admin/announcements/import.html.erb +++ b/app/views/admin/announcements/import.html.erb @@ -38,6 +38,24 @@ <% end %> + + + +
+

Import from Word Press XML

+ <%= hidden_field_tag :authenticity_token, form_authenticity_token %> +
+
+ +
+ +
+
+
+
+ +
+