PageRenderTime 16ms CodeModel.GetById 9ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Plug-in/xajax/xajax_core/plugin_layer/xajaxFunctionPlugin.inc.php

http://prosporous.googlecode.com/
PHP | 230 lines | 95 code | 31 blank | 104 comment | 32 complexity | 33674853742b8a77a5e4eec7323c83fc MD5 | raw file
  1<?php
  2/*
  3	File: xajaxFunctionPlugin.inc.php
  4
  5	Contains the xajaxFunctionPlugin class
  6
  7	Title: xajaxFunctionPlugin class
  8
  9	Please see <copyright.inc.php> for a detailed description, copyright
 10	and license information.
 11*/
 12
 13/*
 14	@package xajax
 15	@version $Id: xajaxFunctionPlugin.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $
 16	@copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson
 17	@license http://www.xajaxproject.org/bsd_license.txt BSD License
 18*/
 19
 20/*
 21	Constant: XAJAX_FUNCTION
 22		Specifies that the item being registered via the <xajax->register> function
 23		is a php function available at global scope, or a specific function from
 24		an instance of an object.
 25*/
 26if (!defined ('XAJAX_FUNCTION')) define ('XAJAX_FUNCTION', 'function');
 27
 28// require_once is necessary here as the xajaxEvent class will include this also
 29//SkipAIO
 30require_once dirname(__FILE__) . '/support/xajaxUserFunction.inc.php';
 31//EndSkipAIO
 32
 33/*
 34	Class: xajaxFunctionPlugin
 35*/
 36class xajaxFunctionPlugin extends xajaxRequestPlugin
 37{
 38	/*
 39		Array: aFunctions
 40		
 41		An array of <xajaxUserFunction> object that are registered and
 42		available via a <xajax.request> call.
 43	*/
 44	var $aFunctions;
 45
 46	/*
 47		String: sXajaxPrefix
 48		
 49		A configuration setting that is stored locally and used during
 50		the client script generation phase.
 51	*/
 52	var $sXajaxPrefix;
 53	
 54	/*
 55		String: sDefer
 56		
 57		Configuration option that can be used to request that the
 58		javascript file is loaded after the page has been fully loaded.
 59	*/
 60	var $sDefer;
 61	
 62	var $bDeferScriptGeneration;
 63
 64	/*
 65		String: sRequestedFunction
 66
 67		This string is used to temporarily hold the name of the function
 68		that is being requested (during the request processing phase).
 69
 70		Since canProcessRequest loads this value from the get or post
 71		data, it is unnecessary to load it again.
 72	*/
 73	var $sRequestedFunction;
 74
 75	/*
 76		Function: xajaxFunctionPlugin
 77		
 78		Constructs and initializes the <xajaxFunctionPlugin>.  The GET and POST
 79		data is searched for xajax function call parameters.  This will later
 80		be used to determine if the request is for a registered function in
 81		<xajaxFunctionPlugin->canProcessRequest>
 82	*/
 83	function xajaxFunctionPlugin()
 84	{
 85		$this->aFunctions = array();
 86
 87		$this->sXajaxPrefix = 'xajax_';
 88		$this->sDefer = '';
 89		$this->bDeferScriptGeneration = false;
 90
 91		$this->sRequestedFunction = NULL;
 92		
 93		if (isset($_GET['xjxfun'])) $this->sRequestedFunction = $_GET['xjxfun'];
 94		if (isset($_POST['xjxfun'])) $this->sRequestedFunction = $_POST['xjxfun'];
 95	}
 96
 97	/*
 98		Function: configure
 99		
100		Sets/stores configuration options used by this plugin.
101	*/
102	function configure($sName, $mValue)
103	{
104		if ('wrapperPrefix' == $sName) {
105			$this->sXajaxPrefix = $mValue;
106		} else if ('scriptDefferal' == $sName) {
107			if (true === $mValue) $this->sDefer = 'defer ';
108			else $this->sDefer = '';
109		} else if ('deferScriptGeneration' == $sName) {
110			if (true === $mValue || false === $mValue)
111				$this->bDeferScriptGeneration = $mValue;
112			else if ('deferred' === $mValue)
113				$this->bDeferScriptGeneration = $mValue;
114		}
115	}
116
117	/*
118		Function: register
119		
120		Provides a mechanism for functions to be registered and made available to
121		the page via the javascript <xajax.request> call.
122	*/
123	function register($aArgs)
124	{
125		if (1 < count($aArgs))
126		{
127			$sType = $aArgs[0];
128
129			if (XAJAX_FUNCTION == $sType)
130			{
131				$xuf =& $aArgs[1];
132
133				if (false === is_a($xuf, 'xajaxUserFunction'))
134					$xuf =& new xajaxUserFunction($xuf);
135
136				if (2 < count($aArgs))
137					if (is_array($aArgs[2]))
138						foreach ($aArgs[2] as $sName => $sValue)
139							$xuf->configure($sName, $sValue);
140
141				$this->aFunctions[] =& $xuf;
142
143				return $xuf->generateRequest($this->sXajaxPrefix);
144			}
145		}
146
147		return false;
148	}
149
150	/*
151		Function: generateClientScript
152		
153		Called by the <xajaxPluginManager> during the client script generation
154		phase.  This is used to generate a block of javascript code that will
155		contain function declarations that can be used on the browser through
156		javascript to initiate xajax requests.
157	*/
158	function generateClientScript()
159	{
160		if (false === $this->bDeferScriptGeneration || 'deferred' === $this->bDeferScriptGeneration)
161		{
162			if (0 < count($this->aFunctions))
163			{
164				echo "\n<script type='text/javascript' " . $this->sDefer . "charset='UTF-8'>\n";
165				echo "/* <![CDATA[ */\n";
166
167				foreach (array_keys($this->aFunctions) as $sKey)
168					$this->aFunctions[$sKey]->generateClientScript($this->sXajaxPrefix);
169
170				echo "/* ]]> */\n";
171				echo "</script>\n";
172			}
173		}
174	}
175
176	/*
177		Function: canProcessRequest
178		
179		Determines whether or not the current request can be processed
180		by this plugin.
181		
182		Returns:
183		
184		boolean - True if the current request can be handled by this plugin;
185			false otherwise.
186	*/
187	function canProcessRequest()
188	{
189		if (NULL == $this->sRequestedFunction)
190			return false;
191
192		return true;
193	}
194
195	/*
196		Function: processRequest
197		
198		Called by the <xajaxPluginManager> when a request needs to be
199		processed.
200		
201		Returns:
202		
203		mixed - True when the request has been processed successfully.
204			An error message when an error has occurred.
205	*/
206	function processRequest()
207	{
208		if (NULL == $this->sRequestedFunction)
209			return false;
210
211		$objArgumentManager =& xajaxArgumentManager::getInstance();
212		$aArgs = $objArgumentManager->process();
213
214		foreach (array_keys($this->aFunctions) as $sKey)
215		{
216			$xuf =& $this->aFunctions[$sKey];
217
218			if ($xuf->getName() == $this->sRequestedFunction)
219			{
220				$xuf->call($aArgs);
221				return true;
222			}
223		}
224
225		return 'Invalid function request received; no request processor found with this name.';
226	}
227}
228
229$objPluginManager =& xajaxPluginManager::getInstance();
230$objPluginManager->registerPlugin(new xajaxFunctionPlugin(), 100);