From ba0d22272efc046f7feaf07ebf190fa9ec6474c7 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Thu, 4 Jan 2007 15:20:32 +0000 Subject: [PATCH] some refactoring --- ND/Forum.pm | 86 +++++++++++++++++++++++++++++++++++++++ ND/Include.pm | 13 +----- forum.pl | 32 +++------------ startup.pl | 3 +- templates/forum.tmpl | 26 +----------- templates/viewthread.tmpl | 21 ++++++++++ 6 files changed, 117 insertions(+), 64 deletions(-) create mode 100644 ND/Forum.pm create mode 100644 templates/viewthread.tmpl diff --git a/ND/Forum.pm b/ND/Forum.pm new file mode 100644 index 0000000..54f2b6f --- /dev/null +++ b/ND/Forum.pm @@ -0,0 +1,86 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ + +package ND::Forum; +use strict; +use warnings FATAL => 'all'; +use CGI qw{:standard}; +use HTML::Template; +use ND::Include; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/viewForumThread addForumPost markThreadAsRead/; + +sub viewForumThread { + my ($thread) = @_; + + my $template = HTML::Template->new(filename => "templates/viewthread.tmpl", global_vars => 1, cache => 1); + + $template->param(Subject => $thread->{subject}); + $template->param(Id => $thread->{id}); + $template->param(Post => $thread->{post}); + + my $posts = $ND::DBH->prepare(q{SELECT u.username,date_trunc('minute',fp.time::timestamp) AS time,fp.message,COALESCE(fp.time > ftv.time,TRUE) AS unread +FROM forum_threads ft JOIN forum_posts fp USING (ftid) NATURAL JOIN users u LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid +WHERE ft.ftid = $1 +ORDER BY fp.time ASC +}); + $posts->execute($thread->{id},$ND::UID) or $ND::ERROR .= p($ND::DBH->errstr); + my @posts; + my $old = 1; + while (my $post = $posts->fetchrow_hashref){ + if ($old && $post->{unread}){ + $old = 0; + $post->{NewPosts} = 1; + } + $post->{message} = parseMarkup($post->{message}); + push @posts,$post; + } + $template->param(Posts => \@posts); + + markThreadAsRead($thread->{id}); + + return $template->output; +} + +sub addForumPost { + my ($dbh,$thread,$uid,$message) = @_; + my $insert = $dbh->prepare(q{INSERT INTO forum_posts (ftid,message,uid) VALUES($1,$2,$3)}); + unless ($insert->execute($thread->{id},escapeHTML($message),$uid)){ + $ND::ERROR .= p($dbh->errstr); + return 0; + } + return 1; +} + +sub markThreadAsRead { + my ($thread) = @_; + my $rows = $ND::DBH->do(q{UPDATE forum_thread_visits SET time = now() +WHERE uid = $1 AND ftid = $2},undef,$ND::UID,$thread); + if ($rows == 0){ + $ND::DBH->do(q{INSERT INTO forum_thread_visits (uid,ftid) VALUES ($1,$2)} + ,undef,$ND::UID,$thread) or $ND::ERROR .= p($ND::DBH->errstr); + }elsif(not defined $rows){ + $ND::ERROR .= p($ND::DBH->errstr); + } +} + +1; diff --git a/ND/Include.pm b/ND/Include.pm index d77121f..5df59fc 100644 --- a/ND/Include.pm +++ b/ND/Include.pm @@ -26,7 +26,7 @@ require Exporter; our @ISA = qw/Exporter/; our @EXPORT = qw/isMember isHC isDC isBC isOfficer isScanner isIntel isTech parseMarkup min max listTargets - alliances intelquery generateClaimXml markThreadAsRead/; + alliances intelquery generateClaimXml/; sub isMember { return exists $ND::GROUPS{Members} || isTech(); @@ -68,17 +68,6 @@ sub parseMarkup { return $text; } -sub markThreadAsRead { - my ($thread) = @_; - my $rows = $ND::DBH->do(q{UPDATE forum_thread_visits SET time = now() -WHERE uid = $1 AND ftid = $2},undef,$ND::UID,$thread); - if ($rows == 0){ - $ND::DBH->do(q{INSERT INTO forum_thread_visits (uid,ftid) VALUES ($1,$2)} - ,undef,$ND::UID,$thread) or $ND::ERROR .= p($ND::DBH->errstr); - }elsif(not defined $rows){ - $ND::ERROR .= p($ND::DBH->errstr); - } -} sub min { my ($x,$y) = @_; diff --git a/forum.pl b/forum.pl index fdd8f1a..1511642 100644 --- a/forum.pl +++ b/forum.pl @@ -19,6 +19,8 @@ use strict; use warnings FATAL => 'all'; +no warnings 'uninitialized'; +use ND::Forum; $ND::TEMPLATE->param(TITLE => 'Forum'); @@ -47,7 +49,7 @@ if(param('t')){ $thread = $DBH->selectrow_hashref($findThread,undef,param('t'),$ND::UID) or $ERROR .= p($DBH->errstr); } -if (defined param('cmd') && param('cmd') eq 'submit'){ +if (defined param('cmd') && param('cmd') eq 'forumpost'){ $DBH->begin_work; if ($board && $board->{post}){ my $insert = $DBH->prepare(q{INSERT INTO forum_threads (fbid,subject) VALUES($1,$2)}); @@ -60,8 +62,7 @@ if (defined param('cmd') && param('cmd') eq 'submit'){ } } if ($thread && $thread->{post}){ - my $insert = $DBH->prepare(q{INSERT INTO forum_posts (ftid,message,uid) VALUES($1,$2,$3)}); - $insert->execute($thread->{id},escapeHTML(param('message')),$ND::UID) or $ERROR .= p($DBH->errstr); + addForumPost($DBH,$thread,$ND::UID,param('message')); } $DBH->commit or $ERROR .= p($DBH->errstr); } @@ -74,30 +75,7 @@ GROUP BY ft.ftid, ft.subject HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= $3}); if ($thread){ #Display the thread - $BODY->param(Thread => 1); - $BODY->param(Subject => $thread->{subject}); - $BODY->param(Id => $thread->{id}); - $BODY->param(Post => $thread->{post}); - - my $posts = $DBH->prepare(q{SELECT u.username,date_trunc('minute',fp.time::timestamp) AS time,fp.message,COALESCE(fp.time > ftv.time,TRUE) AS unread -FROM forum_threads ft JOIN forum_posts fp USING (ftid) NATURAL JOIN users u LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid -WHERE ft.ftid = $1 -ORDER BY fp.time ASC -}); - $posts->execute($thread->{id},$ND::UID) or $ERROR .= p($DBH->errstr); - my @posts; - my $old = 1; - while (my $post = $posts->fetchrow_hashref){ - if ($old && $post->{unread}){ - $old = 0; - $post->{NewPosts} = 1; - } - $post->{message} = parseMarkup($post->{message}); - push @posts,$post; - } - $BODY->param(Posts => \@posts); - - markThreadAsRead($thread->{id}); + $BODY->param(Thread => viewForumThread $thread); }elsif($board){ #List threads in this board $BODY->param(Board => $board->{board}); diff --git a/startup.pl b/startup.pl index cf9b748..5cb7bcd 100644 --- a/startup.pl +++ b/startup.pl @@ -10,8 +10,9 @@ DBI->install_driver("Pg"); use DBI; use DBD::Pg qw(:pg_types); -use DB; +use ND::DB; use ND::Include; +use ND::Forum; use Tie::File; diff --git a/templates/forum.tmpl b/templates/forum.tmpl index 126e845..923d59d 100644 --- a/templates/forum.tmpl +++ b/templates/forum.tmpl @@ -1,26 +1,4 @@ - -

- - -
-

New posts below:

-
-
- : - -
-
- -
New Reply - - - - -
- -
-
-
+

@@ -41,7 +19,7 @@
New Thread

Subject:

- +
diff --git a/templates/viewthread.tmpl b/templates/viewthread.tmpl new file mode 100644 index 0000000..41e7967 --- /dev/null +++ b/templates/viewthread.tmpl @@ -0,0 +1,21 @@ +

+ + +
+

New posts below:

+
+
+ : + +
+
+ +
New Reply + + + + +
+ +
+
-- 2.39.2