<!--#include file="../inc/config.asp"-->
<!--#include file="../inc/conn.asp"-->
<!-- #include file="inc/Session.asp" -->
<!-- #include file="inc/function.asp" -->
<!-- #include file="inc/checkrole.asp" -->
<%
'''''''''''''''''''''''''''''''''''''''''''''''
call checkPrevilege(Session("Name"),"p09")
'''''''''''''''''''''''''''''''''''''''''''''''
dim act,id,dbGather
act=request.querystring("act")
if(request.querystring("id")<>"") then
	id=Clng(request.querystring("id"))
else
	call alertstr("没有记录",1) 
end if
set rs=server.createobject("adodb.recordset")
rs.open "select db_type,db_host,db_name,db_username,db_password,db_table,db_key,target_catalog,templetid,news_status,field_map ,db_order,db_numlimit from t_importdb where id="&id,conn,3,1
if not rs.eof then
	dbGather=rs.GetRows()
else
	call alertstr("没有记录",1) 
end if
rs.close
set rs=nothing
%>
<html>
<head>
<title>数据库采集</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color: #D4D0C8;
	font-size:12px;
}
input{ font-size:12px;}
form{margin:0;}
.style1 {color: #FF0000}
.style2 {color: #CCCCCC}
.style3 {color: #FFFFFF;
	font-weight: bold;
}
-->
</style>
<link href="style/global.css" rel="stylesheet" type="text/css">
<script language="javascript">
function checkStep1(obj){
	if(obj.db_host.value==""){
		alert("主机名或库地址不能为空!");
		obj.db_host.focus();
		return false;
	}
	else if(obj.db_name.value==""){
		alert("库名称不能为空!");
		obj.db_name.focus();
		return false;
	}
	return true;
}
function ConfirmDel()
{
   if(confirm("是否要退出数据库采集？"))
     return true;
   else
     return false;
	 
}
</script>
</head>
<body>
<table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><iframe src="right_top.htm" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" height="30" width="100%"></iframe></td>
  </tr>
</table>
<table width="100%"  border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#999999">
  <tr bgcolor="#FF9900">
    <td width="100%" height="22"><div align="center"><span class="style3">◆ 数据库采集 ◆</span></div></td>
  </tr>
</table>
<fieldset style="margin:5px 50px 5px 50px; border-top:1px solid #ffffff; border-right:1px solid #000000;border-bottom:1px solid #000000;border-left:1px solid #ffffff;">
<%
if(act="") then
	call step_1()
elseif(act="step_2") then
	call step_2()
elseif(act="step_3") then
	call step_3()
elseif(act="step_4") then
	call step_4()
elseif(act="step_5") then
	call step_5()
elseif(act="dele") then
	conn.execute("delete from t_importdb where id="&id)
	response.redirect("importdb_add.asp")
end if
%>
<%
sub step_1()
%>
<form name="form1" method="post" action="?act=step_2&id=<%=id%>" onSubmit="return checkStep1(this)">
	  <legend>第一步设置数据库源</legend>
	<table width="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="34%"><div align="right">数据库类型：</div></td>
    <td width="66%"><div align="left">
      <select name="db_type" id="db_type">
        <option value="0" <%if(dbGather(0,0)=0) then response.write("selected=""selected""") end if%>>Microsoft Access</option>
        <option value="1" <%if(dbGather(0,0)=1) then response.write("selected=""selected""") end if%>>Microsoft Sql Server</option>
      </select>
      </div></td>
    </tr>
  <tr>
    <td><div align="right">主机\库地址：</div></td>
    <td><div align="left">
      <input name="db_host" type="text" id="db_host" value="<%=dbGather(1,0)%>" size="30">
      <a href="#" title="如果是ACCESS库，则设置为该库所在的绝对路径，如C:\cms\source.mdb.如果是Mssql库，则设置为其所在的服务器主机名或IP均可">帮助</a></div></td>
  </tr>
  <tr>
    <td><div align="right">数据库名称：</div></td>
    <td><div align="left">
      <input name="db_name" type="text" id="db_name" value="<%=dbGather(2,0)%>">
    </div></td>
  </tr>
  <tr>
    <td><div align="right">用户名：</div></td>
    <td><input name="db_username" type="text" id="db_username" value="<%=dbGather(3,0)%>" size="20"></td>
  </tr>
  <tr>
    <td><div align="right">密码：</div></td>
    <td><div align="left">
      <input name="db_password" type="text" id="db_password" value="<%=dbGather(4,0)%>" size="20">
    </div></td>
  </tr>  
  <tr>
    <td><div align="right"></div></td>
    <td height="50"><div align="left">	
      <input type="submit" name="Submit" value="下一步">
      <input type="reset" name="Submit2" value="重 写">
    </div></td>
  </tr>
</table>
</form>
<%
end sub
sub step_2()
	dim db_type,db_host,db_name,db_username,db_password,db_conn,db_connstr,rsSchema,g_id
	db_type=Cint(request.form("db_type"))
	db_host=request.form("db_host")
	db_name=request.form("db_name")
	db_username=request.form("db_username")
	db_password=request.form("db_password")
	if(db_type=0) then
		db_connstr="driver={Microsoft Access Driver (*.mdb)};dbq="&db_host&";uid="&db_username&";pwd="&db_password&";"
	elseif(db_type=1) then
		db_connstr="driver={sql server};server="&db_host&";database="&db_name&";uid="&db_username&";pwd="&db_password&";"
	end if
	on error resume next
	set db_conn=server.CreateObject("adodb.connection") 
	db_conn.open db_connstr
	if Err.Number <> 0 then
		Response.Write("<script>alert('无法连接至源数据库!请检查设置是否正确.');history.back();</script>")  
        Response.End
	end if
	application.Lock()
	application("totcms_importdb_conn")=db_conn
	application.UnLock()
	'添加到库
	set rs=server.createobject("adodb.recordset")
	sql="select * from t_importdb where id="&id
	rs.open sql,conn,3,3
	rs("db_type")=db_type
	rs("db_host")=db_host
	rs("db_name")=db_name
	rs("db_username")=db_username
	rs("db_password")=db_password
	rs("moditime")=now()
	rs.update
	rs.close
	set rs=nothing	
%>
<script src="js/xmlhttp.js"></script>
<form name="form2" method="post" action="?act=step_3&id=<%=id%>">
<legend>第二步设置表</legend>
<table width="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="34%"><div align="right">选择要采集数据库中的表：</div></td>
    <td width="66%">
	<select name="db_table" id="db_table" onChange="getField(this.options[this.selectedIndex].value,'aa')">
    <%
	dim sele
	set rsSchema=db_conn.openSchema(20)
	rsSchema.movefirst
	Do Until rsSchema.EOF
		if rsSchema("TABLE_TYPE")="TABLE" then
			if(dbGather(5,0)=rsSchema("TABLE_NAME")) then
				sele=" selected=""selected"""
			else
				sele=""
			end if
			response.Write("<option value="""&rsSchema("TABLE_NAME")&""""&sele&">"&rsSchema("TABLE_NAME")&"</option>"&Chr(10))
		end if
		rsSchema.movenext
	loop
	rsSchema.close
	set rsSchema=nothing
	%>
    </select>
    </td>
  </tr>
  <tr>
    <td colspan="2">
	<div id="aa"align="center" style="display:block;">
     <table width="100%"  border="0" cellpadding="0" cellspacing="0"><tr><td width="34%"><div align="right">选择主键：</div></td>
<td width="66%"><div align="left">
	 <select name="db_key">
	 <option value="<%=dbGather(6,0)%>"><%=dbGather(6,0)%></option>
	 </select>
	 </div></td></tr></table>
    </div></td>
  </tr>
  <tr>
    <td width="34%"><div align="right">设置排序：</div></td>
    <td width="66%">
	<select name="db_order" id="db_order">
		<option value="asc" <%if(dbGather(11,0)="asc") then response.Write("selected=""selected""") end if%>>升序</option>
		<option value="desc" <%if(dbGather(11,0)="desc") then response.Write("selected=""selected""") end if%>>降序</option> 
    </select>
    </td>
  </tr>
  <tr>
    <td width="34%"><div align="right">设置采集记录个数：</div></td>
    <td width="66%">
	<input type="text" name="db_numlimit" value="<%=dbGather(12,0)%>" size="10">
	(0为不限制)
    </td>
  </tr>
  <tr>
    <td><div align="right"></div></td>
    <td height="50"><div align="left">
      <input type="button" name="cancel" onClick='javascript:if(ConfirmDel())location.href="?act=dele&id=<%=id%>"' value="取  消">
	  <input type="submit" name="Submit3" value="下一步">      
    </div></td>
  </tr>
</table>
</form>
<script>
function getField(tbl,fid){
var xmlhttp;	 
	xmlhttp=getXMLRequester();
	xmlhttp.onreadystatechange = function doAction() {		
        if (xmlhttp.readyState == 4) // 收到完整的服务器响应
		{
			if (xmlhttp.status == 500) {//服务器错误				
				document.write("服务器出现内部错误。");	
            }
            else if (xmlhttp.status == 200) {//HTTP服务器响应的值OK
				var ret=xmlhttp.responseText;
				document.getElementById(fid).style.display="block";
				document.getElementById(fid).innerHTML=ret;	
            } 
			else {
                alert(xmlhttp.status);
            }
        }
};
	var ReturnValue=xmlhttp.open("POST", "importdb_getfield.asp?table="+tbl, false);
	xmlhttp.send("");
}
</script>
<%
end sub
sub step_3()
	dim db_table,db_key,db_order,db_numlimit
	db_table=request.form("db_table")
	db_key=request.form("db_key")
	db_order=request.form("db_order")
	db_numlimit=Clng(request.form("db_numlimit"))
	set rs=server.createobject("adodb.recordset")
	sql="select * from t_importdb where id="&id
	rs.open sql,conn,3,3
	rs("db_table")=db_table
	rs("db_key")=db_key
	rs("db_order")=db_order
	rs("db_numlimit")=db_numlimit
	rs.update
	rs.close
	set rs=nothing
%>
	<form name="form3" method="post" action="?act=step_4&id=<%=id%>">
<legend>第三步设置采集的文章内容属性</legend>
<table width="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="34%"><div align="right">采集到：</div></td>
    <td width="66%">
	<select name="catalogid" >
	<%
	dim catalogName
	set rs=server.createobject("adodb.recordset")
	rs.open "select title from catalogs where id="&dbGather(7,0),conn,3,1
	if not rs.eof then
		catalogName=rs(0)
	end if
	rs.close
	set rs=nothing
	%>
    <option value="<%=dbGather(7,0)%>"><%=catalogName%></option>
    </select>
      &nbsp;&nbsp;<a href="#" onClick="addParentOption();">＋</a></td>
  </tr>
  <tr>
    <td width="34%"><div align="right">选择文章绑定模板：</div></td>
    <td width="66%">	
    <%
  	dim Templetlist,sele
	set rs=server.CreateObject("adodb.recordset")
	sql="select id,templetname from templet order by id asc"
	rs.open sql,conn,3,1
	if rs.eof or rs.bof then
		response.Write("<font color=""red"">没有模板!</font> <a href=""templet_add.asp"">添加新模板-></a>")
		rs.close
		set rs=nothing		
	else
		Templetlist=rs.GetRows()
		rs.close
		set rs=nothing
		response.write("<select name=""templetid"" title=""选择新闻生成所用的详细页面的模板"">"&Chr(10))
		for i=0 to ubound(Templetlist,2)
			if(Clng(dbGather(8,0))=Clng(Templetlist(0,i))) then
				sele=" selected=""selected"""
			else
				sele=""
			end if
    		response.write("<option value="""&Templetlist(0,i)&""""&sele&">"&Templetlist(1,i)&"</option>"&Chr(10))
		next
		response.write("</select>")
	end if
	%>
	</td>
  </tr>
  <tr>
  	<td><div align="right">设置文章状态：</div></td>
	<td>
		<select name="news_status">
		<option value="1" <%if(dbGather(9,0)=0) then response.write("selected=""selected""") end if%>>已审</option>
		<option value="0" <%if(dbGather(9,0)=0) then response.write("selected=""selected""") end if%>>未审</option>
		</select>
	</td>
  </tr>
  <tr>
    <td><div align="right"></div></td>
    <td height="50">
	<div align="left">
	  <input type="hidden" name="db_table" value="<%=db_table%>" />
	  <input type="button" name="back" onClick="javascript:history.back();" value="上一步">
      <input type="button" name="cancel" onClick='javascript:if(ConfirmDel())location.href="?act=dele&id=<%=id%>"' value="取  消">
	  <input type="submit" name="Submit3" value="下一步">      
    </div></td>
  </tr>
</table>
</form>
<script>
function addParentOption()
{
	var arr = showModalDialog("tree_container.asp", "", "dialogWidth:410px; dialogHeight:420px; status:0");
  	if (arr != null){  
   		document.form3.catalogid.options[0]=new Option(arr[1],arr[0]);
  	}
	
}
</script>
<%
end sub
sub step_4()
	dim db_table,catalogid,templetid,news_status,conn_cache
	db_table=request.form("db_table")
	if(request.Form("catalogid")<>"") then
		catalogid=request.Form("catalogid")
	else
		call alertstr("请选择栏目",1)
	end if
	if(request.Form("templetid")<>"") then
		templetid=request.Form("templetid")
	else
		call alertstr("请选择模板",1)
	end if
	if(request.Form("news_status")<>"") then
		news_status=request.Form("news_status")
	else
		call alertstr("请设置文章的审核状态",1)
	end if
	'更新参数
	set rs=server.createobject("adodb.recordset")
	sql="select * from t_importdb where id="&id
	rs.open sql,conn,3,3
	rs("target_catalog")=catalogid
	rs("templetid")=templetid
	rs("news_status")=news_status
	rs.update
	rs.close
	set rs=nothing
	if(db_table<>"") then
		conn_cache=application("totcms_importdb_conn")
	end if
	if isnull(conn_cache) or conn_cache="" then
		call alertstr("无法连接至源数据库",0)
	end if
%>
<form name="form4" method="post" action="?act=step_5&id=<%=id%>" onSubmit="return check(this)">
<legend>第四步设置字段映射</legend>
<table width="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="31%"><div align="right">
      <input type="button" name="Submit4" value="删除映射" onClick="removeOption()">
    </div></td>
    <td width="21%">
	<select name="mapitems" size="15" id="mapitems" style="WIDTH: 180px; HEIGHT: 150px">
	<script>
	var fieldMaps="<%=dbGather(10,0)%>";
	var fieldArr=fieldMaps.split("||");
	for(i=0;i<fieldArr.length;i++){
		document.write('<option value="'+fieldArr[i]+'">'+fieldArr[i]+'</option>');
	}
	</script>
    </select></td>
    <td width="48%" valign="middle"><table width="100%" border="0">
      <tr>
        <td>源字段：</td>
        <td>		
		<select name="o_field">
		<%
		set rs=server.createobject("adodb.recordset")
		rs.open "select * from "&db_table&" where 1=0",conn_cache,3,1
		for i=0 to rs.fields.count-1
			response.Write("<option value="""&rs(i).name&""">"&rs(i).name&"</option>"&Chr(10))
		next
		rs.close
		set rs=nothing
		%>          
        </select></td>
      </tr>
      <tr>
        <td>目标字段：</td>
        <td>
		<select name="t_field">
          <option value="title">title(标题)</option>
          <option value="content">content(内容)</option>
		  <option value="author">author(作者)</option>
		  <option value="news_edit">news_edit(编辑)</option>
		  <option value="news_from">news_from(来源)</option>
		  <option value="keywords">keywords(关键字)</option>
        </select></td>
      </tr>
      <tr>
        <td colspan="2"><div align="center">
          <input name="addmap" type="button" id="addmap" value="添加映射" onClick="addMap()">
        </div></td>
        </tr>
    </table></td>
  </tr>
  <tr>
    <td height="50" colspan="3">
	    <div align="center">
	      <input type="hidden" name="db_table" value="<%=db_table%>" />
		  <input type="hidden" name="field_map" value="" />
	      <input type="button" name="back" onClick="javascript:history.back();" value="上一步">
	      <input type="button" name="cancel" onClick='javascript:if(ConfirmDel())location.href="?act=dele&id=<%=id%>"' value="取  消">
	      <input type="submit" name="Submit3" value="下一步">      
      </div></td>
    </tr>
</table>
</form>
<script language="javascript">
function addMap(){
	var myForm=document.form4;
	var mapItms=myForm.mapitems;
	var itemValue=myForm.o_field.value+'<>'+myForm.t_field.value;
	//check if is unique
	for (i=mapItms.options.length-1;i>=0;i--)
	{		
		if(mapItms.options[i].value==itemValue){
			alert('此映射已存在!');
			return;
		}
	}
	addOption(mapItms, itemValue, itemValue);
}
function addOption(formElement, optiontext, optionValue) {
  var optionName = new Option(optiontext, optionValue, false, false)
  var length = formElement.length;
  formElement.options[length] = optionName;
}
function removeOption(selected){
	var myForm = document.form4;
	var mapItms=myForm.mapitems;
	var selected = mapItms.selectedIndex;
  	for (m=mapItms.options.length-1;m>=0;m--)
	{
		if(m==selected){
			mapItms.options[m]=null;
		}
	}
}
function check(obj){
	if(obj.mapitems.options.length<1){
		alert("映射不能为空!");
		return false;
	}
	var ret="";
	for (i=obj.mapitems.options.length-1;i>=0;i--)
	{		
		ret+=obj.mapitems.options[i].value;
		if(i!=0){
			ret+="||";
		}
	}
	obj.field_map.value=ret;
	return true;
}
</script>
<%
end sub
sub step_5()
	dim field_map
	field_map=request.Form("field_map")
	'更新参数
	set rs=server.createobject("adodb.recordset")
	sql="select * from t_importdb where id="&id
	rs.open sql,conn,3,3
	rs("field_map")=field_map	
	rs.update
	rs.close
	set rs=nothing
	if(application("totcms_importdb_conn")<>null) then
		application("totcms_importdb_conn").close
		set application("totcms_importdb_conn")=nothing
		application("totcms_importdb_conn")=null
	end if
	response.Write("<legend>恭喜!任务添加完成</legend><p align=""center""><a href=""importdb_manage.asp"">管理/执行</a>任务-&gt;</p><p></p>")
end sub
%>
</fieldset>
</body>
</html>
<%
call connclose()
%>
