root/trunk/website/forums/cfcs/forum.cfc @ 11

Revision 5, 12.1 kB (checked in by DanWilson, 17 years ago)

Initial Commit Of ModelGlue? Website (upgrade to blogcfc 511)

Line 
1<!---
2        Name         : forum.cfc
3        Author       : Raymond Camden
4        Created      : January 26, 2005
5        Last Updated : November 5, 2006
6        History      : Support dbtype, switched to UUID (rkc 1/26/05)
7                                   getForums now gets last msg, msg count (rkc 4/6/05)
8                                   ms access fix in getForums. I love joins (rkc 4/15/05)
9                                   New init, use of table prefix (rkc 8/27/05)
10                                   getForums returns the thread id for newest thread (rkc 9/15/05)
11                                   Accidently left a hard coded ID in getForums (rkc 9/28/05)
12                                   getForums, conferenceid is NOT required (rkc 9/29/05)
13                                   limit search length (rkc 10/30/05)
14                                   clean up subscription (rkc 11/22/05)
15                                   show last user for post, other small fixes (rkc 7/12/06)                               
16                                   Simple size change (rkc 7/27/06)
17                                   Attachment support (rkc 11/3/06)
18                                   Reverted description to text field (rkc 11/5/06)                               
19        Purpose          :
20--->
21<cfcomponent displayName="Forum" hint="Handles Forums which contain a collection of threads.">
22
23        <cfset variables.dsn = "">
24        <cfset variales.dbtype = "">
25        <cfset variables.tableprefix = "">
26               
27        <cffunction name="init" access="public" returnType="forum" output="false"
28                                hint="Returns an instance of the CFC initialized with the correct DSN.">
29                <cfreturn this>
30               
31        </cffunction>
32
33        <cffunction name="addForum" access="remote" returnType="uuid" roles="forumsadmin" output="false"
34                                hint="Adds a forum.">                           
35                <cfargument name="forum" type="struct" required="true">
36                <cfset var newforum = "">
37                <cfset var newid = createUUID()>
38               
39                <cfif not validForum(arguments.forum)>
40                        <cfset variables.utils.throw("ForumCFC","Invalid data passed to addForum.")>
41                </cfif>
42               
43                <cfquery name="newforum" datasource="#variables.dsn#">
44                        insert into #variables.tableprefix#forums(id,name,description,readonly,active,conferenceidfk,attachments)
45                        values(<cfqueryparam value="#newid#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">,
46                                   <cfqueryparam value="#arguments.forum.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="255">,
47                                   <cfqueryparam value="#arguments.forum.description#" cfsqltype="CF_SQL_VARCHAR" maxlength="255">,
48                                   <cfqueryparam value="#arguments.forum.readonly#" cfsqltype="CF_SQL_BIT">,
49                                   <cfqueryparam value="#arguments.forum.active#" cfsqltype="CF_SQL_BIT">,
50                                   <cfqueryparam value="#arguments.forum.conferenceidfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">,
51                                   <cfqueryparam value="#arguments.forum.attachments#" cfsqltype="CF_SQL_BIT">
52                                   )
53                </cfquery>
54               
55                <cfreturn newid>
56                               
57        </cffunction>
58       
59        <cffunction name="deleteForum" access="public" returnType="void" roles="forumsadmin" output="false"
60                                hint="Deletes a forum along with all of it's children.">
61                <cfargument name="id" type="uuid" required="true">
62                <cfset var threadKids = "">
63                               
64                <!--- first, delete my children --->
65                <cfset threadKids = variables.thread.getThreads(false,arguments.id)>
66                <cfloop query="threadKids">
67                        <cfset variables.thread.deleteThread(threadKids.id)>
68                </cfloop>
69
70                <cfquery datasource="#variables.dsn#">
71                        delete  from #variables.tableprefix#forums
72                        where   id = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">
73                </cfquery>
74
75                <!--- clean up subscriptions --->
76                <cfquery datasource="#variables.dsn#">
77                        delete  from #variables.tableprefix#subscriptions
78                        where   forumidfk = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">
79                </cfquery>
80               
81        </cffunction>
82       
83        <cffunction name="getForum" access="remote" returnType="struct" output="false"
84                                hint="Returns a struct copy of the forum.">
85                <cfargument name="id" type="uuid" required="true">
86                <cfset var qGetForum = "">
87                               
88                <cfquery name="qGetForum" datasource="#variables.dsn#">
89                        select  id, name, description, readonly, active, conferenceidfk, attachments
90                        from    #variables.tableprefix#forums
91                        where   id = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">
92                </cfquery>
93
94                <!--- Throw if invalid id passed --->
95                <cfif not qGetForum.recordCount>
96                        <cfset variables.utils.throw("ForumCFC","Invalid ID")>
97                </cfif>
98               
99                <!--- Only a ForumsAdmin can get bActiveOnly=false --->
100                <cfif not qGetForum.active and not isUserInRole("forumsadmin")>
101                        <cfset variables.utils.throw("ForumCFC","Invalid call to getForum")>
102                </cfif>
103               
104                <cfreturn variables.utils.queryToStruct(qGetForum)>
105                       
106        </cffunction>
107               
108        <cffunction name="getForums" access="remote" returnType="query" output="false"
109                                hint="Returns a list of forums.">
110
111                <cfargument name="bActiveOnly" type="boolean" required="false" default="true">
112                <cfargument name="conferenceid" type="uuid" required="false">
113               
114                <cfset var qGetForums = "">
115                <cfset var getLastUser = "">
116               
117                <!--- Only a ForumsAdmin can be bActiveOnly=false --->
118                <cfif not arguments.bActiveOnly and not isUserInRole("forumsadmin")>
119                        <cfset variables.utils.throw("ForumCFC","Invalid call to getForums")>
120                </cfif>
121               
122                <cfquery name="qGetForums" datasource="#variables.dsn#">
123                        select  #variables.tableprefix#forums.id, #variables.tableprefix#forums.name, #variables.tableprefix#forums.description, #variables.tableprefix#forums.readonly, #variables.tableprefix#forums.attachments,
124                                        #variables.tableprefix#forums.active, #variables.tableprefix#forums.conferenceidfk, #variables.tableprefix#conferences.name as conference,
125                                        max(#variables.tableprefix#messages.posted) as lastpost, count(#variables.tableprefix#messages.id) as messagecount,
126                                       
127                                <!--- Thanks to Shlomy Gantz --->
128                                (
129                                        select threadidfk from #variables.tableprefix#messages m where  m.posted =
130                               
131                                (SELECT max(mm.posted)
132                                        as lastpost
133                                        from (#variables.tableprefix#forums f left join
134                                    #variables.tableprefix#threads t  ON f.id = t.forumidfk) left JOIN #variables.tableprefix#messages mm ON t.id =
135                                        mm.threadidfk  where f.id = #variables.tableprefix#forums.id
136                                        <cfif isDefined("variables.conferenceid")> and #variables.tableprefix#conferences.id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.conferenceid#" maxlength="35"></cfif>
137                                        )
138                                )                               
139                                as threadidfk
140                                       
141                        from    ((#variables.tableprefix#forums inner join #variables.tableprefix#conferences on #variables.tableprefix#forums.conferenceidfk = #variables.tableprefix#conferences.id)
142                                        left join #variables.tableprefix#threads on #variables.tableprefix#forums.id = #variables.tableprefix#threads.forumidfk)
143                                        left join #variables.tableprefix#messages on #variables.tableprefix#threads.id = #variables.tableprefix#messages.threadidfk
144                        where   1=1
145                        <cfif arguments.bActiveOnly>
146                        and             #variables.tableprefix#forums.active = 1
147                        </cfif>
148                        <cfif isDefined("arguments.conferenceid")>
149                        and             #variables.tableprefix#forums.conferenceidfk = <cfqueryparam value="#arguments.conferenceid#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">
150                        </cfif>
151                        group by #variables.tableprefix#forums.id, #variables.tableprefix#forums.name, #variables.tableprefix#forums.description, #variables.tableprefix#forums.readonly, #variables.tableprefix#forums.attachments,  #variables.tableprefix#forums.active, #variables.tableprefix#forums.conferenceidfk, #variables.tableprefix#conferences.name, #variables.tableprefix#conferences.id
152                        order by #variables.tableprefix#forums.name
153                </cfquery>
154               
155                <!--- My ugly hack to add useridfk. There must be a better way to do this. --->
156                <cfset queryAddColumn(qGetForums, "useridfk", arrayNew(1))>
157                <cfloop query="qGetForums">
158                        <cfif lastpost neq "">
159                                <cfquery name="getLastUser" datasource="#variables.dsn#">
160                                select  useridfk
161                                from    #variables.tableprefix#messages
162                                where   threadidfk = <cfqueryparam cfsqltype="cf_sql_varchar" value="#threadidfk#" maxlength="35">
163                                and             posted = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#lastpost#">
164                                </cfquery>
165                                <cfset querySetCell(qGetForums, "useridfk", getLastUser.useridfk, currentRow)>
166                        </cfif>
167                </cfloop>
168               
169                <cfreturn qGetForums>
170                       
171        </cffunction>
172       
173        <cffunction name="saveForum" access="remote" returnType="void" roles="forumsadmin" output="false"
174                                hint="Saves an existing forum.">
175                               
176                <cfargument name="id" type="uuid" required="true">
177                <cfargument name="forum" type="struct" required="true">
178               
179                <cfif not validForum(arguments.forum)>
180                        <cfset variables.utils.throw("ForumCFC","Invalid data passed to saveForum.")>
181                </cfif>
182               
183                <cfquery datasource="#variables.dsn#">
184                        update  #variables.tableprefix#forums
185                        set             name = <cfqueryparam value="#arguments.forum.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="255">,
186                                        description = <cfqueryparam value="#arguments.forum.description#" cfsqltype="CF_SQL_VARCHAR" maxlength="255">,
187                                        readonly = <cfqueryparam value="#arguments.forum.readonly#" cfsqltype="CF_SQL_BIT">,
188                                        active = <cfqueryparam value="#arguments.forum.active#" cfsqltype="CF_SQL_BIT">,
189                                        attachments = <cfqueryparam value="#arguments.forum.attachments#" cfsqltype="CF_SQL_BIT">,
190                                        conferenceidfk = <cfqueryparam value="#arguments.forum.conferenceidfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">
191                        where   id = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">
192                </cfquery>
193               
194        </cffunction>
195
196        <cffunction name="search" access="remote" returnType="query" output="false"
197                                hint="Allows you to search forums.">
198                <cfargument name="searchterms" type="string" required="true">
199                <cfargument name="searchtype" type="string" required="false" default="phrase" hint="Must be: phrase,any,all">
200               
201                <cfset var results  = "">
202                <cfset var x = "">
203                <cfset var joiner = "">
204                <cfset var aTerms = "">
205
206                <cfset arguments.searchTerms = variables.utils.searchSafe(arguments.searchTerms)>
207       
208                <!--- massage search terms into an array --->           
209                <cfset aTerms = listToArray(arguments.searchTerms," ")>
210               
211               
212                <!--- confirm searchtype is ok --->
213                <cfif not listFindNoCase("phrase,any,all", arguments.searchtype)>
214                        <cfset arguments.searchtype = "phrase">
215                <cfelseif arguments.searchtype is "any">
216                        <cfset joiner = "OR">
217                <cfelseif arguments.searchtype is "all">
218                        <cfset joiner = "AND">
219                </cfif>
220               
221                <cfquery name="results" datasource="#variables.dsn#">
222                        select  id, name, description
223                        from    #variables.tableprefix#forums
224                        where   active = 1
225                        and (
226                                <cfif arguments.searchtype is not "phrase">
227                                        <cfloop index="x" from=1 to="#arrayLen(aTerms)#">
228                                                (name like <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="255" value="%#left(aTerms[x],255)#%">
229                                                 or
230                                                 description like '%#aTerms[x]#%')
231                                                 <cfif x is not arrayLen(aTerms)>#joiner#</cfif>
232                                        </cfloop>
233                                <cfelse>
234                                        name like <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="255" value="%#left(arguments.searchTerms,255)#%">
235                                        or
236                                        description like '%#arguments.searchTerms#%'
237                                </cfif>
238                        )
239                </cfquery>
240               
241                <cfreturn results>
242        </cffunction>
243       
244        <cffunction name="validForum" access="private" returnType="boolean" output="false"
245                                hint="Checks a structure to see if it contains all the proper keys/values for a forum.">
246               
247                <cfargument name="cData" type="struct" required="true">
248                <cfset var rList = "name,description,readonly,active,conferenceidfk">
249                <cfset var x = "">
250               
251                <cfloop index="x" list="#rList#">
252                        <cfif not structKeyExists(cData,x)>
253                                <cfreturn false>
254                        </cfif>
255                </cfloop>
256               
257                <cfreturn true>
258               
259        </cffunction>
260       
261        <cffunction name="setSettings" access="public" output="No" returntype="void">
262                <cfargument name="settings" required="true" hint="Setting">
263                <cfset variables.dsn = arguments.settings.getSettings().dsn>
264                <cfset variables.dbtype = arguments.settings.getSettings().dbtype>
265                <cfset variables.tableprefix = arguments.settings.getSettings().tableprefix>
266        </cffunction>
267       
268        <cffunction name="setUtils" access="public" output="No" returntype="void">
269                <cfargument name="utils" required="true" hint="utils">
270                <cfset variables.utils = arguments.utils />
271        </cffunction>
272
273        <cffunction name="setThread" access="public" output="No" returntype="void">
274                <cfargument name="thread" required="true" hint="thread">
275                <cfset variables.thread = arguments.thread />
276        </cffunction>
277       
278</cfcomponent>
Note: See TracBrowser for help on using the browser.