Merge pull request #266 from pachuco/master

Comment extension fixes
This commit is contained in:
Shish 2012-11-24 11:46:43 -08:00
commit acbbad2f80
2 changed files with 34 additions and 15 deletions

View file

@ -178,13 +178,16 @@ class CommentList extends Extension {
$this->build_page($page_num); $this->build_page($page_num);
} }
else if($event->get_arg(0) === "beta-search") { else if($event->get_arg(0) === "beta-search") {
$i_comment_count = Comment::count_comments_by_user($user);
$com_per_page = 50;
$total_pages = ceil($i_comment_count/$com_per_page);
$search = $event->get_arg(1); $search = $event->get_arg(1);
$page_num = int_escape($event->get_arg(2)); $page_num = int_escape($event->get_arg(2));
$page_num = $this->sanity_check_pagenumber($page_num, $total_pages);
$duser = User::by_name($search); $duser = User::by_name($search);
$comments = $this->get_user_comments($duser->id, 50, ($page_num-1) * 50); $comments = $this->get_user_comments($duser->id, $com_per_page, ($page_num-1) * $com_per_page);
$this->theme->display_all_user_comments($comments, $page_num, 10); $this->theme->display_all_user_comments($comments, $page_num, $total_pages);
} }
} }
} }
@ -289,11 +292,11 @@ class CommentList extends Extension {
if(class_exists("Ratings")) { if(class_exists("Ratings")) {
$user_ratings = Ratings::get_user_privs($user); $user_ratings = Ratings::get_user_privs($user);
} }
$total_pages = $database->cache->get("comment_pages");
if(is_null($current_page) || $current_page <= 0) { if(is_null($current_page) || $current_page <= 0) {
$current_page = 1; $current_page = 1;
} }
$current_page = $this->sanity_check_pagenumber($current_page, $total_pages);
$threads_per_page = 10; $threads_per_page = 10;
$start = $threads_per_page * ($current_page - 1); $start = $threads_per_page * ($current_page - 1);
@ -307,7 +310,6 @@ class CommentList extends Extension {
"; ";
$result = $database->Execute($get_threads, array("limit"=>$threads_per_page, "offset"=>$start)); $result = $database->Execute($get_threads, array("limit"=>$threads_per_page, "offset"=>$start));
$total_pages = $database->cache->get("comment_pages");
if(empty($total_pages)) { if(empty($total_pages)) {
$total_pages = (int)($database->get_one("SELECT COUNT(c1) FROM (SELECT COUNT(image_id) AS c1 FROM comments $where GROUP BY image_id) AS s1") / 10); $total_pages = (int)($database->get_one("SELECT COUNT(c1) FROM (SELECT COUNT(image_id) AS c1 FROM comments $where GROUP BY image_id) AS s1") / 10);
$database->cache->set("comment_pages", $total_pages, 600); $database->cache->set("comment_pages", $total_pages, 600);
@ -474,7 +476,19 @@ class CommentList extends Extension {
global $database; global $database;
return ($database->get_row("SELECT * FROM comments WHERE image_id=:image_id AND comment=:comment", array("image_id"=>$image_id, "comment"=>$comment))); return ($database->get_row("SELECT * FROM comments WHERE image_id=:image_id AND comment=:comment", array("image_id"=>$image_id, "comment"=>$comment)));
} }
// do some checks
private function sanity_check_pagenumber($pagenum, $maxpage){
if (!is_numeric($pagenum)){
$pagenum=1;
}
if ($pagenum>$maxpage){
$pagenum=$maxpage;
}
if ($pagenum<=0){
$pagenum=1;
}
return $pagenum;
}
private function add_comment_wrapper(/*int*/ $image_id, User $user, /*string*/ $comment) { private function add_comment_wrapper(/*int*/ $image_id, User $user, /*string*/ $comment) {
global $database; global $database;
global $config; global $config;

View file

@ -36,7 +36,7 @@ class CommentListTheme extends Themelet {
$h_prev = ($page_number <= 1) ? "Prev" : $h_prev = ($page_number <= 1) ? "Prev" :
'<a href="'.make_link('comment/list/'.$prev).'">Prev</a>'; '<a href="'.make_link('comment/list/'.$prev).'">Prev</a>';
$h_index = "<a href='".make_link("comment/list")."'>Index</a>"; $h_index = "<a href='".make_link("post/list")."'>Index</a>";
$h_next = ($page_number >= $total_pages) ? "Next" : $h_next = ($page_number >= $total_pages) ? "Next" :
'<a href="'.make_link('comment/list/'.$next).'">Next</a>'; '<a href="'.make_link('comment/list/'.$next).'">Next</a>';
@ -171,7 +171,11 @@ class CommentListTheme extends Themelet {
} }
public function display_all_user_comments($comments, $page_number, $total_pages) { public function display_all_user_comments($comments, $page_number, $total_pages) {
global $page; global $page, $user;
assert(is_numeric($page_number));
assert(is_numeric($total_pages));
$html = ""; $html = "";
foreach($comments as $comment) { foreach($comments as $comment) {
$html .= $this->comment_to_html($comment, true); $html .= $this->comment_to_html($comment, true);
@ -185,18 +189,19 @@ class CommentListTheme extends Themelet {
$prev = $page_number - 1; $prev = $page_number - 1;
$next = $page_number + 1; $next = $page_number + 1;
$u_tags = url_escape(implode(" ", $search_terms)); //$search_terms = array('I','have','no','idea','what','this','does!');
$query = empty($u_tags) ? "" : '/'.$u_tags; //$u_tags = url_escape(implode(" ", $search_terms));
//$query = empty($u_tags) ? "" : '/'.$u_tags;
$h_prev = ($page_number <= 1) ? "Prev" : "<a href='$prev'>Prev</a>"; $h_prev = ($page_number <= 1) ? "Prev" : "<a href='$prev'>Prev</a>";
$h_index = "<a href='".make_link("post/list")."'>Index</a>";
$h_next = ($page_number >= $total_pages) ? "Next" : "<a href='$next'>Next</a>"; $h_next = ($page_number >= $total_pages) ? "Next" : "<a href='$next'>Next</a>";
$page->add_block(new Block("Navigation", $h_prev.' | '.$h_next, "left", 0)); $page->add_block(new Block("Navigation", $h_prev.' | '.$h_index.' | '.$h_next, "left", 0));
$this->display_paginator($page, 'comment/beta-search/'.$user->name, null, $page_number, $total_pages);
} }
protected function comment_to_html($comment, $trim=false) { protected function comment_to_html($comment, $trim=false) {
global $config, $user; global $config, $user;