root/coldbox/trunk/src/system/beans/requestContext.cfc @ 823

Revision 823, 14.3 kB (checked in by lmajano, 6 years ago)

Almost there.

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