Ticket #7: controller.cfc

File controller.cfc, 15.7 kB (added by luis, 3 years ago)
Line 
1 <!-----------------------------------------------------------------------
2 Author                  : Luis Majano
3 Date                    : September 23, 2005
4 Last Update     : June 18, 2006
5 ----------------------------------------------------------------------->
6 <cfcomponent name="controller" hint="This is the ColdBox Controller. I practically do everything">
7
8         <cfset variables.currentPath = getCurrentTemplatePath()>
9         <cfset variables.DebugMode = false>
10
11         <cffunction name="init" returntype="any" access="Public" hint="I am the constructor" output="false">
12                 <cfreturn this>
13         </cffunction>
14
15         <cffunction name="configLoader" returntype="void" access="Public" hint="I Load the configurations and init the framework variables" output="false">
16                 <cfargument name="reloadStructures" type="any" required="false" default="false" hint="Flag to reload the structures.">
17                 <!--- ************************************************************* --->
18                 <cfset var XMLParser = getPlugin("XMLParser")>
19                 <cfset var ConfigFileLocation = "">
20                 <cfset var ConfigTimeStamp = "">
21                 <cfset var HandlersDirectory = "">
22                 <!--- Load Coldbox Config Structure --->
23                 <cfif not structKeyExists(application, "FWSettingsStruct") or arguments.reloadStructures>
24                         <cfif not structKeyExists(application, "FWSettingsStruct") or arguments.reloadStructures>
25                                 <cfset XMLParser.loadFramework()>
26                         </cfif>
27                 </cfif>
28                 <!--- Load Application Config Structure if Needed--->
29                 <cfif not structKeyExists(application, "ConfigStruct") or arguments.reloadStructures>
30                         <!--- Get app's config.xml Location's and TimeStamps' --->
31                         <cfset ConfigFileLocation = getSetting("ConfigFileLocation", true)>
32                         <!--- Check If Config.xml file Exits --->
33                         <cfif not fileExists(ConfigFileLocation)>
34                                 <cfthrow type="Framework.ConfigFileNotFoundException" message="Framework.loadConfig: The Config File: #ConfigFileLocation# can't be found. ">
35                         </cfif>
36                         <!--- Load Config from XML --->
37                         <cfif not structKeyExists(application, "ConfigStruct") or arguments.reloadStructures>
38                                 <cfset application.configStruct = XMLParser.parseConfig(configTimeStamp)>
39                                 <!--- Load Handlers Directory --->
40                                 <cfset application.configStruct.handlersList = getInternalHandlersList()>
41                                 <!--- Set Config DebugMode --->
42                                 <cfset setDebugMode(getSetting("DebugMode"))>
43                         </cfif>
44                         <!---Load i18N if needed --->
45                         <cfif getSetting("DefaultLocale") neq "">
46                                 <cfset getPlugin("i18n").initBundle(getSetting("DefaultResourceBundle"),getSetting("DefaultLocale"))>
47                         </cfif>
48                 </cfif>
49         </cffunction>
50
51         <cffunction name="getInternalHandlersList" access="private" hint="Get the handlers list" returntype="any">
52                 <cfset var HandlersDirectory = "">
53                 <cfset HandlersDirectory = getSetting("ApplicationPath",1) & getSetting("OSFileSeparator",1) & "handlers">
54                 <cfif directoryExists(HandlersDirectory)>
55                         <cfdirectory action="list" name="qHandlers" filter="*.cfc" directory="#HandlersDirectory#">
56                         <cfreturn replacenocase(valuelist(qHandlers.name),".cfc","","all")>
57                 <cfelse>
58                         <cfthrow type="Framework.HandlersDirectoryNotFoundException" message="Framework.loadConfig: The handlers directory does not exist please check your application structure.">
59                 </cfif>
60         </cffunction>
61
62         <cffunction name="reqCapture" access="Public" returntype="void" hint="I capture the FORM, URL scopes into the request collection object." output="false">
63                 <cfargument name="FORM" hint="The form scope" required="true" type="any">
64                 <cfargument name="URL"  hint="The url scope"  required="true" type="any">
65                 <!--- ************************************************************* --->
66                 <cfset var ehListPosition = 0>
67                 <cfset var handlersList = "">
68                 <cfmodule template="includes/timer.cfm" timertag="Request Capture" >
69                         <cfset request.reqCollection = structNew()>
70                         <cfset StructAppend(request.reqCollection, arguments.FORM)>
71                         <cfset StructAppend(request.reqCollection, arguments.URL)>
72                         <!--- Debug Mode Checks --->
73                         <cfif structKeyExists(request.reqCollection,"debugMode") and isBoolean(request.reqCollection.debugmode)>
74                                 <cfif getSetting("debugPassword") eq "">
75                                         <cfset setDebugMode(request.reqCollection.debugmode)>
76                                 <cfelseif structKeyExists(request.reqCollection,"debugpass") and compareNoCase(getSetting("debugPassword"),request.reqCollection.debugpass) eq 0>
77                                         <cfset setDebugMode(request.reqCollection.debugmode)>
78                                 </cfif>
79                         </cfif>
80                         <!---Default Event Definition --->
81                         <cfif not structkeyExists(request.reqCollection,"event") >
82                                 <cfset request.reqCollection.event = getSetting("DefaultEvent")>
83                         </cfif>
84                         <!---Event More Than 1 Check, grab the first event instance, other's are discarded for now.
85                         Still thinking about url event chaining. --->
86                         <cfif listLen(request.reqCollection.event) gte 2>
87                                 <cfset request.reqCollection.event = getToken(event,1,",")>
88                         </cfif>
89                         <cfset EventSyntaxCheck(request.reqCollection.event)>
90                         <!--- Check AutoReload Setting --->
91                         <cfif getSetting("HandlersIndexAutoReload")>
92                                 <cfset application.configStruct.handlersList = getInternalHandlersList()>
93                         </cfif>
94                         <cfset handlersList = getSetting("handlersList")>
95                         <cfset ehListPosition = listfindnocase(handlersList,getToken(request.reqCollection.event,1,"."))>
96                         <cfif ehListPosition eq 0>
97                                 <cfthrow type="Framework.EventHandlerNotFoundException" message="The event handler: #request.reqCollection.event# does not exist in the handlers directory. If you have just added a new event handler to your application or renamed it, please reinitialize the application by <a href='index.cfm?fwreinit=1'>clicking here.</a> or go to index.cfm?fwreinit=1">
98                         <cfelse>
99                                 <!--- Use the internal listing not the user defined --->
100                                 <cfset request.reqCollection.event = listSetAt(request.reqCollection.event,1,listgetat(handlersList,ehListPosition),".")>
101                         </cfif>
102                 </cfmodule>
103         </cffunction>
104
105         <cffunction name="EventSyntaxCheck" access="public" hint="I test the event syntax and throw errors." returntype="any"  output="false">
106                 <cfargument name="event" hint="The event to check" type="any" required="true">
107                 <cfif refindnocase("^eh[a-zA-Z]+\.(dsp|do|on)[a-zA-Z]+", trim(arguments.event))>
108                         <cfreturn true>
109                 <cfelse>
110                         <cfthrow type="Framework.EventSyntaxInvalidException" message="The event syntax: #request.reqCollection.event# is invalid. Please check the documentation for valid syntax.">
111                 </cfif>
112         </cffunction>
113
114         <cffunction name="getSetting" hint="I get a setting from the FW Config structures. Use the FWSetting boolean argument to retrieve from the fwSettingsStruct." access="public" returntype="any" output="false">
115                 <cfargument name="name"             type="any"   required="true"        hint="Name of the setting key to retrieve"  >
116                 <cfargument name="FWSetting"    type="boolean"   required="false"  hint="Boolean Flag. If true, it will retrieve from the fwSettingsStruct else from the configStruct. Default is false." default="false">
117                 <!--- ************************************************************* --->
118                 <cfif arguments.FWSetting and structKeyExists(application.FWSettingsStruct,"#arguments.name#")>
119                         <cfreturn application.FWSettingsStruct["#arguments.name#"]>
120                 <cfelseif structKeyExists(application.configstruct,"#arguments.name#")>
121                          <cfreturn application.configstruct["#arguments.name#"]>
122                 <cfelse>
123                         <cfthrow type="Framework.SettingNotFoundException" message="The setting #arguments.name# does not exist." detail="FWSetting flag is #arguments.FWSetting#">
124                 </cfif>
125         </cffunction>
126
127         <cffunction name="getPlugin" access="Public" returntype="any" hint="I am the Plugin cfc object factory." output="false">
128                 <cfargument name="plugin" type="any" required="true" hint="The Plugin object's name to instantiate" >
129                 <!--- ************************************************************* --->
130                 <cftry>
131                         <cfreturn CreateObject("component", "plugins.#trim(arguments.plugin)#").init(this)>
132                         <cfcatch type="any">
133                                 <cfthrow type="Framework.InvalidPluginInstantiationException"    message="Framework.getPlugin: Error Instantiating Plugin Object (#trim(arguments.plugin)#)<br><br>Diagnostics: #cfcatch.Message#<br>#cfcatch.detail#">
134                         </cfcatch>
135                 </cftry>
136         </cffunction>
137
138         <cffunction name="getCurrentPath" access="public" hint="I Get the currentPath of the controller" returntype="any"  output="false">
139                 <cfreturn variables.currentPath>
140         </cffunction>
141
142         <cffunction name="getDebugMode" access="public" hint="I Get the current controller debugmode" returntype="boolean"  output="false">
143                 <cfreturn variables.DebugMode>
144         </cffunction>
145
146         <cffunction name="setDebugMode" access="private" hint="I set the current controller debugmode" returntype="void"  output="false">
147                 <cfargument name="mode" required="true" type="boolean">
148                 <cfset variables.DebugMode = arguments.mode>
149         </cffunction>
150
151         <cffunction name="getCollection" returntype="any" access="Public" hint="I Get a reference or deep copy of the request Collection" output="false">
152                 <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">
153                 <!--- ************************************************************* --->
154                 <cfif arguments.DeepCopyFlag>
155                         <cfreturn duplicate(request.reqCollection)>
156                 <cfelse>
157                         <cfreturn request.reqCollection>
158                 </cfif>
159         </cffunction>
160
161         <cffunction name="valueExists" returntype="any" access="Public" hint="I Check if a value exists in the request collection." output="false">
162                 <cfargument name="name" hint="Name of the variable to find in the request collection" type="String" required="true">
163                 <!--- ************************************************************* --->
164                 <cfreturn isDefined("request.reqCollection.#arguments.name#") >
165         </cffunction>
166
167         <cffunction name="getValue" returntype="Any" access="Public" hint="I Get a value from the request collection." output="false">
168                 <cfargument name="name" hint="Name of the variable to get from the request collection" type="any" required="true">
169                 <cfargument name="defaultValue"
170                                         hint="Default value to return if not found.There are no default values for complex structures. You can send [array][struct][query] and the
171                                                   method will return the empty complex variable.Please remember to include the brackets, syntax sensitive.You can also send complex variables
172                                                   as the defaultValue argument."
173                                         type="any" required="No" default="NONE">
174                 <!--- ************************************************************* --->
175                 <cfif isDefined("request.reqCollection.#arguments.name#") >
176                         <cfreturn Evaluate("request.reqCollection.#arguments.name#")>
177                 <cfelseif isSimpleValue(arguments.defaultValue) and arguments.defaultValue eq "NONE">
178                         <cfthrow type="Framework.ValueNotInRequestCollectionException" message="The variable: #arguments.name# is undefined in the request collection.">
179                 <cfelseif isSimpleValue(arguments.defaultValue) >
180                         <cfif refind("\[[A-Za-z]*\]", arguments.defaultValue) >
181                                 <cfif findnocase("array", arguments.defaultvalue)>
182                                         <cfreturn ArrayNew(1)>
183                                 <cfelseif findnocase("struct", arguments.defaultvalue)>
184                                         <cfreturn StructNew()>
185                                 <cfelseif findnocase("query", arguments.defaultvalue)>
186                                         <cfreturn QueryNew("")>
187                                 </cfif>
188                         <cfelse>
189                                 <cfreturn arguments.defaultValue>
190                         </cfif>
191                 <cfelse>
192                         <cfreturn arguments.defaultValue>
193                 </cfif>
194         </cffunction>
195
196         <cffunction name="setValue" access="Public" hint="I Set a value in the request collection" output="false" returntype="void">
197                 <cfargument name="name"  hint="The name of the variable to set." type="any"  required="true" >
198                 <cfargument name="value" hint="The value of the variable to set" type="Any"     required="true" >
199                 <!--- ************************************************************* --->
200                 <cfset "request.reqCollection.#arguments.name#" = arguments.value>
201         </cffunction>
202
203         <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">
204                 <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="any" required="Yes" >
205                 <!--- ************************************************************* --->
206             <cfif StructKeyExists(application.ConfigStruct.ViewLayouts, trim(arguments.name)) >
207                         <cfset request.reqCollection.currentLayout = application.ConfigStruct.ViewLayouts[trim(arguments.name)]>
208                 <cfelse>
209                         <cfset request.reqCollection.currentLayout = application.ConfigStruct.DefaultLayout>
210                 </cfif>
211                 <cfset request.reqCollection.currentView = trim(arguments.name)>
212         </cffunction>
213
214         <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">
215                 <cfargument name="name"  hint="The name of the layout file to set." type="any" required="true" >
216                 <!--- ************************************************************* --->
217                 <cfset request.reqCollection.currentLayout = trim(arguments.name) & ".cfm">
218         </cffunction>
219
220         <cffunction name="setNextEvent" access="Public" returntype="void" hint="I Set the next event to run and relocate the browser to that event."  output="false">
221                 <cfargument name="event"                        hint="The name of the event to run."                      type="any" required="true" >
222                 <cfargument name="queryString"          hint="The query string to append, if needed."   type="any" required="No" default="" >
223                 <!--- ************************************************************* --->
224             <cfif EventSyntaxCheck(arguments.event) >
225                         <cflocation url="#cgi.SCRIPT_NAME#?event=#trim(arguments.event)#&#trim(arguments.queryString)#" addtoken="no">
226                 </cfif>
227         </cffunction>
228
229         <cffunction name="overrideEvent" access="Public" hint="I Override the current event in the request collection without relocating.  Request Collection name: event"  output="false" returntype="void">
230                 <cfargument name="event"                        hint="The name of the event to override." type="any" required="true" >
231                 <!--- ************************************************************* --->
232             <cfif EventSyntaxCheck(arguments.event)>
233                         <cfset setValue("event",trim(arguments.event))>
234                 </cfif>
235         </cffunction>
236
237         <cffunction name="runEvent" returntype="void" access="Public" hint="I am an event handler runnable factory. If no event is passed in then it will run the current event in the reqCollection. If no event is set in the request collection it runs the default event.">
238                 <cfargument name="event" hint="The event to run. If no current event is set, use the default event from the config.xml" type="any" required="no" default="#request.reqCollection.event#">
239                 <!--- ************************************************************* --->
240                 <cfset var method = "">
241                 <cfset var objEventHandler = "">
242                 <cfset var handlerDir = "handlers">
243                 <cfset arguments.event = trim(arguments.event)>
244                 <!--- Start Timer --->
245                 <cfmodule template="includes/timer.cfm" timertag="invoking runEvent [#arguments.event#]">
246                         <cfif CompareNocase(getSetting("AppName"),getSetting("DashboardName",1)) eq 0>
247                                 <cfset handlerDir = "admin.handlers">
248                         <cfelseif getSetting("AppCFMXMapping") neq "">
249                                 <cfset handlerDir = "#replace(getSetting("AppCFMXMapping"),"/",".","all")#.handlers">
250                         </cfif>
251                         <cfif EventSyntaxCheck(arguments.event) >
252                                 <cfset method = getToken(arguments.event,2,".")>
253                                 <cftry>
254                                         <cfset objEventHandler = CreateObject("component","#handlerDir#.#getToken(arguments.event,1,".")#").init(this)>
255                                         <cfcatch type="any">
256                                                 <cfthrow type="Framework.EventHandlerInstantiationException" message="Framework.runEvent: Error Instantiating Event Handler: (#getToken(arguments.event,1,".")#) <br><br>Diagnostics: #cfcatch.Message#<br>#cfcatch.detail#">
257                                         </cfcatch>
258                                 </cftry>
259                                 <!---  Run The handler's Method --->
260                                 <cfset evaluate("objEventHandler.#method#()")>
261                         </cfif>
262                 </cfmodule>
263         </cffunction>
264 </cfcomponent>

Copyright 2006 ColdBox Framework by Luis Majano