SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Adding Discussion Forums : Administration Pages

Equally important as the user pages are the administration pages. From these pages you can create new forums and edit or delete existing messages. The pages for creating forums and choosing a topic are very similar to the pages used to view forums and topics. Therefore, we are not going to go into those pages details (they are included in the download). However, we are going to show you the script for the edit topic page. This page is interesting because it provides one-click in place editing of each message.

<%@ Language=VBScript %>
<% option explicit 


' The ID of the topic you are editing
dim topicid
topicid = Request.QueryString("topic_id")


if topicID="" or not isnumeric(topicID) then Response.redirect("forums.asp")

' This should be updated to the administrators account
' (for real security - the admin pages should be 
' in an NT-secured administrator directory
if session("u_id")="" then Response.Redirect("/members/logon.asp")

Dim oCmd, oRS

' The 3 actions available
const ACTIONDELETE = "Delete"
const ACTIONEDIT = "Edit"
const ACTIONSAVE = "Save"

if Request.Form("action")=ACTIONDELETE then
  ' Delete the message
  Set oCmd = GetStoredProcedure(getConnection(),"sp_DeleteMessage")
  oCmd.Parameters.append oCmd.CreateParameter("topic_id", adInteger, adParamInput,200,Request.Form("topic_id"))
  oCmd.execute()
end if

if Request.Form("action")=ACTIONSAVE then
  ' Save Updated Message
  Set oCmd = GetStoredProcedure(getConnection(),"sp_EditMessage")
  oCmd.Parameters.append oCmd.CreateParameter("topic_id", adInteger, adParamInput,10,Request.Form("topic_id"))
  oCmd.Parameters.append oCmd.CreateParameter("topic_name", adVarChar, adParamInput,200,escapeString(Request.Form("topic_name")))
  oCmd.Parameters.append oCmd.CreateParameter("topic_message", adLongVarChar, adParamInput,32000,escapeStringWithCR(Request.Form("topic_message")))
  oCmd.execute()
end if

' Get the messages for the topic 
' (same technique as the user page)
Set oCmd = GetStoredProcedure(getConnection(),"sp_getMessagesForTopic")
oCmd.Parameters.append oCmd.CreateParameter("topic_id", adInteger, adParamInput,10,topicID)
set oRS = oCmd.execute()

if oRS.eof then Response.Redirect("editForums.asp")

Dim sForumName, sForumDesc, iForumID, iReplies
' Forum information
iForumID = oRS.fields("forum_id")
sForumName = oRS.fields("forum_name")
iReplies = oRS.fields("topic_count")

%>
<HTML>
<HEAD>
<!-- #include virtual="/inc/utility.asp" -->
<TITLE><%=sForumName%></TITLE>
</HEAD>
<BODY>

<%


Response.Write("<H2>" & HOMEPAGE & " : <A HREF=""manager.asp"">Site Administrator</A> : <A HREF=""editforums.asp"">Forum Manager</A> : <A HREF=""adminforum.asp?forum_id=" & iForumID & """>" & sForumName & "</A></H2>")

Response.Write("<P>You can edit or delete messages in this topic. Deleting the starting message removes the entire topic.")
Response.Write(BuildForm("","POST") & "<TABLE BGCOLOR=lightgrey WIDTH=400>")
if cInt(Request.Form("topic_id"))=cInt(oRS.fields("topic_id")) and Request.Form("action")=ACTIONEDIT then
  ' Editing the original topic
  Response.Write("<TR><TD>" & BuildInput("text","topic_name",oRS.fields("topic_name"),25,200) & "</TD></TR>")
  Response.Write("<TR><TD>" & BuildTextArea("topic_message",oRS.fields("topic_message"),15,35) & "</TD></TR>")
  Response.Write("<TR><TD><SMALL><EM>Started By " & oRS.fields("u_name") & " on:" & oRS.fields("topic_create") & "</EM></SMALL></TD></TR>")
  Response.Write("<TR><TD>" & BuildInput("hidden","topic_id",oRS.fields("topic_id"),"","") & BuildInput("submit","action",ACTIONSAVE,"","") & " " & BuildInput("submit","action",ACTIONDELETE,"","") & "</TD></TR>")
else
  ' Not editing original topic - just output 
  Response.Write("<TR><TD><FONT SIZE=+1><B>" & oRS.fields("topic_name") & "</B></FONT></TD></TR>")
  Response.Write("<TR><TD>" & oRS.fields("topic_message") & "</TD></TR>")
  Response.Write("<TR><TD><SMALL><EM>Started By " & oRS.fields("u_name"))
  Response.Write(" on:" & oRS.fields("topic_create") & "</EM></SMALL></TD></TR>")
  ' Output edit and delete buttons
  Response.Write("<TR><TD>" & BuildInput("hidden","topic_id",oRS.fields("topic_id"),"",""))
  Response.Write(BuildInput("submit","action",ACTIONEDIT,"","") & " ")
  Response.Write(BuildInput("submit","action",ACTIONDELETE,"","") & "</TD></TR>")
end if
Response.Write("</TABLE></FORM>")
set oRS = oRS.nextrecordset

' Output messages
' We reused much of the code from viewTopic.asp
if (not oRS.eof) then
  if iReplies=1 then
    Response.Write("<P>1 Reply")
  else
    Response.Write("<P>" & iReplies & " Replies")
  end if
  while not oRS.eof
    Response.Write(BuildForm("","POST"))
    if cInt(Request.Form("topic_id"))=cInt(oRS.fields("topic_id")) and Request.Form("action")=ACTIONEDIT then
      ' Editing reply - display form
      Response.Write("<P><B>" & oRS.fields("u_name") & " on " & oRS.fields("topic_update") & "</B><BR>")
      Response.Write("<TR><TD>" & BuildTextArea("topic_message",oRS.fields("topic_message"),15,35) & "</TD></TR>")
      Response.Write("<BR>" & BuildInput("hidden","topic_name","","","") & BuildInput("hidden","topic_id",oRS.fields("topic_id"),"","") & BuildInput("submit","action",ACTIONSAVE,"","") & " " & BuildInput("submit","action",ACTIONDELETE,"","") & "</FORM>")
    else
      ' Not editing reply - output
      Response.Write("<P><B>" & oRS.fields("u_name") & " on " & oRS.fields("topic_update") & "</B><BR>")
      Response.Write(oRS.fields("topic_message"))
      ' Output Edit and Delete buttons
      Response.Write("<TR><TD>" & BuildInput("hidden","topic_id",oRS.fields("topic_id"),"",""))
      Response.Write(BuildInput("submit","action",ACTIONEDIT,"","") & " ")
      Response.Write(BuildInput("submit","action",ACTIONDELETE,"","") & "</TD></TR>")
    end if
    oRS.movenext
  wend
end if
%>
</BODY>
</HTML>

That's it! You now have seen all the SQL queries and ASP scripts required to add simple discussion forums to your web site. On the next page we explain how to download and install the sample files.