| 1 | <!--- |
|---|
| 2 | Name : thread.cfc |
|---|
| 3 | Author : Raymond Camden |
|---|
| 4 | Created : January 26, 2005 |
|---|
| 5 | Last Updated : May 1, 2007 |
|---|
| 6 | History : Support for dbtype, and uuid (rkc 1/26/05) |
|---|
| 7 | New init, tableprefix (rkc 8/27/05) |
|---|
| 8 | Sticky (rkc 8/29/05) |
|---|
| 9 | Access dbs did stickies wrong, return conf on getall (rkc 9/9/05) |
|---|
| 10 | Remove from subscriptions (rkc 11/22/05) |
|---|
| 11 | limit search length (rkc 10/30/05) |
|---|
| 12 | show last user (rkc 7/12/06) |
|---|
| 13 | Simple size change (rkc 7/27/06) |
|---|
| 14 | change to isTheUserInAnyRole (rkc 5/1/07) |
|---|
| 15 | Purpose : |
|---|
| 16 | ---> |
|---|
| 17 | <cfcomponent displayName="Thread" hint="Handles Threads which contain a collection of message."> |
|---|
| 18 | |
|---|
| 19 | <cfset variables.dsn = ""> |
|---|
| 20 | <cfset variables.dbtype = ""> |
|---|
| 21 | <cfset variables.tableprefix = ""> |
|---|
| 22 | <cfset variables.settings = ""> |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | <cffunction name="init" access="public" returnType="thread" output="false" |
|---|
| 26 | hint="Returns an instance of the CFC initialized with the correct DSN."> |
|---|
| 27 | <cfreturn this> |
|---|
| 28 | |
|---|
| 29 | </cffunction> |
|---|
| 30 | |
|---|
| 31 | <cffunction name="addThread" access="remote" returnType="uuid" output="false" |
|---|
| 32 | hint="Adds a thread."> |
|---|
| 33 | |
|---|
| 34 | <cfargument name="thread" type="struct" required="true"> |
|---|
| 35 | <cfset var newthread = ""> |
|---|
| 36 | <cfset var newid = createUUID()> |
|---|
| 37 | |
|---|
| 38 | <!--- First see if we can add a thread. Because roles= doesn't allow for OR, we use a UDF ---> |
|---|
| 39 | <cfif not variables.utils.isTheUserInAnyRole("forumsadmin,forumsmoderator,forumsmember")> |
|---|
| 40 | <cfset variables.utils.throw("ThreadCFC","Unauthorized execution of addThread.")> |
|---|
| 41 | </cfif> |
|---|
| 42 | |
|---|
| 43 | <cfif not validThread(arguments.thread)> |
|---|
| 44 | <cfset variables.utils.throw("ThreadCFC","Invalid data passed to addThread.")> |
|---|
| 45 | </cfif> |
|---|
| 46 | |
|---|
| 47 | <cfquery name="newthread" datasource="#variables.dsn#"> |
|---|
| 48 | insert into #variables.tableprefix#threads(id,name,readonly,active,forumidfk,useridfk,datecreated,sticky) |
|---|
| 49 | values(<cfqueryparam value="#newid#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">, |
|---|
| 50 | <cfqueryparam value="#arguments.thread.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="255">, |
|---|
| 51 | <cfqueryparam value="#arguments.thread.readonly#" cfsqltype="CF_SQL_BIT">, |
|---|
| 52 | <cfqueryparam value="#arguments.thread.active#" cfsqltype="CF_SQL_BIT">, |
|---|
| 53 | <cfqueryparam value="#arguments.thread.forumidfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">, |
|---|
| 54 | <cfqueryparam value="#arguments.thread.useridfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">, |
|---|
| 55 | <cfqueryparam value="#arguments.thread.datecreated#" cfsqltype="CF_SQL_TIMESTAMP">, |
|---|
| 56 | <cfqueryparam value="#arguments.thread.sticky#" cfsqltype="CF_SQL_BIT"> |
|---|
| 57 | ) |
|---|
| 58 | </cfquery> |
|---|
| 59 | |
|---|
| 60 | <cfreturn newid> |
|---|
| 61 | </cffunction> |
|---|
| 62 | |
|---|
| 63 | <cffunction name="deleteThread" access="public" returnType="void" roles="forumsadmin" output="false" |
|---|
| 64 | hint="Deletes a thread along with all of it's children."> |
|---|
| 65 | |
|---|
| 66 | <cfargument name="id" type="uuid" required="true"> |
|---|
| 67 | |
|---|
| 68 | <!--- delete kids ---> |
|---|
| 69 | <cfset var msgKids = variables.message.getMessages(arguments.id)> |
|---|
| 70 | |
|---|
| 71 | <cfloop query="msgKids"> |
|---|
| 72 | <cfset variables.message.deleteMessage(msgKids.id)> |
|---|
| 73 | </cfloop> |
|---|
| 74 | |
|---|
| 75 | <cfquery datasource="#variables.dsn#"> |
|---|
| 76 | delete from #variables.tableprefix#threads |
|---|
| 77 | where id = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 78 | </cfquery> |
|---|
| 79 | |
|---|
| 80 | <!--- clean up subscriptions ---> |
|---|
| 81 | <cfquery datasource="#variables.dsn#"> |
|---|
| 82 | delete from #variables.tableprefix#subscriptions |
|---|
| 83 | where threadidfk = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 84 | </cfquery> |
|---|
| 85 | |
|---|
| 86 | </cffunction> |
|---|
| 87 | |
|---|
| 88 | <cffunction name="getThread" access="remote" returnType="struct" output="false" |
|---|
| 89 | hint="Returns a struct copy of the thread."> |
|---|
| 90 | <cfargument name="id" type="uuid" required="true"> |
|---|
| 91 | <cfset var qGetThread = ""> |
|---|
| 92 | |
|---|
| 93 | <cfquery name="qGetThread" datasource="#variables.dsn#"> |
|---|
| 94 | select id, name, readonly, active, forumidfk, useridfk, datecreated, sticky |
|---|
| 95 | from #variables.tableprefix#threads |
|---|
| 96 | where id = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 97 | </cfquery> |
|---|
| 98 | |
|---|
| 99 | <!--- Throw if invalid id passed ---> |
|---|
| 100 | <cfif not qGetThread.recordCount> |
|---|
| 101 | <cfset variables.utils.throw("ThreadCFC","Invalid ID")> |
|---|
| 102 | </cfif> |
|---|
| 103 | |
|---|
| 104 | <!--- Only a ForumsAdmin can get bActiveOnly=false ---> |
|---|
| 105 | <cfif not qGetThread.active and not isUserInRole("forumsadmin")> |
|---|
| 106 | <cfset variables.utils.throw("ThreadCFC","Invalid call to getThread")> |
|---|
| 107 | </cfif> |
|---|
| 108 | |
|---|
| 109 | <cfreturn variables.utils.queryToStruct(qGetThread)> |
|---|
| 110 | |
|---|
| 111 | </cffunction> |
|---|
| 112 | |
|---|
| 113 | <cffunction name="getThreads" access="remote" returnType="query" output="false" |
|---|
| 114 | hint="Returns a list of threads."> |
|---|
| 115 | |
|---|
| 116 | <cfargument name="bActiveOnly" type="boolean" required="false" default="true"> |
|---|
| 117 | <cfargument name="forumid" type="uuid" required="false"> |
|---|
| 118 | |
|---|
| 119 | <cfset var qGetThreads = ""> |
|---|
| 120 | <cfset var getLastUser = ""> |
|---|
| 121 | |
|---|
| 122 | <!--- Only a ForumsAdmin can be bActiveOnly=false ---> |
|---|
| 123 | <cfif not arguments.bActiveOnly and not isUserInRole("forumsadmin")> |
|---|
| 124 | <cfset variables.utils.throw("ThreadCFC","Invalid call to getThreads")> |
|---|
| 125 | </cfif> |
|---|
| 126 | |
|---|
| 127 | <cfquery name="qGetThreads" datasource="#variables.dsn#"> |
|---|
| 128 | select #variables.tableprefix#threads.id, #variables.tableprefix#threads.name, #variables.tableprefix#threads.readonly, |
|---|
| 129 | #variables.tableprefix#threads.active, #variables.tableprefix#threads.forumidfk, #variables.tableprefix#threads.useridfk, |
|---|
| 130 | #variables.tableprefix#threads.datecreated, #variables.tableprefix#forums.name as forum, #variables.tableprefix#users.username, |
|---|
| 131 | max(#variables.tableprefix#messages.posted) as lastpost, count(#variables.tableprefix#messages.id) as messagecount, #variables.tableprefix#threads.sticky, |
|---|
| 132 | #variables.tableprefix#conferences.name as conference |
|---|
| 133 | from (((#variables.tableprefix#threads left join #variables.tableprefix#messages on #variables.tableprefix#threads.id = #variables.tableprefix#messages.threadidfk) |
|---|
| 134 | inner join #variables.tableprefix#forums on #variables.tableprefix#threads.forumidfk = #variables.tableprefix#forums.id) |
|---|
| 135 | inner join #variables.tableprefix#conferences on #variables.tableprefix#forums.conferenceidfk = #variables.tableprefix#conferences.id) |
|---|
| 136 | |
|---|
| 137 | inner join #variables.tableprefix#users on #variables.tableprefix#threads.useridfk = #variables.tableprefix#users.id |
|---|
| 138 | |
|---|
| 139 | where 1=1 |
|---|
| 140 | <cfif arguments.bActiveOnly> |
|---|
| 141 | and #variables.tableprefix#threads.active = 1 |
|---|
| 142 | </cfif> |
|---|
| 143 | <cfif isDefined("arguments.forumid")> |
|---|
| 144 | and #variables.tableprefix#threads.forumidfk = <cfqueryparam value="#arguments.forumid#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 145 | </cfif> |
|---|
| 146 | |
|---|
| 147 | group by #variables.tableprefix#threads.id, #variables.tableprefix#threads.name, #variables.tableprefix#threads.readonly, #variables.tableprefix#threads.active, |
|---|
| 148 | #variables.tableprefix#threads.forumidfk, #variables.tableprefix#threads.useridfk, #variables.tableprefix#threads.datecreated, #variables.tableprefix#forums.name, #variables.tableprefix#users.username, #variables.tableprefix#threads.sticky, #variables.tableprefix#conferences.name |
|---|
| 149 | |
|---|
| 150 | order by #variables.tableprefix#threads.sticky <cfif variables.dbtype is not "msaccess">desc<cfelse>asc</cfif>, |
|---|
| 151 | <cfif variables.dbtype is not "mysql"> |
|---|
| 152 | max(#variables.tableprefix#messages.posted) desc |
|---|
| 153 | <cfelse> |
|---|
| 154 | lastpost desc |
|---|
| 155 | </cfif> |
|---|
| 156 | </cfquery> |
|---|
| 157 | |
|---|
| 158 | <!--- My ugly hack to add useridfk. There must be a better way to do this. ---> |
|---|
| 159 | <cfset queryAddColumn(qGetThreads, "lastuseridfk", arrayNew(1))> |
|---|
| 160 | <cfloop query="qGetThreads"> |
|---|
| 161 | <cfif len(lastpost)> |
|---|
| 162 | <cfquery name="getLastUser" datasource="#variables.dsn#"> |
|---|
| 163 | select useridfk |
|---|
| 164 | from #variables.tableprefix#messages |
|---|
| 165 | where threadidfk = <cfqueryparam cfsqltype="cf_sql_varchar" value="#id#" maxlength="35"> |
|---|
| 166 | and posted = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#lastpost#"> |
|---|
| 167 | </cfquery> |
|---|
| 168 | <cfset querySetCell(qGetThreads, "lastuseridfk", getLastUser.useridfk, currentRow)> |
|---|
| 169 | </cfif> |
|---|
| 170 | </cfloop> |
|---|
| 171 | |
|---|
| 172 | <cfreturn qGetThreads> |
|---|
| 173 | |
|---|
| 174 | </cffunction> |
|---|
| 175 | |
|---|
| 176 | <cffunction name="saveThread" access="remote" returnType="void" roles="forumsadmin" output="false" |
|---|
| 177 | hint="Saves an existing thread."> |
|---|
| 178 | |
|---|
| 179 | <cfargument name="id" type="uuid" required="true"> |
|---|
| 180 | <cfargument name="thread" type="struct" required="true"> |
|---|
| 181 | |
|---|
| 182 | <cfif not validThread(arguments.thread)> |
|---|
| 183 | <cfset variables.utils.throw("ThreadCFC","Invalid data passed to saveThread.")> |
|---|
| 184 | </cfif> |
|---|
| 185 | |
|---|
| 186 | <cfquery datasource="#variables.dsn#"> |
|---|
| 187 | update #variables.tableprefix#threads |
|---|
| 188 | set name = <cfqueryparam value="#arguments.thread.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="255">, |
|---|
| 189 | readonly = <cfqueryparam value="#arguments.thread.readonly#" cfsqltype="CF_SQL_BIT">, |
|---|
| 190 | active = <cfqueryparam value="#arguments.thread.active#" cfsqltype="CF_SQL_BIT">, |
|---|
| 191 | forumidfk = <cfqueryparam value="#arguments.thread.forumidfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">, |
|---|
| 192 | useridfk = <cfqueryparam value="#arguments.thread.useridfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">, |
|---|
| 193 | datecreated = <cfqueryparam value="#arguments.thread.datecreated#" cfsqltype="CF_SQL_TIMESTAMP">, |
|---|
| 194 | sticky = <cfqueryparam value="#arguments.thread.sticky#" cfsqltype="CF_SQL_BIT"> |
|---|
| 195 | where id = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 196 | </cfquery> |
|---|
| 197 | |
|---|
| 198 | </cffunction> |
|---|
| 199 | |
|---|
| 200 | <cffunction name="search" access="remote" returnType="query" output="false" |
|---|
| 201 | hint="Allows you to search threads."> |
|---|
| 202 | <cfargument name="searchterms" type="string" required="true"> |
|---|
| 203 | <cfargument name="searchtype" type="string" required="false" default="phrase" hint="Must be: phrase,any,all"> |
|---|
| 204 | |
|---|
| 205 | <cfset var results = ""> |
|---|
| 206 | <cfset var x = ""> |
|---|
| 207 | <cfset var joiner = ""> |
|---|
| 208 | <cfset var aTerms = ""> |
|---|
| 209 | |
|---|
| 210 | <cfset arguments.searchTerms = variables.utils.searchSafe(arguments.searchTerms)> |
|---|
| 211 | |
|---|
| 212 | <!--- massage search terms into an array ---> |
|---|
| 213 | <cfset aTerms = listToArray(arguments.searchTerms," ")> |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | <!--- confirm searchtype is ok ---> |
|---|
| 217 | <cfif not listFindNoCase("phrase,any,all", arguments.searchtype)> |
|---|
| 218 | <cfset arguments.searchtype = "phrase"> |
|---|
| 219 | <cfelseif arguments.searchtype is "any"> |
|---|
| 220 | <cfset joiner = "OR"> |
|---|
| 221 | <cfelseif arguments.searchtype is "all"> |
|---|
| 222 | <cfset joiner = "AND"> |
|---|
| 223 | </cfif> |
|---|
| 224 | |
|---|
| 225 | <cfquery name="results" datasource="#variables.dsn#"> |
|---|
| 226 | select id, name |
|---|
| 227 | from #variables.tableprefix#threads |
|---|
| 228 | where active = 1 |
|---|
| 229 | and ( |
|---|
| 230 | <cfif arguments.searchtype is not "phrase"> |
|---|
| 231 | <cfloop index="x" from=1 to="#arrayLen(aTerms)#"> |
|---|
| 232 | (name like <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="255" value="%#left(aTerms[x],255)#%">) |
|---|
| 233 | <cfif x is not arrayLen(aTerms)>#joiner#</cfif> |
|---|
| 234 | </cfloop> |
|---|
| 235 | <cfelse> |
|---|
| 236 | name like <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="255" value="%#left(arguments.searchTerms,255)#%"> |
|---|
| 237 | </cfif> |
|---|
| 238 | ) |
|---|
| 239 | </cfquery> |
|---|
| 240 | |
|---|
| 241 | <cfreturn results> |
|---|
| 242 | </cffunction> |
|---|
| 243 | |
|---|
| 244 | <cffunction name="validThread" access="private" returnType="boolean" output="false" |
|---|
| 245 | hint="Checks a structure to see if it contains all the proper keys/values for a thread."> |
|---|
| 246 | |
|---|
| 247 | <cfargument name="cData" type="struct" required="true"> |
|---|
| 248 | <cfset var rList = "name,readonly,active,forumidfk,useridfk,datecreated,sticky"> |
|---|
| 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 | <!--- keep a global copy to pass later on ---> |
|---|
| 267 | <cfset variables.settings = arguments.settings.getSettings() /> |
|---|
| 268 | </cffunction> |
|---|
| 269 | |
|---|
| 270 | <cffunction name="setUtils" access="public" output="No" returntype="void"> |
|---|
| 271 | <cfargument name="utils" required="true" hint="utils"> |
|---|
| 272 | <cfset variables.utils = arguments.utils /> |
|---|
| 273 | </cffunction> |
|---|
| 274 | |
|---|
| 275 | <cffunction name="setMessage" access="public" output="No" returntype="void"> |
|---|
| 276 | <cfargument name="message" required="true" hint="utils"> |
|---|
| 277 | <cfset variables.message = arguments.message /> |
|---|
| 278 | </cffunction> |
|---|
| 279 | |
|---|
| 280 | </cfcomponent> |
|---|