[ticket/10328] Renamed the JSON class, also now using autoloading.

It is no longer static, and uses autoloading. It has also been renamed
from JSON to phpbb_json_response.

PHPBB3-10328
This commit is contained in:
Callum Macrae 2011-08-24 12:25:54 +01:00 committed by Igor Wiedler
parent c92b30d66c
commit 7a933bdb5a
6 changed files with 28 additions and 31 deletions

View file

@ -74,7 +74,6 @@ if (!empty($load_extensions) && function_exists('dl'))
// Include files // Include files
require($phpbb_root_path . 'includes/class_loader.' . $phpEx); require($phpbb_root_path . 'includes/class_loader.' . $phpEx);
require($phpbb_root_path . 'includes/json.' . $phpEx);
require($phpbb_root_path . 'includes/session.' . $phpEx); require($phpbb_root_path . 'includes/session.' . $phpEx);
require($phpbb_root_path . 'includes/auth.' . $phpEx); require($phpbb_root_path . 'includes/auth.' . $phpEx);

View file

@ -258,7 +258,8 @@ class acp_forums
if ($request->is_ajax()) if ($request->is_ajax())
{ {
JSON::send(array('success' => ($move_forum_name !== false))); $json_response = new phpbb_json_response;
$json_response->send(array('success' => ($move_forum_name !== false)));
} }
break; break;

View file

@ -2713,7 +2713,8 @@ function confirm_box($check, $title = '', $hidden = '', $html_body = 'confirm_bo
if ($request->is_ajax()) if ($request->is_ajax())
{ {
$u_action .= '&confirm_uid=' . $user->data['user_id'] . '&sess=' . $user->session_id . '&sid=' . $user->session_id; $u_action .= '&confirm_uid=' . $user->data['user_id'] . '&sess=' . $user->session_id . '&sid=' . $user->session_id;
JSON::send(array( $json_response = new phpbb_json_response;
$json_response->send(array(
'MESSAGE_TITLE' => (!isset($user->lang[$title])) ? $user->lang['CONFIRM'] : $user->lang[$title], 'MESSAGE_TITLE' => (!isset($user->lang[$title])) ? $user->lang['CONFIRM'] : $user->lang[$title],
'MESSAGE_TEXT' => (!isset($user->lang[$title . '_CONFIRM'])) ? $title : $user->lang[$title . '_CONFIRM'], 'MESSAGE_TEXT' => (!isset($user->lang[$title . '_CONFIRM'])) ? $title : $user->lang[$title . '_CONFIRM'],
@ -3950,7 +3951,8 @@ function msg_handler($errno, $msg_text, $errfile, $errline)
{ {
global $refresh_data; global $refresh_data;
JSON::send(array( $json_response = new phpbb_json_response;
$json_response->send(array(
'MESSAGE_TITLE' => $msg_title, 'MESSAGE_TITLE' => $msg_title,
'MESSAGE_TEXT' => $msg_text, 'MESSAGE_TEXT' => $msg_text,
'S_USER_WARNING' => ($errno == E_USER_WARNING) ? true : false, 'S_USER_WARNING' => ($errno == E_USER_WARNING) ? true : false,

View file

@ -20,25 +20,18 @@ if (!defined('IN_PHPBB'))
* JSON class * JSON class
* @package phpBB3 * @package phpBB3
*/ */
class JSON class phpbb_json_response
{ {
private static $data = array();
/** /**
* Send the data to the client and exit the script. * Send the data to the client and exit the script.
* *
* @param array $data Any additional data to send. * @param array $data Any additional data to send.
* @param bool $exit Will exit the script if true. * @param bool $exit Will exit the script if true.
*/ */
public static function send($data = false, $exit = true) public function send($data, $exit = true)
{ {
if ($data)
{
self::add($data);
}
header('Content-type: application/json'); header('Content-type: application/json');
echo json_encode(self::$data); echo json_encode($data);
if ($exit) if ($exit)
{ {
@ -46,14 +39,4 @@ class JSON
exit_handler(); exit_handler();
} }
} }
/**
* Saves some data to be written when JSON::send() is called.
*
* @param array $data Data to save to be sent.
*/
public static function add($data)
{
self::$data = array_merge(self::$data, $data);
}
} }

View file

@ -25,7 +25,7 @@ class ucp_zebra
function main($id, $mode) function main($id, $mode)
{ {
global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx; global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx, $request;
$submit = (isset($_POST['submit']) || isset($_GET['add']) || isset($_GET['remove'])) ? true : false; $submit = (isset($_POST['submit']) || isset($_GET['add']) || isset($_GET['remove'])) ? true : false;
$s_hidden_fields = ''; $s_hidden_fields = '';
@ -198,13 +198,25 @@ class ucp_zebra
} }
} }
} }
if ($updated) if ($request->is_ajax())
{ {
JSON::add(array( $message = ($updated) ? $user->lang[$l_mode . '_UPDATED'] : implode('<br />', $error);
'message' => $user->lang[$l_mode . '_UPDATED'],
'success' => true $json_response = new phpbb_json_response;
$json_response->send(array(
'success' => $updated,
'MESSAGE_TITLE' => $user->lang['INFORMATION'],
'MESSAGE_TEXT' => $message,
'REFRESH_DATA' => array(
'time' => 3,
'url' => $this->u_action
)
)); ));
}
else if ($updated)
{
meta_refresh(3, $this->u_action); meta_refresh(3, $this->u_action);
$message = $user->lang[$l_mode . '_UPDATED'] . '<br />' . implode('<br />', $error) . ((sizeof($error)) ? '<br />' : '') . '<br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $this->u_action . '">', '</a>'); $message = $user->lang[$l_mode . '_UPDATED'] . '<br />' . implode('<br />', $error) . ((sizeof($error)) ? '<br />' : '') . '<br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $this->u_action . '">', '</a>');
trigger_error($message); trigger_error($message);

View file

@ -365,7 +365,7 @@ phpbb.add_ajax_callback('post_delete', function(el) {
} }
}).add_ajax_callback('zebra', function(el, res) { }).add_ajax_callback('zebra', function(el, res) {
if (res.success) { if (res.success) {
$('.zebra').html(res.message); $('.zebra').html(res.MESSAGE_TEXT);
$($('.zebra').get(1)).remove(); $($('.zebra').get(1)).remove();
} }
});; });;