<%@language=vbscript codepage=936 %>
<%
option explicit
response.buffer=true	
Const PurviewLevel=2    '操作权限
%>
<!--#include file="Admin_ChkPurview.asp"-->
<!--#include file="inc/conn.asp"-->
<!--#include file="inc/ubbcode.asp"-->
<!--#include file="inc/function.asp"-->
<%
dim sql,rs
dim Action,FoundErr,ErrMsg
Action=Trim(Request("Action"))
%>
<html>
<head>
<title>公告管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="Admin_Style.css">
</head>
<body leftmargin="2" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" Class="border">
  <tr class="topbg"> 
    <td height="22" colspan=2 align=center><b>网 站 公 告 管 理</b></td>
  </tr>
  <tr class="tdbg"> 
    <td width="70" height="30"><strong>管理导航：</strong></td>
    <td height="30"><a href="Admin_Announce.asp">网站公告管理首页</a> | <a href="Admin_Announce.asp?Action=Add">添加新公告</a></td>
  </tr>
</table>
<%
if Action="Add" then
	call AddAnnounce()
elseif Action="SaveAdd" then
	call SaveAdd()
elseif Action="Modify" then
	call Modify()
elseif Action="SaveModify" then
	call SaveModify()
elseif Action="Set" then
	call SetNew()
elseif Action="Del" then
	call DelAnnounce()
else
	call main()
end if
if FoundErr=True then
	call WriteErrMsg()
end if
call CloseConn()


sub main()
	sql="select * from Announce order by id desc"
	set rs=server.createobject("adodb.recordset")
	rs.open sql,conn,1,1
%>
<form method="POST" action="Admin_Announce.asp">
  <table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" Class="border">
    <tr class="title"> 
      <td width="30" height="22" align="center"><strong>选择</strong></td>
      <td width="30" height="22" align="center"><strong>ID</strong></td>
      <td height="22" align="center"><strong>标 题</strong></td>
      <td width="60" height="22" align="center"><strong>作者</strong></td>
      <td width="120" height="22" align="center"><strong>发布时间</strong></td>
      <td width="60" height="22" align="center"><strong>操作</strong></td>
    </tr>
    <%
	if not(rs.bof and rs.eof) then
		do while not rs.eof
%>
    <tr class="tdbg" onmouseout="this.style.backgroundColor=''" onmouseover="this.style.backgroundColor='#BFDFFF'"> 
      <td width="30" align="center"> <input type="checkbox" value=<%=rs("ID")%><%if rs("IsSelected")=true then%> checked<%end if%> name="AnnounceID"></td>
      <td width="30" align="center"><%=rs("ID")%></td>
      <td><a href="Admin_AnnounceModify.asp?ID=<%=rs("ID")%>" title="<%=left(nohtml(rs("content")),200)%>"><%=rs("Title")%></a></td>
      <td width="60" align="center"><%=rs("Author")%></td>
      <td width="120" align="center"><%=rs("DateAndTime")%></td>
      <td width="60" align="center"><%
	  response.write "<a href='Admin_Announce.asp?Action=Modify&ID=" & rs("ID") & "'>修改</a>&nbsp;&nbsp;"
	  response.write "<a href='Admin_Announce.asp?Action=Del&ID=" & rs("ID") & "'>删除</a>"
	  %></td>
    </tr>
	<%
		rs.movenext
	loop
	%>
    <tr class="tdbg"> 
      <td colspan=6 align=center><input name="Action" type="hidden" id="Action" value="Set"> 
        <input type="submit" value="将选定的公告设为最新公告" name="submit"> </td>
    </tr>
    <% end if%>
  </table>
</form>
<%
	rs.close
	set rs=nothing
end sub

sub AddAnnounce()
%>
<form name="form1" method="post" action="Admin_Announce.asp">
  <table width="100%" border="0" align="center" cellpadding="2" cellspacing="2" class="border">
    <tr align="center" class="title"> 
      <td height="22" colspan="2"><strong>添 加 公 告</strong></td>
    </tr>
    <tr class="tdbg"> 
      <td width="100" align="right">标题：</td>
      <td width="400"> <textarea name="Title" cols="50" rows="5" id="Title"></textarea></td>
    </tr>
    <tr class="tdbg"> 
      <td width="100" align="right">内容：</td>
      <td width="400"> <textarea name="Content" cols="50" rows="10" id="Content"></textarea></td>
    </tr>
    <tr class="tdbg"> 
      <td width="100" align="right">发布人：</td>
      <td width="400"> <input name="Author" type="text" id="Author" value="<%=session("Admin")%>" size="20" maxlength="20"></td>
    </tr>
    <tr class="tdbg"> 
      <td align="right">发布时间：</td>
      <td> <input name="DateAndTime" type="text" id="DateAndTime" value="<%=now()%>" size="20" maxlength="20"></td>
    </tr>
    <tr class="tdbg">
      <td align="right">&nbsp;</td>
      <td><input name="IsSelected" type="checkbox" id="IsSelected" value="True" checked>
        设为最新公告</td>
    </tr>
    <tr class="tdbg"> 
      <td height="40" colspan="2" align="center"><input name="Action" type="hidden" id="Action" value="SaveAdd">
        <input type="submit" name="Submit" value=" 添 加 "></td>
    </tr>
  </table>
</form>
<%
end sub

sub Modify()
	dim ID
	ID=trim(request("ID"))
	if ID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定要修改的通告ID！</li>"
	else
		ID=Clng(ID)
	end if
	sql="select * from Announce where ID=" & ID
	set rs=server.createobject("adodb.recordset")
	rs.open sql,conn,1,1
	if rs.bof and rs.eof then
		FoundErr=true
		ErrMsg=ErrMsg & "<br><li>找不到指定的通告！</li>"
		rs.close
		set rs=nothing
		exit sub
	end if
%>
<form name="form1" method="post" action="Admin_Announce.asp">
  <table width="500" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
    <tr align="center" class="title"> 
      <td height="22" colspan="2"><strong>修 改 公 告</strong></td>
    </tr>
    <tr class="tdbg"> 
      <td width="100" align="right">标题：</td>
      <td width="400"> <textarea name="Title" cols="50" rows="5" id="Title"><%=rs("Title")%></textarea></td>
    </tr>
    <tr class="tdbg"> 
      <td width="100" align="right">内容：</td>
      <td width="400"> <textarea name="Content" cols="50" rows="10" id="Content"><%=rs("Content")%></textarea></td>
    </tr>
    <tr class="tdbg"> 
      <td width="100" align="right">发布人：</td>
      <td width="400"> <input name="Author" type="text" id="Author" value="<%=rs("Author")%>" size="20" maxlength="20"></td>
    </tr>
    <tr class="tdbg"> 
      <td align="right">发布时间：</td>
      <td> <input name="DateAndTime" type="text" id="DateAndTime" value="<%=rs("DateAndTime")%>" size="20" maxlength="20"></td>
    </tr>
    <tr class="tdbg"> 
      <td align="right">&nbsp;</td>
      <td><input name="IsSelected" type="checkbox" id="IsSelected" value="True" <% if rs("IsSelected")=true then response.write "checked"%>>
        设为最新公告</td>
    </tr>
    <tr class="tdbg"> 
      <td height="40" colspan="2" align="center"><input name="Action" type="hidden" id="Action" value="SaveModify">
        <input type="submit" name="Submit" value=" 保 存 "> <input name="ID" type="hidden" id="ID" value="<%=rs("ID")%>"></td>
    </tr>
  </table>
</form>
<%
	rs.close
	set rs=nothing
end sub
%>
</body>
</html>
<%

sub SaveAdd()
	dim Title,Content,Author,DateAndTime,IsSelected,sql
	Title=trim(request("Title"))
	Content=trim(request("Content"))
	Author=trim(request("Author"))
	DateAndTime=trim(request("DateAndTime"))
	IsSelected=trim(request("IsSelected"))
	if Title="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>通告标题不能为空！</li>"
	end if
	if Content="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>通告内容不能为空！</li>"
	end if
	if FoundErr=True then
		exit sub
	end if
	
	Title=dvHtmlEncode(Title)
	Content=dvHtmlEncode(Content)
	Author=dvHtmlEncode(Author)
	if DateAndTime<>"" and IsDate(DateAndTime)=true then
		DateAndTime=Cdate(DateAndTime)
	else
		DateAndTime=now()
	end if
	if IsSelected="True" then
		IsSelected=True
	else
		IsSelected=False
	end if
	sql="Insert Into Announce (Title,Content,Author,DateAndTime,IsSelected) values ('" & Title & "','" & Content & "','" & Author & "',#" & DateAndTime & "#," & IsSelected & ")"
	conn.execute sql
	call CloseConn()
	response.redirect "Admin_Announce.asp"
end sub

sub SaveModify()
	dim ID,Title,Content,Author,DateAndTime,IsSelected
	ID=trim(request("ID"))
	Title=trim(request("Title"))
	Content=trim(request("Content"))
	Author=trim(request("Author"))
	DateAndTime=trim(request("DateAndTime"))
	IsSelected=trim(request("IsSelected"))
	if ID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定要修改的通告ID！</li>"
	else
		ID=Clng(ID)
	end if
	if Title="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>通告标题不能为空！</li>"
	end if
	if Content="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>通告内容不能为空！</li>"
	end if
	if FoundErr=True then
		exit sub
	end if
	sql="select * from Announce where ID=" & ID
	set rs=server.createobject("adodb.recordset")
	rs.open sql,conn,1,3
	if rs.bof and rs.eof then
		FoundErr=true
		ErrMsg=ErrMsg & "<br><li>找不到指定的通告！</li>"
		rs.close
		set rs=nothing
		exit sub
	end if

	Title=dvHtmlEncode(Title)
	Content=dvHtmlEncode(Content)
	Author=dvHtmlEncode(Author)
	if DateAndTime<>"" and IsDate(DateAndTime)=true then
		DateAndTime=Cdate(DateAndTime)
	else
		DateAndTime=now()
	end if
	if IsSelected="True" then
		IsSelected=True
	else
		IsSelected=false
	end if
	rs("Title")=Title
	rs("Content")=Content
	rs("Author")=Author
	rs("DateAndTime")=DateAndTime
	rs("IsSelected")=IsSelected
	rs.update
	rs.close
	set rs=nothing
	call CloseConn()
	response.redirect "Admin_Announce.asp"
end sub

sub SetNew()
	dim ID
	ID=Trim(Request("AnnounceID"))
	if ID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定通告ID</li>"
		exit sub
	end if
	conn.execute "Update Announce set IsSelected=False where IsSelected=True"
	if Instr(ID,",")>0 then
		dim arrID,i
		arrID=split(ID,",")
		for i=0 to Ubound(arrID)
			conn.execute "Update Announce set IsSelected=True Where ID=" & CLng(arrID(i))
		next
	else
		conn.execute "Update Announce set IsSelected=True Where ID=" & CLng(ID)
	end if
	call WriteSuccessMsg("设置成功！")
end sub

sub DelAnnounce()
	dim AnnounceID
	AnnounceID=trim(Request("ID"))
	if AnnounceID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定要删除的通告ID！</li>"
		exit sub
	else
		AnnounceID=Clng(AnnounceID)
	end if
	
	sql="delete from Announce where ID=" & AnnounceID
	conn.Execute sql
	call CloseConn()      
	response.redirect "Admin_Announce.asp"
end sub
%>
