diff --git a/app/controllers/admin/import_controller.rb b/app/controllers/admin/import_controller.rb index 4a2bbbc..ecfae1f 100644 --- a/app/controllers/admin/import_controller.rb +++ b/app/controllers/admin/import_controller.rb @@ -289,7 +289,17 @@ class Admin::ImportController < OrbitAdminController render :json => {"success" => false, "current_album_id" => @@import_stats["current_album_id"], "current_album_name" => @@import_stats["current_album_name"]} end end - end + when "links" + if @@thread.alive? + render :json => {"success" => true, "current_import" => @@import_stats["current_import"], "total_links" => @@import_stats["total_links"], "current_status" => @@import_stats["current_status"], "current_link_id" => @@import_stats["current_link_id"], "current_link_name" => @@import_stats["current_album_name"]} + else + if @@import_stats["current_status"] == @@import_stats["total_links"] + render :json => {"success" => true, "current_import" => @@import_stats["current_import"], "total_links" => @@import_stats["total_links"], "current_status" => @@import_stats["current_status"], "current_link_id" => @@import_stats["current_link_id"], "current_link_name" => @@import_stats["current_link_name"]} + else + render :json => {"success" => false, "current_link_id" => @@import_stats["current_link_id"], "current_link_name" => @@import_stats["current_link_name"]} + end + end + end end def rss2_galleries @@ -340,6 +350,50 @@ class Admin::ImportController < OrbitAdminController render :json => {"success" => true}.to_json end + def rss2_links + uri = URI.parse(params['url']) + @@thread = Thread.new do + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Get.new(uri.request_uri) + response = http.request(request) + data = response.body + data = JSON.parse(data) + @@import_stats["total_links"] = data["total_links"].to_i + @@import_stats["current_import"] = "categories" + @@import_stats["current_status"] = 0 + categories = data["categories"] + current_locale = I18n.locale + I18n.locale = :zh_tw + module_app = ModuleApp.find_by_key("web_resource") + categories.each do |category| + cat = Category.where(:title => category[1]["zh_tw"], :module_app_id => module_app.id).first + if cat.nil? + cat = Category.new + cat.title_translations = {"en" => category[1]["en"], "zh_tw" => category[1]["zh_tw"]} + cat.save + module_app.categories << cat + end + end + @@import_stats["current_import"] = "links" + links = data["links"] + links.each do |link| + l = WebLink.where(:rss2_id => link["link_id"]).first + if l.nil? + @@import_stats["current_link_id"] = link["link_id"] + @@import_stats["current_link_name"] = link["title"]["zh_tw"] + weblink = WebLink.new(:title_translations => {"en" => link["title"]["en"], "zh_tw" => link["title"]["zh_tw"]}, :context_translations => {:en => link["description"]["en"], :zh_tw => link["description"]["zh_tw"]}, :url_translations => {"en" => link["url"], "zh_tw" => link["url"]}, :rss2_id => link["link_id"]) + category = Category.where(:title => link["category_name"], :module_app_id => module_app).first + weblink.category_id = category.id + weblink.save + end + @@import_stats["current_status"] = @@import_stats["current_status"] + 1 + end + I18n.locale = current_locale + end + @@thread.abort_on_exception = true + render :json => {"success" => true}.to_json + end + private def page_present?(page) diff --git a/app/views/admin/import/index.html.erb b/app/views/admin/import/index.html.erb index c96d3af..ce62863 100644 --- a/app/views/admin/import/index.html.erb +++ b/app/views/admin/import/index.html.erb @@ -144,6 +144,13 @@ <%= t("gallery.gallery") %> + +
@@ -187,6 +194,9 @@ case "gallery": importGallery(); break; + case "links": + importLinks(); + break; default: getModuleCategories(); } @@ -211,6 +221,19 @@ }) } + var importLinks = function(){ + $("#import-progress .bar").css('width','0%'); + $("#import-progress-text").html("Importing Links...
"); + $.ajax({ + type: "post", + dataType: "json", + url: '<%= admin_import_rss2_links_path %>', + data : {"url" : url+'?module='+module} + }).done(function(){ + startCheckinglinksImportStatus(); + }) + } + var importGallery = function(){ $("#import-progress .bar").css('width','0%'); $("#import-progress-text").html("Importing Galleries...
"); @@ -224,6 +247,40 @@ }) } + var startCheckinglinksImportStatus = function(){ + $.ajax({ + type : "get", + dataType : "json", + data : {"module" : module}, + url : "<%= admin_import_get_import_status_path %>" + }).done(function(status){ + if(status.success == true){ + if(status.current_import == "categories"){ + $("#import-progress-text").html("Importing categories
"); + setTimeout("startCheckinglinksImportStatus()",700); + }else{ + if(status.total_links != null){ + var percent = (status.current_status * 100) / status.total_links; + $("#import-progress .bar").css('width',percent + "%"); + $("#import-progress-text").html("Importing links " + status.current_link_name + " .. " + status.current_status + "/" + status.total_links + "
"); + } + if(status.total_links != status.current_status){ + setTimeout("startCheckinglinksImportStatus()",700); + }else{ + if(status.total_links == null){ + setTimeout("startCheckinglinksImportStatus()",700); + } + $("#import-progress-text").html("Finished importing links.
"); + } + } + }else if(status.success == false){ + $("#import-progress-text").html("Importing failed, click on start to try again."); + $("#alert-msg").text("Error importing link " + status.current_link_name + " ID : " + status.current_link_id) + $(".alert-danger").removeClass("hide").show(); + } + }) + } + var startCheckingImportStatus = function(){ $.ajax({ type : "get", diff --git a/config/routes.rb b/config/routes.rb index 9b7680e..7237292 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -208,6 +208,7 @@ Orbit::Application.routes.draw do post "import/rss2_pages" => "import#rss2_pages" get "import/get_import_status" => "import#get_import_status" post "import/rss2_galleries" => "import#rss2_galleries" + post "import/rss2_links" => "import#rss2_links" end get 'mobile', to: 'pages#home'