From d420ceb9c717b83ba29dde3734b563881051e51a Mon Sep 17 00:00:00 2001 From: Callum Macrae Date: Thu, 14 Jul 2011 13:33:42 +0100 Subject: [PATCH] [ticket/10270] Added JavaScript popups and basic AJAX functionality to PHP. This commit adds the phpbb object (JavaScript), and alert and confirm box methods. It also adds the first basic AJAX functionality, to deleting posts in viewtopic. PHPBB3-10270 --- phpBB/includes/functions.php | 53 ++++++++++-- .../prosilver/template/overall_footer.html | 3 + phpBB/styles/prosilver/theme/common.css | 30 ++++++- phpBB/styles/script.js | 80 +++++++++++++++++++ 4 files changed, 158 insertions(+), 8 deletions(-) create mode 100644 phpBB/styles/script.js diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 7a96dd3609..572986bb4b 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2448,15 +2448,25 @@ function build_url($strip_vars = false) */ function meta_refresh($time, $url, $disable_cd_check = false) { - global $template; + global $template, $refresh_data; - $url = redirect($url, true, $disable_cd_check); - $url = str_replace('&', '&', $url); + if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') + { + $refresh_data = array( + 'time' => $time, + 'url' => str_replace('&', '&', $url) + ); + } + else + { + $url = redirect($url, true, $disable_cd_check); + $url = str_replace('&', '&', $url); - // For XHTML compatibility we change back & to & - $template->assign_vars(array( - 'META' => '') - ); + // For XHTML compatibility we change back & to & + $template->assign_vars(array( + 'META' => '') + ); + } return $url; } @@ -2699,6 +2709,21 @@ function confirm_box($check, $title = '', $hidden = '', $html_body = 'confirm_bo WHERE user_id = " . $user->data['user_id']; $db->sql_query($sql); + + if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') + { + $u_action .= '&confirm_uid=' . $user->data['user_id'] . '&sess=' . $user->session_id . '&sid=' . $user->session_id; + echo json_encode(array( + 'MESSAGE_TITLE' => (!isset($user->lang[$title])) ? $user->lang['CONFIRM'] : $user->lang[$title], + 'MESSAGE_TEXT' => (!isset($user->lang[$title . '_CONFIRM'])) ? $title : $user->lang[$title . '_CONFIRM'], + + 'YES_VALUE' => $user->lang['YES'], + 'S_CONFIRM_ACTION' => str_replace('&', '&', $u_action), //inefficient, rewrite whole function + 'S_HIDDEN_FIELDS' => $hidden . $s_hidden_fields + )); + exit; + } + if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin']) { adm_page_footer(); @@ -3922,6 +3947,20 @@ function msg_handler($errno, $msg_text, $errfile, $errline) 'S_USER_NOTICE' => ($errno == E_USER_NOTICE) ? true : false) ); + if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') + { + global $refresh_data; + + echo json_encode(array( + 'MESSAGE_TITLE' => $msg_title, + 'MESSAGE_TEXT' => $msg_text, + 'S_USER_WARNING' => ($errno == E_USER_WARNING) ? true : false, + 'S_USER_NOTICE' => ($errno == E_USER_NOTICE) ? true : false, + 'REFRESH_DATA' => (!empty($refresh_data)) ? $refresh_data : null + )); + exit; + } + // We do not want the cron script to be called on error messages define('IN_CRON', true); diff --git a/phpBB/styles/prosilver/template/overall_footer.html b/phpBB/styles/prosilver/template/overall_footer.html index 4456d6b37d..0d2fd4d27a 100644 --- a/phpBB/styles/prosilver/template/overall_footer.html +++ b/phpBB/styles/prosilver/template/overall_footer.html @@ -24,6 +24,9 @@
{DEBUG_OUTPUT}
{L_ACP} + + + diff --git a/phpBB/styles/prosilver/theme/common.css b/phpBB/styles/prosilver/theme/common.css index 27a55caf7a..5cf12be1ce 100644 --- a/phpBB/styles/prosilver/theme/common.css +++ b/phpBB/styles/prosilver/theme/common.css @@ -468,7 +468,7 @@ table.info tbody th { /* Misc layout styles ---------------------------------------- */ -/* column[1-2] styles are containers for two column layouts +/* column[1-2] styles are containers for two column layouts Also see tweaks.css */ .column1 { float: left; @@ -580,6 +580,34 @@ li.pagination { background: none 0 50% no-repeat; } +.row .pagination span a, li.pagination span a { + background-color: #FFFFFF; +} + +.row .pagination span a:hover, li.pagination span a:hover { + background-color: #d2d2d2; +} + +/* jQuery popups +---------------------------------------- */ +.jalert { + background-color: #FFFFFF; + border: 1px solid #999999; + display: none; + position: fixed; + top: 100px; + left: 35%; + width: 30%; + z-index: 50; + padding: 25px; + padding: 0 25px 20px 25px; +} + +.jalert p { + margin: 8px 0; + padding-bottom: 8px; +} + /* Miscellaneous styles ---------------------------------------- */ #forum-permissions { diff --git a/phpBB/styles/script.js b/phpBB/styles/script.js new file mode 100644 index 0000000000..9be3efd4ce --- /dev/null +++ b/phpBB/styles/script.js @@ -0,0 +1,80 @@ +var phpbb = {}; + +/** + * Display a simple alert. + * + * @param string title Title of the message, eg "Information" + * @param string msg Message to display. Can be HTML. + */ +phpbb.alert = function(title, msg) { + var div = $('

' + title + '

' + msg + '

'); + + $(document).bind('click', function(e) { + if ($(e.target).parents('.jalert').length) + { + return true; + } + div.hide(300, function() { + div.remove(); + }); + return false; + }); + + $('body').append(div); + div.show(300); +} + +/** + * Display a simple yes / no box to the user. + * + * @param string msg Message to display. Can be HTML. + * @param function callback Callback. + */ +phpbb.confirm = function(msg, callback) { + var div = $('

' + msg + '

\ +  \ +
'); + + $('body').append(div); + + $('.jalertbut').bind('click', function(event) { + div.hide(300, function() { + div.remove(); + }); + callback(this.value === 'Yes'); + return false; + }); + div.show(300); +} + + + +$('.delete-icon a').click(function() +{ + var pid = this.href.split('&p=')[1]; + var __self = this; + $.get(this.href, function(res) { + res = JSON.parse(res); + phpbb.confirm(res.MESSAGE_TEXT, function(del) { + if (del) + { + var p = res.S_CONFIRM_ACTION.split('?'); + p[1] += '&confirm=Yes' + $.post(p[0], p[1], function(res) { + res = JSON.parse(res); + phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT) + $(__self).parents('div #p' + pid).remove(); + + //if there is a refresh, check that it isn't to the same place + if (res.REFRESH_DATA && res.REFRESH_DATA.url.indexOf('t=') === -1) + { + setTimeout(function() { + window.location = res.REFRESH_DATA.url; + }, res.REFRESH_DATA.time * 1000); + } + }); + } + }); + }); + return false; +});