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

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

Ticket #213
updates to use coldbox.xml.cfm and config.xml.cfm

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