]> ruin.nu Git - ndwebbie.git/blob - ND/Forum.pm
78ee5a0a0e7f306fb5fb36e5f5ef8501c2ef62f3
[ndwebbie.git] / ND / Forum.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
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.                                   *
8 #                                                                         *
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.                          *
13 #                                                                         *
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 #**************************************************************************/
19
20 package ND::Forum;
21 use strict;
22 use warnings FATAL => 'all';
23 use CGI qw{:standard};
24 use HTML::Template;
25 use ND::Include;
26 require Exporter;
27
28 our @ISA = qw/Exporter/;
29
30 our @EXPORT = qw/viewForumThread addForumPost markThreadAsRead/;
31
32 sub viewForumThread {
33         my ($thread) = @_;
34
35         my $template = HTML::Template->new(filename => "templates/viewthread.tmpl", global_vars => 1, cache => 1);
36
37         $template->param(Subject => $thread->{subject});
38         $template->param(Id => $thread->{id});
39         $template->param(Post => $thread->{post});
40
41         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
42 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
43 WHERE ft.ftid = $1
44 ORDER BY fp.time ASC
45 });
46         $posts->execute($thread->{id},$ND::UID) or $ND::ERROR .= p($ND::DBH->errstr);
47         my @posts;
48         my $old = 1;
49         while (my $post = $posts->fetchrow_hashref){
50                 if ($old && $post->{unread}){
51                         $old = 0;
52                         $post->{NewPosts} = 1;
53                 }
54                 $post->{message} = parseMarkup($post->{message});
55                 push @posts,$post;
56         }
57         $template->param(Posts => \@posts);
58
59         markThreadAsRead($thread->{id});
60
61         return $template->output;
62 }
63
64 sub addForumPost {
65         my ($dbh,$thread,$uid,$message) = @_;
66         my $insert = $dbh->prepare(q{INSERT INTO forum_posts (ftid,message,uid) VALUES($1,$2,$3)});
67         unless ($insert->execute($thread->{id},escapeHTML($message),$uid)){
68                 $ND::ERROR .= p($dbh->errstr);
69                 return 0;
70         }
71         return 1;
72 }
73
74 sub markThreadAsRead {
75         my ($thread) = @_;
76         my $rows = $ND::DBH->do(q{UPDATE forum_thread_visits SET time = now() 
77 WHERE uid =     $1 AND ftid = $2},undef,$ND::UID,$thread);
78         if ($rows == 0){
79                 $ND::DBH->do(q{INSERT INTO forum_thread_visits (uid,ftid) VALUES ($1,$2)}
80                         ,undef,$ND::UID,$thread) or $ND::ERROR .= p($ND::DBH->errstr);
81         }elsif(not defined $rows){
82                 $ND::ERROR .= p($ND::DBH->errstr);
83         }
84 }
85
86 1;