Ticket #288: EventContext.cfc

File EventContext.cfc, 10.9 kB (added by ArmchairDeity, 19 years ago)

Fixes #288

Line 
1<cfcomponent displayName="EventContext" output="false" hint="I am the Event that is manipulated inside of a Controller's listener functions." extends="ModelGlue.Core.Event">
2  <cffunction name="Init" access="public" returnType="any" output="false" hint="I build a new Event.">
3                <cfargument name="message" />
4    <cfargument name="eventRequest" />
5    <cfargument name="eventHandler" />
6    <cfargument name="framework" />
7    <cfargument name="beanMaker" />
8
9    <cfset variables._eventRequest = arguments.eventRequest />
10    <!--- <cfset variables._event = arguments.eventHandler /> --->
11    <cfset variables._possibleResults = arguments.eventHandler.getResultMappings() />
12    <cfset variables._state = arguments.eventRequest.getStateContainer() />
13    <cfset variables._arguments = arguments.message.getArguments() />
14    <cfset variables._results = arrayNew(1) />
15    <cfset variables._beanMaker = arguments.beanMaker />
16                <cfset variables._message = arguments.message />
17                <cfset variables._framework = arguments.framework />
18
19    <cfreturn this />
20  </cffunction>
21
22  <cffunction name="GetModelglue" returntype="ModelGlue.unity.framework.ModelGlue" access="public" output="false">
23        <cfreturn variables._framework />
24  </cffunction>
25
26  <cffunction name="GetMessage" access="public" output="false">
27        <cfreturn variables._message />
28  </cffunction>
29
30  <cffunction name="GetEventRequest" access="public" output="false">
31                <cfreturn variables._eventRequest />
32  </cffunction>
33
34  <cffunction name="AddResult" access="public" returnType="void" output="false" hint="I add a result to the event.">
35    <cfargument name="name" type="string" required="true" hint="Name of the result to add.">
36
37                <cfset var mappings = "" />
38                <cfset var i = "" />
39
40                <cfif structKeyExists(variables._possibleResults, arguments.name)>
41                        <cfset mappings = variables._possibleResults[arguments.name] />
42                        <cfloop from="1" to="#arrayLen(mappings)#" index="i">
43                                <cfif mappings[i].redirect>
44                                        <cfset ForwardResult(mappings[i].event, mappings[i].append, mappings[i].anchor, mappings[i].preserveState) />
45                                </cfif>
46                        </cfloop>
47                  <cfset arrayAppend(variables._results, arguments.name) />
48                </cfif>
49  </cffunction>
50
51  <cffunction name="GetResults" access="public" returnType="array" output="false" hint="I return the events requested for addition to the queue.">
52    <cfreturn variables._results />
53  </cffunction>
54
55  <cffunction name="SetValue" access="public" returnType="void" output="false" hint="I set a value in the view state.">
56    <cfargument name="name" type="string" required="true" hint="I am the name of the value.">
57    <cfargument name="value" type="any" required="true" hint="I am the value.">
58    <cfset variables._state.setValue(argumentCollection=arguments) />
59  </cffunction>
60<!---
61  <cffunction name="GetValue" access="public" returnType="any" output="false" hint="I get a value from view state.">
62    <cfargument name="name" type="string" required="true" hint="I am the name of the value." />
63    <cfargument name="default" required="false" />
64        <cfset var retVal = "">
65        <cfinvoke component="#variables._state#" method="getValue" returnvariable="retVal">
66                <cfinvokeargument name="name" value="#arguments.name#" />
67                <cfif structKeyExists(arguments,"default")>
68                        <cfinvokeargument name="default" value="#arguments.default#" />
69                </cfif>
70        </cfinvoke>
71    <cfreturn retVal />
72  </cffunction>
73 --->
74
75  <cffunction name="GetValue" access="public" returnType="any" output="false" hint="I get a value from view state.">
76    <cfargument name="name" type="string" required="true" hint="I am the name of the value." />
77    <cfargument name="default" required="false" />
78    <cfreturn variables._state.getValue(argumentCollection=arguments) />
79  </cffunction>
80
81  <cffunction name="GetAllValues" access="public" returnType="struct" output="false" hint="I return the entire view's current state.">
82    <cfreturn variables._state.getAll() />
83  </cffunction>
84
85  <cffunction name="ValueExists" access="public" returnType="boolean" output="false" hint="I state if a value exists in the view's current state.">
86    <cfargument name="name" type="string" required="true" hint="I am the name of the value.">
87    <cfreturn variables._state.exists(arguments.name) />
88  </cffunction>
89
90  <cffunction name="RemoveValue" access="public" returnType="void" output="false" hint="I remove a value from the view's state.">
91    <cfargument name="name" type="string" required="true" hint="I am the name of the value to remove.">
92    <cfset variables._state.removeValue(arguments.name) />
93  </cffunction>
94
95  <cffunction name="GetArgument" access="public" returnType="string" output="false" hint="I get a value from the message's arguments (ARGUMENT tag inside a MESSAGE tag).">
96    <cfargument name="name" type="string" required="true" hint="I am the name of the argument to get.">
97    <cfargument name="defaultValue" type="string" required="false" />
98
99    <cfif not structKeyExists(variables._arguments, arguments.name) and structKeyExists(arguments, "defaultValue")>
100        <cfreturn arguments.defaultValue />
101    <cfelseif not structKeyExists(variables._arguments, arguments.name)>
102        <cfreturn "" />
103    </cfif>
104
105    <cfreturn variables._arguments[arguments.name] />
106  </cffunction>
107
108  <cffunction name="GetAllArguments" access="public" returnType="struct" output="false" hint="I get all of the arguments for the message (by value).">
109    <cfreturn duplicate(variables._arguments) />
110  </cffunction>
111
112  <cffunction name="ArgumentExists" access="public" returnType="boolean" output="false" hint="I state if a given argument exists.">
113    <cfargument name="name" type="string" required="true" hint="I am the name of the argument.">
114    <cfreturn structKeyExists(variables._arguments, arguments.name) />
115  </cffunction>
116
117  <cffunction name="Trace" access="public" returnType="Void" output="false" hint="I add a message to the trace log.">
118    <cfargument name="name" type="string" required="true" />
119    <cfargument name="value" type="any" required="true" />
120    <cfargument name="tag" type="string" required="false" default="" />
121                <cfargument name="type" type="string" required="false" default="USER" />
122                <cfset var message = "" />
123
124                <cfif isSimpleValue(arguments.value)>
125                        <cfset message = arguments.value>
126                <cfelse>
127                        <cfsavecontent variable="message"><cfdump var="#arguments.value#"></cfsavecontent>
128                </cfif>
129
130                <cfset variables._eventRequest.trace(arguments.name, message, arguments.tag, arguments.type) />
131  </cffunction>
132
133  <cffunction name="GetTrace" access="private" returnType="array" output="false" hint="I return the tracelog.">
134    <cfreturn variables._eventRequest.getTrace() />
135  </cffunction>
136
137        <cffunction name="MakeEventBean" access="public" returnType="any" output="false" hint="I make a CFC and populate it (via like-named ""setters"") from the event values">
138                <cfargument name="type" type="any" required="true" hint="I am the CFC type to create or an instance of a CFC to populate." />
139                <cfargument name="fields" type="string" required="false" hint="I am the [optional] list of fields to populate." />
140
141                <cfif structKeyExists(arguments, "fields")>
142                        <cfreturn variables._beanMaker.MakeBean(variables._state, arguments.type, arguments.fields) />
143                <cfelse>
144                        <cfreturn variables._beanMaker.MakeBean(variables._state, arguments.type) />
145                </cfif>
146        </cffunction>
147
148        <!--- REQUEST FORWARDING --->
149        <cffunction name="Forward" access="public" output="false" hint="I forward the request to a new event handler using a CFLocation, maintaining all state.">
150                <cfargument name="event" type="string" required="true" hint="The event to forward to.">
151                <cfargument name="append" type="string" required="false" hint="A list of state values to append to the URL.">
152                <cfargument name="stateful" type="boolean" required="false" default="true" hint="A list of state values to append to the URL.">
153                <cfargument name="anchor" type="string" required="false" hint="An anchor to append to the URL." default="">
154
155                <cfparam name="arguments.append" default="" />
156                <cfparam name="arguments.stateful" default="true" />
157                <cfparam name="arguments.anchor" default="" />
158
159                <cfset ForwardResult(arguments.event, arguments.append, arguments.anchor, arguments.stateful) />
160        </cffunction>
161
162        <cffunction name="ForwardResult" access="private" output="false" hint="I relocate the user to another event after storing a state container in session, to be picked up later.">
163                <cfargument name="event" type="string" required="true" hint="The event to forward to.">
164                <cfargument name="append" type="string" required="true" hint="A list of state values to append to the URL.">
165                <cfargument name="anchor" type="string" required="false" defailt="" hint="An anchor to target in the target event">
166                <cfargument name="stateful" type="boolean" required="false" default="true" hint="Whether or not to maintain the state container across the redirect." />
167                <cfargument name="stateContainer" default="#variables._eventRequest.getStateContainer()#" type="ModelGlue.Util.GenericCollection" required="true" hint="An existing state container to store in session.">
168
169                <cfset var appendedState = "" />
170                <cfset var i = "" />
171                <cfset var useSession = getModelGlue().getUseSession() />
172
173                <cfif Len(arguments.anchor)>
174                        <cfset arguments.anchor = "###arguments.anchor#" />
175                </cfif>
176
177                <cfif not stateContainer.exists("myself")>
178                        <cfthrow message="Model-Glue:  I can't forward the event request because you've removed the ""myself"" variable from the event's state." />
179                </cfif>
180
181                <cfif arguments.stateful and useSession>
182                        <cfset session._ModelGlue.forwardedStateContainer = arguments.stateContainer />
183                </cfif>
184
185                <cfif useSession>
186                        <cfset session._ModelGlue.forwardedRequestLog = variables._eventRequest.getLog() />
187                </cfif>
188
189                <cfloop list="#arguments.append#" index="i">
190                        <cfif arguments.stateContainer.exists(i) and isSimpleValue(arguments.stateContainer.getValue(i))>
191                                <cfset appendedState = appendedState & "&" & i & "=" & arguments.stateContainer.getValue(i) />
192                        </cfif>
193                </cfloop>
194
195                <cfset variables._eventRequest.trace("Forward", "The request is being forwarded/redirected to the ""#arguments.event#"" event-handler.") />
196
197                <cflocation url="#stateContainer.getValue("myself")##arguments.event##appendedState##anchor#" addToken="false" />
198        </cffunction>
199
200        <!--- UTILITY --->
201        <cffunction name="GetEventHandlerName" access="public" output="false" hint="Returns the current event handler name.">
202                <cfreturn getEventRequest().getCurrentEventHandler().getName() />
203        </cffunction>
204
205        <!--- VIEWS --->
206        <cffunction name="GetView" access="public" output="false" hint="I return the HTML of any already rendered view by name.  If the view doesn't exist, I return an empty string.">
207    <cfargument name="name" type="string" required="true" hint="I am the name of the view to get.">
208    <cfreturn getEventRequest().getRenderedView(arguments.name) />
209        </cffunction>
210</cfcomponent>