<!--#include file="Admin_CommonCode_SDMS.asp"-->
<!--#include file="../Include/PowerEasy.FSO.asp"-->
<%
'**************************************************************
' Software name: PowerEasy SiteWeaver
' Web: http://www.powereasy.net
' Copyright (C) 2005－2009 佛山市动易网络科技有限公司 版权所有
'**************************************************************

Const NeedCheckComeUrl = True   '是否需要检查外部访问

Const PurviewLevel = 1      '0--不检查，1--超级管理员，2--普通管理员
Const PurviewLevel_Channel = 0   '0--不检查，1--频道管理员，2--栏目总编，3--栏目管理员
Const PurviewLevel_Others = ""   '其他权限
Call CloseSdmsConn

Dim dbpath, Barwidth

dbpath = Server.MapPath("../Sdms/Sdms.mdb")
Barwidth = 500
Response.Write "<html><head><title>学籍数据库管理</title>" & vbCrLf
Response.Write "<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>" & vbCrLf
Response.Write "<link href='Admin_Style.css' rel='stylesheet' type='text/css'>" & vbCrLf
Response.Write "</head>" & vbCrLf
Response.Write "<body leftmargin='2' topmargin='0' marginwidth='0' marginheight='0'>" & vbCrLf
Response.Write "<table width='100%' border='0' align='center' cellpadding='2' cellspacing='1' class='border'>" & vbCrLf
Call ShowPageTitle("学 籍 数 据 库 管 理", 10304)
Response.Write "  <tr class='tdbg'>" & vbCrLf
Response.Write "    <td width='70' height='30' ><strong>管理导航：</strong></td><td>"
Response.Write "<a href='Admin_SdmsDatabaseManage.asp?Action=Backup'>备份学籍数据库</a>&nbsp;|&nbsp;"
Response.Write "<a href='Admin_SdmsDatabaseManage.asp?Action=Restore'>恢复学籍数据库</a>&nbsp;|&nbsp;"
Response.Write "<a href='Admin_SdmsDatabaseManage.asp?Action=Compact'>压缩学籍数据库</a>&nbsp;|&nbsp;"
Response.Write "<a href='Admin_SdmsDatabaseManage.asp?Action=Init'>学籍系统初始化</a>&nbsp;|&nbsp;"
Response.Write "<a href='Admin_SdmsDatabaseManage.asp?Action=SpaceSize'>学籍系统空间占用情况</a>"
Response.Write "    </td>" & vbCrLf
Response.Write "  </tr>" & vbCrLf
Response.Write "</table>" & vbCrLf

Select Case Action
Case "Backup"
    Call ShowBackup
Case "BackupData"
    Call BackupData
Case "Compact"
    Call ShowCompact
Case "CompactData"
    Call CompactData
Case "Restore"
    Call ShowRestore
Case "RestoreData"
    Call RestoreData
Case "Init", "Clear"
    Call OpenSdmsConn
    Call ShowInit
    Call CloseSdmsConn
Case "SpaceSize"
    Call SpaceSize
Case Else
    Call ShowBackup
End Select
If FoundErr = True Then
    Call WriteErrMsg(ErrMsg, ComeUrl)
End If
Response.Write "</body></html>" & vbCrLf
Call CloseConn


Sub ShowBackup()
    Response.Write "<form method='post' action='Admin_SdmsDatabaseManage.asp?action=BackupData'>"
    Response.Write "<table width='100%' border='0' align='center' cellpadding='0' cellspacing='0' class='border'>"
    Response.Write "  <tr class='title'>"
    Response.Write "      <td align='center' height='22' valign='middle'><b>备 份 学 籍 数 据 库</b></td>"
    Response.Write "  </tr>"
    Response.Write "  <tr class='tdbg'>"
    Response.Write "    <td height='150' align='center' valign='middle'>"
    Response.Write "<table cellpadding='3' cellspacing='1' border='0' width='100%'>"
    Response.Write "  <tr>"
    Response.Write " <td width='200' height='33' align='right'>备份目录：</td>"
    Response.Write " <td><input type=text size=20 maxlength=50 name=bkfolder value=Sdmsbackup></td>"
    Response.Write " <td>相对路径目录，如目录不存在，将自动创建</td>"
    Response.Write "  </tr>"
    Response.Write "  <tr>"
    Response.Write " <td width='200' height='34' align='right'>备份名称：</td>"
    Response.Write " <td height='34'><input type=text maxlength=50 size=20 name=bkDBname value='" & Date & "'></td>"
    Response.Write " <td height='34'>不用输入文件名后缀（默认为“.asa”）。如有同名文件，将覆盖</td>"
    Response.Write "  </tr>"
    Response.Write "  <tr align='center'>"
    Response.Write " <td height='40' colspan='3'><input name='submit' type=submit value=' 开始备份 '"
    If ObjInstalled_FSO = False Then
        Response.Write " disabled"
    End If
    Response.Write "></td>"
    Response.Write "  </tr>"
    Response.Write "</table>"
    If ObjInstalled_FSO = False Then
        Response.Write "<b><font color=red>你的服务器不支持 FSO(Scripting.FileSystemObject)! 不能使用本功能</font></b>"
    End If
    Response.Write "    </td>"
    Response.Write "  </tr>"
    Response.Write "</table>"
    Response.Write "</form>"
End Sub

Sub ShowCompact()
    Response.Write "<form method='post' action='Admin_SdmsDatabaseManage.asp?action=CompactData'>"
    Response.Write "<table class='border' width='100%' border='0' align='center' cellpadding='0' cellspacing='0'>"
    Response.Write " <tr class='title'>"
    Response.Write "     <td align='center' height='22' valign='middle'><b>学籍数据库在线压缩</b></td>"
    Response.Write " </tr>"
    Response.Write " <tr class='tdbg'>"
    Response.Write "     <td align='center' height='150' valign='middle'>"
    Response.Write "      <br>"
    Response.Write "      <br>"
    Response.Write "      压缩前，建议先备份学籍数据库，以免发生意外错误。 <br>"
    Response.Write "      <br>"
    Response.Write "      <br>"
    Response.Write " <input name='submit' type=submit value=' 压缩学籍数据库 '"
    Response.Write "><br><br>"
    If ObjInstalled_FSO = False Or ObjInstalled_FSO = False Then
        Response.Write "<b><font color=red>你的服务器不支持 FSO(Scripting.FileSystemObject)! 不能使用本功能</font></b>"
    End If
    Response.Write "    </td>"
    Response.Write "  </tr>"
    Response.Write "</table>"
    Response.Write "</form>"
End Sub

Sub ShowRestore()
    Response.Write "<form method='post' action='Admin_SdmsDatabaseManage.asp?action=RestoreData'>"
    Response.Write "<table width='100%' class='border' border='0' align='center' cellpadding='0' cellspacing='0'>"
    Response.Write "  <tr class='title'>"
    Response.Write "    <td align='center' height='22' valign='middle'><b>学籍数据库恢复</b></td>"
    Response.Write "  </tr>"
    Response.Write "  <tr class='tdbg'>"
    Response.Write "    <td align='center' height='150' valign='middle'>"
    Response.Write "      <table width='100%' border='0' cellspacing='0' cellpadding='0'>"
    Response.Write "        <tr>"
    Response.Write "          <td width='200' height='30' align='right'>原备份学籍数据库路径（相对）：</td>"
    Response.Write "          <td height='30'><input name=backpath type=text id='backpath' value='Sdmsbackup\Data_Sdms.asa' size=50 maxlength='200'></td>"
    Response.Write "        </tr>"
    Response.Write "        <tr align='center'>"
    Response.Write "          <td height='40' colspan='2'><input name='submit' type=submit value=' 恢复数据 '"
    If ObjInstalled_FSO = False Then
        Response.Write " disabled"
    End If
    Response.Write ">"
    Response.Write "          </td>"
    Response.Write "        </tr>"
    Response.Write "      </table>"
    If ObjInstalled_FSO = False Then
        Response.Write "<b><font color=red>你的服务器不支持 FSO(Scripting.FileSystemObject)! 不能使用本功能</font></b>"
    End If
    Response.Write "    </td>"
    Response.Write "  </tr>"
    Response.Write "</table>"
    Response.Write "</form>"

    Response.Write "<br><b>说明：</b><br>&nbsp;&nbsp;&nbsp;&nbsp;原备份学籍数据库的扩展名必须为：asa或者asp<br><br>"
End Sub

Sub ShowInit()
    Dim TestTable, rsTest, sqlTest
    Response.Write "<script language = 'JavaScript'>" & vbCrLf
    Response.Write "function CheckForm(){" & vbCrLf
    Response.Write "  if(confirm('确实要清除选定的表吗？一旦清除将无法恢复！'))" & vbCrLf
    Response.Write "    {" & vbCrLf
    Response.Write "         return true;" & vbCrLf
    Response.Write "    }" & vbCrLf
    Response.Write "  else" & vbCrLf
    Response.Write "    return false;" & vbCrLf
    Response.Write "}" & vbCrLf
    Response.Write "function UnCheckTest(){" & vbCrLf
    Response.Write "  if(document.myform.chkTest.checked){" & vbCrLf
    Response.Write "    document.myform.chkTest.checked = document.myform.chkTest.checked&0;" & vbCrLf
    Response.Write "  }" & vbCrLf
    Response.Write "}" & vbCrLf
    Response.Write "function CheckTest(form){" & vbCrLf
    Response.Write "  for (var i=0;i<form.elements.length;i++){" & vbCrLf
    Response.Write "    var e = form.elements[i];" & vbCrLf
    Response.Write "    if (e.name){" & vbCrLf
    Response.Write "      if (e.name.substr(0,7) == 'T_Sdms_' && e.disabled==false)" & vbCrLf
    Response.Write "         e.checked = form.chkTest.checked;" & vbCrLf
    Response.Write "    }" & vbCrLf
    Response.Write "  }" & vbCrLf
    Response.Write "}" & vbCrLf

    Response.Write "function UnCheckOther(){" & vbCrLf
    Response.Write "  if(document.myform.chkOther.checked){" & vbCrLf
    Response.Write "    document.myform.chkOther.checked = document.myform.chkOther.checked&0;" & vbCrLf
    Response.Write "  }" & vbCrLf
    Response.Write "}" & vbCrLf
    Response.Write "function CheckOther(form){" & vbCrLf
    Response.Write "  for (var i=0;i<form.elements.length;i++){" & vbCrLf
    Response.Write "    var e = form.elements[i];" & vbCrLf
    Response.Write "    if (e.name){" & vbCrLf
    Response.Write "      if (e.name.substr(0,5) == 'Sdms_' && e.disabled==false)" & vbCrLf
    Response.Write "         e.checked = form.chkOther.checked;" & vbCrLf
    Response.Write "    }" & vbCrLf
    Response.Write "  }" & vbCrLf
    Response.Write "}" & vbCrLf
    Response.Write "</script>" & vbCrLf
    Response.Write "<form action='Admin_SdmsDatabaseManage.asp' method='post' name='myform' id='myform' onSubmit='return CheckForm();'>"
    Response.Write "<table class='border' width='100%' border='0' align='center' cellpadding='0' cellspacing='0'>"
    Response.Write "  <tr class='title'>"
    Response.Write "    <td align='center' height='22' valign='middle'><b>系 统 初 始 化</b></td>"
    Response.Write "  </tr>"
    Response.Write "  <tr class='tdbg'>"
    Response.Write "    <td width='100%' height='150' align=center valign='middle'>"
    If Action = "Clear" Then
        Response.Write "      <div align='left'>"
        Call ClearData
        Response.Write "      </div>"
    Else
        Response.Write "      <table border='0' cellspacing='0' cellpadding='5'>"
        Response.Write "        <tr>"
        Response.Write "          <td>"
        Response.Write "            <fieldset name='TestData'><legend>考试表数据 <input name='chkTest' type='checkbox' id='chkTest' onclick='CheckTest(this.form)' value=''></legend><table width='600' border='0' cellpadding='0' cellspacing='5'>"
        Response.Write "              <tr>"
        sqlTest = "select * from TestName order by ID"
        Set rsTest = Conn_Sdms.Execute(sqlTest)
        Dim i
        i = 0
        Do While Not rsTest.EOF
            Response.Write "                <td width='50%'><input name='T_Sdms_" & rsTest("SheetName") & "' type='checkbox' id='T_Sdms_" & rsTest("SheetName") & "' onclick='UnCheckTest()' value='yes'> " & Left(rsTest("TestName"), 16) & "(" & rsTest("SheetName") & ")</td>"
            i = i + 1
            If i Mod 2 = 0 Then Response.Write "              </tr><tr>"
            rsTest.MoveNext
        Loop
        rsTest.Close
        Set rsTest = Nothing
        Response.Write "              <tr><td colspan='2'>"
        Response.Write "<font color='green'>&nbsp;&nbsp;清除所选考试表数据的同时删除该表：&nbsp;&nbsp;<input type='radio' name='delTable' value='1'>&nbsp;是"
        Response.Write "&nbsp;&nbsp;<input type='radio' name='delTable' value='0' checked>&nbsp;否</font>"
        Response.Write "              </td>"
        Response.Write "              </tr>"
        Response.Write "            </table></fieldset>"
        Response.Write "          </td>"
        Response.Write "        </tr>"


        Response.Write "        <tr>"
        Response.Write "          <td>"
        Response.Write "            <fieldset name='OtherData'><legend>其他表数据 <input name='chkOther' type='checkbox' id='chkOther' onclick='CheckOther(this.form)' value=''></legend><table width='600' border='0' cellpadding='0' cellspacing='5'>"
        Response.Write "              <tr>"
        Response.Write "                <td width='33%'><input name='Sdms_Grade' type='checkbox' id='Sdms_Grade' onclick='UnCheckOther()' value='yes'> 年级数据表</td>"
        Response.Write "                <td width='33%'><input name='Sdms_Class' type='checkbox' id='Sdms_Class' onclick='UnCheckOther()' value='yes'> 班级数据表</td>"
        Response.Write "                <td width='33%'><input name='Sdms_StudentInfo' type='checkbox' id='StudentInfo' onclick='UnCheckOther()' value='yes'> 学生信息数据表</td>"

        Response.Write "              </tr>"
        Response.Write "              <tr>"
        Response.Write "                <td width='33%'><input name='Sdms_Subject' type='checkbox' id='Sdms_Subject' onclick='UnCheckOther()' value='yes'> 科目数据表</td>"
        Response.Write "                <td width='33%'><input name='Sdms_TargetScore' type='checkbox' id='Sdms_TargetScore' onclick='UnCheckOther()' value='yes'> 目标分数据表</td>"
        Response.Write "                <td width='33%'><input name='Sdms_TestName' type='checkbox' id='Sdms_TestName' onclick='UnCheckOther()' value='yes'> 考试名称数据表</td>"

        Response.Write "              </tr>"
        Response.Write "            </table></fieldset>"
        Response.Write "          </td>"
        Response.Write "        </tr>"
        Response.Write "        <tr>"
        Response.Write "          <td align='center'><input name='Action' type='hidden' id='Action' value='Clear'>"
        Response.Write "            <input type='submit' name='Submit' value='清除所选学籍数据库内容'>"
        Response.Write "          </td>"
        Response.Write "        </tr>"
        Response.Write "      </table>"
    End If
    Response.Write "    </td>"
    Response.Write "  </tr>"
    Response.Write "</table>"
    Response.Write "</form>"
    If Action <> "Clear" Then
        Response.Write "<b>说明：</b>&nbsp;&nbsp;<font color='#FF0000'>请慎用此功能，因为一旦清除将无法恢复！</font><br>"
    End If
End Sub

Sub SpaceSize()
    'On Error Resume Next
    Response.Write "<br><table class='border' width='100%' border='0' align='center' cellpadding='0' cellspacing='0'>"
    Response.Write "  <tr class='title'>"
    Response.Write "    <td align='center' height='22' valign='middle'><b>学籍系统空间占用情况</b></td>"
    Response.Write "  </tr>"
    Response.Write "  <tr class='tdbg'>"
    Response.Write "    <td width='100%' height='150' valign='middle'>"
    Response.Write "    <blockquote>"
    Response.Write "      <br><b>学籍系统文件占用空间情况：</b><br>"


    Response.Write "      <br>"
    Response.Write "      学籍数据库占用空间：" & ShowSpace("Sdms\database")
    Response.Write "      <br>"
    Response.Write "      学籍管理占用总空间：" & ShowSpace("Sdms")
    Response.Write "      <br><br>"

    Response.Write "      <br>整网站占用空间总计：" & ShowSpace(" ")
    Response.Write "    </blockquote>"
    Response.Write "    </td>"
    Response.Write "  </tr>"
    Response.Write "</table>"
End Sub

Sub BackupData()
    Dim bkfolder, bkdbname
    bkfolder = Trim(Request("bkfolder"))
    bkdbname = Trim(Request("bkdbname"))
    If bkfolder = "" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>请指定备份目录！</li>"
    End If
    If bkdbname = "" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>请指定备份文件名</li>"
    End If
    If FoundErr = True Then Exit Sub
    bkfolder = Server.MapPath(bkfolder)
    If fso.FileExists(dbpath) Then
        If fso.FolderExists(bkfolder) = False Then
            fso.CreateFolder (bkfolder)
        End If
        fso.copyfile dbpath, bkfolder & "\" & bkdbname & ".asa"
        Call WriteSuccessMsg("备份学籍数据库成功，备份的学籍数据库为：<br>" & bkfolder & "\" & bkdbname & ".asa", ComeUrl)
    Else
        FoundErr = True
        ErrMsg = ErrMsg & "<li>找不到源学籍数据库文件，请检查本文件中第35行“dbpath”的配置。</li>"
    End If
End Sub

Sub CompactData()
    Dim Engine, strDBPath
    Call CloseSdmsConn
    strDBPath = Left(dbpath, InStrRev(dbpath, "\"))
    If fso.FileExists(dbpath) Then
        Set Engine = Server.CreateObject("JRO.JetEngine")
        Engine.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath, " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & "temp.mdb"
        fso.copyfile strDBPath & "temp.mdb", dbpath
        fso.DeleteFile (strDBPath & "temp.mdb")
        Set Engine = Nothing
        Call WriteSuccessMsg("学籍数据库压缩成功!", ComeUrl)
    Else
        FoundErr = True
        ErrMsg = ErrMsg & "<li>学籍数据库没有找到!</li>"
    End If
End Sub

Sub RestoreData()
    Dim backpath
    backpath = Trim(Request.Form("backpath"))
    If backpath = "" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>请指定原备份的学籍数据库文件名！</li>"
        Exit Sub
    End If
    If GetFileExt(backpath) <> "asa" And GetFileExt(backpath) <> "asp" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>原备份学籍数据库文件的扩展名必须为asa或asp！</li>"
        Exit Sub
    End If
    backpath = Server.MapPath(backpath)
    If fso.FileExists(backpath) Then
        fso.copyfile backpath, dbpath
        Call WriteSuccessMsg("成功恢复数据！", ComeUrl)
    Else
        FoundErr = True
        ErrMsg = ErrMsg & "<li>找不到指定的备份文件！</li>"
    End If
    
End Sub

Sub ClearData()
    Dim strClear, z
    z = 0
    strClear = strClear & "<b>清空内容列表：</b>"
    strClear = strClear & "<br><br><b>考试表数据：</b>"
    Dim TestTable, rsTest, sqlTest
    sqlTest = "select * from TestName order by ID"
    Set rsTest = Conn_Sdms.Execute(sqlTest)
    Do While Not rsTest.EOF
        If Request("T_Sdms_" & rsTest("SheetName")) = "yes" Then
            Conn_Sdms.Execute ("delete from " & rsTest("SheetName") & "")
            strClear = strClear & rsTest("TestName") & "(" & rsTest("SheetName") & ")&nbsp;&nbsp;"
            z = z + 1
        End If
        rsTest.MoveNext
    Loop
    rsTest.Close
    Set rsTest = Nothing

    If Request("delTable") = 1 Then
        Dim rsTestT, sqlTestT
        sqlTestT = "select * from TestName order by ID"
        Set rsTestT = Server.CreateObject("adodb.recordset")
        rsTestT.Open sqlTestT, Conn_Sdms, 1, 3
        Do While Not rsTestT.EOF
            If Request("T_Sdms_" & rsTestT("SheetName")) = "yes" Then
                Conn_Sdms.Execute ("drop table " & rsTestT("SheetName") & "")
                rsTestT.Delete
                rsTestT.Update
            End If
            rsTestT.MoveNext
        Loop
        rsTestT.Close
        Set rsTestT = Nothing
        strClear = strClear & "<br><br><b>同时删除数据库中上列考试表！</b>"
    Else
        strClear = strClear & "<br><br><b>无须删除数据库中上列考试表！</b>"
    End If
    strClear = strClear & "<br><br><b>其他表数据：</b>"
    If Request("Sdms_Grade") = "yes" Then
        Conn_Sdms.Execute ("delete from Grade")
        strClear = strClear & "年级数据表&nbsp;&nbsp;"
        z = z + 1
    End If
    If Request("Sdms_Class") = "yes" Then
        Conn_Sdms.Execute ("delete from Class")
        strClear = strClear & "班级数据表&nbsp;&nbsp;"
        z = z + 1
    End If
    If Request("Sdms_StudentInfo") = "yes" Then
        Conn_Sdms.Execute ("delete from StudentInfo")
        strClear = strClear & "学生信息表&nbsp;&nbsp;"
        z = z + 1
    End If
    If Request("Sdms_Subject") = "yes" Then
        Conn_Sdms.Execute ("delete from Subject")
        strClear = strClear & "科目数据表&nbsp;&nbsp;"
        z = z + 1
    End If
    If Request("Sdms_TargetScore") = "yes" Then
        Conn_Sdms.Execute ("delete from TargetScore")
        strClear = strClear & "目标分表&nbsp;&nbsp;"
        z = z + 1
    End If
    If Request("Sdms_TestName") = "yes" Then
        Conn_Sdms.Execute ("delete from TestName")
        strClear = strClear & "考试名称表&nbsp;&nbsp;"
        z = z + 1
    End If

    If z > 0 Then
        Response.Write "学籍系统初始化成功！共计 <font color='red'>" & CStr(z) & "</font> 项内容被清空。<br><br>"
        Response.Write strClear

    Else
        Response.Write "您没有选择任何学籍数据库内容，请重新选择后再进行操作。"
    End If
End Sub

Function ShowSpace(FolderPath)
    Dim ft, fd, fs, TotalSize, SpaceSize, FolderBarWidth, arrPath, strSize, i
    Set ft = fso.GetFolder(Server.MapPath(InstallDir))
    TotalSize = ft.size
    If TotalSize = 0 Then TotalSize = 1

    SpaceSize = 0
    arrPath = Split(FolderPath, "|")
    For i = 0 To UBound(arrPath)
        If arrPath(i) = "SiteRoot" Then
            Set fd = fso.GetFolder(Server.MapPath(InstallDir))
            For Each fs In fd.Files
                SpaceSize = SpaceSize + fs.size
            Next
        Else
            If fso.FolderExists(Server.MapPath(InstallDir & arrPath(i))) Then
                Set fd = fso.GetFolder(Server.MapPath(InstallDir & arrPath(i)))
                SpaceSize = SpaceSize + fd.size
            End If
        End If
    Next
    FolderBarWidth = PE_CLng((SpaceSize / TotalSize) * Barwidth)

    strSize = SpaceSize & "&nbsp;Byte"
    If SpaceSize > 1024 Then
       SpaceSize = (SpaceSize / 1024)
       strSize = FormatNumber(SpaceSize, 2, vbTrue, vbFalse, vbTrue) & "&nbsp;KB"
    End If
    If SpaceSize > 1024 Then
       SpaceSize = (SpaceSize / 1024)
       strSize = FormatNumber(SpaceSize, 2, vbTrue, vbFalse, vbTrue) & "&nbsp;MB"
    End If
    If SpaceSize > 1024 Then
       SpaceSize = (SpaceSize / 1024)
       strSize = FormatNumber(SpaceSize, 2, vbTrue, vbFalse, vbTrue) & "&nbsp;GB"
    End If
    strSize = "<font face=verdana>" & strSize & "</font>"
    ShowSpace = "&nbsp;<img src='../images/bar.gif' width='" & FolderBarWidth & "' height='10' title='" & FolderPath & "'>&nbsp;" & strSize
End Function

%>
