orbit4-5/app/views/admin/import/index.html.erb

740 lines
25 KiB
Plaintext

<style type="text/css">
.import-wrapper{
padding: 10px;
}
.import-wrapper .import-url{
width: 565px;
}
.import-wrapper .btn{
margin-bottom: 10px;
}
#import-container{
width: auto;
margin: 0 auto;
}
#loading{
margin: 20px 0;
text-align: center;
display:none;
}
#loading img{
width: 50px;
}
#import-head{
font-size: 18px;
text-align: center;
text-shadow: 1px 1px 1px #FFF;
padding-bottom: 10px;
margin-top: 100px;
}
#import-head i{
font-size: 64px;
}
#site-url-wrapper{
height: 100px;
width: 600px;
margin: 0 auto;
}
#import-modules{
margin-top: 30px;
text-align: center;
}
.module{
text-align: center;
width: 120px;
display: inline-block;
margin: 8px 5px;
padding: 15px 0px;
}
.module i{
font-size: 48px;
border-radius: 10px;
border: 1px solid #FFF;
background: #888;
cursor: pointer;
padding: 15px 18px;
box-shadow: 0 0 10px #555 inset;
color: #FFF;
}
.module i:hover{
background: #438CDB;
box-shadow: none;
}
#import-modules .lead{
margin-bottom: 0px;
font-size: 12px;
}
.alert{
width: 500px;
margin: 20px auto 0;
}
.module-icon{
font-size: 64px;
border-radius: 10px;
padding: 15px 18px;
color: #888;
}
#import-progress{
border: 0px solid #CCC;
text-align: center;
width: 400px;
margin: 100px auto 0 auto;
}
#import-progress .progress{
margin-bottom: 0px;
}
#import-progress-text{
font-size: 16px;
line-height: 25px;
color: #666;
height: 50px;
margin-bottom: 5px;
}
</style>
<div id="import-container">
<div id="import-head" class="muted">
<i class='icon-cloud-download'></i><br/>
Import RSS2
</div>
<div id="loading">
<%= image_tag 'preloader.gif' %><br/>
<span id="progress_msg"></span>
</div>
<div id="site-url-wrapper" class="import-wrapper">
<input type="text" class="import-url" name="url" value="" placeholder="http://www.rulingcom.com">
<button id="check-site-url" class="btn btn-primary pull-right">Next <i class="icons-arrow-right"></i></button>
<span><a href='http://installer.tp.rulingcom.com/rss2-export-api.zip'>Download Rss2 Export API</a></span>
</div>
<div id="import-modules" class="hide">
<div id="members-module" class="module" onclick="importModule('members')">
<div class="lead muted">
<i class="icons-users"></i><br/>
<%= t("member_") %>
</div>
</div>
<div id="announcement-module" class="module" onclick="importModule('announcement')">
<div class="lead muted">
<i class="icons-megaphone"></i><br/>
<%= t("announcement.announcement") %>
</div>
</div>
<div id="archive-module" class="module" onclick="importModule('archive')">
<div class="lead muted">
<i class="icons-archive"></i><br/>
<%= t("archive.archive") %>
</div>
</div>
<div id="pages-module" class="module" onclick="importModule('pages')">
<div class="lead muted">
<i class="icons-newspaper"></i><br/>
<%= t("page_content.page") %>
</div>
</div>
<div id="gallery-module" class="module" onclick="importModule('gallery')">
<div class="lead muted">
<i class="icons-pictures"></i><br/>
<%= t("gallery.gallery") %>
</div>
</div>
<div id="links-module" class="module" onclick="importModule('links')">
<div class="lead muted">
<i class="icons-link"></i><br/>
<%= t("web_resource") %>
</div>
</div>
</div>
<div id="import-progress" class="hide">
<div class="head" style="border: 0px solid #CCC; margin: 5px; text-align: center;">
<i class=""></i>
</div>
<div class="content text-right" style="border: 0px solid #CCC; height: 100px; margin: 5px">
<div class="progress">
<div class="bar" style="width: 0%;"></div>
</div>
<div id="import-progress-text" class="text-center"></div>
<button id="import-start-btn" class="btn btn-primary">Start</i></button>
<button id="import-clean-btn" class="btn btn-danger hide">Clean</i></button>
<button id="import-back-btn" class="btn pull-left">Back</i></button>
</div>
</div>
<div class="alert alert-danger hide">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
<span id="alert-msg"><%= @msg %></span>
</div>
</div>
<script type="text/javascript">
var url;
var categories=null;
var dataList=Array();
var module;
var progress = 0;
$(".import-url").keyup(function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if (code==13) { checkSiteUrl(); }
});
$("#check-site-url").click(function(){checkSiteUrl();});
$("#import-start-btn").click(function(){
switch(module){
case "pages":
$(this).fadeOut();
getPagesInfo();
break;
case "gallery":
$(this).fadeOut();
importGallery();
break;
case "links":
$(this).fadeOut();
importLinks();
break;
case "members":
$(this).fadeOut();
importMembersInfo();
break;
default:
getModuleCategories();
}
});
$("#import-clean-btn").click(function(){
$("#import-progress-text").html("Cleaning up " + module + ". Please wait.");
$.ajax({
url : "<%= admin_import_clean_old_data_path %>",
type : "post",
dataType : "json",
data : {"module" : module}
}).done(function(){
$("#import-progress-text").html("Cleaning up successful. Ready to import.");
})
})
$("#import-back-btn").click(function(){
$("#import-progress").fadeOut(300,function(){
$("#import-head").fadeIn(0);
$('#import-start-btn').show();
$("#import-modules").fadeIn(300);
});
});
var pagesData = null;
var childPagesData = null;
var getPagesInfo = function(){
$("#import-progress .bar").css('width','0%');
$("#import-progress-text").html("Importing Pages...<br/>");
$.ajax({
type: "get",
dataType: "json",
url: '<%= admin_import_rss2_pages_info_path %>',
data : {"url" : url+'?module='+module}
}).done(function(data){
if(data.success){
pagesData = data.pages;
if(pagesData.length > 0){
importPage(0);
}else{
$("#import-progress .bar").css('width','100%');
$("#import-progress-text").html("No pages to import.");
}
}
}).fail(function(){
$("#import-progress-text").html("Importing failed, click on start to try again.");
$("#alert-msg").text("There was an unknown error");
$(".alert-danger").removeClass("hide").show();
})
}
var importPage = function(number){
var current_page = pagesData[number];
$.ajax({
type : "get",
dataType : "json",
data : {"url" : url+'?module=' + module + "&page_id=" + current_page.ID},
url : "<%= admin_import_rss2_pages_path %>"
}).done(function(data){
if(data.success == true){
var percent = ((number + 1) * 100) / pagesData.length;
$("#import-progress .bar").css('width',percent + "%");
$("#import-progress-text").html("Importing page " + current_page.ID + "<br/>");
if(data.childpages.length > 0){
childPagesData = data.childpages;
importChildPage(0,number,data.site_url,data.page_id, current_page.ID);
}else if((number + 1) < pagesData.length){
importPage(number + 1);
}else{
$("#import-progress .bar").css('width',"100%");
$("#import-progress-text").html("Finished importing pages.");
}
}
}).fail(function(){
$("#alert-msg").append("Error importing page " + current_page.ID + "<br />");
$(".alert-danger").removeClass("hide").show();
if((number + 1) < pagesData.length){
importPage(number + 1);
}else{
$("#import-progress .bar").css('width',"100%");
$("#import-progress-text").html("Finished importing pages.");
}
})
}
var importChildPage = function(childPageNumber, parentPageNumber, site_url, page_id, current_page_id){
var current_page = childPagesData[childPageNumber];
$("#import-progress-text").html("Importing child pages for page " + current_page_id + ". This may take a while.<br/>");
$.ajax({
type : "post",
dataType : "json",
data : {"childpage" : current_page, "site_url" : site_url, "page_id" : page_id, "current_index" : childPageNumber},
url : "<%= admin_import_rss2_child_page_path %>"
}).done(function(data){
if(data.success == true){
if((childPageNumber + 1) < childPagesData.length){
importChildPage((childPageNumber + 1), parentPageNumber, site_url, page_id, current_page_id);
}else if((parentPageNumber + 1) < pagesData.length){
importPage(parentPageNumber + 1)
}else{
$("#import-progress .bar").css('width',"100%");
$("#import-progress-text").html("Finished importing pages.");
}
}
}).fail(function(){
$("#alert-msg").append("Importing failed for page " + current_page[title][zh_tw] + ", please try manually or retry.<br />");
$(".alert-danger").removeClass("hide").show();
if((childPageNumber + 1) < childPagesData.length){
importChildPage((childPageNumber + 1), parentPageNumber);
}else if((parentPageNumber + 1) < pagesData.length){
importPage(parentPageNumber + 1)
}else{
$("#import-progress .bar").css('width',"100%");
$("#import-progress-text").html("Finished importing pages.");
}
})
}
var importLinks = function(){
$("#import-progress .bar").css('width','0%');
$("#import-progress-text").html("Importing Links...<br/>");
$.ajax({
type: "post",
dataType: "json",
url: '<%= admin_import_rss2_links_path %>',
data : {"url" : url+'?module='+module}
}).done(function(){
}).fail(function(){
$("#import-progress-text").html("Importing failed, click on start to try again.");
$("#alert-msg").text("There was an unknown error");
$(".alert-danger").removeClass("hide").show();
})
startCheckinglinksImportStatus();
}
var gallery_data = null;
var importGallery = function(){
$("#import-progress .bar").css('width','0%');
$("#import-progress-text").html("Importing Galleries...<br/>").delay(1000).html("Creating albums...<br />");
$.ajax({
type: "post",
dataType: "json",
url: '<%= admin_import_rss2_galleries_path %>',
data : {"url" : url+'?module='+module}
}).done(function(data){
$("#import-progress-text").html("Importing Images...<br/>");
gallery_data = data.images;
if(gallery_data.length > 0){
importImagesForAlbums(0);
}else{
$("#import-progress .bar").css('width','100%');
$("#import-progress-text").html("No images to import.");
}
}).fail(function(){
$("#import-progress-text").html("Importing failed, click on start to try again.");
$("#alert-msg").text("There was an unknown error");
$(".alert-danger").removeClass("hide").show();
})
}
var importImagesForAlbums = function(number){
var image = gallery_data[number],
total = gallery_data.length;
$("#import-progress-text").html("Importing Album " + image.album_name + "..." + (number + 1) + "/" + total + "<br/>");
$.ajax({
type: "post",
url: "<%= admin_import_rss2_album_image_path %>",
dataType : "json",
data : image
}).done(function(status){
if(status.success == true){
var percent = ((number + 1) * 100) / total;
$("#import-progress .bar").css('width',percent + "%");
if(number != (total - 1)){
setTimeout(importImagesForAlbums(number + 1),300);
}else{
$("#import-progress-text").html("Finished importing albums.");
}
}
}).fail(function(){
$("#import-progress-text").html("Error importing album " + image.album_name);
$("#alert-msg").text("Error importing album " + image.album_name);
$(".alert-danger").removeClass("hide").show();
})
}
var sto = null,
new_members_data = [],
number_of_members_to_import = 5,
total_members = 0,
maximum_member_pages = 0;
var importMembersInfo = function(){
$("#import-progress .bar").css('width', '0%');
$("#import-progress-text").html("Getting Member Info..<br/>");
$.ajax({
type: "post",
url: '<%= admin_import_rss2_members_info_path %>',
data : {"url" : url+'?module='+module}
}).done(function(data){
if(data.success == true){
$("#import-progress-text").html("Now importing Members.<br/>");
total_members = parseInt(data.total_users);
if(total_members > 0){
maximum_member_pages = Math.ceil(total_members / number_of_members_to_import) - 1;
importMembers(0);
}else{
$("#import-progress .bar").css('width','100%');
$("#import-progress-text").html("No users to import.");
}
}
}).fail(function(){
$("#import-progress-text").html("Importing failed, click on start to try again.");
$("#alert-msg").text("There was an unknown error");
$(".alert-danger").removeClass("hide").show();
})
}
var importMembers = function(page_number){
$("#import-progress-text").html("Importing Members..<br/>");
$.ajax({
type: "post",
url: '<%= admin_import_rss2_members_path %>',
data : {"url" : url+'?module='+module+"&page=" + page_number + "&total=" + number_of_members_to_import}
}).done(function(data){
if(data.success == true){
var percent = ((number_of_members_to_import * (page_number + 1)) * 100) / total_members;
$("#import-progress .bar").css('width', percent + "%");
new_members_data = new_members_data.concat(data.members);
if(page_number < maximum_member_pages){
importMembers(page_number + 1);
}else{
$("#import-progress .bar").css('width', '100%');
importPlugins(0);
}
}
}).fail(function(){
$("#import-progress-text").html("Importing failed, click on start to try again.");
$("#alert-msg").text("There was an unknown error");
$(".alert-danger").removeClass("hide").show();
})
}
var plugins = ["patents","books", "projects", "researches", "honors", "diplomas", "experiences", "journalpapers", "conferencespapers"];
var failed_plugins = [];
var importPlugins = function(number){
var member = new_members_data[number],
member_status = (member.name == null ? "Importing plugins for id " + member.rss2_id : "Importing plugins for member " + member.name);
$("#import-progress .bar").css('width', '0%');
$("#import-progress-text").html(member_status + "<br/><div id='plugin_name'></div>");
importplugin(member.rss2_id, 0, number, member.name);
}
var importplugin = function(id,p,number,name){
var plugin = plugins[p];
$("#import-progress-text #plugin_name").html(plugin.charAt(0).toUpperCase() + plugin.slice(1));
$.ajax({
url : "/admin/import/import_plugin",
type : "get",
data : {"rss2_id" : id, "plugin" : plugin, "url" : url},
dataType : "json"
}).done(function(){
var percent = ((p + 1) * 100) / plugins.length;
$("#import-progress .bar").css('width', percent + '%');
if(p != plugins.length - 1){
importplugin(id, p + 1,number);
}else if(p == plugins.length - 1){
if((number + 1) != new_members_data.length){
importPlugins(number + 1);
}else{
$("#import-progress-text").html("Importing members finished.");
}
}
}).fail(function(){
failed_plugins.push(plugin);
$("#alert-msg").prepend("There was an unknown error importing " + plugin + " for " + (typeof name == "undefined" ? id : name) + ". <br/>");
$(".alert-danger").removeClass("hide").show();
if(p != plugins.length - 1){
importplugin(id, p + 1, number);
}else if(p == plugins.length - 1){
if((number + 1) != new_members_data.length){
importPlugins(number + 1);
}else{
$("#import-progress .bar").css('width', '100%');
$("#import-progress-text").html("Importing members finished.");
}
}
})
}
var importfailedplugin = function(id,p){
var plugin = failed_plugins[p];
$("#import-progress-text").append("Retrying " + plugin);
$.ajax({
url : "/admin/import_plugin",
type : "get",
data : {"rss2_id" : id, "plugin" : plugin},
dataType : "json"
}).done(function(){
var percent = ((p + failed_plugins.length + 1) * 100) / plugins.length;
$("#import-progress .bar").css('width', percent + '%');
if(p != failed_plugins.length - 1){
importfailedplugin(id, p + 1);
}
}).fail(function(){
$("#alert-msg").text("There was an unknown error importing" + plugin + ".");
$(".alert-danger").removeClass("hide").show();
if(p != failed_plugins.length - 1){
importfailedplugin(id, p + 1);
}
})
return true;
}
var startCheckingMemberImportStatus = 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_members != null){
var percent = (status.current_status * 100) / status.total_members;
$("#import-progress .bar").css('width',percent + "%");
$("#import-progress-text").html("Importing Member " + status.current_member_name + " .. " + status.current_status + "/" + status.total_members + "<br/>");
}
if(status.total_members != status.current_status){
sto = setTimeout("startCheckingMemberImportStatus()",700);
}else{
if(status.total_members == null){
sto = setTimeout("startCheckingMemberImportStatus()",700);
}
$("#import-progress-text").html("Finished importing Members.<br/>");
}
}else if(status.success == false){
$("#import-progress-text").html("Importing failed, click on start to try again.");
$("#alert-msg").text("Error importing member " + status.current_member_name + " Email : " + status.current_member)
$(".alert-danger").removeClass("hide").show();
}
}).fail(function(){
$("#import-progress-text").html("Importing failed, click on start to try again.");
$("#alert-msg").text("There was an unknown error");
$(".alert-danger").removeClass("hide").show();
})
}
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 <br/>");
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 + "<br/>");
}
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.<br/>");
}
}
}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();
}
}).fail(function(){
$("#import-progress-text").html("Importing failed, click on start to try again.");
$("#alert-msg").text("There was an unknown error");
$(".alert-danger").removeClass("hide").show();
})
}
var importModule = function(m){
module = m;
progress = 0;
dataList=Array();
categories=null;
$('#import-progress i').attr('class', $('#'+m+'-module i').attr('class')+' module-icon');
$("#import-progress .bar").css('width','0%');
$("#import-progress-text").html("");
$("#import-modules").fadeOut(300,function(){
$("#import-head").fadeOut(0);
$("#import-progress").fadeIn(300,function(){
});
});
}
var getModuleCategories= function(){
$("#import-progress-text").html("Importing Categories<br/><br/>");
$.ajax({
type: 'GET',
dataType: 'json',
url: '<%= admin_import_module_categories_path %>',
data: {module: module, url: url+'?module='+module+'&type=categories'}
}).done(function(data){
$("#import-progress-text").html("Done<br/><br/>");
categories = data;
getModuleDataList();
});
}
var getModuleDataList = function(){
$.ajax({
type: 'GET',
dataType: 'json',
url: '<%= admin_import_module_data_list_path %>',
data: {module: module, url: url+'?module='+module+'&type=dataList'}
}).done(function(data){
for(Sn in data){
dataList.push({'Sn':Sn, 'category':data[Sn]});
}
importModuleDatas();
});
}
var importModuleDatas = function(){
progress = 0;
$('#import-start-btn').hide();
$("#import-progress-text").html("Start import...<br/><br/>");
if(dataList!=null && categories!=null){
importModuleData();
}else{
setTimeout("importModuleDatas()", 3000);
}
}
var importModuleData = function(){
if(progress>=dataList.length) return;
var data = dataList[progress];
if(data['category']==0){
progress++;
importModuleData();
return;
}
if(!categories[data['category']]){
progress++;
importModuleData();
return;
}
var category_id = categories[data['category']]['id'];
var Sn = data['Sn'];
percentage = parseInt((progress/dataList.length)*100)+'%';
$("#import-progress-text").html(progress+' / '+dataList.length+'<br/>'+percentage);
$.ajax({
type: 'GET',
dataType: 'json',
url: '<%= admin_import_module_data_path %>',
data: {
module: module,
url: url+'?module='+module+'&type=data&Sn='+Sn,
type: 'data',
category: category_id
}
}).done(function(data){
}).fail(function(data){
$("#alert-msg").html('Faild to import Sn:'+progress+'<br/>Message:'+data['status']+'-'+data['statusText']);
$(".alert").fadeIn(300).delay(5000).fadeOut(300);
}).always(function(){
progress++;
percentage = parseInt((progress/dataList.length)*100)+'%';
$("#import-progress .bar").css('width',percentage);
$("#import-progress-text").html(progress+' / '+dataList.length+'<br/>'+percentage);
// if(percentage=="100%"){
// $("#import-progress").delay(1000).fadeOut(300,function(){
// $("#import-head").fadeIn(0);
// $("#import-modules").fadeIn(300);
// $('#import-start-btn').show();
// });
// }
importModuleData();
});
}
var checkSiteUrl = function(){
url = $(".import-url").val();
url = (url[url.length-1]=='/') ? url+'export/' : url+'/export/';
$(".alert").hide();
if(/http[s]?:\/\/[a-zA-Z0-9.-\/]+/.test(url)){
$("#site-url-wrapper").fadeOut(300, function(){
$("#progress_msg").html("Checking connection...");
$("#loading").fadeIn(0);
$.getJSON('<%= admin_import_check_url_path %>', {url: url},function(data){
$("#loading").fadeOut(function(){
if(data['status']=="ok"){
$("#import-head").animate({ marginTop: '0px' },{
duration: 300,
complete: function() {
$("#import-modules").fadeIn(300);
}
});
}else{
$("#alert-msg").html(data['status']);
$(".alert").fadeIn(300);
$("#site-url-wrapper").fadeIn(300);
}
});
});
});
}else{
$("#alert-msg").html("Invalid URL");
$(".alert").fadeIn(300).delay(2000).fadeOut(300);
}
}
</script>