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