PageRenderTime 16ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/event/pluginEventHandler.cfc

http://github.com/Lagaffe/MobileMura
ColdFusion CFScript | 221 lines | 169 code | 45 blank | 7 comment | 10 complexity | dcfbefcff1afe4890a37ddc9fbaae488 MD5 | raw file
  1<!---
  2
  3MobileMura/event/pluginEventHandler.cfc
  4
  5Copyright 2011 Guust Nieuwenhuis 
  6
  7Licensed under the Apache License, Version 2.0 (the "License"); 
  8you may not use this file except in compliance with the License. 
  9You may obtain a copy of the License at 
 10
 11http://www.apache.org/licenses/LICENSE-2.0 
 12
 13Unless required by applicable law or agreed to in writing, software 
 14distributed under the License is distributed on an "AS IS" BASIS, 
 15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 16See the License for the specific language governing permissions and 
 17limitations under the License. 
 18
 19--->
 20
 21<cfcomponent extends="mura.plugin.pluginGenericEventHandler">
 22
 23	<cfset variables.preserveKeyList = 'context,base,cfcbase,subsystem,subsystembase,section,item,services,action,controllerExecutionStarted' />
 24	<!--- Include FW/1 configuration that is shared between the Mura CMS and the FW/1 application. --->
 25	<cfset variables.framework = getFramework() />
 26
 27	<!--- ********** Mura Specific Events ************* --->
 28
 29	<cffunction name="onApplicationLoad" output="false">
 30		<cfargument name="$" required="true" hint="mura scope" />
 31		<cfset var state=preseveInternalState(request)>
 32		<cfinvoke component="#variables.pluginConfig.getPackage()#.Application" method="onApplicationStart" />
 33		<cfset restoreInternalState(request,state)>
 34		<cfset variables.pluginConfig.addEventHandler(this)>
 35	</cffunction>
 36	
 37	<cffunction name="onGlobalSessionStart" output="false">
 38		<cfargument name="$" required="true" hint="mura scope" />
 39		<cfset var state=preseveInternalState(request)>
 40		<cfinvoke component="#pluginConfig.getPackage()#.Application" method="onSessionStart" />
 41		<cfset restoreInternalState(request,state)>
 42	</cffunction>
 43
 44	<cffunction name="onSiteRequestStart" output="false">
 45        <cfargument name="$" required="true" hint="mura scope" />
 46        <cfset $[variables.framework.applicationKey] = this />        
 47    </cffunction>
 48
 49	<cffunction name="onRenderStart" output="false" returntype="any">
 50		<cfargument name="$" />
 51		<cfscript>
 52			// this allows you to call methods here by accessing '$.mfw1.methodName(argumentCollection=args)'
 53			$.mfw1 = this;
 54		</cfscript>
 55	</cffunction>
 56
 57	<!--- ********** MobileMura Specific Events ************* --->
 58
 59	<cffunction name="standardMobileHandler" output="false" returntype="any">
 60		<cfargument name="$" />
 61		
 62		<cfset MobileMura = createObject("component","#pluginConfig.getPackage()#.MobileMura") />
 63		
 64		<cfset MobileMura.updateTemplate($.content()) />
 65		<cfset MobileMura.updateTheme($) />
 66
 67		<cfset renderer.showAdminToolbar=false>
 68		<cfset renderer.showMemberToolbar=false>
 69		<cfset renderer.showEditableObjects=false>
 70
 71		<cfreturn />
 72	</cffunction>
 73
 74	<cffunction name="onContentEdit" returntype="any" output="true">
 75		<cfargument name="event" />
 76		
 77		<cfinclude template="../admin/views/onContentEdit/onContentEdit.cfm" />
 78		
 79		<cfreturn />
 80	</cffunction>
 81
 82	<!--- ********** display object/s ************ --->	
 83
 84	<cffunction name="renderApp" output="false">
 85		<cfargument name="$" required="true" hint="mura scope" />
 86		<cfargument name="action" required="false" default="" 
 87			hint="if only rendering a 'widget', then pass in the action such as 'public:main.default' ... otherwise, just leave it blank!" />
 88		<cfreturn doEvent(arguments.$,arguments.action) />
 89	</cffunction>
 90
 91	<!--- ********** FW/1 ************* --->
 92
 93	<cffunction name="doEvent" output="false">
 94		<cfargument name="$" required="true" />
 95		<cfargument name="action" type="string" required="false" default="" 
 96					hint="Optional: If not passed it looks into the event for a defined action, else it uses the default" />
 97		<cfreturn doAction(arguments.$,arguments.action) />
 98	</cffunction>
 99	
100	<cffunction name="doAction" output="false">
101		<cfargument name="$" />
102		<cfargument name="action" type="string" required="false" default="" 
103					hint="Optional: If not passed it looks into the event for a defined action, else it uses the default" />
104		<cfscript>
105			var local = StructNew();
106			var state = StructNew();
107			var result = '';
108			var savedEvent = '';
109			var savedAction = '';
110			var fw1 = CreateObject('component','#pluginConfig.getPackage()#.Application');
111
112			// Put the event url struct, to be used by FW/1
113			url.$ = arguments.$;
114			if ( not len( arguments.action ) ) {
115				if ( len(arguments.$.event(variables.framework.action)) ) {
116					arguments.action = arguments.$.event(variables.framework.action);
117				} else {
118					arguments.action = variables.framework.home;
119				};
120			};
121		
122			// put the action passed into the url scope, saving any pre-existing value
123			if ( StructKeyExists(request, variables.framework.action) ) {
124				savedEvent = request[variables.framework.action];
125			};
126
127			if ( StructKeyExists(url,variables.framework.action) ) {
128				savedAction = url[variables.framework.action];
129			};
130
131			url[variables.framework.action] = arguments.action;
132			state = preseveInternalState(request);
133
134			// call the frameworks onRequestStart
135			fw1.onRequestStart(CGI.SCRIPT_NAME);
136		</cfscript>
137
138		<!--- call the frameworks onRequest --->
139		<!--- we save the results via cfsavecontent so we can display it in mura --->
140		<cfsavecontent variable="result">
141			<cfset fw1.onRequest(CGI.SCRIPT_NAME) />
142		</cfsavecontent>
143		
144		<cfscript>
145			// restore the url scope
146			if ( StructKeyExists(url,variables.framework.action) ) {
147				StructDelete(url,variables.framework.action);
148			};
149
150			// if there was a passed in action via the url then restore it
151			if ( Len(savedAction) ) {
152				url[variables.framework.action] = savedAction;
153			};
154
155			// if there was a passed in request event then restore it
156			if ( Len(savedEvent) ) {
157				request[variables.framework.action] = savedEvent;
158			};
159			
160			restoreInternalState(request,state);
161
162			return result;
163		</cfscript>
164	</cffunction>
165
166	<cffunction name="checkFrameworkConfig" output="false">
167		<cfargument name="$" />
168		<cfset var str="">
169		<cfset var configPath="#expandPath('/plugins')#/#variables.pluginConfig.getDirectory()#/frameworkConfig.cfm">
170		<cfset var lineBreak=chr(13) & chr(10)>
171		<cfif variables.framework.applicationKey neq variables.pluginConfig.getPackage() & lineBreak>
172			<cfset str='<cfset variables.framework=structNew()>' & lineBreak>
173			<cfset str=str & '<cfset variables.framework.applicationKey="#variables.pluginConfig.getPackage()#">' & lineBreak>
174			<cfset str=str & '<cfset variables.framework.base="/#variables.pluginConfig.getPackage()#">' & lineBreak>
175			<cfset str=str & '<cfset variables.framework.usingsubsystems=false>' & lineBreak>
176			<cfset str=str & '<cfset variables.framework.action="action">' & lineBreak>
177			<cfset str=str & '<cfset variables.framework.home="main.default">' & lineBreak>
178			<cfset str=str & '<cfset variables.framework.baseURL="useRequestURI">' & lineBreak>
179			<cfset str=str & '<cfset variables.framework.SESOmitIndex="true">' & lineBreak>
180			<cfset $.getBean('fileWriter').writeFile(file=configPath, output=str)>
181			<cfinclude template="../fw1config.cfm">
182		</cfif>
183	</cffunction>
184	
185	<cffunction name="preseveInternalState" output="false">
186		<cfargument name="state" />
187		<cfset var preserveKeys=structNew()>
188		<cfset var k="">
189
190		<cfif StructKeyExists(request, 'controllers')>
191			<cfset StructDelete(request, 'controllers') />
192		</cfif>
193		
194		<cfloop list="#variables.preserveKeyList#" index="k">
195			<cfif isDefined("arguments.state.#k#")>
196				<cfset preserveKeys[k]=arguments.state[k]>
197				<cfset structDelete(arguments.state,k)>
198			</cfif>
199		</cfloop>
200		<cfset structDelete( arguments.state, "serviceExecutionComplete" )>
201		<cfreturn preserveKeys>
202	</cffunction>
203	
204	<cffunction name="restoreInternalState" output="false">
205		<cfargument name="state" />
206		<cfargument name="restore" />
207		<cfloop list="#variables.preserveKeyList#" index="k">
208			<cfset StructDelete(arguments.state,k)>
209		</cfloop>
210		<cfset StructAppend( state,restore, true )>
211		<cfset StructDelete( state, "serviceExecutionComplete" )>
212	</cffunction>
213
214	<!--- apparently needed for CF8 (thanks Grant Shepert!) --->
215	<cffunction name="getFramework" output="false" returntype="any">
216		<cfset var framework = StructNew() />
217		<cfinclude template="../fw1config.cfm" />
218		<cfreturn framework />
219	</cffunction>
220
221</cfcomponent>