diff --git a/app/controllers/admin/import_controller.rb b/app/controllers/admin/import_controller.rb index 91659a8..4a2bbbc 100644 --- a/app/controllers/admin/import_controller.rb +++ b/app/controllers/admin/import_controller.rb @@ -204,7 +204,7 @@ class Admin::ImportController < OrbitAdminController data = response.body data = JSON.parse(data) pages = data["pages"] - @@import_stats["total"] = data["total_pages"] + @@import_stats["total"] = data["total_pages"].to_i @@import_stats["current_stattus"] = 0 pages.each do |page| @@import_stats["current_page"] = page["page_id"] @@ -267,16 +267,77 @@ class Admin::ImportController < OrbitAdminController end def get_import_status - if @@thread.alive? - render :json => {"success" => true, "total_pages" => @@import_stats["total"], "current_status" => @@import_stats["current_stattus"], "current_page" => @@import_stats["current_page"], "current_page_name" => @@import_stats["current_page_name"]} - else - if @@import_stats["current_stattus"] == @@import_stats["total"] - render :json => {"success" => true, "total_pages" => @@import_stats["total"], "current_status" => @@import_stats["current_stattus"], "current_page" => @@import_stats["current_page"], "current_page_name" => @@import_stats["current_page_name"]} - + case params["module"] + when "pages" + if @@thread.alive? + render :json => {"success" => true, "total_pages" => @@import_stats["total"], "current_status" => @@import_stats["current_stattus"], "current_page" => @@import_stats["current_page"], "current_page_name" => @@import_stats["current_page_name"]} else - render :json => {"success" => false, "current_page" => @@import_stats["current_page"], "current_page_name" => @@import_stats["current_page_name"]} + if @@import_stats["current_stattus"] == @@import_stats["total"] + render :json => {"success" => true, "total_pages" => @@import_stats["total"], "current_status" => @@import_stats["current_stattus"], "current_page" => @@import_stats["current_page"], "current_page_name" => @@import_stats["current_page_name"]} + + else + render :json => {"success" => false, "current_page" => @@import_stats["current_page"], "current_page_name" => @@import_stats["current_page_name"]} + end + end + when "gallery" + if @@thread.alive? + render :json => {"success" => true, "total_images" => @@import_stats["total_images"], "current_status" => @@import_stats["current_status"], "current_album_id" => @@import_stats["current_album_id"], "current_album_name" => @@import_stats["current_album_name"]} + else + if @@import_stats["current_status"] == @@import_stats["total_images"] + render :json => {"success" => true, "total_images" => @@import_stats["total_images"], "current_status" => @@import_stats["current_status"], "current_album_id" => @@import_stats["current_album_id"], "current_album_name" => @@import_stats["current_album_name"]} + else + render :json => {"success" => false, "current_album_id" => @@import_stats["current_album_id"], "current_album_name" => @@import_stats["current_album_name"]} + end + end end + end + + def rss2_galleries + 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_images"] = data["total_images"].to_i + @@import_stats["current_status"] = 0 + albums = data["albums"] + current_locale = I18n.locale + I18n.locale = :en + category = Category.where(:title => "RSS2 Gallery").first + I18n.locale = current_locale + if category.nil? + module_app = ModuleApp.find_by_key("gallery") + category = Category.new + category.title_translations = {"en" => "RSS2 Gallery", "zh_tw" => "RSS2畫廊"} + category.save + module_app.categories << category + end + albums.each do |album| + @@import_stats["current_album_id"] = album["albumid"] + if Album.where(:rss2_id => album["albumid"]).count == 0 + @@import_stats["current_album_name"] = album["name"]["zh_tw"] + newalbum = Album.new(:name_translations => {"en" => album["name"]["en"], "zh_tw" => album["name"]["zh_tw"]}, :description_translations => {"en" => album["description"]["en"], "zh_tw" => album["description"]["zh_tw"]},"rss2_id" => album["albumid"]) + newalbum.category_id = category.id + newalbum.save + images = album["images"] + if !images.blank? + images.each do |image| + if AlbumImage.where(:rss2_id => image["photoid"]).count == 0 + img = newalbum.album_images.new + img.description_translations = {"en" => image["description"]["en"], "zh_tw" => image["description"]["zh_tw"]} + img.remote_file_url = image["image"] + img.save + end + @@import_stats["current_status"] = @@import_stats["current_status"] + 1 + end + end + end + end end + @@thread.abort_on_exception = true + render :json => {"success" => true}.to_json end private diff --git a/app/templates/orbit_bootstrap/home/pagination.html.erb b/app/templates/orbit_bootstrap/home/pagination.html.erb index 7a5e994..96c2b2f 100644 --- a/app/templates/orbit_bootstrap/home/pagination.html.erb +++ b/app/templates/orbit_bootstrap/home/pagination.html.erb @@ -1,7 +1,3 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/app/views/admin/import/index.html.erb b/app/views/admin/import/index.html.erb index e91cfc3..c96d3af 100644 --- a/app/views/admin/import/index.html.erb +++ b/app/views/admin/import/index.html.erb @@ -9,7 +9,7 @@ margin-bottom: 10px; } #import-container{ - width: 600px; + width: auto; margin: 0 auto; } #loading{ @@ -32,6 +32,8 @@ } #site-url-wrapper{ height: 100px; + width: 600px; + margin: 0 auto; } #import-modules{ margin-top: 30px; @@ -63,6 +65,8 @@ font-size: 12px; } .alert{ + width: 500px; + margin: 0 auto; } .module-icon{ font-size: 64px; @@ -133,6 +137,13 @@ <%= t("page_content.page") %> + +
@@ -173,6 +184,9 @@ case "pages": importPages(); break; + case "gallery": + importGallery(); + break; default: getModuleCategories(); } @@ -186,6 +200,7 @@ var importPages = function(){ $("#import-progress .bar").css('width','0%'); + $("#import-progress-text").html("Importing Pages...
"); $.ajax({ type: "post", dataType: "json", @@ -193,7 +208,19 @@ data : {"url" : url+'?module='+module} }).done(function(){ startCheckingImportStatus(); - $("#import-progress-text").html("Importing Pages...
"); + }) + } + + var importGallery = function(){ + $("#import-progress .bar").css('width','0%'); + $("#import-progress-text").html("Importing Galleries...
"); + $.ajax({ + type: "post", + dataType: "json", + url: '<%= admin_import_rss2_galleries_path %>', + data : {"url" : url+'?module='+module} + }).done(function(){ + startCheckingGalleryImportStatus(); }) } @@ -201,6 +228,7 @@ $.ajax({ type : "get", dataType : "json", + data : {"module" : module}, url : "<%= admin_import_get_import_status_path %>" }).done(function(status){ if(status.success == true){ @@ -225,6 +253,34 @@ }) } + var startCheckingGalleryImportStatus = 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.total_images != null){ + var percent = (status.current_status * 100) / status.total_images; + $("#import-progress .bar").css('width',percent + "%"); + $("#import-progress-text").html("Importing album " + status.current_album_name + " .. " + status.current_status + "/" + status.total_images + "Images
"); + } + if(status.total_images != status.current_status){ + setTimeout("startCheckingGalleryImportStatus()",700); + }else if(status.total_images == null){ + setTimeout("startCheckingGalleryImportStatus()",700); + }else{ + $("#import-progress-text").html("Finished importing gallery.
"); + } + }else if(status.success == false){ + $("#import-progress-text").html("Importing failed, click on start to try again."); + $("#alert-msg").text("Error importing album " + status.current_album_name + " ID : " + status.current_album_id) + $(".alert-danger").removeClass("hide").show(); + } + }) + } + var importModule = function(m){ module = m; dataList=null; diff --git a/config/routes.rb b/config/routes.rb index 853a3f1..5a0552b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -206,6 +206,7 @@ Orbit::Application.routes.draw do post "import/rss2_archive" => "import#rss2_archive" post "import/rss2_pages" => "import#rss2_pages" get "import/get_import_status" => "import#get_import_status" + post "import/rss2_galleries" => "import#rss2_galleries" end get 'mobile', to: 'pages#home'