<!--#include file="base.asp"-->
<%
''' EYCMS 模型
''' ==================================================================

	sub adddb()
		on error resume next
		dim t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12
		t0=eycms.enhtml(eycms.fpost("t0",0))
		t1=eycms.enhtml(eycms.fpost("t1",0))
		t2=eycms.enhtml(eycms.fpost("t2",0))
		t3=eycms.enhtml(eycms.fpost("t3",0))
		t4=eycms.enhtml(eycms.fpost("t4",0))
		t5=eycms.enhtml(eycms.fpost("t5",0))
		t6=eycms.enhtml(eycms.fpost("t6",0))
		t7=eycms.enhtml(eycms.fpost("t7",0))
		t8=eycms.enhtml(eycms.fpost("t8",0))
		t9=eycms.enhtml(eycms.fpost("t9",0))
		t10=eycms.enhtml(eycms.fpost("t10",0))
		t11=eycms.enhtml(eycms.fpost("t11",0))
		t12=eycms.getint(eycms.fpost("t12",0),0)
		if eycms.strlen(t0)=0 then eycms.ajaxjson "&#27169;&#22411;&#21517;&#31216;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		if eycms.strlen(t1)=0 then eycms.ajaxjson "&#25968;&#25454;&#34920;&#21517;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		if eycms.strlen(t3)=0 then eycms.ajaxjson "&#31649;&#29702;&#36335;&#24452;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		'if eycms.strlen(t4)=0 then eycms.ajaxjson "&#39057;&#36947;&#27169;&#26495;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		if eycms.strlen(t5)=0 then eycms.ajaxjson "&#21015;&#34920;&#27169;&#26495;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		'if eycms.strlen(t6)=0 then eycms.ajaxjson "&#20869;&#23481;&#27169;&#26495;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		if eycms.strlen(t7)=0 then eycms.ajaxjson "&#21518;&#21488;&#20869;&#23481;&#28155;&#21152;&#27169;&#26495;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		if eycms.strlen(t8)=0 then eycms.ajaxjson "&#21518;&#21488;&#20869;&#23481;&#20462;&#25913;&#27169;&#26495;&#19981;&#33021;&#20026;&#31354;",0:exit Sub
		t1="ey_model_"&t1
		If eycms.regtest(t1,"^[a-z0-9_]{2,20}$")=false Then
			eycms.ajaxjson "数据表名必须为小写字母或数字,且不能以数字开头",0
			exit sub
		End if
		dim data
		data=array(array("modelname",t0,22),array("tablename",t1,22),array("modeldesc",t2,255),array("adminurl",t3,255),array("default_temp",t4,255),array("list_temp",t5,255),array("show_temp",t6,255),array("islock",1,0),array("admin_add_temp",t7,255),array("admin_edit_temp",t8,255),array("admin_publish_temp",t9,255),array("pre_publish_temp",t10,255),array("pre_publish_url",t11,255),array("model_allowpost",t12,0))
		if lcase(left(t1,20))="ey_model_field" or lcase(left(t1,20))="ey_model_page"  then
			eycms.ajaxjson "数据表名为系统保留，请更换",0
			exit sub
		end If
		if eycms.db.dbnew("ey_model",data,"tablename='"&t1&"'")=0 then
			eycms.ajaxjson t1&"&#25968;&#25454;&#34920;&#21517;&#24050;&#23384;&#22312;&#65292;&#35831;&#25442;&#20010;&#35797;&#35797;",0:exit sub
		else
			t1=left(t1,20)
			dim sql:sql="CREATE TABLE ["&t1&"] (["&replace(t1,"ey_model_","")&"id] int IDENTITY (1, 1) PRIMARY KEY NOT NULL,[cid] int NULL,[content] ntext NULL)"
			eycms.db.exedb(sql)
			eycms.ajaxjson "保存成功",1
		end If
		dim modeid:modeid=eycms.db.insertid("modelid","ey_model")		
		if eycms.strlen(modeid)>0 then creatModel modeid
		If Err Then
			eycms.echo "0向数据库中添加"& t1 &"表时错误，原因：" & Err.Description
			Err.Clear
			exit sub
		End If
	end sub
	
	sub editdb()
		dim t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12
		t0=eycms.enhtml(eycms.fpost("t0",0))
		t2=eycms.enhtml(eycms.fpost("t2",0))
		t3=eycms.enhtml(eycms.fpost("t3",0))
		t4=eycms.enhtml(eycms.fpost("t4",0))
		t5=eycms.enhtml(eycms.fpost("t5",0))
		t6=eycms.enhtml(eycms.fpost("t6",0))
		t7=eycms.enhtml(eycms.fpost("t7",0))
		t8=eycms.enhtml(eycms.fpost("t8",0))
		t9=eycms.enhtml(eycms.fpost("t9",0))
		t10=eycms.enhtml(eycms.fpost("t10",0))
		t11=eycms.enhtml(eycms.fpost("t11",0))
		t12=eycms.getint(eycms.fpost("t12",0),0)
		if eycms.strlen(t0)=0 then eycms.ajaxjson "&#27169;&#22411;&#21517;&#31216;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		if eycms.strlen(t3)=0 then eycms.ajaxjson "&#31649;&#29702;&#36335;&#24452;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		'if eycms.strlen(t4)=0 then eycms.ajaxjson "&#39057;&#36947;&#27169;&#26495;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		if eycms.strlen(t5)=0 then eycms.ajaxjson "&#21015;&#34920;&#27169;&#26495;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		'if eycms.strlen(t6)=0 then eycms.ajaxjson "&#20869;&#23481;&#27169;&#26495;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		if eycms.strlen(t7)=0 then eycms.ajaxjson "&#21518;&#21488;&#20869;&#23481;&#28155;&#21152;&#27169;&#26495;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		if eycms.strlen(t8)=0 then eycms.ajaxjson "&#21518;&#21488;&#20869;&#23481;&#20462;&#25913;&#27169;&#26495;&#19981;&#33021;&#20026;&#31354;",0:exit sub
		
		dim data,id
		id=eycms.getint(eycms.fget("id",0),0)
		if eycms.strlen(t4)=0 then delModelfile "default_temp",id
		if eycms.strlen(t5)=0 then delModelfile "list_temp",id
		if eycms.strlen(t6)=0 then delModelfile "show_temp",id
		if eycms.strlen(t10)=0 then delModelfile "pre_publish_temp",id
		data=array(array("modelname",t0,20,1),array("modeldesc",t2,255,1),array("adminurl",t3,255,1),array("default_temp",t4,255,1),array("list_temp",t5,255,1),array("show_temp",t6,255,1),array("islock",1,0,0),array("admin_add_temp",t7,255,1),array("admin_edit_temp",t8,255,1),array("admin_publish_temp",t9,255,1),array("pre_publish_temp",t10,255,1),array("pre_publish_url",t11,255,1),array("model_allowpost",t12,0,0))
		if eycms.db.dbupdate("ey_model","modelid="&id&"",data)=1 then
			creatModel id
			eycms.ajaxjson "保存成功",1
		end if
	end sub
	
	sub deldb()
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		dim tablename,cnum
		tablename=eycms.db.dbload(1,"tablename","ey_model","modelid="&id&" and modelid>6","")(0,0)
		if tablename="" then
			eycms.echo "0&#21442;&#25968;&#38169;&#35823;&#65292;&#21024;&#38500;&#22833;&#36133;"
			exit sub
		else
			cnum=eycms.db.dbcount(tablename,"")
			if cnum>0 then
				eycms.echo "0&#24403;&#21069;&#27169;&#22411;&#20013;&#26377;&#20869;&#23481;&#19981;&#33021;&#21024;&#38500;"
				exit sub
			end if
			eycms.db.dbdel "ey_model_field","modelid="&id
			eycms.db.exedb("drop table "&tablename&"")
			delModel id
		end if
		eycms.db.dbdel "ey_model","modelid="&id
		eycms.echo "1"
	end sub

	sub recache()
		eycms.delcache "category"
	end sub
	
	sub doset()
		dim id:id=eycms.enhtml(eycms.fget("id",0))
		dim go:go=eycms.enhtml(eycms.fget("go",0))
		dim idarr:idarr=split(id,",")
		if ubound(idarr)<0 then
			eycms.echo "0&#33267;&#23569;&#36873;&#25321;&#19968;&#26465;&#20449;&#24687;"
		else
			dim i
			for i=0 to ubound(idarr)
				if not isnumeric(idarr(i)) then
					eycms.echo "0&#21442;&#25968;&#65306;"&id(i)&"&#19981;&#27491;&#30830;&#65292;&#35831;&#30830;&#35748;&#21518;&#20877;&#25805;&#20316;"
					exit sub
				end if
			next
			dim filed,result
			select case go
				case "islock":filed="islock":result=0
				case "nolock":filed="islock":result=1
				case else:eycms.echo "0&#21442;&#25968;&#38169;&#35823;":exit sub
			end select
			eycms.db.dbupdate "ey_model","modelid in("&id&")",array(array(filed,result,2,0))
			eycms.echo "1"
		end if
	end Sub

	sub creatModel(t0)
		if eycms.strlen(t0)=0 then exit sub

		dim r_mid:r_mid="<%"
		r_mid=r_mid&"if not in_eycms then response.write(""template load fail""):response.end() end if"
		r_mid=r_mid&"%"
		r_mid=r_mid&">"

		dim datadb,default_temp,list_temp,show_temp,publish_temp
		dim fpath,fname,arr
		datadb=eycms.db.dbload("","default_temp,list_temp,show_temp,pre_publish_temp","ey_model","modelid="&t0&"","")
		if ubound(datadb)>=0 then
			default_temp=datadb(0,0)
			list_temp=datadb(1,0)
			show_temp=datadb(2,0)
			publish_temp=datadb(3,0)
		end if
		if eycms.strlen(default_temp)>0 then 
			arr=split(default_temp,"/")
			fname=arr(ubound(arr))
			if webremote=0 then
				fpath="../theme/" & theme_root & "/"& replace(default_temp,"/"&fname,"")
			else
				fpath="../encry/theme/" & theme_root & "/"& replace(default_temp,"/"&fname,"")
			end if 
			if not eycms.isfile(fpath&"/"&fname) Then
				eycms.newfile fpath&"/",fname,r_mid,""
				creatBaseXml replace(default_temp,"/"&fname,"")&"/"&fname
			end if
		end if
		if eycms.strlen(list_temp)>0 then 
			arr=split(list_temp,"/")
			fname=arr(ubound(arr))
			if webremote=0 then
				fpath="../theme/" & theme_root & "/"& replace(list_temp,"/"&fname,"")
			else
				fpath="../encry/theme/" & theme_root & "/"& replace(list_temp,"/"&fname,"")
			end if
			if not eycms.isfile(fpath&"/"&fname) Then
				eycms.newfile fpath&"/",fname,r_mid,""
				creatBaseXml replace(list_temp,"/"&fname,"")&"/"&fname
			end if
		end if
		if eycms.strlen(show_temp)>0 then 
			arr=split(show_temp,"/")
			fname=arr(ubound(arr))
			if webremote=0 then
				fpath="../theme/" & theme_root & "/"& replace(show_temp,"/"&fname,"")
			else
				fpath="../encry/theme/" & theme_root & "/"& replace(show_temp,"/"&fname,"")
			end if
			if not eycms.isfile(fpath&"/"&fname) Then
				eycms.newfile fpath&"/",fname,r_mid,""
				creatBaseXml replace(show_temp,"/"&fname,"")&"/"&fname
			end if
		end if
		if eycms.strlen(publish_temp)>0 then 
			arr=split(publish_temp,"/")
			fname=arr(ubound(arr))
			if webremote=0 then
				fpath="../theme/" & theme_root & "/"& replace(publish_temp,"/"&fname,"")
			else
				fpath="../encry/theme/" & theme_root & "/"& replace(publish_temp,"/"&fname,"")
			end if
			if not eycms.isfile(fpath&"/"&fname) Then
				eycms.newfile fpath&"/",fname,r_mid,""
				creatBaseXml replace(publish_temp,"/"&fname,"")&"/"&fname
			end if
		end if
	end Sub

	sub creatBaseXml(t0)
		if eycms.isfile("../theme/"&theme_root&"/base.xml") Then
			dim xml,i,item,settag,seturl,setname,haveitem,node,fname
			fname=lcase(t0)
			haveitem=False
			set xml=server.createobject("microsoft.xmldom")
			xml.async=false
			xml.load(server.mappath("../theme/"&theme_root&"/base.xml"))
			set item=xml.getElementsByTagName("item")
			for i=0 to (item.length-1)
				set seturl=item.item(i).getElementsByTagName("seturl")
				if seturl.item(0).Text=fname then 
					haveitem=true
				end if
			Next
			If haveitem=False Then
				Set node=xml.createNode(1,"item","")
				set settag = xml.createNode(1,"settag","")
				set seturl = xml.createNode(1,"seturl","")
				set setname = xml.createNode(1,"setname","")
				settag.text=get_tag(fname)
				seturl.text=fname
				setname.text=""
				node.appendChild(settag)
				node.appendChild(seturl)
				node.appendChild(setname)
				xml.documentElement.appendChild(node)
			End if
			xml.save(server.mappath("../theme/"&theme_root&"/base.xml")) 
			set xml=nothing
			set item=nothing
		End If
	end Sub

	sub delModel(t0)
		if eycms.strlen(t0)=0 then exit sub
		dim datadb,default_temp,list_temp,show_temp,publish_temp
		dim fpath,fname,arr,fmodel
		datadb=eycms.db.dbload("","default_temp,list_temp,show_temp,pre_publish_temp","ey_model","modelid="&t0&"","")
		if ubound(datadb)>=0 then
			default_temp=datadb(0,0)
			list_temp=datadb(1,0)
			show_temp=datadb(2,0)
			publish_temp=datadb(3,0)
		end if
		if eycms.strlen(list_temp)>0 then 
			arr=split(list_temp,"/")
			fname=arr(ubound(arr))
			fpath="../theme/" & theme_root & "/"& replace(list_temp,"/"&fname,"")
			fmodel=split(fpath,"/")(ubound(split(fpath,"/")))
			if not instr("news|page|photo|video|down|pro|gift",fmodel) then
				if eycms.isfolder(fpath&"/") Then
					eycms.delfolder fpath
				end if
			end if
		end if
		if eycms.strlen(default_temp)>0 then 
			arr=split(default_temp,"/")
			fname=arr(ubound(arr))
			delBaseXml replace(default_temp,"/"&fname,"")&"/"&fname
		end if
		if eycms.strlen(list_temp)>0 then 
			arr=split(list_temp,"/")
			fname=arr(ubound(arr))
			delBaseXml replace(list_temp,"/"&fname,"")&"/"&fname
		end if
		if eycms.strlen(show_temp)>0 then 
			arr=split(show_temp,"/")
			fname=arr(ubound(arr))
			delBaseXml replace(show_temp,"/"&fname,"")&"/"&fname
		end if
		if eycms.strlen(publish_temp)>0 then 
			arr=split(publish_temp,"/")
			fname=arr(ubound(arr))
			delBaseXml replace(publish_temp,"/"&fname,"")&"/"&fname
		end if
	end Sub
	
	sub delModelfile(filed,id)
		if eycms.strlen(filed)=0 then exit sub
		if eycms.strlen(id)=0 then exit sub
		dim datadb,tempfile,fname,arr
		datadb=eycms.db.dbload("",filed,"ey_model","modelid="&id&"","")
		if ubound(datadb)>=0 then
			tempfile=datadb(0,0)
		end if

		if eycms.strlen(tempfile)>0 then 
			if eycms.isfile("../theme/"&theme_root&"/"&tempfile) Then
				eycms.delfile "../theme/"&theme_root&"/"&tempfile
			end if
			arr=split(tempfile,"/")
			fname=arr(ubound(arr))
			delBaseXml replace(tempfile,"/"&fname,"")&"/"&fname
		end if
	end Sub

	sub delBaseXml(t0)
		on error resume next
		dim fname
		fname=lcase(t0)
		if eycms.isfile("../theme/"&theme_root&"/base.xml") Then
			dim xml,i,objNode,rootNode
			set xml=server.createobject("microsoft.xmldom")
			xml.async=false
			xml.load(server.mappath("../theme/"&theme_root&"/base.xml"))
			set objNode = xml.documentElement
			set rootNode = objNode.selectSingleNode("/config/item/seturl[.='"&fname&"']").parentNode
			if rootNode.childNodes.length>0 then
				xml.DocumentElement.RemoveChild(rootNode)
				xml.save(server.mappath("../theme/"&theme_root&"/base.xml")) 
			end if
			set rootNode=nothing
			set objNode=nothing
			set xml=nothing
		End If
	end Sub
	
	islogin
	dim act:act=lcase(eycms.fget("act",0))
	select case act
		case "add":checkpagelever 18:load eval("theme_model"&act)
		case "adddb":checkpagelever 18:adddb:recache
		case "edit"
		checkpagelever 18
			dim datadb,id
			id=eycms.getint(eycms.fget("id",0),0)
			datadb=eycms.db.dbload("","modelname,tablename,modeldesc,adminurl,default_temp,list_temp,show_temp,admin_add_temp,admin_edit_temp,admin_publish_temp,pre_publish_temp,pre_publish_url,model_allowpost","ey_model","modelid="&id&"","")
			load eval("theme_model"&act)
		case "editdb":checkpagelever 18:editdb:recache
		case "del":checkpagelever 18:deldb:recache
		case "doset":checkpagelever 18:doset:recache
		case else:checkpagelever 18:load theme_modellist
	end select
		
	eycms.db.dbclose
%>
