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 #**************************************************************************/
21 use warnings FATAL => 'all';
22 no warnings 'uninitialized';
25 $ND::TEMPLATE->param(TITLE => 'Forum');
34 my $boards = $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
38 GROUP BY fb.fbid,fb.board});
39 $board = $DBH->selectrow_hashref($boards,undef,param('b'),$ND::UID) or $ERROR .= p($DBH->errstr);
43 my $findThread = $DBH->prepare(q{SELECT ft.ftid AS id,ft.subject, bool_or(fa.post) AS post
44 FROM forum_boards fb NATURAL JOIN forum_access fa NATURAL JOIN forum_threads ft
45 WHERE ft.ftid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
47 GROUP BY ft.ftid,ft.subject});
49 $thread = $DBH->selectrow_hashref($findThread,undef,param('t'),$ND::UID) or $ERROR .= p($DBH->errstr);
52 if (defined param('cmd') && param('cmd') eq 'forumpost'){
54 if ($board && $board->{post}){
55 $thread = addForumThread $DBH,$board,$ND::UID,param('subject');
57 if ($thread && $thread->{post}){
58 addForumPost($DBH,$thread,$ND::UID,param('message'));
60 $DBH->commit or $ERROR .= p($DBH->errstr);
63 my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM forum_categories ORDER BY fcid});
64 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
65 FROM forum_threads ft JOIN forum_posts fp USING (ftid) LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
67 GROUP BY ft.ftid, ft.subject
68 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= $3});
70 if ($thread){ #Display the thread
71 $BODY->param(Thread => viewForumThread $thread);
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 $threads->execute($board->{id},$ND::UID,0) or $ERROR .= p($DBH->errstr);
80 while (my $thread = $threads->fetchrow_hashref){
82 $thread->{Odd} = $i % 2;
83 push @threads,$thread;
85 $BODY->param(Threads => \@threads);
87 }elsif(defined param('allUnread')){ #List threads in this board
88 $BODY->param(AllUnread => 1);
89 $BODY->param(Id => $board->{id});
90 $categories->execute or $ERROR .= p($DBH->errstr);
92 my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
93 FROM forum_boards fb NATURAL JOIN forum_access fa
94 WHERE fb.fcid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
96 GROUP BY fb.fbid,fb.board
98 while (my $category = $categories->fetchrow_hashref){
99 $boards->execute($category->{id},$ND::UID) or $ERROR .= p($DBH->errstr);
101 while (my $board = $boards->fetchrow_hashref){
102 $threads->execute($board->{id},$ND::UID,1) or $ERROR .= p($DBH->errstr);
105 while (my $thread = $threads->fetchrow_hashref){
107 $thread->{Odd} = $i % 2;
108 push @threads,$thread;
110 $board->{Threads} = \@threads;
111 delete $board->{post};
112 push @boards,$board if $i > 0;
114 $category->{Boards} = \@boards;
115 delete $category->{id};
116 push @categories,$category if @boards;
118 $BODY->param(Categories => \@categories);
121 $BODY->param(Overview => 1);
122 $categories->execute or $ERROR .= p($DBH->errstr);
123 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
124 FROM forum_boards fb LEFT OUTER JOIN (forum_threads ft JOIN forum_posts fp USING (ftid)) ON fb.fbid = ft.fbid LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
125 WHERE fb.fcid = $1 AND
126 fb.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($2)))
127 GROUP BY fb.fbid, fb.board
130 while (my $category = $categories->fetchrow_hashref){
131 $boards->execute($category->{id},$ND::UID) or $ERROR .= p($DBH->errstr);
134 while (my $board = $boards->fetchrow_hashref){
136 $board->{Odd} = $i % 2;
139 $category->{Boards} = \@boards;
140 delete $category->{id};
141 push @categories,$category if $i > 0;
143 $BODY->param(Categories => \@categories);