]> ruin.nu Git - ndwebbie.git/blob - forum.pl
show unread, and list threads for a board
[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 #TODO: fetch thread info.
31
32 my $board;
33 if(param('b')){
34         my $query = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post)
35 FROM forum_boards fb NATURAL JOIN forum_access fa
36 WHERE fb.fbid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
37         WHERE uid = $2))
38 GROUP BY fb.fbid,fb.board});
39         $board = $DBH->selectrow_hashref($query,undef,param('b'),$ND::UID) or $error .= p($DBH->errstr);
40 }
41
42 if ($thread){ #Display the thread
43 }elsif($board){ #List threads in this board
44         $BODY->param(Board => 1);
45         my $threads = $DBH->prepare(q{SELECT ft.ftid AS id,ft.subject,count(NULLIF(COALESCE(fp.fpid::boolean,FALSE) AND COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread,count(fp.fpid) AS posts
46 FROM forum_threads ft JOIN forum_posts fp USING (ftid) LEFT OUTER JOIN forum_thread_visits ftv ON ftv.ftid = ft.ftid
47 WHERE ft.fbid = $1
48 GROUP BY ft.ftid, ft.subject});
49         $threads->execute($board->{id}) or $error .= p($DBH->errstr);
50         my $i = 0;
51         my @threads;
52         while (my $thread = $threads->fetchrow_hashref){
53                 $i++;
54                 $thread->{Odd} = $i % 2;
55                 push @threads,$thread;
56         }
57         $BODY->param(Threads => \@threads);
58
59 }else{ #List boards
60         $BODY->param(Overview => 1);
61         my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM
62                 forum_categories});
63         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
64 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
65 WHERE fb.fcid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
66                 WHERE uid = $2))
67 AND (ftv.uid IS NULL OR ftv.uid = $2)
68 GROUP BY fb.fbid, fb.board
69                 });
70         $categories->execute or $error .= p($DBH->errstr);
71         my @categories;
72         while (my $category = $categories->fetchrow_hashref){
73                 $boards->execute($category->{id},$ND::UID) or $error .= p($DBH->errstr);
74                 my @boards;
75                 my $i = 0;
76                 while (my $board = $boards->fetchrow_hashref){
77                         $i++;
78                         $board->{Odd} = $i % 2;
79                         push @boards,$board;
80                 }
81                 $category->{Boards} = \@boards;
82                 delete $category->{id};
83                 push @categories,$category if $i > 0;
84         }
85         $BODY->param(Categories => \@categories);
86
87 }
88 $BODY->param(Error => $error);
89
90 1;
91