commit
1acf839902
3 changed files with 124 additions and 68 deletions
|
@ -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 .= "<div id='error'>No title supplied.</div>";
|
||||
}
|
||||
else if (strlen($_POST["title"]) == 0)
|
||||
{
|
||||
$hasErrors = true;
|
||||
$errors .= "<div id='error'>You cannot have an empty title.</div>";
|
||||
}
|
||||
else if (strlen(html_escape($_POST["title"])) > 255)
|
||||
{
|
||||
$hasErrors = true;
|
||||
$errors .= "<div id='error'>Your title is too long.</div>";
|
||||
}
|
||||
|
||||
if (!array_key_exists("message", $_POST))
|
||||
{
|
||||
$hasErrors = true;
|
||||
$errors .= "<div id='error'>No message supplied.</div>";
|
||||
}
|
||||
else if (strlen($_POST["message"]) == 0)
|
||||
{
|
||||
$hasErrors = true;
|
||||
$errors .= "<div id='error'>You cannot have an empty message.</div>";
|
||||
}
|
||||
|
||||
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 = "<div id='error'>No thread ID supplied.</div>";
|
||||
}
|
||||
else if (strlen($_POST["threadID"]) == 0)
|
||||
{
|
||||
$hasErrors = true;
|
||||
$errors = "<div id='error'>No thread ID supplied.</div>";
|
||||
}
|
||||
else if (is_numeric($_POST["threadID"]))
|
||||
|
||||
if (!array_key_exists("message", $_POST))
|
||||
{
|
||||
$hasErrors = true;
|
||||
$errors .= "<div id='error'>No message supplied.</div>";
|
||||
}
|
||||
else if (strlen($_POST["message"]) == 0)
|
||||
{
|
||||
$hasErrors = true;
|
||||
$errors .= "<div id='error'>You cannot have an empty message.</div>";
|
||||
}
|
||||
|
||||
return array($hasErrors, $errors);
|
||||
return array($errors);
|
||||
}
|
||||
private function sanity_check_viewed_thread($threadID)
|
||||
{
|
||||
global $database;
|
||||
$errors = null;
|
||||
if (!$this->threadExists($threadID))
|
||||
{
|
||||
$errors = "<div id='error'>Inexistent thread.</div>";
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
27
ext/forum/style.css
Normal file
27
ext/forum/style.css
Normal file
|
@ -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;
|
||||
}
|
|
@ -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 = "<table id='postList' class='zebra'>".
|
||||
$html =
|
||||
"<div id=returnLink>[<a href=".make_link("forum/index/").">Return</a>]</div><br><br>".
|
||||
"<table id='threadPosts' class='zebra'>".
|
||||
"<thead><tr>".
|
||||
"<th>User</th>".
|
||||
"<th id=threadHeadUser>User</th>".
|
||||
"<th>Message</th>".
|
||||
"</tr></thead>";
|
||||
|
||||
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 = "<sup>{$post["user_class"]}</sup>";
|
||||
$rank = "<sup class='user_rank'>{$post["user_class"]}</sup>";
|
||||
|
||||
$postID = $post['id'];
|
||||
|
||||
|
@ -128,14 +132,25 @@ class ForumTheme extends Themelet {
|
|||
}else{
|
||||
$delete_link = "";
|
||||
}
|
||||
|
||||
$html .= "<tr>".
|
||||
"<td class='forum_user'>".$user."<br>".$rank."<br>".$gravatar."</td>".
|
||||
"<td class='forum_message'>".$message."</td>"."</tr>
|
||||
<tr class='$oe'>
|
||||
<td class='forum_subuser'><small>".autodate($post["date"])."</small></td>
|
||||
<td class='forum_submessage'>".$delete_link."</td>
|
||||
</tr>";
|
||||
|
||||
$post_number = (($pageNumber-1)*$posts_per_page)+$current_post;
|
||||
$html .= "<tr >
|
||||
<tr class='postHead'>
|
||||
<td class='forumSupuser'></td>
|
||||
<td class='forumSupmessage'><div class=deleteLink>".$delete_link."</div></td>
|
||||
</tr>
|
||||
<tr class='posBody'>
|
||||
<td class='forumUser'>".$user."<br>".$rank."<br>".$gravatar."<br></td>
|
||||
<td class='forumMessage'>
|
||||
<div class=postDate><small>".autodate($post['date'])."</small></div>
|
||||
<div class=postNumber> #".$post_number."</div>
|
||||
<br>
|
||||
<div class=postMessage>".$message."</td>
|
||||
</tr>
|
||||
<tr class='postFoot'>
|
||||
<td class='forumSubuser'></td>
|
||||
<td class='forumSubmessage'></td>
|
||||
</tr>";
|
||||
|
||||
}
|
||||
|
||||
|
@ -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 = '<a href="'.make_link("forum/nuke/".$threadID).'">Delete this thread and its posts.</a>';
|
||||
|
||||
$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 .= "</tr></thead><tbody>";
|
||||
|
||||
|
||||
$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');
|
||||
|
|
Reference in a new issue