From 9cebef5a500cb54e686703d455a048e6cf5720a5 Mon Sep 17 00:00:00 2001 From: pachuco Date: Tue, 25 Sep 2012 20:33:56 +0300 Subject: [PATCH 1/4] *Inexistent threads are no longer accesible. *Pages outside index cut to last one(thread index and threads). *Return to last page in thread after posting reply. --- ext/forum/main.php | 112 +++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 49 deletions(-) diff --git a/ext/forum/main.php b/ext/forum/main.php index 9cd774aa..c4ecf120 100644 --- a/ext/forum/main.php +++ b/ext/forum/main.php @@ -7,7 +7,14 @@ * Description: Rough forum extension * Documentation: */ +/* +Todo: +*Quote buttons on posts +*Move delete and quote buttons away from each other +*Bring us on par with comment extension(post linking, image linking, thumb links, URL autolink) +*Smiley filter, word filter, etc should work with our extension +*/ class Forum extends Extension { public function onInitExt(InitExtEvent $event) { global $config, $database; @@ -92,12 +99,19 @@ class Forum extends Extension { } case "view": { - $threadID = int_escape($event->get_arg(1)); - $pageNumber = int_escape($event->get_arg(2)); - - $this->show_posts($event, $user->is_admin()); - if($user->is_admin()) $this->theme->add_actions_block($page, $threadID); - if(!$user->is_anonymous()) $this->theme->display_new_post_composer($page, $threadID); + $threadID = int_escape($event->get_arg(1)); + $pageNumber = int_escape($event->get_arg(2)); + list($errors) = $this->sanity_check_viewed_thread($threadID); + + if($errors!=null) + { + $this->theme->display_error(500, "Error", $errors); + break; + } + + $this->show_posts($event, $user->is_admin()); + if($user->is_admin()) $this->theme->add_actions_block($page, $threadID); + if(!$user->is_anonymous()) $this->theme->display_new_post_composer($page, $threadID); break; } case "new": @@ -111,12 +125,11 @@ class Forum extends Extension { $redirectTo = "forum/index"; if (!$user->is_anonymous()) { - list($hasErrors, $errors) = $this->valid_values_for_new_thread(); + list($errors) = $this->sanity_check_new_thread(); - if($hasErrors) + if($errors!=null) { $this->theme->display_error(500, "Error", $errors); - $this->theme->display_new_thread_composer($page, $_POST["message"], $_POST["title"], false); break; } @@ -149,24 +162,21 @@ class Forum extends Extension { $page->set_redirect(make_link("forum/index")); break; case "answer": + $threadID = int_escape($_POST["threadID"]); + $total_pages = $this->get_total_pages_for_thread($threadID); if (!$user->is_anonymous()) { - list($hasErrors, $errors) = $this->valid_values_for_new_post(); + list($errors) = $this->sanity_check_new_post(); - if ($hasErrors) + if ($errors!=null) { $this->theme->display_error(500, "Error", $errors); - $this->theme->display_new_post_composer($page, $_POST["threadID"], $_POST["message"], $_POST["title"], false); break; } - - $threadID = int_escape($_POST["threadID"]); - $this->save_new_post($threadID, $user); } - $page->set_mode("redirect"); - $page->set_redirect(make_link("forum/view/".$threadID."/1")); + $page->set_redirect(make_link("forum/view/".$threadID."/".$total_pages)); break; default: { @@ -187,70 +197,66 @@ class Forum extends Extension { return ceil($result["count"] / $config->get_int("forumPostsPerPage")); } - private function valid_values_for_new_thread() + private function sanity_check_new_thread() { - $hasErrors = false; - - $errors = ""; - + $errors = null; if (!array_key_exists("title", $_POST)) { - $hasErrors = true; $errors .= "
No title supplied.
"; } else if (strlen($_POST["title"]) == 0) { - $hasErrors = true; $errors .= "
You cannot have an empty title.
"; } else if (strlen(html_escape($_POST["title"])) > 255) { - $hasErrors = true; $errors .= "
Your title is too long.
"; } if (!array_key_exists("message", $_POST)) { - $hasErrors = true; $errors .= "
No message supplied.
"; } else if (strlen($_POST["message"]) == 0) { - $hasErrors = true; $errors .= "
You cannot have an empty message.
"; } - return array($hasErrors, $errors); + return array($errors); } - private function valid_values_for_new_post() + private function sanity_check_new_post() { - $hasErrors = false; - - $errors = ""; + $errors = null; if (!array_key_exists("threadID", $_POST)) { - $hasErrors = true; $errors = "
No thread ID supplied.
"; } else if (strlen($_POST["threadID"]) == 0) { - $hasErrors = true; $errors = "
No thread ID supplied.
"; } else if (is_numeric($_POST["threadID"])) if (!array_key_exists("message", $_POST)) { - $hasErrors = true; $errors .= "
No message supplied.
"; } else if (strlen($_POST["message"]) == 0) { - $hasErrors = true; $errors .= "
You cannot have an empty message.
"; } - return array($hasErrors, $errors); + return array($errors); + } + private function sanity_check_viewed_thread($threadID) + { + global $database; + $errors = null; + if (!$this->threadExists($threadID)) + { + $errors = "
Inexistent thread.
"; + } + return array($errors); } private function get_thread_title($threadID) { @@ -263,14 +269,17 @@ class Forum extends Extension { { global $config, $database; $pageNumber = $event->get_arg(1); + $threadsPerPage = $config->get_int('forumThreadsPerPage', 15); + $totalPages = ceil($database->get_one("SELECT COUNT(*) FROM forum_threads") / $threadsPerPage); + if(is_null($pageNumber) || !is_numeric($pageNumber)) $pageNumber = 0; else if ($pageNumber <= 0) $pageNumber = 0; + else if ($pageNumber > $totalPages) + $pageNumber = $totalPages - 1; else $pageNumber--; - - $threadsPerPage = $config->get_int('forumThreadsPerPage', 15); $threads = $database->get_all( "SELECT f.id, f.sticky, f.title, f.date, f.uptodate, u.name AS user_name, u.email AS user_email, u.class AS user_class, sum(1) - 1 AS response_count ". @@ -284,25 +293,26 @@ class Forum extends Extension { , array("limit"=>$threadsPerPage, "offset"=>$pageNumber * $threadsPerPage) ); - $totalPages = ceil($database->get_one("SELECT COUNT(*) FROM forum_threads") / $threadsPerPage); - $this->theme->display_thread_list($page, $threads, $showAdminOptions, $pageNumber + 1, $totalPages); } private function show_posts($event, $showAdminOptions = false) { global $config, $database, $user; - $threadID = $event->get_arg(1); $pageNumber = $event->get_arg(2); + $postsPerPage = $config->get_int('forumPostsPerPage', 15); + $totalPages = ceil($database->get_one("SELECT COUNT(*) FROM forum_posts WHERE thread_id = ?", array($threadID)) / $postsPerPage); + $threadTitle = $this->get_thread_title($threadID); + if(is_null($pageNumber) || !is_numeric($pageNumber)) $pageNumber = 0; else if ($pageNumber <= 0) $pageNumber = 0; + else if ($pageNumber > $totalPages) + $pageNumber = $totalPages - 1; else $pageNumber--; - - $postsPerPage = $config->get_int('forumPostsPerPage', 15); $posts = $database->get_all( "SELECT p.id, p.date, p.message, u.name as user_name, u.email AS user_email, u.class AS user_class ". @@ -314,11 +324,6 @@ class Forum extends Extension { "LIMIT :limit OFFSET :offset" , array("thread_id"=>$threadID, "offset"=>$pageNumber * $postsPerPage, "limit"=>$postsPerPage) ); - - $totalPages = ceil($database->get_one("SELECT COUNT(*) FROM forum_posts WHERE thread_id = ?", array($threadID)) / $postsPerPage); - - $threadTitle = $this->get_thread_title($threadID); - $this->theme->display_thread($posts, $showAdminOptions, $threadTitle, $threadID, $pageNumber + 1, $totalPages); } @@ -397,5 +402,14 @@ class Forum extends Extension { global $database; $database->execute("DELETE FROM forum_posts WHERE id = ?", array($postID)); } + private function threadExists($threadID){ + global $database; + $result=$database->get_one("SELECT EXISTS (SELECT * FROM forum_threads WHERE id= ?)", array($threadID)); + if ($result==1){ + return true; + }else{ + return false; + } + } } ?> From 1db92e6f2af92fac57d9d5393a36c69ae113cb6d Mon Sep 17 00:00:00 2001 From: pachuco Date: Tue, 25 Sep 2012 20:34:50 +0300 Subject: [PATCH 2/4] Some CSS love. --- ext/forum/style.css | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ext/forum/style.css diff --git a/ext/forum/style.css b/ext/forum/style.css new file mode 100644 index 00000000..38a3be41 --- /dev/null +++ b/ext/forum/style.css @@ -0,0 +1,27 @@ +TABLE#threadPosts { + table-layout:fixed; + word-wrap:break-word; + text-align:left; + width: 100%; +} +TH#threadHeadUser{ + width: 15%; + overflow: hidden; +} +DIV#returnLink{ + float:left; + font-size:150%; +} +TD.forumUser, TD.forumMessage{ + vertical-align:top !important; /*Someone, somewhere will hate me*/ + text-align:left !important; /*Someone, somewhere will hate me*/ +} +DIV.postDate { + float:left; +} +DIV.deleteLink , DIV.postNumber{ + float:right; +} +DIV.postMessage { + text-align:left; +} \ No newline at end of file From d333345a2c2ddc72f385ebc5233d026f5519dc7c Mon Sep 17 00:00:00 2001 From: pachuco Date: Tue, 25 Sep 2012 20:37:20 +0300 Subject: [PATCH 3/4] *Cosmetic changes to thread posts. *Return link to forum index in thread page. *Fixed pesky undefined $oe in theme.php. *Reordered paginator for accessability *Numbered posts. --- ext/forum/theme.php | 53 +++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/ext/forum/theme.php b/ext/forum/theme.php index 1ff6de51..101ff5a4 100644 --- a/ext/forum/theme.php +++ b/ext/forum/theme.php @@ -46,7 +46,7 @@ class ForumTheme extends Themelet { $blockTitle = "Write a new thread"; $page->set_title(html_escape($blockTitle)); $page->set_heading(html_escape($blockTitle)); - $page->add_block(new Block($blockTitle, $html, "main", 20)); + $page->add_block(new Block($blockTitle, $html, "main", 120)); } @@ -73,7 +73,7 @@ class ForumTheme extends Themelet { "; $blockTitle = "Answer to this thread"; - $page->add_block(new Block($blockTitle, $html, "main", 30)); + $page->add_block(new Block($blockTitle, $html, "main", 130)); } @@ -81,20 +81,24 @@ class ForumTheme extends Themelet { public function display_thread($posts, $showAdminOptions, $threadTitle, $threadID, $pageNumber, $totalPages) { global $config, $page/*, $user*/; - + + $posts_per_page = $config->get_int('forumPostsPerPage'); $theme_name = $config->get_string('theme'); $html = ""; - $n = 0; + $current_post = 0; - $html = "". + $html = + "

". + "
". "". - "". + "". "". ""; foreach ($posts as $post) { + $current_post++; $message = $post["message"]; $tfe = new TextFormattingEvent($message); @@ -113,7 +117,7 @@ class ForumTheme extends Themelet { $poster = User::by_name($post["user_name"]); $gravatar = $poster->get_avatar_html(); - $rank = "{$post["user_class"]}"; + $rank = "{$post["user_class"]}"; $postID = $post['id']; @@ -128,14 +132,25 @@ class ForumTheme extends Themelet { }else{ $delete_link = ""; } - - $html .= "". - "". - ""." - - - - "; + + $post_number = (($pageNumber-1)*$posts_per_page)+$current_post; + $html .= " + + + + + + + + + + + + "; } @@ -145,7 +160,7 @@ class ForumTheme extends Themelet { $page->set_title(html_escape($threadTitle)); $page->set_heading(html_escape($threadTitle)); - $page->add_block(new Block("Thread", $html, "main", 20)); + $page->add_block(new Block($threadTitle, $html, "main", 20)); } @@ -155,7 +170,7 @@ class ForumTheme extends Themelet { { $html = 'Delete this thread and its posts.'; - $page->add_block(new Block("Admin Actions", $html, "main", 40)); + $page->add_block(new Block("Admin Actions", $html, "main", 140)); } @@ -177,10 +192,10 @@ class ForumTheme extends Themelet { $html .= ""; - $n = 0; + $current_post = 0; foreach($threads as $thread) { - $oe = ($n++ % 2 == 0) ? "even" : "odd"; + $oe = ($current_post++ % 2 == 0) ? "even" : "odd"; global $config; $titleSubString = $config->get_int('forumTitleSubString'); From 936c1a65ca3e7461fa6711d880530af42c897b29 Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 6 Oct 2012 15:59:13 +0100 Subject: [PATCH 4/4] fix off-by-ones --- ext/forum/main.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/forum/main.php b/ext/forum/main.php index c4ecf120..c820610f 100644 --- a/ext/forum/main.php +++ b/ext/forum/main.php @@ -276,7 +276,7 @@ class Forum extends Extension { $pageNumber = 0; else if ($pageNumber <= 0) $pageNumber = 0; - else if ($pageNumber > $totalPages) + else if ($pageNumber >= $totalPages) $pageNumber = $totalPages - 1; else $pageNumber--; @@ -309,7 +309,7 @@ class Forum extends Extension { $pageNumber = 0; else if ($pageNumber <= 0) $pageNumber = 0; - else if ($pageNumber > $totalPages) + else if ($pageNumber >= $totalPages) $pageNumber = $totalPages - 1; else $pageNumber--;
UserUserMessage
".$user."
".$rank."
".$gravatar."
".$message."
".autodate($post["date"])."".$delete_link."
".$user."
".$rank."
".$gravatar."
+ +
#".$post_number."
+
+
".$message."