mirror of
https://github.com/EGroupware/egroupware.git
synced 2024-12-22 23:00:56 +01:00
Added PHP and JS JSON libraries for replacement of XAJAX
This commit is contained in:
parent
2aa6baddcb
commit
2bbe2e1203
145
phpgwapi/inc/class.egw_json.inc.php
Normal file
145
phpgwapi/inc/class.egw_json.inc.php
Normal file
@ -0,0 +1,145 @@
|
||||
<?php
|
||||
/**
|
||||
* eGroupWare API: JSON - Contains functions and classes for doing JSON requests.
|
||||
*
|
||||
* @link http://www.egroupware.org
|
||||
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
||||
* @package api
|
||||
* @subpackage ajax
|
||||
* @author Andreas Stoeckel
|
||||
* @version $Id:$
|
||||
*/
|
||||
|
||||
/* Class which handles JSON requests to the server */
|
||||
class egw_json_request
|
||||
{
|
||||
/* Parses the raw input data supplied with the input_data parameter and calls the
|
||||
callback function, passing the "menuaction" and all parameters supplied in the request to it.
|
||||
@param string $input_data is the RAW input data as it was received from the client
|
||||
@param callback $callback(string $menuaction, array $parameters) called when a valid request has been received. The result of the callback function will be returned by parseRequest
|
||||
@returns NULL if parsing the request failed, or the result of the callback function if the request has been successfully decoded.*/
|
||||
public function parseRequest($input_data, $callback)
|
||||
{
|
||||
//Decode the JSON input data into associative arrays
|
||||
if ($json = json_decode(stripslashes($input_data), true))
|
||||
{
|
||||
//Get the request array
|
||||
if (isset($json['request']))
|
||||
{
|
||||
$request = $json['request'];
|
||||
|
||||
//Check whether the "menuaction" string exists
|
||||
if (isset($request['menuaction']))
|
||||
{
|
||||
//Check whether any parameters were supplied along with the request
|
||||
$parameters = array();
|
||||
if (isset($request['parameters']))
|
||||
$parameters = $request['parameters'];
|
||||
|
||||
//Call the supplied callback function along with the menuaction and the passed parameters
|
||||
return call_user_func($callback, $request['menuaction'], $parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class egw_json_response
|
||||
{
|
||||
/* A response can only contain one generic data part. This variable is used to
|
||||
store, whether a data part had already been added to the response.*/
|
||||
private $hasData = false;
|
||||
|
||||
/* Holds the actual response data which is then encoded to JSON once the "getJSON"
|
||||
function is called */
|
||||
protected $responseArray = array();
|
||||
|
||||
/* Adds any type of data to the response array */
|
||||
protected function addGeneric($key, $data)
|
||||
{
|
||||
$this->responseArray[] = array(
|
||||
"type" => $key,
|
||||
"data" => $data);
|
||||
}
|
||||
|
||||
/* Adds a "data" response to the json response. This function may only be called once
|
||||
for a single JSON response object.
|
||||
@param object/array/string $data can be of any data type and will be added JSON Encoded to your response.*/
|
||||
public function addData($data)
|
||||
{
|
||||
/* Only allow adding the data response once */
|
||||
if (!$this->hasData)
|
||||
{
|
||||
$this->addGeneric('data', $data);
|
||||
$this->hasData = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Adding more than one data response to a JSON response is not allowed.");
|
||||
}
|
||||
}
|
||||
|
||||
/* Adds an "alert" to the response which can be handeled on the client side. The default implementation simply displays
|
||||
the text supplied here with the JavaScript function "alert".
|
||||
@param string $message contains the actual message being sent to the client.
|
||||
@param string $details (optional) can be used to inform the user on the client side about additional details about the error. This might be information how the error can be resolved/why it was raised or simply some debug data.*/
|
||||
public function addAlert($message, $details = '')
|
||||
{
|
||||
if (is_string($message) && is_string($details))
|
||||
{
|
||||
$this->addGeneric('alert', array(
|
||||
"message" => $message,
|
||||
"details" => $details));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid parameters supplied.");
|
||||
}
|
||||
}
|
||||
|
||||
/* Allows you to add a generic java script to the response which will be executed upon the request gets received. Deprecated.
|
||||
@deprecated
|
||||
@param string $script the script code which should be executed upon receiving*/
|
||||
public function addScript($script)
|
||||
{
|
||||
if (is_string($script))
|
||||
{
|
||||
$this->addGeneric('script', $script);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid parameters supplied.");
|
||||
}
|
||||
}
|
||||
|
||||
/* Adds an html assign to the response, which is excecuted upon the request is received. Deprecated, just for compatibility with XAJAX
|
||||
@deprecated
|
||||
@param string $id the identifier of the html element in which the assign shall take place
|
||||
@param string $key the key in the html element which should be modified when the assign takes place.
|
||||
@param string $value the value which should be assigned to the given key*/
|
||||
public function addAssign($id, $key, $value)
|
||||
{
|
||||
if (is_string($id) && is_string($key) && (is_string($value) || is_numeric($value)))
|
||||
{
|
||||
$this->addGeneric('assign', array(
|
||||
"id" => $id,
|
||||
"key" => $key,
|
||||
"value" => $value));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid parameters supplied");
|
||||
}
|
||||
}
|
||||
|
||||
/* Returns the actual JSON code generated by calling the above "add" function.*/
|
||||
public function getJSON()
|
||||
{
|
||||
/* Wrap the result array into a parent "response" Object */
|
||||
$res['response'] = $this->responseArray;
|
||||
return json_encode($res, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
|
||||
}
|
||||
}
|
144
phpgwapi/js/egw_json.js
Normal file
144
phpgwapi/js/egw_json.js
Normal file
@ -0,0 +1,144 @@
|
||||
/**
|
||||
* eGroupWare API: JSON - Contains the client side javascript implementation of class.egw_json.inc.php
|
||||
*
|
||||
* @link http://www.egroupware.org
|
||||
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
||||
* @package api
|
||||
* @subpackage ajax
|
||||
* @author Andreas Stoeckel
|
||||
* @version $Id:$
|
||||
*/
|
||||
|
||||
/* The egw_json_request is the javaScript side implementation of class.egw_json.inc.php.*/
|
||||
|
||||
/* The constructor of the egw_json_request class.
|
||||
* @param string _url the url of the AJAX handler on the server
|
||||
* @param string _menuaction the menuaction function which should be called and which handles the actual request
|
||||
* @param array _parameters which should be passed to the menuaction function.
|
||||
*/
|
||||
function egw_json_request(_url, _menuaction, _parameters)
|
||||
{
|
||||
//Copy the supplied parameters
|
||||
this.menuaction = _menuaction;
|
||||
this.parameters = _parameters;
|
||||
this.url = _url;
|
||||
this.sender = null;
|
||||
this.callback = null;
|
||||
this.alertHandler = this.alertFunc;
|
||||
if (document.alertHandler)
|
||||
{
|
||||
this.alertHandler = document.alertHandler;
|
||||
}
|
||||
}
|
||||
|
||||
/* Sends the AJAX JSON request.
|
||||
* @param boolean _async specifies whether the request should be handeled asynchronously (true, the sendRequest function immediately returns to the caller) or asynchronously (false, the sendRequest function waits until the request is received)
|
||||
* @param _callback is an additional callback function which should be called upon a "data" response is received
|
||||
* @param _sender is the reference object the callback function should get
|
||||
*/
|
||||
egw_json_request.prototype.sendRequest = function(_async, _callback, _sender)
|
||||
{
|
||||
//Store the sender and callback parameter inside this class
|
||||
this.sender = _sender;
|
||||
if (typeof _callback != "undefined")
|
||||
this.callback = _callback;
|
||||
|
||||
//Copy the async parameter which defaults to "true"
|
||||
var is_async = true;
|
||||
if (typeof _async != "undefined")
|
||||
is_async = _async;
|
||||
|
||||
//Assemble the actual request string
|
||||
var request = '{';
|
||||
request += '"request":{';
|
||||
request += '"menuaction":"' + this.menuaction + '"';
|
||||
if (this.parameters)
|
||||
{
|
||||
request += ',"parameters":[';
|
||||
for (var i = 0; i < this.parameters.length; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
request += ',';
|
||||
}
|
||||
request += '"' + this.parameters[i] + '"';
|
||||
}
|
||||
request += ']';
|
||||
}
|
||||
request += '}}';
|
||||
|
||||
var request_obj = new Object();
|
||||
request_obj.json_data = request;
|
||||
|
||||
//Send the request via the jquery AJAX interface to the server
|
||||
$.ajax({url: this.url,
|
||||
async: is_async,
|
||||
context: this,
|
||||
data: request_obj,
|
||||
dataType: 'json',
|
||||
type: 'POST',
|
||||
success: this.handleResponse});
|
||||
}
|
||||
|
||||
egw_json_request.prototype.alertFunc = function(_message, _details)
|
||||
{
|
||||
alert(_message);
|
||||
}
|
||||
|
||||
/* Internal function which handles the response from the server */
|
||||
egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRequest)
|
||||
{
|
||||
if (data.response)
|
||||
{
|
||||
var hasResponse = false;
|
||||
for (var i = 0; i < data.response.length; i++)
|
||||
{
|
||||
switch (data.response[i].type)
|
||||
{
|
||||
case 'alert':
|
||||
//Check whether all needed parameters have been passed and call the alertHandler function
|
||||
if ((typeof data.response[i].data.message != 'undefined') &&
|
||||
(typeof data.response[i].data.details != 'undefined'))
|
||||
{
|
||||
this.alertHandler(
|
||||
data.response[i].data.message,
|
||||
data.response[i].data.details)
|
||||
hasResponse = true;
|
||||
}
|
||||
break;
|
||||
case 'assign':
|
||||
//Check whether all needed parameters have been passed and call the alertHandler function
|
||||
if ((typeof data.response[i].data.id != 'undefined') &&
|
||||
(typeof data.response[i].data.key != 'undefined') &&
|
||||
(typeof data.response[i].data.value != 'undefined'))
|
||||
{
|
||||
var obj = document.getElementById(data.response[i].data.id);
|
||||
if (obj)
|
||||
{
|
||||
obj[data.response[i].data.key] = data.response[i].data.value;
|
||||
}
|
||||
hasResponse = true;
|
||||
}
|
||||
break;
|
||||
case 'data':
|
||||
//Callback the caller in order to allow him to handle the data
|
||||
this.callback.call(this.sender, data.response[i].data);
|
||||
hasResponse = true;
|
||||
break;
|
||||
case 'script':
|
||||
if (typeof data.response[i].data == 'string')
|
||||
{
|
||||
eval(data.response[i].data);
|
||||
hasResponse = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* If no explicit response has been specified, call the callback (if one was set) */
|
||||
if (!hasResponse && this.callback)
|
||||
{
|
||||
this.callback.call(this.sender, data.response[i].data);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user