]> ruin.nu Git - ndwebbie.git/blob - forum.pl
small fix
[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) AS 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         $BODY->param(Post => $board->{post});
46         $BODY->param(Id => $board->{id});
47         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
48 FROM forum_threads ft JOIN forum_posts fp USING (ftid) LEFT OUTER JOIN forum_thread_visits ftv ON ftv.ftid = ft.ftid
49 WHERE ft.fbid = $1
50 GROUP BY ft.ftid, ft.subject});
51         $threads->execute($board->{id}) or $error .= p($DBH->errstr);
52         my $i = 0;
53         my @threads;
54         while (my $thread = $threads->fetchrow_hashref){
55                 $i++;
56                 $thread->{Odd} = $i % 2;
57                 push @threads,$thread;
58         }
59         $BODY->param(Threads => \@threads);
60
61 }else{ #List boards
62         $BODY->param(Overview => 1);
63         my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM
64                 forum_categories});
65         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
66 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
67 WHERE fb.fcid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
68                 WHERE uid = $2))
69 AND (ftv.uid IS NULL OR ftv.uid = $2)
70 GROUP BY fb.fbid, fb.board
71                 });
72         $categories->execute or $error .= p($DBH->errstr);
73         my @categories;
74         while (my $category = $categories->fetchrow_hashref){
75                 $boards->execute($category->{id},$ND::UID) or $error .= p($DBH->errstr);
76                 my @boards;
77                 my $i = 0;
78                 while (my $board = $boards->fetchrow_hashref){
79                         $i++;
80                         $board->{Odd} = $i % 2;
81                         push @boards,$board;
82                 }
83                 $category->{Boards} = \@boards;
84                 delete $category->{id};
85                 push @categories,$category if $i > 0;
86         }
87         $BODY->param(Categories => \@categories);
88
89 }
90 $BODY->param(Error => $error);
91
92 1;
93