1 #**************************************************************************
2 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
4 # This program is free software; you can redistribute it and/or modify *
5 # it under the terms of the GNU General Public License as published by *
6 # the Free Software Foundation; either version 2 of the License, or *
7 # (at your option) any later version. *
9 # This program is distributed in the hope that it will be useful, *
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 # GNU General Public License for more details. *
14 # You should have received a copy of the GNU General Public License *
15 # along with this program; if not, write to the *
16 # Free Software Foundation, Inc., *
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 #**************************************************************************/
20 package ND::Web::Forum;
22 use warnings FATAL => 'all';
23 use CGI qw{:standard};
28 our @ISA = qw/Exporter/;
30 our @EXPORT = qw/viewForumThread addForumPost addForumThread markThreadAsRead/;
35 my $template = HTML::Template->new(filename => "templates/viewthread.tmpl", global_vars => 1, cache => 1);
37 $template->param(Id => $thread->{id});
38 $template->param(Post => $thread->{post});
40 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
41 FROM forum_threads ft JOIN forum_posts fp USING (ftid) JOIN users u USING (uid) LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
45 $posts->execute($thread->{id},$ND::UID) or $ND::ERROR .= p($ND::DBH->errstr);
48 while (my $post = $posts->fetchrow_hashref){
49 if ($old && $post->{unread}){
51 $post->{NewPosts} = 1;
53 $post->{message} = parseMarkup($post->{message});
57 if (defined param('cmd') && param('cmd') eq 'Preview'){
58 push @posts,{message => parseMarkup(escapeHTML(param('message'))), unread => 1, username => 'PREVIEW', Time => 'Not submitted yet', NewPosts => $old ? 1 : 0};
60 $template->param(Posts => \@posts);
61 $template->param(Message => param('message'));
63 markThreadAsRead($thread->{id});
65 return $template->output;
69 my ($dbh,$thread,$uid,$message) = @_;
70 my $insert = $dbh->prepare(q{INSERT INTO forum_posts (ftid,message,uid) VALUES($1,$2,$3)});
71 unless ($insert->execute($thread->{id},escapeHTML($message),$uid)){
72 $ND::ERROR .= p($dbh->errstr);
79 my ($dbh,$board,$uid,$subject) = @_;
81 my $insert = $dbh->prepare(q{INSERT INTO forum_threads (fbid,subject) VALUES($1,$2)});
83 if ($insert->execute($board->{id},escapeHTML($subject))){
84 my $id = $dbh->last_insert_id(undef,undef,undef,undef,"forum_threads_ftid_seq");
85 return $dbh->selectrow_hashref(q{SELECT ftid AS id, subject, $2::boolean AS post FROM forum_threads WHERE ftid = $1}
86 ,undef,$id,$board->{post})
87 or $ND::ERROR .= p($dbh->errstr);
89 $ND::ERROR .= p($dbh->errstr);
93 sub markThreadAsRead {
95 my $rows = $ND::DBH->do(q{UPDATE forum_thread_visits SET time = now()
96 WHERE uid = $1 AND ftid = $2},undef,$ND::UID,$thread);
98 $ND::DBH->do(q{INSERT INTO forum_thread_visits (uid,ftid) VALUES ($1,$2)}
99 ,undef,$ND::UID,$thread) or $ND::ERROR .= p($ND::DBH->errstr);
100 }elsif(not defined $rows){
101 $ND::ERROR .= p($ND::DBH->errstr);