From f4b832a27d128ec84142dfe6133dc6ba96b8d3a9 Mon Sep 17 00:00:00 2001 From: Cesar G Date: Sat, 23 Nov 2013 20:11:49 -0800 Subject: [PATCH 1/3] [ticket/12034] AJAXify notifications popup. PHPBB3-12034 --- phpBB/includes/functions.php | 3 +- phpBB/includes/ucp/ucp_notifications.php | 14 ++++- phpBB/index.php | 48 +++++++++++++----- phpBB/language/en/common.php | 2 + phpBB/phpbb/notification/type/base.php | 6 ++- phpBB/styles/prosilver/template/ajax.js | 44 ++++++++++++++++ .../prosilver/template/overall_header.html | 18 ++++--- phpBB/styles/prosilver/theme/colours.css | 6 +++ phpBB/styles/prosilver/theme/common.css | 1 + .../prosilver/theme/images/icon_mark.gif | Bin 0 -> 360 bytes phpBB/styles/prosilver/theme/links.css | 23 +++++++++ 11 files changed, 141 insertions(+), 24 deletions(-) create mode 100644 phpBB/styles/prosilver/theme/images/icon_mark.gif diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 0d6c7be117..b688902e62 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -5099,7 +5099,7 @@ function page_header($page_title = '', $display_online_list = true, $item_id = 0 } $hidden_fields_for_jumpbox = phpbb_build_hidden_fields_for_query_params($request, array('f')); - + $notification_mark_hash = generate_link_hash('mark_all_notifications_read'); // The following assigns all _common_ variables that may be used at any point in a template. $template->assign_vars(array( @@ -5119,6 +5119,7 @@ function page_header($page_title = '', $display_online_list = true, $item_id = 0 'UNREAD_NOTIFICATIONS_COUNT' => ($notifications !== false) ? $notifications['unread_count'] : '', 'NOTIFICATIONS_COUNT' => ($notifications !== false) ? $notifications['unread_count'] : '', 'U_VIEW_ALL_NOTIFICATIONS' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=ucp_notifications'), + 'U_MARK_ALL_NOTIFICATIONS' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=ucp_notifications&mode=notification_list&mark=all&token=' . $notification_mark_hash), 'U_NOTIFICATION_SETTINGS' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=ucp_notifications&mode=notification_options'), 'S_NOTIFICATIONS_DISPLAY' => $config['load_notifications'], diff --git a/phpBB/includes/ucp/ucp_notifications.php b/phpBB/includes/ucp/ucp_notifications.php index 79055095bc..c5963a1ebc 100644 --- a/phpBB/includes/ucp/ucp_notifications.php +++ b/phpBB/includes/ucp/ucp_notifications.php @@ -98,7 +98,19 @@ class ucp_notifications $phpbb_notifications->mark_notifications_read(false, false, $user->data['user_id'], $form_time); meta_refresh(3, $this->u_action); - $message = $user->lang['NOTIFICATIONS_MARK_ALL_READ_SUCCESS'] . '

' . sprintf($user->lang['RETURN_UCP'], '', ''); + $message = $user->lang['NOTIFICATIONS_MARK_ALL_READ_SUCCESS']; + + if ($request->is_ajax()) + { + $json_response = new \phpbb\json_response(); + $json_response->send(array( + 'MESSAGE_TITLE' => $user->lang['INFORMATION'], + 'MESSAGE_TEXT' => $message, + 'success' => true, + )); + } + $message .= '

' . $user->lang('RETURN_UCP', '', ''); + trigger_error($message); } else diff --git a/phpBB/index.php b/phpBB/index.php index 74fc1b9bda..51cd9064b8 100644 --- a/phpBB/index.php +++ b/phpBB/index.php @@ -27,24 +27,44 @@ $user->setup('viewforum'); // Mark notifications read if (($mark_notification = $request->variable('mark_notification', 0))) { - $phpbb_notifications = $phpbb_container->get('notification_manager'); - - $notification = $phpbb_notifications->load_notifications(array( - 'notification_id' => $mark_notification - )); - - if (isset($notification['notifications'][$mark_notification])) + if ($user->data['user_id'] == ANONYMOUS) { - $notification = $notification['notifications'][$mark_notification]; - - $notification->mark_read(); - - if (($redirect = $request->variable('redirect', ''))) + if ($request->is_ajax()) { - redirect(append_sid($phpbb_root_path . $redirect)); + trigger_error('LOGIN_REQUIRED'); } + login_box('', $user->lang['LOGIN_REQUIRED']); + } - redirect($notification->get_url()); + if (check_link_hash($request->variable('hash', ''), 'mark_notification_read')) + { + $phpbb_notifications = $phpbb_container->get('notification_manager'); + + $notification = $phpbb_notifications->load_notifications(array( + 'notification_id' => $mark_notification + )); + + if (isset($notification['notifications'][$mark_notification])) + { + $notification = $notification['notifications'][$mark_notification]; + + $notification->mark_read(); + + if ($request->is_ajax()) + { + $json_response = new \phpbb\json_response(); + $json_response->send(array( + 'success' => true, + )); + } + + if (($redirect = $request->variable('redirect', ''))) + { + redirect(append_sid($phpbb_root_path . $redirect)); + } + + redirect($notification->get_url()); + } } } diff --git a/phpBB/language/en/common.php b/phpBB/language/en/common.php index 851ffe8ec4..abe930c387 100644 --- a/phpBB/language/en/common.php +++ b/phpBB/language/en/common.php @@ -371,6 +371,7 @@ $lang = array_merge($lang, array( 'LOGIN_VIEWFORUM' => 'The board requires you to be registered and logged in to view this forum.', 'LOGIN_EXPLAIN_EDIT' => 'In order to edit posts in this forum you have to be registered and logged in.', 'LOGIN_EXPLAIN_VIEWONLINE' => 'In order to view the online list you have to be registered and logged in.', + 'LOGIN_REQUIRED' => 'You need to login to perform this action.', 'LOGOUT' => 'Logout', 'LOGOUT_USER' => 'Logout [ %s ]', 'LOG_ME_IN' => 'Remember me', @@ -378,6 +379,7 @@ $lang = array_merge($lang, array( 'MAIN' => 'Main', 'MARK' => 'Mark', 'MARK_ALL' => 'Mark all', + 'MARK_ALL_READ' => 'Mark all read', 'MARK_FORUMS_READ' => 'Mark forums read', 'MARK_READ' => 'Mark read', 'MARK_SUBFORUMS_READ' => 'Mark subforums read', diff --git a/phpBB/phpbb/notification/type/base.php b/phpBB/phpbb/notification/type/base.php index 951585853f..10c876b286 100644 --- a/phpBB/phpbb/notification/type/base.php +++ b/phpBB/phpbb/notification/type/base.php @@ -282,15 +282,17 @@ abstract class base implements \phpbb\notification\type\type_interface */ public function prepare_for_display() { + $mark_hash = generate_link_hash('mark_notification_read'); + if ($this->get_url()) { - $u_mark_read = append_sid($this->phpbb_root_path . 'index.' . $this->php_ext, 'mark_notification=' . $this->notification_id); + $u_mark_read = append_sid($this->phpbb_root_path . 'index.' . $this->php_ext, 'mark_notification=' . $this->notification_id . '&hash=' . $mark_hash); } else { $redirect = (($this->user->page['page_dir']) ? $this->user->page['page_dir'] . '/' : '') . $this->user->page['page_name'] . (($this->user->page['query_string']) ? '?' . $this->user->page['query_string'] : ''); - $u_mark_read = append_sid($this->phpbb_root_path . 'index.' . $this->php_ext, 'mark_notification=' . $this->notification_id . '&redirect=' . urlencode($redirect)); + $u_mark_read = append_sid($this->phpbb_root_path . 'index.' . $this->php_ext, 'mark_notification=' . $this->notification_id . '&hash=' . $mark_hash . '&redirect=' . urlencode($redirect)); } return array( diff --git a/phpBB/styles/prosilver/template/ajax.js b/phpBB/styles/prosilver/template/ajax.js index 28656d47d3..38f9f80b44 100644 --- a/phpBB/styles/prosilver/template/ajax.js +++ b/phpBB/styles/prosilver/template/ajax.js @@ -106,6 +106,50 @@ phpbb.addAjaxCallback('mark_topics_read', function(res, update_topic_links) { phpbb.closeDarkenWrapper(3000); }); +// This callback will mark all notifications read +phpbb.addAjaxCallback('notification.mark_all_read', function(res) { + if (typeof res.success !== 'undefined') { + var unreadRows = $('#notification_list li.bg2'); + + // Remove the unread status. + unreadRows.removeClass('bg2'); + unreadRows.find('a.mark_read').remove(); + + // Update the notification link to the real URL. + unreadRows.each(function() { + var link = $(this).find('a'); + link.attr('href', link.attr('data-real-url')); + }); + // Set the unread count to 0. + $('#notification_list_button strong').html(0); + // Remove the Mark all read link + $('#mark_all_notifications').remove(); + + phpbb.closeDarkenWrapper(3000); + } +}); + +// This callback will mark a notification read +phpbb.addAjaxCallback('notification.mark_read', function(res) { + if (typeof res.success !== 'undefined') { + var el = $(this), + unreadCountEl = $('#notification_list_button strong'), + unreadCount = Number(unreadCountEl.html()), + link = el.siblings('a'); + + // Remove the unread status. + el.parent('li.bg2').removeClass('bg2'); + el.remove(); + link.attr('href', link.attr('data-real-url')); + // Update the unread count + unreadCountEl.html(--unreadCount); + // Remove the Mark all read link if there are no unread notifications. + if (!unreadCount) { + $('#mark_all_notifications').remove(); + } + } +}); + // This callback finds the post from the delete link, and removes it. phpbb.addAjaxCallback('post_delete', function() { var el = $(this), diff --git a/phpBB/styles/prosilver/template/overall_header.html b/phpBB/styles/prosilver/template/overall_header.html index e4e1b3443b..529e6625fe 100644 --- a/phpBB/styles/prosilver/template/overall_header.html +++ b/phpBB/styles/prosilver/template/overall_header.html @@ -107,7 +107,12 @@