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

	sub adddb()
		dim t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,relation,gmode,multi
		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)
		t5=eycms.getint(eycms.fpost("t5",0),0)
		t6=eycms.enhtml(eycms.fpost("t6",0))
		t7=eycms.enhtml(eycms.fpost("t7",0))
		t8=eycms.getint(eycms.fpost("t8",0),0)
		t9=eycms.getint(eycms.fpost("t9",0),0)
		t10=eycms.getint(eycms.fpost("t10",0),0)
		t11=eycms.getint(eycms.fpost("t11",0),0)
		t12=eycms.getint(eycms.fpost("t12",0),0)
		t13=eycms.getint(eycms.fpost("t13",0),0)
		relation=eycms.getint(eycms.fpost("relation",0),0)
		gmode=eycms.enhtml(eycms.fpost("gmode",0))
		multi=eycms.getint(eycms.fpost("multi",0),0)
		if eycms.strlen(t0)=0 then eycms.ajaxjson "字段名称不能为空",0:exit sub
		if eycms.strlen(t1)=0 then eycms.ajaxjson "字段别名不能为空",0:exit sub
		if eycms.strlen(t2)=0 then eycms.ajaxjson "字段类型不能为空",0:exit Sub
		If relation=1 Then
			if eycms.strlen(gmode)=0 then eycms.ajaxjson "请选择关联的模型表",0:exit Sub
		End If
		if (t3=2 or t3=5) and t6="" then t6=0
		t0="my_"&t0
		if t8=0 then t9=0
		'字段类型
		dim dtype
		select case t2
			case "1"
				select case t3
					case "1","4":dtype="文本"
					case "2":dtype="数字"
					case "3":dtype="日期"
					case "5":dtype="货币"
				end select
			case "2"
				dtype="备注"
			case "6"
				dtype="备注"
				t8=0
			case "4","5"
				dtype="文本"
				t5=255
			case else
				select case t4
					case "1":dtype="文本"
					'case "2":dtype="数字"
				end select
		end select
		t0=left(t0,20)
		dim sqlstr
		select case dtype
			case "文本":sqlstr=""&t0&" nvarchar("&t5&") null"
			case "数字":sqlstr=""&t0&" int null"
			case "货币":sqlstr=""&t0&" money null"
			case "日期":sqlstr=""&t0&" datetime null"
			case "备注":sqlstr=""&t0&" ntext null"	
		end select
		dim mdata:mdata=eycms.db.dbload(1,"tablename","ey_model","modelid="&modelid&"","modelid")
		if ubound(mdata)<0 then
			eycms.ajaxjson "模型数据错误"&modelid&"",0:exit sub
		end if
		dim tablename:tablename=mdata(0,0)
		sqlstr="alter table "&tablename&" add "&sqlstr
		dim data
		data=array(array("fname",t0,0),array("falias",t1,9),array("fclass",t2,0),array("fmode",t3,0),array("fdatatype",t4,0),array("flength",t5,5),array("fdefault",t6,255),array("foptions",t7,0),array("fismust",t8,0),array("frules",t9,0),array("fistrim",t10,0),array("ordnum",t11,0),array("islock",t12,0),array("fupload",t13,0),array("modelid",modelid,0),array("relation",relation,0,0),array("relateto",gmode,50,1),array("multi",multi,0,0))
		if eycms.db.dbnew("ey_model_field",data,"fname='"&t0&"' and modelid="&modelid&"")=0 then
			eycms.ajaxjson "字段名称已存在，请换个试试",0:exit sub
		else
			eycms.db.exedb(sqlstr)
			If eycms.strlen(t6)>0 Then
				If dtype="数字" Or dtype="货币" then
					eycms.db.dbupdate tablename,"",array(array(t0,t6,0,0))
				Else
					eycms.db.dbupdate tablename,"",array(array(t0,t6,0,1))
				End If
			End if
			eycms.ajaxjson "保存成功",1
		end if
		
	end sub
	
	sub editdb()
		dim t1,t3,t6,t7,t8,t9,t10,t11,t12,t13,relation,gmode,multi
		t1=eycms.enhtml(eycms.fpost("t1",0))
		t6=eycms.enhtml(eycms.fpost("t6",0))
		t7=eycms.enhtml(eycms.fpost("t7",0))
		t8=eycms.getint(eycms.fpost("t8",0),0)
		t9=eycms.getint(eycms.fpost("t9",0),0)
		t10=eycms.getint(eycms.fpost("t10",0),0)
		t11=eycms.getint(eycms.fpost("t11",0),0)
		t12=eycms.getint(eycms.fpost("t12",0),0)
		t13=eycms.getint(eycms.fpost("t13",0),0)
		relation=eycms.getint(eycms.fpost("relation",0),0)
		gmode=eycms.enhtml(eycms.fpost("gmode",0))
		multi=eycms.getint(eycms.fpost("multi",0),0)
		if eycms.strlen(t1)=0 then eycms.ajaxjson "字段别名不能为空",0:exit Sub
		If relation=1 Then
			if eycms.strlen(gmode)=0 then eycms.ajaxjson "请选择关联的模型表",0:exit Sub
		End If
		if t8=0 then t9=0
		dim data,id
		id=eycms.getint(eycms.fget("id",0),0)
		t3=eycms.db.dbloadone("fmode","ey_model_field","id="&id&"")
		if (t3=2 or t3=5) and t6="" then t6=0
		data=array(array("falias",t1,7,1),array("fdefault",t6,255,1),array("foptions",t7,0,1),array("fismust",t8,0,0),array("frules",t9,0,0),array("fistrim",t10,0,0),array("ordnum",t11,0,0),array("islock",t12,0,0),array("fupload",t13,0,0),array("relation",relation,0,0),array("relateto",gmode,50,1),array("multi",multi,0,0))
		if eycms.db.dbupdate("ey_model_field","id="&id&"",data)=1 then
			eycms.ajaxjson "保存成功",1
		end if
	end sub
	
	sub orderdb()
		dim t0,t1,t2,t3,i
		t0=eycms.enhtml(eycms.fpost("ordnum",0))
		t1=eycms.enhtml(eycms.fpost("id",0))
		t2=split(t0,", ")
		t3=split(t1,", ")
		if ubound(t2)-ubound(t3)<>0 then eycms.ajaxjson "0&#21442;&#25968;&#38169;&#35823;",0:exit sub
		for i=0 to ubound(t2)
			if eycms.isnum(t2(i)) then
				eycms.db.dbupdate "ey_model_field","id="&t3(i)&"",array(array("ordnum",t2(i),10,0,0))
			end if
		next
		eycms.ajaxjson "保存成功",1
	end sub
	
	sub deldb()
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		dim data:data=eycms.db.dbload(1,"modelid,fname","ey_model_field","id="&id&"","")
		if ubound(data)<0 then
			exit sub
		end if
		dim mdata:mdata=eycms.db.dbload(1,"tablename","ey_model","modelid="&data(0,0)&"","modelid")
		if ubound(mdata)<0 then
			exit sub
		end if
		dim sqlstr:sqlstr="alter table "&mdata(0,0)&" drop "&data(1,0)
		eycms.db.exedb(sqlstr)
		eycms.db.dbdel "ey_model_field","id="&id
		eycms.echo "1"
	end sub
	
	sub delcache()
		eycms.delcache "model_field"&modelid
	end sub
	
	function get_type(byval t0)
		select case t0
			case "1":get_type="单行文本框"
			case "2":get_type="多行文本框"
			case "3":get_type="下拉列表"
			case "4":get_type="单选按钮"
			case "5":get_type="复选框"
			case "6":get_type="HTML编辑器"
		end select
	end function
	
	function get_data(byval t0)
		select case t0
			case "1":get_data="文本"
			case "2":get_data="数字"
			case "3":get_data="货币"
			case "4":get_data="日期"
			case "5":get_data="备注"
		end select
	end Function
	
	'*****************************
	'注释：获取所有模型
	'*****************************
	Function loadmodeltable(t0,t1)
		Dim mdata:mdata=eycms.db.dbload("","tablename,modelname,modelid","ey_model","islock=1","modelid asc")
		dim str01,str02,str03,i,selected,disabled
		selected=""
		If CStr(t1)="ey_admin" Then selected=" selected"
		str01="<select name=""gmode"" id=""gmode"" class=""ip""><option value="""">模型表</option>"
		str03="</select>"
		if ubound(mdata)>=0 then
			for i=0 to ubound(mdata,2)
				selected=""
				disabled=""
				If CStr(t1)=mdata(0,i) Then selected=" selected"
				If t0=mdata(2,i) Then disabled=" disabled"
				str02=str02&"<option value="""&mdata(2,i)&""""&selected&disabled&">"&mdata(0,i)&" "&mdata(1,i)&"</option>"
			next
		end If
		loadmodeltable = str01&str02&str03
	end Function
	
	islogin
	dim act:act=lcase(eycms.fget("act",0))
	dim modelid:modelid=eycms.getint(eycms.fget("modelid",0),0)
	dim modename:modename=eycms.db.dbloadone("modelname","ey_model","modelid="&modelid)
	select case act
		case "add":checkpagelever 18:load eval("theme_model_field_"&act)
		case "adddb":checkpagelever 18:adddb:delcache
		case "edit"
			checkpagelever 18
			dim datadb,id
			id=eycms.getint(eycms.fget("id",0),0)
			datadb=eycms.db.dbload("","fname,falias,fclass,fmode,fdatatype,flength,fdefault,foptions,fismust,frules,fistrim,ordnum,islock,fupload,relation,relateto,multi","ey_model_field","id="&id&"","")
			load eval("theme_model_field_"&act)
		case "editdb":checkpagelever 18:editdb:delcache
		case "order":checkpagelever 18:orderdb:delcache
		case "del":checkpagelever 18:deldb:delcache
		case else:checkpagelever 18:load theme_model_field
	end select
		
	eycms.db.dbclose
%>
