| 1 | <!----------------------------------------------------------------------- |
|---|
| 2 | ******************************************************************************** |
|---|
| 3 | Copyright 2005-2007 ColdBox Framework by Luis Majano and Ortus Solutions, Corp |
|---|
| 4 | www.coldboxframework.com | www.luismajano.com | www.ortussolutions.com |
|---|
| 5 | ******************************************************************************** |
|---|
| 6 | |
|---|
| 7 | Author : Luis Majano |
|---|
| 8 | Date : June 30, 2006 |
|---|
| 9 | Description : |
|---|
| 10 | I model a coldbox request. I hold the request's variables and more. |
|---|
| 11 | |
|---|
| 12 | Modification History: |
|---|
| 13 | |
|---|
| 14 | -----------------------------------------------------------------------> |
|---|
| 15 | <cfcomponent name="requestContext" |
|---|
| 16 | hint="I am a coldbox request" |
|---|
| 17 | output="false"> |
|---|
| 18 | |
|---|
| 19 | <!------------------------------------------- CONSTRUCTOR -------------------------------------------> |
|---|
| 20 | |
|---|
| 21 | <cfscript> |
|---|
| 22 | variables.context = structnew(); |
|---|
| 23 | variables.defaultLayout = ""; |
|---|
| 24 | variables.ViewLayouts = ""; |
|---|
| 25 | variables.eventName = ""; |
|---|
| 26 | </cfscript> |
|---|
| 27 | |
|---|
| 28 | <cffunction name="init" access="public" output="false" hint="constructor" returntype="any"> |
|---|
| 29 | <!--- ************************************************************* ---> |
|---|
| 30 | <cfargument name="struct1" type="any" required="true" hint="Usually the FORM scope"> |
|---|
| 31 | <cfargument name="struct2" type="any" required="true" hint="Usually the URL scope"> |
|---|
| 32 | <cfargument name="DefaultLayout" type="string" required="true"> |
|---|
| 33 | <cfargument name="ViewLayouts" type="struct" required="true"> |
|---|
| 34 | <cfargument name="EventName" type="string" required="true"/> |
|---|
| 35 | <!--- ************************************************************* ---> |
|---|
| 36 | <cfscript> |
|---|
| 37 | collectionAppend(arguments.struct1); |
|---|
| 38 | collectionAppend(arguments.struct2); |
|---|
| 39 | setDefaultLayout(arguments.DefaultLayout); |
|---|
| 40 | setViewLayouts(arguments.ViewLayouts); |
|---|
| 41 | setEventName(arguments.EventName); |
|---|
| 42 | return this; |
|---|
| 43 | </cfscript> |
|---|
| 44 | </cffunction> |
|---|
| 45 | |
|---|
| 46 | <!------------------------------------------- PUBLIC -------------------------------------------> |
|---|
| 47 | |
|---|
| 48 | <cffunction name="getCollection" returntype="struct" access="Public" hint="I Get a reference or deep copy of the request Collection" output="false"> |
|---|
| 49 | <cfargument name="DeepCopyFlag" hint="Default is false, gives a reference to the collection. True, creates a deep copy of the collection." type="boolean" required="no" default="false"> |
|---|
| 50 | <cfscript> |
|---|
| 51 | if ( arguments.DeepCopyFlag ) |
|---|
| 52 | return duplicate(variables.context); |
|---|
| 53 | else |
|---|
| 54 | return variables.context; |
|---|
| 55 | </cfscript> |
|---|
| 56 | </cffunction> |
|---|
| 57 | |
|---|
| 58 | <!--- ************************************************************* ---> |
|---|
| 59 | |
|---|
| 60 | <cffunction name="setCollection" access="public" returntype="void" output="false" hint="Overwrite the collection with another collection"> |
|---|
| 61 | <cfargument name="collection" type="struct" required="true"> |
|---|
| 62 | <cfset variables.context = arguments.collection> |
|---|
| 63 | </cffunction> |
|---|
| 64 | |
|---|
| 65 | <!--- ************************************************************* ---> |
|---|
| 66 | |
|---|
| 67 | <cffunction name="clearCollection" access="public" returntype="void" output="false" hint="Clear the entire collection"> |
|---|
| 68 | <cfset structClear(variables.context)> |
|---|
| 69 | </cffunction> |
|---|
| 70 | |
|---|
| 71 | <!--- ************************************************************* ---> |
|---|
| 72 | |
|---|
| 73 | <cffunction name="collectionAppend" access="public" returntype="void" output="false" hint="Append a structure to the collection, with overwrite or not. Overwrite = false by default"> |
|---|
| 74 | <cfargument name="collection" type="struct" required="true"> |
|---|
| 75 | <cfargument name="overwrite" type="boolean" required="false" default="false" hint="If you need to override data in the collection, set this to true."> |
|---|
| 76 | <cfset structAppend(variables.context,arguments.collection, arguments.overwrite)> |
|---|
| 77 | </cffunction> |
|---|
| 78 | |
|---|
| 79 | <!--- ************************************************************* ---> |
|---|
| 80 | |
|---|
| 81 | <cffunction name="getSize" access="public" returntype="numeric" output="false" hint="The number of elements in the collection"> |
|---|
| 82 | <cfreturn structCount(variables.context)> |
|---|
| 83 | </cffunction> |
|---|
| 84 | |
|---|
| 85 | <!--- ************************************************************* ---> |
|---|
| 86 | |
|---|
| 87 | <cffunction name="getValue" returntype="Any" access="Public" hint="I Get a value from the request collection." output="false"> |
|---|
| 88 | <cfargument name="name" hint="Name of the variable to get from the request collection" type="string"> |
|---|
| 89 | <cfargument name="defaultValue" |
|---|
| 90 | hint="Default value to return if not found.There are no default values for complex structures. You can send [array][struct][query] and the |
|---|
| 91 | method will return the empty complex variable.Please remember to include the brackets, syntax sensitive.You can also send complex variables |
|---|
| 92 | as the defaultValue argument." |
|---|
| 93 | type="any" required="No" default="NONE"> |
|---|
| 94 | <!--- ************************************************************* ---> |
|---|
| 95 | <cfscript> |
|---|
| 96 | if ( isDefined("variables.context.#arguments.name#") ){ |
|---|
| 97 | return Evaluate("variables.context.#arguments.name#"); |
|---|
| 98 | } |
|---|
| 99 | else if ( isSimpleValue(arguments.defaultValue) and arguments.defaultValue eq "NONE" ) |
|---|
| 100 | throw("The variable: #arguments.name# is undefined in the request collection.","","Framework.ValueNotInRequestCollectionException"); |
|---|
| 101 | else if ( isSimpleValue(arguments.defaultValue) ){ |
|---|
| 102 | if ( refind("\[[A-Za-z]*\]", arguments.defaultValue) ){ |
|---|
| 103 | if ( findnocase("array", arguments.defaultvalue) ) |
|---|
| 104 | return ArrayNew(1); |
|---|
| 105 | else if ( findnocase("struct", arguments.defaultvalue) ) |
|---|
| 106 | return StructNew(); |
|---|
| 107 | else if ( findnocase("query", arguments.defaultvalue) ) |
|---|
| 108 | return QueryNew(""); |
|---|
| 109 | } |
|---|
| 110 | else |
|---|
| 111 | return arguments.defaultValue; |
|---|
| 112 | } |
|---|
| 113 | else |
|---|
| 114 | return arguments.defaultValue; |
|---|
| 115 | </cfscript> |
|---|
| 116 | </cffunction> |
|---|
| 117 | |
|---|
| 118 | <!--- ************************************************************* ---> |
|---|
| 119 | |
|---|
| 120 | <cffunction name="setValue" access="Public" hint="I Set a value in the request collection" output="false" returntype="void"> |
|---|
| 121 | <cfargument name="name" hint="The name of the variable to set." type="string" > |
|---|
| 122 | <cfargument name="value" hint="The value of the variable to set" type="Any" > |
|---|
| 123 | <!--- ************************************************************* ---> |
|---|
| 124 | <cfscript> |
|---|
| 125 | "variables.context.#arguments.name#" = arguments.value; |
|---|
| 126 | </cfscript> |
|---|
| 127 | </cffunction> |
|---|
| 128 | |
|---|
| 129 | <!--- ************************************************************* ---> |
|---|
| 130 | |
|---|
| 131 | <cffunction name="removeValue" access="Public" hint="I remove a value in the request collection" output="false" returntype="void"> |
|---|
| 132 | <cfargument name="name" hint="The name of the variable to remove." type="string" > |
|---|
| 133 | <!--- ************************************************************* ---> |
|---|
| 134 | <cfscript> |
|---|
| 135 | structDelete(variables.context,"#arguments.name#"); |
|---|
| 136 | </cfscript> |
|---|
| 137 | </cffunction> |
|---|
| 138 | |
|---|
| 139 | <!--- ************************************************************* ---> |
|---|
| 140 | |
|---|
| 141 | <cffunction name="valueExists" returntype="boolean" access="Public" hint="I Check if a value exists in the request collection." output="false"> |
|---|
| 142 | <cfargument name="name" hint="Name of the variable to find in the request collection" type="string"> |
|---|
| 143 | <!--- ************************************************************* ---> |
|---|
| 144 | <cfscript> |
|---|
| 145 | return isDefined("variables.context.#arguments.name#"); |
|---|
| 146 | </cfscript> |
|---|
| 147 | </cffunction> |
|---|
| 148 | |
|---|
| 149 | <!--- ************************************************************* ---> |
|---|
| 150 | |
|---|
| 151 | <cffunction name="paramValue" returntype="void" access="Public" hint="Just like cfparam, but for the request collection" output="false"> |
|---|
| 152 | <cfargument name="name" hint="Name of the variable to param in the request collection" type="string"> |
|---|
| 153 | <cfargument name="value" hint="The value of the variable to set if not found." type="Any" > |
|---|
| 154 | <!--- ************************************************************* ---> |
|---|
| 155 | <cfscript> |
|---|
| 156 | if ( not valueExists(arguments.name) ) |
|---|
| 157 | setValue(arguments.name, arguments.value); |
|---|
| 158 | </cfscript> |
|---|
| 159 | </cffunction> |
|---|
| 160 | |
|---|
| 161 | <!--- ************************************************************* ---> |
|---|
| 162 | |
|---|
| 163 | <!------------------------------------------- GET/SET CURRENT REQUEST VARIABLES -------------------------------------------> |
|---|
| 164 | |
|---|
| 165 | <!--- ************************************************************* ---> |
|---|
| 166 | |
|---|
| 167 | <cffunction name="getCurrentView" access="public" hint="Gets the current set view" returntype="string" output="false"> |
|---|
| 168 | <cfreturn getValue("currentView","")> |
|---|
| 169 | </cffunction> |
|---|
| 170 | |
|---|
| 171 | <!--- ************************************************************* ---> |
|---|
| 172 | |
|---|
| 173 | <cffunction name="setView" access="public" returntype="void" hint="I Set the view to render in this request.I am called from event handlers. Request Collection Name: currentView, currentLayout" output="false"> |
|---|
| 174 | <cfargument name="name" hint="The name of the view to set. If a layout has been defined it will assign it, else if will assign the default layout." type="string"> |
|---|
| 175 | <cfargument name="nolayout" type="boolean" required="false" default="false" hint="Boolean flag, wether the view sent in will be using a layout or not. Default is false. Uses a pre set layout or the default layout."> |
|---|
| 176 | <!--- ************************************************************* ---> |
|---|
| 177 | <cfscript> |
|---|
| 178 | if ( not arguments.nolayout ){ |
|---|
| 179 | if ( not getValue("layoutoverride",false) ){ |
|---|
| 180 | if ( StructKeyExists(variables.ViewLayouts, arguments.name) ) |
|---|
| 181 | setValue("currentLayout",variables.ViewLayouts[arguments.name]); |
|---|
| 182 | else |
|---|
| 183 | setValue("currentLayout", variables.DefaultLayout); |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | setValue("currentView",arguments.name); |
|---|
| 187 | </cfscript> |
|---|
| 188 | </cffunction> |
|---|
| 189 | |
|---|
| 190 | <!--- ************************************************************* ---> |
|---|
| 191 | |
|---|
| 192 | <cffunction name="getCurrentLayout" access="public" hint="Gets the current set layout" returntype="string" output="false"> |
|---|
| 193 | <cfreturn getValue("currentLayout","")> |
|---|
| 194 | </cffunction> |
|---|
| 195 | |
|---|
| 196 | <cffunction name="setLayout" access="public" returntype="void" hint="I Set the layout to override and render. Layouts are pre-defined in the config.xml file. However I can override these settings if needed. Do not append a the cfm extension. Request Collection name: currentLayout" output="false"> |
|---|
| 197 | <cfargument name="name" hint="The name of the layout file to set." type="string" > |
|---|
| 198 | <!--- ************************************************************* ---> |
|---|
| 199 | <cfscript> |
|---|
| 200 | setValue("currentLayout",trim(arguments.name) & ".cfm" ); |
|---|
| 201 | setValue("layoutoverride",true); |
|---|
| 202 | </cfscript> |
|---|
| 203 | </cffunction> |
|---|
| 204 | |
|---|
| 205 | <!--- ************************************************************* ---> |
|---|
| 206 | |
|---|
| 207 | <cffunction name="getCurrentEvent" access="public" hint="Gets the current set event" returntype="string" output="false"> |
|---|
| 208 | <cfreturn getValue(getEventName(),"")> |
|---|
| 209 | </cffunction> |
|---|
| 210 | |
|---|
| 211 | <!--- ************************************************************* ---> |
|---|
| 212 | |
|---|
| 213 | <cffunction name="overrideEvent" access="Public" hint="I Override the current event in the request collection. This method does not execute the event, it just replaces the event to be executed by the framework's RunEvent() method. This method is usually called from an onRequestStart or onApplicationStart method." output="false" returntype="void"> |
|---|
| 214 | <cfargument name="event" hint="The name of the event to override." type="string"> |
|---|
| 215 | <!--- ************************************************************* ---> |
|---|
| 216 | <cfscript> |
|---|
| 217 | setValue(getEventName(),arguments.event); |
|---|
| 218 | </cfscript> |
|---|
| 219 | </cffunction> |
|---|
| 220 | |
|---|
| 221 | <!--- ************************************************************* ---> |
|---|
| 222 | |
|---|
| 223 | <cffunction name="showdebugpanel" access="public" returntype="void" hint="I can override to show or not the debug panel. Very useful in AJAX debugging"> |
|---|
| 224 | <cfargument name="show" type="boolean" required="true"> |
|---|
| 225 | <cfset setValue("coldbox_debugpanel",arguments.show)> |
|---|
| 226 | </cffunction> |
|---|
| 227 | |
|---|
| 228 | <!--- ************************************************************* ---> |
|---|
| 229 | |
|---|
| 230 | <cffunction name="getdebugpanelFlag" access="public" returntype="boolean" hint="I return the debugpanel flag for this request."> |
|---|
| 231 | <cfreturn getValue("coldbox_debugpanel",true)> |
|---|
| 232 | </cffunction> |
|---|
| 233 | |
|---|
| 234 | <!------------------------------------------- ACCESSORS/MUTATORS -------------------------------------------> |
|---|
| 235 | |
|---|
| 236 | <!--- ************************************************************* ---> |
|---|
| 237 | |
|---|
| 238 | <cffunction name="getDefaultLayout" access="public" returntype="string" output="false"> |
|---|
| 239 | <cfreturn variables.DefaultLayout> |
|---|
| 240 | </cffunction> |
|---|
| 241 | |
|---|
| 242 | <!--- ************************************************************* ---> |
|---|
| 243 | |
|---|
| 244 | <cffunction name="setDefaultLayout" access="public" returntype="void" output="false"> |
|---|
| 245 | <cfargument name="DefaultLayout" type="string" required="true"> |
|---|
| 246 | <cfset variables.DefaultLayout = arguments.DefaultLayout> |
|---|
| 247 | </cffunction> |
|---|
| 248 | |
|---|
| 249 | <!--- ************************************************************* ---> |
|---|
| 250 | |
|---|
| 251 | <cffunction name="getViewLayouts" access="public" returntype="struct" output="false"> |
|---|
| 252 | <cfreturn variables.ViewLayouts> |
|---|
| 253 | </cffunction> |
|---|
| 254 | |
|---|
| 255 | <!--- ************************************************************* ---> |
|---|
| 256 | |
|---|
| 257 | <cffunction name="setViewLayouts" access="public" returntype="void" output="false"> |
|---|
| 258 | <cfargument name="ViewLayouts" type="struct" required="true"> |
|---|
| 259 | <cfset variables.ViewLayouts = arguments.ViewLayouts> |
|---|
| 260 | </cffunction> |
|---|
| 261 | |
|---|
| 262 | <!--- ************************************************************* ---> |
|---|
| 263 | |
|---|
| 264 | <cffunction name="getEventName" access="public" returntype="string" output="false"> |
|---|
| 265 | <cfreturn variables.EventName> |
|---|
| 266 | </cffunction> |
|---|
| 267 | |
|---|
| 268 | <!--- ************************************************************* ---> |
|---|
| 269 | |
|---|
| 270 | <cffunction name="setEventName" access="public" returntype="void" output="false"> |
|---|
| 271 | <cfargument name="EventName" type="string" required="true"> |
|---|
| 272 | <cfset variables.EventName = arguments.EventName> |
|---|
| 273 | </cffunction> |
|---|
| 274 | |
|---|
| 275 | <!------------------------------------------- PRIVATE -------------------------------------------> |
|---|
| 276 | |
|---|
| 277 | <cffunction name="throw" access="private" hint="Facade for cfthrow" output="false"> |
|---|
| 278 | <!--- ************************************************************* ---> |
|---|
| 279 | <cfargument name="message" type="string" required="yes"> |
|---|
| 280 | <cfargument name="detail" type="string" required="no" default=""> |
|---|
| 281 | <cfargument name="type" type="string" required="no" default="Framework"> |
|---|
| 282 | <!--- ************************************************************* ---> |
|---|
| 283 | <cfthrow type="#arguments.type#" message="#arguments.message#" detail="#arguments.detail#"> |
|---|
| 284 | </cffunction> |
|---|
| 285 | |
|---|
| 286 | </cfcomponent> |
|---|