1: <?php
2:
3: /**
4: * Picon Framework
5: * http://code.google.com/p/picon-framework/
6: *
7: * Copyright (C) 2011-2012 Martin Cassidy <martin.cassidy@webquub.com>
8:
9: * Picon Framework is free software: you can redistribute it and/or modify
10: * it under the terms of the GNU General Public License as published by
11: * the Free Software Foundation, either version 3 of the License, or
12: * (at your option) any later version.
13:
14: * Picon Framework is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * General Public License for more details.
18:
19: * You should have received a copy of the GNU General Public License
20: * along with Picon Framework. If not, see <http://www.gnu.org/licenses/>.
21: * */
22:
23: namespace picon;
24:
25: /**
26: * Allows a user specified function to be rendered but wraps with a callback in the following way
27: *
28: * [optionName] : function([args...])
29: * {
30: * var callBackURL = '[url]';
31: * [userFucntionCode]
32: * piconAjaxGet(callBackURL, function(){}, function(){});
33: * }
34: *
35: * This allows the callback url to be altered by user defined code with function arguments before
36: * it is sent
37: *
38: * @author Martin Cassidy
39: * @package web/jQuery
40: */
41: class CallbackFunctionOption extends AbstractCallableOption
42: {
43: private $function;
44: private $args = array();
45: private $callback;
46:
47: /**
48: *
49: * @param type $name
50: * @param type $function
51: * @param ... args the names of the arguments the javascript function should take
52: */
53: public function __construct($name, $callback, $function)
54: {
55: parent::__construct($name);
56: Args::isString($function, 'function');
57: Args::callBackArgs($callback, 1, 'callback');
58: $this->callback = new SerializableClosure($callback);
59: $this->function = $function;
60:
61: $args = func_get_args();
62: for($i=3;$i<count($args);$i++)
63: {
64: array_push($this->args, $args[$i]);
65: }
66: }
67:
68: public function render(AbstractJQueryBehaviour $behaviour)
69: {
70: return sprintf("%s : function(%s) {var callBackURL = '%s'; %s piconAjaxGet(callBackURL, function(){}, function(){});}", $this->getName(), implode(', ', $this->args), $this->getUrl($behaviour), $this->function);
71: }
72:
73: public function call(AjaxRequestTarget $target)
74: {
75: $callable = $this->callback;
76: $callable($target);
77: }
78: }
79:
80: ?>
81: