Click here to Skip to main content
15,883,796 members
Articles / Programming Languages / ASP

The Code Project Discussion boards

,
Rate me:
Please Sign up or sign in to vote.
4.78/5 (39 votes)
25 Aug 2001CPOL 2.9M   15.2K   144  
The Discussion board ASP scripts as used in The Code Project. This is an open source project for the Code Project community.
<%
' ###########################################################################
' #
' # file:
' # 	forum.inc
' #
' # description:
' # 	forum related function.
' #
' # version:
' # 	1.02  1999-12-15 Uwe Keim   createForum() added.
' # 	1.01  1999-12-10 Uwe Keim   commented heavily.
' # 	1.00  1999-12-08 Uwe Keim   file created.
' #
' ###########################################################################

	   
' ///////////////////////////////////////////////////////////////////////////
' // helper class for forum.

' this class is the main communication point that contains all forum-related
' functionality. you never communicate with the forum-active-x directly, 
' but always indirectly with this class.

class Forum
	' ---------------------------------------------
	' uri functions.

	' these functions should be used whenever you need a <a href="...">
	' tag. instead of specifying an asp page and adding some cryptic
	' parameters, you call one of these functions.
	' with this approach, you can centralize the url-parameters
	' in one place.
	'
	' you have to modify the asp filenames in the following functions,
	' when you change the name of the real files.

	' content page.
	public function getUriNoContent
		getUriNoContent = "nocontent.asp"
	end function

'reply.asp 
	public function getUriReplyForum
		getUriReplyForum = "reply.asp?" &toUrl
	end function

'reply.asp 
	public function getUriSendReply
		getUriSendReply = "insert_reply.asp?" &toUrl
	end function

'mail.asp 
	public function getUriReplyAuthor
		getUriReplyAuthor = "mail.asp?" &toUrl
	end function

'modify.asp  
	' list page.
	public function getUriModifyCurMsg( id )
		getUriModifyCurMsg = "modify.asp?" & _
			toUrlValue2("modify", id, "select",id)
	end function

'delete.asp
	public function getUriDeleteCurMsg( id ) 
		getUriDeleteCurMsg  = "delete.asp?" & _
			toUrlValue2("delkind","one", "del",id)
	end function

'delete.asp
public function getUriDeleteCurMsgSubs(id) 
		getUriDeleteCurMsgSubs  = "delete.asp?" & _
			toUrlValue2("delkind","branch", "del", id)
	end function
	
'index.asp 
	public function getUriSelectMsg(id)
		getUriSelectMsg = "start.asp?" & _
			toUrlValue("select",id)
		appendAnchor getUriSelectMsg, id
	end function

'index.asp 
	public function getUriListWithForum( forum_id )
		
		getUriListWithForum = "start.asp?"& _		
			toUrlValue("forumid",forum_id)				
		'traceX toUrlValue("forumid",forum_id)
	end function

'insert_reply.asp 
	' reply page(s).
	public function getUriInsertReply
		getUriInsertReply = "reply.asp?" & toUrl
	end function

'failure.asp 
	public function	getUriReplyError( err_text )
		getUriReplyError = "failure.asp?err_txt=" & _
			Server.UrlEncode( err_text )
	end function

'index.asp
	public function getUriMain( article_id )
		getUriMain = "start.asp?" &toUrl
	appendAnchor getUriMain, article_id
	end function

'send_mail.asp 
	' mail page(s).
	public function getUriSendMail
		getUriSendMail = "send_mail.asp?" & toUrl
	end function

'failure.asp 
	public function	getUriMailError( err_text )
		getUriMailError = "failure.asp?err_txt=" & _
			Server.UrlEncode( err_text )
	end function

'insert_new.asp 
	' new page(s).
	public function getUriInsertNew
		getUriInsertNew = "insert_new.asp?" & toUrl
	end function

'failure.asp 
	public function	getUriNewError( err_text )
		getUriNewError = "failure.asp?err_txt=" & _
			Server.UrlEncode( err_text )
	end function

'insert_modify.asp 
	' modify page(s).
	public function getUriModify
		getUriModify = "insert_modify.asp?" & toUrl
	end function

'failure.asp
	public function	getUriModifyError( err_text )
		getUriModifyError = "failure.asp?err_txt=" & _
			Server.UrlEncode( err_text )
	end function

'new.asp 
	' misc page(s).
	public function getUriNew( forum_id )
		getUriNew = "new.asp?" &_
			toUrlValue("forumid",forum_id)
	end function

	' ---------------------------------------------
	' faked constructor.

	' always call this immediately after you created
	' a new instance of this class.
	public sub init()
		
		Set m_Dic = CreateObject("Scripting.Dictionary")
		
		'db connection
		set m_conn = Server.CreateObject("ADODB.Connection")
		m_conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" &_
		"Persist Security Info=False;" &_
		"Data Source=" &Server.MapPath("../database/forum.mdb")

		' read in all parameters from the current page's url.
		fromUrl( myselfComplete )
	end sub

	public sub readArticles( forum_id )
		m_Dic.Item( "forumid" ) = forum_id
	end sub

	' ---------------------------------------------


	' checks, whether the forum is currently in admin mode.
	' in admin mode, you can modify and delete articles.
	public function isInAdminMode()
		isInAdminMode = false

		if Request("ad")<>"" then
			if CStr(Request("ad"))=CStr(ADMIN_PW) then
				isInAdminMode = true
			end if
		end if
	end function

	' get the id of a forum from its short-name.
	' returns null if not found.
	' you may need to strengthen the sql query (the 'LIKE')
	' when you have forums with similar names.
	public function getForumId( forum_name )
		dim rs
		set rs = m_Conn.Execute( "SELECT * FROM Forum " & _
			"WHERE Shortname LIKE '%" &forum_name& "%'" )

		if not (rs.BOF and rs.EOF) then
			getForumId = rs("ID")
		else
			getForumId = null
		end if
	end function

	' creates a forum with a long name.
	' returns the id of the forum.
	' if the forum already exists, only its it
	' is returned.
	public function createForum( forum_long_name )
		dim rs
		set rs = m_Conn.Execute( "SELECT * FROM Forum " & _
			"WHERE Name LIKE '%" &forum_long_name& "%'" )

		if not (rs.BOF and rs.EOF) then
			createForum = rs("ID")
		else
			rs.Close

			set rs = Server.CreateObject("ADODB.Recordset")
			rs.Open "Forum", m_Conn, 2, 3

			rs.AddNew
			rs("Name")        = forum_long_name
			rs.Update
			
			createForum = rs("ID")
		end if
	end function

	' returns the long name of a forum, given the id of a forum.
	public function getForumName( forum_id )
		dim rs
		set rs = m_Conn.Execute( "SELECT * FROM Forum WHERE ID=" &forum_id )
		getForumName = rs("Name")
	end function

	' gets the recordset of an article, given the id of the article.
	public function getArticleRs( article_id )
		set getArticleRs = m_Conn.Execute( _
			"SELECT * FROM Article WHERE ID=" &article_id )
	end function

	' gets the recordset of a forum, given the id of the forum.
	public function getForumRs( forum_id )
		set getForumRs = m_Conn.Execute( _
			"SELECT * FROM Forum WHERE ID=" &forum_id )
	end function

	' gets the recordset with all forums.
	public function getForumsRs()
		set getForumsRs = m_Conn.Execute( "SELECT * FROM Forum" )
	end function

	' insert a child-article for an already present article.
	' set 'parent_id' to zero for inserting a root article
	' for the given forum.
	public function insertNew( parent_id, forum_id, subject, author_name, _
		author_email, text, email_notify )

		dim rs
		set rs = Server.CreateObject("ADODB.Recordset")
		rs.Open "Article", m_Conn, 2, 3

		'------------------------------------------- 
		'assortment of the entries:
		'every entry has a position-number (position), which determines the assortment
		'of the output. The most up-to-date entry of the first level gets the lowest number.
		'The answer-entries get the first number of their parent`s position and, after a ".",
		'the next number which is sorted ascending.
		
		dim sql_pos
		dim rs_pos
		dim sql_child
		dim rs_child
		dim position
		dim last
		dim x
		dim indent

			'if it is a parent entry
			if parent_id = "0" then
				sql_pos = "SELECT * FROM Article WHERE ForumID =" &forum_id& " ORDER BY [Position]"
				set rs_pos = m_Conn.Execute(sql_pos) 
				
				if rs_pos.Eof then
					position = "999999"
				else
					x = CLng(rs_pos("Position"))-1

					'if there are more than 999999 entries
					if x = 0 then x = 1

					while Len(x) < 6
						 x = "0" & x
					wend
					position = x
				end if
			indent = "0"

			'if it is a child entry
			else
				sql_pos = "SELECT * FROM Article WHERE ID =" &parent_id
				set rs_pos = m_Conn.Execute(sql_pos)


				sql_child = "SELECT * FROM Article WHERE ParentID =" &parent_id& " ORDER BY Date DESC"
				set rs_child = m_Conn.Execute(sql_child)
				indent = CLng(rs_pos("Indent"))+1

				'if there are no other childs
				if (rs_child.Bof and rs_child.Eof) then
					position = rs_pos("Position") & ".000001"

				'if there are other childs
				else
					last = Right(rs_child("Position"), 6)

					x = CStr(CLng(last)+1)
					while Len(x) < 6
						 x = "0" & x
					wend
					'if there are more than 999999 childs
					if x > 999999 then x = 999999
					position = rs_pos("Position") & "." & x
				end if
			end if
			
		'--------------------------------------------



		'--------------------------------------------
		'Database entry

		rs.AddNew
		rs("ParentID")    = parent_id
		rs("ForumID")     = forum_id
		rs("Subject")     = subject
		rs("AuthorName")  = author_name
		rs("AuthorEmail") = author_email
		rs("Date")        = Now()
		rs("Text")        = text
		rs("Notify")      = email_notify
		rs("Position")    = position
		rs("Indent")      = indent
		rs.Update

		' mark the added article as selected.
		m_Dic.Item("select") = rs("ID")

		insertNew = rs("ID")
		'---------------------------------------------
	end function

	' modifes an already present article.
	public sub modifyArticle( article_id, subject, author_name, _
		author_email, text, email_notify )

		m_Conn.Execute "UPDATE Article SET " & _
			"Article.Subject='" &subject& _
			"',Article.AuthorName='" &author_name& _
			"',Article.AuthorEmail='" &author_email& "',Article.Text='" &text& _
			"',Article.Notify='" &email_notify& _
			"' WHERE Article.ID=" &article_id
		dim rs
		set rs = Server.CreateObject("ADODB.Recordset")
		rs.Open "Article", m_Conn, 2, 3
	end sub

	' returns the article-id of the article 
	' at the current file pointer position.
	'public function getCurID
		'getCurID = "201"
	'end function


	' deletes an article and all its childs, given the 
	' article id of this article.
	public sub deleteArticle( article_id )

	dim parent_position, forum_id
	dim rs_p, sql_p
	
	sql_p = "SELECT * FROM Article WHERE ID ="&Request("del")
	set rs_p = m_Conn.Execute(sql_p)

	parent_position =	rs_p("Position")
	forum_id =			Request("forumid")

		m_conn.Execute "DELETE FROM Article WHERE [Position] LIKE '%"&parent_position&"%' AND ForumID ="&forum_id

	end sub

	
	'--------------------------------------------------------------------------------
	' build the internal map of name-value pairs from a given url.
	function fromURL(url)

		dim url_element

		if InStr(url, "?") then
			
			url_element = split(url, "?")

			'if the url has no parameters
			if url_element(1) = "" then
				exit function
			'if the url has parameters
			else
				dim params
				dim item
				params = split(url_element(1), "&")
				

				dim x

				For each x in params
					if InStr(x, "=") then
						item = split(x, "=")
						
						'add key-item to object
						m_Dic.Add item(0), item(1)
					end if
				Next

			end if
		end if
	 
	end function
	



	'--------------------------------------------------------------------------------
	' returns all values of the internal map url-encoded
	' in the form "name=value&name=value&name=value&" etc.
	' you can append this directly to an url.
	
	function toUrl
		dim x,s,a,b
		a = m_Dic.Keys
		b = m_Dic.Items

		For x = 0 To m_Dic.Count -1
			s = s & a(x) & "=" & Server.URLEncode(b(x)) & "&"
		Next
		toUrl = s
		
	end function
	

	'--------------------------------------------------------------------------------
	' the same as "toUrl" except that the value of the
	' map-entry specified by "name" is replaced by "new_value".
	function toUrlValue(key,new_item)
		dim x,s,a,b

		if not m_Dic.Exists(key)then
			m_Dic.Add key, new_item
		end if

		a = m_Dic.Keys
		b = m_Dic.Items

		For x = 0 To m_Dic.Count -1
			if a(x)=key then
				s = s & a(x) & "=" & Server.URLEncode(new_item) & "&"
			else
				s = s & a(x) & "=" & Server.URLEncode(b(x)) & "&"
			end if
		Next

		toUrlValue = s
		
	end function
	

	'--------------------------------------------------------------------------------
	' the same as "toUrl" except that the value of the
	' map-entry specified by "name1" is replaced by "new_value1" and
	' the value of "name2" is replaced by "new_value2".
	function toUrlValue2( key1, new_item1, key2, new_item2 )
		dim x,s,a,b

		if not m_Dic.Exists(key1)then
			m_Dic.Add key1, new_item1
		end if

		if not m_Dic.Exists(key2) then
			m_Dic.Add key2, new_item2
		end if

		a = m_Dic.Keys
		b = m_Dic.Items

		For x = 0 To m_Dic.Count -1
			if a(x)=key1 then
				s = s & a(x) & "=" & Server.URLEncode(new_item1) & "&"
			elseif a(x)=key2 then
				s = s & a(x) & "=" & Server.URLEncode(new_item2) & "&"
			else
				s = s & a(x) & "=" & Server.URLEncode(b(x)) & "&"
			end if
		Next
		toUrlValue2 = s
	end function

	' ---------------------------------------------

	' helper function. opens the database connection.
	private function openDb()
		set openDb = Server.CreateObject("ADODB.Connection")
		openDb.Open DB, DB_ID, DB_PW
	end function

	' appends an anchor-link "#xxx" to an url.
	private sub appendAnchor( byref url, anchor_name )

		'-temp: exit until it works.
		exit sub

		' remove any '?' or '&' at the end.
		while Len(url)>0 and _
			(Right(url,1)="&" or Right(url,1)="?")
			url = delete(url, Len(url), 1)
		wend
		
		' append the anchor.
		url = url& "#" &anchor_name
	end sub

	' ---------------------------------------------

	public m_Conn		' the database-connection.
	'private m_Ax		' the connection to the active-x object.
	Private m_Dic		' scripting.dictionary
end class


' ***************************************************************************

%>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions