From de3ee335a926e0172132828d25e84aca3fad141b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20St=C3=B6ckel?= Date: Thu, 10 Jun 2010 10:48:59 +0000 Subject: [PATCH] Fixed handling of json requests using etemplate.process_exec and fixes in the json encoding of hierarchical values --- phpgwapi/inc/class.egw_json.inc.php | 16 ++-- phpgwapi/js/egw_json.js | 143 ++++++++++++++++++++++++---- 2 files changed, 132 insertions(+), 27 deletions(-) diff --git a/phpgwapi/inc/class.egw_json.inc.php b/phpgwapi/inc/class.egw_json.inc.php index 53ce04295c..8e8874c947 100644 --- a/phpgwapi/inc/class.egw_json.inc.php +++ b/phpgwapi/inc/class.egw_json.inc.php @@ -51,7 +51,6 @@ class egw_json_request $parameters = $request['parameters']; /*$parameters = array_stripslashes($request['parameters']);*/ } - //Call the supplied callback function along with the menuaction and the passed parameters $this->handleRequest($menuaction, $parameters); } @@ -82,15 +81,15 @@ class egw_json_request switch($handler) { case '/etemplate/process_exec': - $menuaction = $appName.'.'.$className.'.'.$functionName; + $_GET['menuaction'] = $appName.'.'.$className.'.'.$functionName; $appName = $className = 'etemplate'; $functionName = 'process_exec'; $menuaction = 'etemplate.etemplate.process_exec'; $parameters = array( - $argList[0]['etemplate_exec_id'], - $argList[0]['submit_button'], - $argList[0], + $parameters[0]['etemplate_exec_id'], + $parameters[0]['submit_button'], + $parameters[0], 'xajaxResponse', ); //error_log("xajax_doXMLHTTP() /etemplate/process_exec handler: arg0='$menuaction', menuaction='$_GET[menuaction]'"); @@ -127,6 +126,10 @@ class egw_json_request $parameters = translation::convert($parameters, 'utf-8'); +// error_log(print_r($parameters, true)); + +// _debug_array($parameters); + call_user_func_array(array($ajaxClass, $functionName), $parameters); } } @@ -351,8 +354,7 @@ class egw_json_response /** * Deprecated legacy xajax wrapper functions for the new egw_json interface */ -/* -class xajaxResponse extends egw_json_response +/*class xajaxResponse extends egw_json_response { public function addScript($script) { diff --git a/phpgwapi/js/egw_json.js b/phpgwapi/js/egw_json.js index ce7d392b3d..f93fee4d9c 100644 --- a/phpgwapi/js/egw_json.js +++ b/phpgwapi/js/egw_json.js @@ -117,6 +117,27 @@ egw_json_request.prototype.sendRequest = function(_async, _callback, _sender) success: this.handleResponse}); } +egw_json_request.prototype.getFormValues = function(_form) +{ + var elem = null; + if (typeof _form == 'object') + { + elem = _form; + } + else + { + elem = document.getElementsByName(_form)[0]; + } + + var serialized = new Object; + if (typeof elem != "undefined" && elem && elem.childNodes) + { + _egw_json_getFormValues(serialized, elem.childNodes) + } + + return serialized; +} + egw_json_request.prototype.alertFunc = function(_message, _details) { alert(_message); @@ -222,30 +243,28 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe } - /** * Deprecated legacy xajax wrapper functions for the new egw_json interface */ -/* _xajax_doXMLHTTP = function(_async, _menuaction, _arguments) { - /* Assemble the parameter array *//* + /* Assemble the parameter array */ var paramarray = new Array(); for (var i = 1; i < _arguments.length; i++) { paramarray[paramarray.length] = _arguments[i]; } - /* Create a new request, passing the menuaction and the parameter array *//* + /* Create a new request, passing the menuaction and the parameter array */ var request = new egw_json_request(_menuaction, paramarray); - /* Send the request *//* + /* Send the request */ request.sendRequest(_async); return request; } -xajax_doXMLHTTP = function(_menuaction) +/*xajax_doXMLHTTP = function(_menuaction) { return _xajax_doXMLHTTP(true, _menuaction, arguments); } @@ -257,19 +276,103 @@ xajax_doXMLHTTPsync = function(_menuaction) window.xajax = { "getFormValues": function(_form) - { - var elem = null; - if (typeof _form == 'object') - { - elem = _form; - } - else - { - elem = document.getElementsByName(_form)[0]; - } - - var serialized = $(_form).serializeArray(); - alert("\nSerialized:\n" + serialized); - return serialized; + { + return egw_json_request.prototype.getFormValues(_form); } };*/ + +/* + The following code is adapted from the xajax project which is licensed under + the following license + @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson + @copyright Copyright (c) 2008-2009 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson + @license http://www.xajaxproject.org/bsd_license.txt BSD License +*/ + +/** + * used internally by the legacy "egw_json_response.getFormValues" to recursively + * run over all form elements + * @param serialized is the object which will contain the form data + * @param children is the children node of the form we're runing over + */ +function _egw_json_getFormValues(serialized, children) +{ + for (var i = 0; i < children.length; ++i) { + var child = children[i]; + + if (typeof child.childNodes != "undefined") + _egw_json_getFormValues(serialized, child.childNodes); + + _egw_json_getFormValue(serialized, child); + } +} + +/** + * used internally to serialize + */ +function _egw_json_getFormValue(serialized, child) +{ + //Return if the child doesn't have a name, is disabled, or is a radio-/checkbox and not checked + if ((typeof child.name == "undefined") || (child.disabled && child.disabled == true) || + (child.type && (child.type == 'radio' || child.type == 'checkbox') && (!child.checked))) + { + return; + } + + var name = child.name; + var values = null; + + if ('select-multiple' == child.type) + { + values = new Array; + for (var j = 0; j < child.length; ++j) + { + var option = child.options[j]; + if (option.selected == true) + values.push(option.value); + } + } + else + { + values = child.value; + } + + //Special treatment if the name of the child contains a [] - then all theese + //values are added to an array. + var keyBegin = name.indexOf('['); + if (0 <= keyBegin) { + var n = name; + var k = n.substr(0, n.indexOf('[')); + var a = n.substr(n.indexOf('[')); + if (typeof serialized[k] == 'undefined') + serialized[k] = new Object; + var p = serialized; // pointer reset + while (a.length != 0) { + var sa = a.substr(0, a.indexOf(']')+1); + + var lk = k; //save last key + var lp = p; //save last pointer + + a = a.substr(a.indexOf(']')+1); + p = p[k]; + k = sa.substr(1, sa.length-2); + if (k == '') { + if ('select-multiple' == child.type) { + k = lk; //restore last key + p = lp; + } else { + k = p.length; + } + } + if (typeof p[k] == 'undefined') + p[k] = new Object; + } + p[k] = values; + } else { + //Add the value to the result object with the given name + if (typeof values != "undefined") + { + serialized[name] = values; + } + } +}