]> ruin.nu Git - ndwebbie.git/blob - forum.pl
mark threads as read
[ndwebbie.git] / forum.pl
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 use strict;
21 use warnings FATAL => 'all';
22
23 $ND::TEMPLATE->param(TITLE => 'Forum');
24
25 our $BODY;
26 our $DBH;
27 my $error;
28
29 my $thread;
30 if(param('t')){
31         my $query = $DBH->prepare(q{SELECT ft.ftid AS id,ft.subject, bool_or(fa.post) AS post
32 FROM forum_boards fb NATURAL JOIN forum_access fa NATURAL JOIN forum_threads ft
33 WHERE ft.ftid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
34         WHERE uid = $2))
35 GROUP BY ft.ftid,ft.subject});
36         $thread = $DBH->selectrow_hashref($query,undef,param('t'),$ND::UID) or $error .= p($DBH->errstr);
37 }
38
39 my $board;
40 if(param('b')){
41         my $query = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
42 FROM forum_boards fb NATURAL JOIN forum_access fa
43 WHERE fb.fbid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
44         WHERE uid = $2))
45 GROUP BY fb.fbid,fb.board});
46         $board = $DBH->selectrow_hashref($query,undef,param('b'),$ND::UID) or $error .= p($DBH->errstr);
47 }
48
49 if ($thread){ #Display the thread
50         $BODY->param(Thread => 1);
51         $BODY->param(Subject => $thread->{subject});
52         $BODY->param(Id => $thread->{id});
53         $BODY->param(Post => $thread->{post});
54
55         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
56 FROM forum_threads ft JOIN forum_posts fp USING (ftid) NATURAL JOIN users u LEFT OUTER JOIN forum_thread_visits ftv ON ftv.ftid = ft.ftid
57 WHERE ft.ftid = $1});
58         $posts->execute($thread->{id}) or $error .= p($DBH->errstr);
59         my @posts;
60         my $old = 1;
61         while (my $post = $posts->fetchrow_hashref){
62                 if ($old && $post->{unread}){
63                         $old = 0;
64                         push @posts,{Username=> 'New posts', Message => '<a name="NewPosts>"/>'};
65                 }
66                 $post->{message} = parseMarkup($post->{message});
67                 push @posts,$post;
68         }
69         $BODY->param(Posts => \@posts);
70
71         markThreadAsRead($thread->{id});
72
73 }elsif($board){ #List threads in this board
74         $BODY->param(Board => $board->{board});
75         $BODY->param(Post => $board->{post});
76         $BODY->param(Id => $board->{id});
77         my $threads = $DBH->prepare(q{SELECT ft.ftid AS id,ft.subject,count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread,count(fp.fpid) AS posts
78 FROM forum_threads ft JOIN forum_posts fp USING (ftid) LEFT OUTER JOIN forum_thread_visits ftv ON ftv.ftid = ft.ftid
79 WHERE ft.fbid = $1
80 GROUP BY ft.ftid, ft.subject});
81         $threads->execute($board->{id}) or $error .= p($DBH->errstr);
82         my $i = 0;
83         my @threads;
84         while (my $thread = $threads->fetchrow_hashref){
85                 $i++;
86                 $thread->{Odd} = $i % 2;
87                 push @threads,$thread;
88         }
89         $BODY->param(Threads => \@threads);
90
91 }else{ #List boards
92         $BODY->param(Overview => 1);
93         my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM
94                 forum_categories});
95         my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board,count(NULLIF(COALESCE(fp.fpid::boolean,FALSE) AND COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread
96 FROM forum_boards fb NATURAL JOIN forum_access fa LEFT OUTER JOIN (forum_threads ft JOIN forum_posts fp USING (ftid)) ON fb.fbid = ft.fbid LEFT OUTER JOIN forum_thread_visits ftv ON ftv.ftid = ft.ftid
97 WHERE fb.fcid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
98                 WHERE uid = $2))
99 AND (ftv.uid IS NULL OR ftv.uid = $2)
100 GROUP BY fb.fbid, fb.board
101                 });
102         $categories->execute or $error .= p($DBH->errstr);
103         my @categories;
104         while (my $category = $categories->fetchrow_hashref){
105                 $boards->execute($category->{id},$ND::UID) or $error .= p($DBH->errstr);
106                 my @boards;
107                 my $i = 0;
108                 while (my $board = $boards->fetchrow_hashref){
109                         $i++;
110                         $board->{Odd} = $i % 2;
111                         push @boards,$board;
112                 }
113                 $category->{Boards} = \@boards;
114                 delete $category->{id};
115                 push @categories,$category if $i > 0;
116         }
117         $BODY->param(Categories => \@categories);
118
119 }
120 $BODY->param(Error => $error);
121
122 1;
123