send direct output of json / etemplate callback back to client via addGeneric('output',...) or alert, in case there's already some JSON response, make sure content-type header is only send once

This commit is contained in:
Ralf Becker 2012-03-06 07:32:51 +00:00
parent 68d8b02525
commit 1fef159630

View File

@ -1,6 +1,6 @@
<?php <?php
/** /**
* eGroupWare API: JSON - Contains functions and classes for doing JSON requests. * EGroupware API: JSON - Contains functions and classes for doing JSON requests.
* *
* @link http://www.egroupware.org * @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
@ -193,6 +193,7 @@ class egw_json_response
if (!isset(self::$response)) if (!isset(self::$response))
{ {
self::$response = new egw_json_response(); self::$response = new egw_json_response();
self::sendHeader();
} }
return self::$response; return self::$response;
} }
@ -202,11 +203,26 @@ class egw_json_response
return isset(self::$response); return isset(self::$response);
} }
/**
* Do we have a JSON response to send back
*
* @return boolean
*/
public function haveJSONResponse()
{
return (boolean) $this->responseArray;
}
/** /**
* Private function used to send the HTTP header of the JSON response * Private function used to send the HTTP header of the JSON response
*/ */
private function sendHeader() private function sendHeader()
{ {
if (headers_sent($file, $line))
{
error_log(__METHOD__."() header already sent by $file line $line: ".function_backtrace());
}
else
//Send the character encoding header //Send the character encoding header
header('content-type: application/json; charset='.translation::charset()); header('content-type: application/json; charset='.translation::charset());
} }
@ -220,9 +236,25 @@ class egw_json_response
//Call each attached before send data proc //Call each attached before send data proc
foreach ($inst->beforeSendDataProcs as $proc) foreach ($inst->beforeSendDataProcs as $proc)
{
call_user_func_array($proc['proc'], $proc['params']); call_user_func_array($proc['proc'], $proc['params']);
}
$inst->sendHeader(); //$inst->sendHeader();
// check if application made some direct output
if (($output = ob_get_clean()))
{
if (!$inst->haveJSONResponse())
{
error_log(__METHOD__."() adding output with inst->addGeneric('output', '$output')");
$inst->addGeneric('output', $output);
}
else
{
$inst->alert('Application echoed something', $output);
}
}
echo $inst->getJSON(); echo $inst->getJSON();
} }