From 490b74068287c4240bf1e5def51968fcddd33f30 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 11 Jul 2012 19:24:28 +0000 Subject: [PATCH] - web: improve forum pagination svn path=/trunk/boinc/; revision=25863 --- checkin_notes | 13 +++ client/client_msgs.cpp | 7 +- html/inc/forum.inc | 171 ++++++++++++++++++++----------------- html/user/forum_forum.php | 17 ++-- html/user/forum_reply.php | 8 +- html/user/forum_thread.php | 12 +-- 6 files changed, 127 insertions(+), 101 deletions(-) diff --git a/checkin_notes b/checkin_notes index 09e43efc62..b939b54ac7 100644 --- a/checkin_notes +++ b/checkin_notes @@ -4854,3 +4854,16 @@ Charlie 11 July 2012 project.pbxproj wrapper/ BuildMacWrapper.sh + +David 11 July 2012 + - web: improve forum pagination + + client/ + client_msgs.cpp + html/ + inc/ + forum.inc + user/ + forum_forum.php + forum_reply.php + forum_threads.php diff --git a/client/client_msgs.cpp b/client/client_msgs.cpp index dd9328dcbb..5f1b0ac9ea 100644 --- a/client/client_msgs.cpp +++ b/client/client_msgs.cpp @@ -163,16 +163,15 @@ void msg_printf_notice(PROJ_AM *p, bool is_html, const char* link, const char *f // add to cache, and delete old messages if cache too big. // If high priority, create a notice. // -void MESSAGE_DESCS::insert( - PROJ_AM* p, int priority, int now, char* message -) { +void MESSAGE_DESCS::insert(PROJ_AM* p, int priority, int now, char* message) { MESSAGE_DESC* mdp = new MESSAGE_DESC; static int seqno = 1; - strcpy(mdp->project_name, ""); if (p) { strlcpy( mdp->project_name, p->get_project_name(), sizeof(mdp->project_name) ); + } else { + strcpy(mdp->project_name, ""); } mdp->priority = (priority==MSG_SCHEDULER_ALERT)?MSG_USER_ALERT:priority; mdp->timestamp = now; diff --git a/html/inc/forum.inc b/html/inc/forum.inc index 396b587293..583cd53401 100644 --- a/html/inc/forum.inc +++ b/html/inc/forum.inc @@ -220,54 +220,64 @@ function start_forum_table($headings, $extra=null) { echo "\n"; } -// return a string containing a paged navigation bar -// for the given forum. The default start place is 0. -// -function show_page_nav($forum, $sort, $start=0){ - // How many pages to potentially show before and after this one: - $preshow = 5; $postshow = 10; - $navbar = ""; - - if ($forum->threads > THREADS_PER_PAGE) { - $total = ceil($forum->threads / THREADS_PER_PAGE); - $curpage = ceil($start / THREADS_PER_PAGE); - // If this is not the first page, display "previous" - if ($curpage > 0){ - $navbar = ' <-- '.tra("Previous")." "; - } - - // Display a list of pages surrounding this one - for ($i=$curpage-$preshow;$i<($curpage+$postshow);$i++){ - if ($i<1) continue; - if ($i>$total) break; - // If this is the current page, emphasize it. - if ($i == $curpage+1) { - $navbar .= "$i"; - } else { - $navbar.=''.$i.''; - } - $navbar .= " | "; - } - - // If there is a next page - if ($curpage+1 < $total){ - $navbar.= ' '.tra("Next")." -->"; - } - - } - return $navbar; +function page_link($url, $page_num, $items_per_page, $text) { + return " $text "; } -function thread_last_visit($user, $thread) { - if (!$user) return false; - $log = BoincForumLogging::lookup($user->id, $thread->id); - return $log->timestamp; +// return a string for navigating pages +// +function page_links($url, $nitems, $items_per_page, $start){ + // How many pages to potentially show before and after this one: + $preshow = 3; + $postshow = 3; + + $x = ""; + + if ($nitems <= $items_per_page) return ""; + $npages = ceil($nitems / $items_per_page); + $curpage = ceil($start / $items_per_page); + + // If this is not the first page, display "previous" + // + if ($curpage > 0){ + $x .= page_link( + $url, $curpage-1, $items_per_page, + tra("Previous")." · " + ); + } + + if ($curpage - $preshow > 0) { + $x .= page_link($url, 0, $items_per_page, "1"); + $x .= " . . . "; + } + // Display a list of pages surrounding this one + // + for ($i=$curpage-$preshow; $i<=$curpage+$postshow; $i++){ + $page_str = (string)($i+1); + if ($i < 0) continue; + if ($i >= $npages) break; + + if ($i == $curpage) { + $x .= "$page_str"; + } else { + $x .= page_link($url, $i, $items_per_page, $page_str); + } + $x .= " · "; + } + + if ($curpage + $postshow < $npages-1) { + $x .= " . . . "; + $x .= page_link($url, $npages-1, $items_per_page, $npages); + } + // If there is a next page + // + if ($curpage < $npages-1){ + $x .= page_link( + $url, $curpage+1, $items_per_page, + " · ".tra("Next") + ); + } + return $x; } function thread_is_unread($user, $thread) { @@ -297,12 +307,11 @@ function can_reply($thread, $forum, $user) { // Show the posts in a thread for a user. // function show_posts( - $thread, $forum, $offset, $sort_style, $filter, $logged_in_user, + $thread, $forum, $start, $sort_style, $filter, $logged_in_user, $show_controls=true ) { $n = 1; $first_unread_post = null; - $last_visit = 0; if ($show_controls) { $controls = FORUM_CONTROLS; @@ -315,66 +324,66 @@ function show_posts( $num_to_show = $logged_in_user->prefs->display_wrap_postcount; } - // If logged in user is moderator, - // let him see all posts - including hidden ones + // let moderators see all posts, including hidden ones // if (is_moderator($logged_in_user, $forum)) { $show_hidden = true; } else { $show_hidden = false; } + $posts = get_thread_posts($thread->id, $sort_style, $show_hidden); $postcount = (sizeof($posts)-1); + + $latest_viewed = 0; if ($logged_in_user) { - $last_visit = thread_last_visit($logged_in_user, $thread); + $log = BoincForumLogging::lookup($logged_in_user->id, $thread->id); + if ($log) { + $latest_viewed = $log->timestamp; + } } - if ($offset) { - $new_off = $offset - $num_to_show; - if ($new_off < 0) $new_off = 0; - echo ""; - show_button( - "forum_thread.php?id=$thread->id&offset=$new_off&sort_style=$sort_style", - "Previous $num_to_show posts", - "Show previous $num_to_show posts" - ); - echo "\n"; - } - $show_next_link = false; + $page_nav = page_links( + "forum_thread.php?id=$thread->id&sort_style=$sort_style", + sizeof($posts), + $num_to_show, + $start + ); + + echo $page_nav; + $num_shown = 0; $num_skipped = 0; + $headings = array(array(tra("Author"),"authorcol"), array(tra("Message"),"")); + start_forum_table($headings, "id=\"thread\" cellspacing=0"); + + $latest_post_shown = 0; foreach ($posts as $post){ - if ($num_skipped < $offset) { + if ($num_skipped < $start) { $num_skipped++; continue; } if ($num_shown == $num_to_show) { - $show_next_link = true; break; } show_post( - $post, $thread, $forum, $logged_in_user, $last_visit, $n, + $post, $thread, $forum, $logged_in_user, $latest_viewed, $n, $controls, $filter ); $n = ($n+1)%2; - if (($post->timestamp>$last_visit) && + if (($post->timestamp>$latest_viewed) && ((!$first_unread_post) || ($post->timestamp<$first_unread_post->timestamp)) ){ $first_unread_post = $post; } + if ($post->timestamp > $latest_post_shown) { + $latest_post_shown = $post->timestamp; + } $num_shown++; } - if ($show_next_link) { - $new_off = $offset + $num_to_show; - echo ""; - show_button( - "forum_thread.php?id=$thread->id&offset=$new_off&sort_style=$sort_style", - "Next $num_to_show posts", - "Show next $num_to_show posts" - ); - echo "\n"; - } + end_table(); + echo $page_nav; if ($logged_in_user && $logged_in_user->prefs->jump_to_unread){ if ($first_unread_post){ @@ -385,7 +394,9 @@ function show_posts( } if ($logged_in_user) { - BoincForumLogging::replace($logged_in_user->id, $thread->id, time()); + BoincForumLogging::replace( + $logged_in_user->id, $thread->id, $latest_post_shown + ); } } @@ -424,7 +435,7 @@ function is_ignoring($user, $other_user) { // Display an individual post // function show_post( - $post, $thread, $forum, $logged_in_user, $last_visit, $n, + $post, $thread, $forum, $logged_in_user, $latest_viewed, $n, $controls=FORUM_CONTROLS, $filter=true ) { global $country_to_iso3166_2; @@ -554,7 +565,7 @@ function show_post( echo "
id, "\" method=\"post\">"; } - if ($logged_in_user && $post->timestamp>$last_visit){ + if ($logged_in_user && $post->timestamp>$latest_viewed){ show_image(NEW_IMAGE, tra("You haven't read this message yet"), tra("Unread"), NEW_IMAGE_HEIGHT); } diff --git a/html/user/forum_forum.php b/html/user/forum_forum.php index 1012e47456..79c3bc346e 100644 --- a/html/user/forum_forum.php +++ b/html/user/forum_forum.php @@ -123,12 +123,16 @@ page_tail(); // and using the features for the logged in user in $user. // function show_forum($forum, $start, $sort_style, $user) { - $gotoStr = ""; - $nav = show_page_nav($forum, $sort_style, $start); - if ($nav) { - $gotoStr = "
$nav

"; + $page_nav = page_links( + "forum_forum.php?id=$forum->id&sort=$sort_style", + sizeof($forum->threads), + THREADS_PER_PAGE, + start + ); + if ($page_nav) { + $page_nav = "
$page_nav

"; } - echo $gotoStr; // Display the navbar + echo $page_nav; start_forum_table(array( "", tra("Threads"), @@ -153,6 +157,7 @@ function show_forum($forum, $start, $sort_style, $user) { } // Run through the list of threads, displaying each of them + // $n = 0; $i=0; foreach ($threads as $thread) { $owner = BoincUser::lookup_id($thread->owner); @@ -219,7 +224,7 @@ function show_forum($forum, $start, $sort_style, $user) { flush(); } end_table(); - echo "
$gotoStr"; // show page links + echo "
$page_nav"; // show page links } ?> diff --git a/html/user/forum_reply.php b/html/user/forum_reply.php index 769b698463..353397e522 100644 --- a/html/user/forum_reply.php +++ b/html/user/forum_reply.php @@ -112,9 +112,11 @@ if ($preview == tra("Preview")) { start_forum_table(array(tra("Author"), tra("Message"))); show_message_row($thread, $parent_post); -show_posts( - $thread, $forum, 0, $sort_style, $filter, $logged_in_user, true -); +if ($parent_post) { + show_post( + $parent_post, $thread, $forum, $logged_in_user, 0, 0, false, false + ); +} end_table(); page_tail(); diff --git a/html/user/forum_thread.php b/html/user/forum_thread.php index e74062a2f1..0c004b327f 100644 --- a/html/user/forum_thread.php +++ b/html/user/forum_thread.php @@ -25,8 +25,8 @@ require_once('../inc/news.inc'); $threadid = get_int('id'); $sort_style = get_int('sort', true); -$offset = get_int('offset', true); -if (!$offset) $offset = 0; +$start = get_int('start', true); +if (!$start) $start = 0; $filter = get_str('filter', true); if ($filter != "false"){ @@ -258,17 +258,13 @@ echo "
"; -// Here is where the actual thread begins. -$headings = array(array(tra("Author"),"authorcol"), array(tra("Message"),"")); - -start_forum_table($headings, "id=\"thread\" cellspacing=0"); show_posts( - $thread, $forum, $offset, $sort_style, $filter, + $thread, $forum, $start, $sort_style, $filter, $logged_in_user, true ); -end_table(); if ($reply_url) { + echo "
"; show_button( $reply_url, tra("Post to thread"),