<!--#include file="base.asp"-->
<%
''' EYCMS 表单
''' ==================================================================

	sub adddb()
		dim t0,t1,t2,t3,t4
		t0=eycms.enhtml(eycms.fpost("t0",0))
		t1=eycms.enhtml(eycms.fpost("t1",0))
		t2=eycms.getint(eycms.fpost("t2",0),0)
		t3=eycms.getint(eycms.fpost("t3",0),0)
		t4=eycms.getint(eycms.fpost("t4",0),0)
		if eycms.strlen(t0)=0 then eycms.ajaxjson "表单名称不能为空",0:exit sub
		if eycms.strlen(t1)=0 then eycms.ajaxjson "数据表名不能为空",0:exit sub
		t1="ey_form_"&t1
		dim data:data=array(array("title",t0,52),array("tablename",t1,25),array("ispost",t2,0),array("iscode",t3,0),array("islock",t4,0))
		if eycms.db.dbnew("ey_form",data,"tablename='"&t1&"'")=0 then
			eycms.ajaxjson "数据表名已存在，请更换",0:exit sub
		else
			eycms.db.exedb "CREATE TABLE ["&t1&"] ([ID] int IDENTITY (1, 1) PRIMARY KEY NOT NULL ,[userip] nvarchar (50) NULL,[createdate] datetime NULL )"
			creatFrom
			eycms.ajaxjson "保存成功",1
		end if
	end sub
	
	sub editdb()
		dim t0,t1,t2,t3,t4
		t0=eycms.enhtml(eycms.fpost("t0",0))
		t2=eycms.getint(eycms.fpost("t2",0),0)
		t3=eycms.getint(eycms.fpost("t3",0),0)
		t4=eycms.getint(eycms.fpost("t4",0),0)
		if eycms.strlen(t0)=0 then eycms.ajaxjson "表单名称不能为空",0:exit sub
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		dim data:data=array(array("title",t0,50,1),array("ispost",t2,0,0),array("iscode",t3,0,0),array("islock",t4,0,0))
		if eycms.db.dbupdate("ey_form","id="&id&"",data)=1 then
			eycms.ajaxjson "保存成功",1
		end if
	end sub
	
	sub deldb()
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		dim tablename:tablename=eycms.db.dbload(1,"tablename","ey_form","id="&id&"","")(0,0)
		eycms.db.exedb "DROP TABLE ["&tablename&"]"
		eycms.db.dbdel "ey_form_field","formid="&id
		eycms.db.dbdel "ey_form","id="&id
		eycms.echo "1" 
	end sub
	
	sub did()
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		dim sid:sid=eycms.getint(eycms.fget("sid",0),0)
		dim data:data=eycms.db.dbload(1,"tablename","ey_form","id="&id&"","")
		if ubound(data)>=0 then
			eycms.db.dbdel data(0,0),"id="&sid
		end if
		eycms.echo "1"
	end sub

	function gettrans(byval t0,byval t1,byval t2)
		dim form_data,i,foption,Options,Ival,Iname,Item
		form_data=eycms.get_form_field(t0)
		if isarray(form_data) then
			for i=0 to ubound(form_data,2)
				if form_data(0,i)=t1 then
					if form_data(2,i)=3 then
						foption = form_data(7,i)
						Options = Split(foption,vbcrlf)
						for each Item in Options
							If Trim(Item)<>"" then
								Ival=Split(Item,"|")(1)
								Iname=Split(Item,"|")(0)
								If Trim(Ival)<>"" then
									If eycms.isSame(t2,Ival) Then
										gettrans=Iname
									End If
								End if
							End if
						Next
					else
						gettrans=t2
					end if
				end if
			next
		else
			gettrans=t2
		end if
	end Function

	dim get_basexml
	sub creatFrom()
		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&">"

		if not eycms.isfile("../theme/" & theme_root&"/"&"eycms_form.asp") Then
			eycms.newfile "../theme/" & theme_root&"/","eycms_form.asp",r_mid,""
		end if
		
		'if not(eycms.isfile("../theme/"&theme_root&"/base.xml")) Then
			getbasexml(theme_root)
			dim html:html="<?xml version=""1.0"" encoding=""utf-8""?>"
			html=html&"<config>"
			html=html&get_basexml
			html=html&"</config>"
			eycms.newfile "../theme/"&theme_root&"/","base.xml",html,""
		'end If
	end Sub

	sub getbasexml(byval t0)
		Dim t1,t2
		If InStr(t0,"/")>0 Then 
			t1=Split(t0,"/")(0)&"/"  
			t2=Replace(t0,t1,"")&"/" 
		Else
			t1=t0
			t2=""
		End if
		dim fso,fsofolder,fsocontent,fsocount
		set fso=createobject("scripting.filesystemobject")
		set fsofolder=fso.getfolder(server.mappath(""&webroot&"theme/"&t0))  
		If t2<>"model/" then
			set fsocontent=fsofolder.files
			dim fsoitem
			for each fsoitem in fsocontent
				if lcase(fsoitem.name)<>"default.gif" and lcase(fsoitem.name)<>"base.xml" and lcase(fsoitem.name)<>"config.xml" and Right(lcase(fsoitem.name),4)<>".bak" then
					dim name:name=fsoitem.name
					get_basexml=get_basexml & "<item><settag>"&get_tag(t2&fsoitem.name)&"</settag><seturl>"&t2&fsoitem.name&"</seturl><setname>"&getblockname(t0&"/"&fsoitem.name)&"</setname></item>" & vbcrlf
				end If
			Next
		End if
		for each fsocount in fsofolder.subfolders
			if InStr(lcase(fsocount.name),"model")>0 Or InStr(t0,"model")>0 Or InStr(t0,"block")>0 Or InStr(lcase(fsocount.name),"user")>0 Then
				getbasexml(t0&"/"&fsocount.name)
			end if
		Next
		set fso=nothing
		set fsofolder=nothing
		set fsocontent=nothing
	end Sub

	islogin
	dim act:act=lcase(eycms.fget("act",0))
	select case act
		case "add":checkpagelever 63:load theme_form_add
		case "adddb":checkpagelever 63:adddb
		case "edit","list"
			checkpagelever 63
			dim datadb,id
			id=eycms.getint(eycms.fget("id",0),0)
			datadb=eycms.db.dbload("","title,tablename,ispost,iscode,islock","ey_form","id="&id&"","")
			if act="list" then
				dim fdata:fdata=eycms.db.dbload("","falias,fname","ey_form_field","islock=1 and isshow=1 and formid="&id&"","ordnum,id")
			end if
			load eval("theme_form_"&act)
		case "editdb":checkpagelever 63:editdb
		case "show"
			checkpagelever 63
			id=eycms.getint(eycms.fget("id",0),0)
			dim sid:sid=eycms.getint(eycms.fget("sid",0),0)
			dim data:data=eycms.db.dbload(1,"tablename","ey_form","id="&id&"","")
			dim rsshow
			if ubound(data)>=0 then
				set rsshow=eycms.db.exedb("select top 1 * from "&data(0,0)&" where id="&sid&"")
				if rsshow.eof then
					eycms.echo "参数错误":eycms.die
				end if
				dim form_data:form_data=eycms.get_form_field(id)
				load theme_form_show
			end if
		case "del":checkpagelever 63:deldb
		case "did":checkpagelever 63:did
		case else:checkpagelever 63:load theme_form
	end select
		
	eycms.db.dbclose
%>
