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