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 #**************************************************************************/
20 package ND::Web::Pages::Forum;
24 use CGI qw/:standard/;
27 $ND::PAGES{forum} = {parse => \&parse, process => \&process, render=> \&render};
31 if ($uri =~ m{^/.*/allUnread}){
43 $ND::TEMPLATE->param(TITLE => 'Forum');
45 $DBH->do(q{UPDATE users SET last_forum_visit = NOW() WHERE uid = $1},undef,$ND::UID) or $ND::ERROR .= p($DBH->errstr);
49 my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post, bool_or(fa.moderate) AS moderate
50 FROM forum_boards fb NATURAL JOIN forum_access fa
51 WHERE fb.fbid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
53 GROUP BY fb.fbid,fb.board});
54 $board = $DBH->selectrow_hashref($boards,undef,param('b'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
56 if (param('markAsRead')){
57 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, max(fp.time)::timestamp as last_post
58 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
59 WHERE ((ft.fbid IS NULL AND $1 IS NULL) OR ft.fbid = $1) AND fp.time <= $3
60 GROUP BY ft.ftid, ft.subject
61 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= 1
64 $threads->bind_param('$1',$board->{id},{TYPE => DBI::SQL_INTEGER }) or $ND::ERROR .= p($DBH->errstr);
65 $threads->bind_param('$2',$ND::UID,{TYPE => DBI::SQL_INTEGER }) or $ND::ERROR .= p($DBH->errstr);
66 $threads->bind_param('$3',param('markAsRead')) or $ND::ERROR .= p($DBH->errstr);
67 $threads->execute or $ND::ERROR .= p($DBH->errstr);
68 while (my $thread = $threads->fetchrow_hashref){
69 markThreadAsRead $thread->{id};
74 my $findThread = $DBH->prepare(q{SELECT ft.ftid AS id,ft.subject, bool_or(fa.post) AS post, bool_or(fa.moderate) AS moderate
75 FROM forum_boards fb NATURAL JOIN forum_access fa NATURAL JOIN forum_threads ft
76 WHERE ft.ftid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
78 GROUP BY ft.ftid,ft.subject});
80 $thread = $DBH->selectrow_hashref($findThread,undef,param('t'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
83 if (defined param('cmd')){
84 if(param('cmd') eq 'Submit'){
86 if ($board && $board->{post}){
87 $thread = addForumThread $DBH,$board,$ND::UID,param('subject');
89 if ($thread && $thread->{post}){
90 addForumPost($DBH,$thread,$ND::UID,param('message'));
92 $DBH->commit or $ND::ERROR .= p($DBH->errstr);
94 if(param('cmd') eq 'Move' && $board->{moderate}){
96 my $moveThread = $DBH->prepare(q{UPDATE forum_threads SET fbid = $1 WHERE ftid = $2 AND fbid = $3});
97 for my $param (param()){
98 if ($param =~ /t:(\d+)/){
99 $moveThread->execute(param('board'),$1,$board->{id}) or $ND::ERROR .= p($DBH->errstr)
102 $DBH->commit or $ND::ERROR .= p($DBH->errstr);
106 my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM forum_categories ORDER BY fcid});
107 my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
108 FROM forum_boards fb NATURAL JOIN forum_access fa
109 WHERE fb.fcid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
111 GROUP BY fb.fbid,fb.board
114 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, date_trunc('seconds',max(fp.time)::timestamp) as last_post
115 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
117 GROUP BY ft.ftid, ft.subject
118 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= $3
119 ORDER BY last_post DESC});
121 if ($thread){ #Display the thread
122 $BODY->param(Thread => viewForumThread $thread);
124 }elsif(defined param('allUnread')){ #List threads in this board
125 $BODY->param(AllUnread => 1);
126 $BODY->param(Id => $board->{id});
127 my ($time) = $DBH->selectrow_array('SELECT now()::timestamp',undef);
128 $BODY->param(Date => $time);
129 $categories->execute or $ND::ERROR .= p($DBH->errstr);
131 while (my $category = $categories->fetchrow_hashref){
132 $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
134 while (my $board = $boards->fetchrow_hashref){
135 $threads->execute($board->{id},$ND::UID,1) or $ND::ERROR .= p($DBH->errstr);
138 while (my $thread = $threads->fetchrow_hashref){
140 $thread->{Odd} = $i % 2;
141 push @threads,$thread;
143 $board->{Threads} = \@threads;
144 delete $board->{post};
145 push @boards,$board if $i > 0;
147 $category->{Boards} = \@boards;
148 delete $category->{id};
149 push @categories,$category if @boards;
151 $BODY->param(Categories => \@categories);
153 }elsif($board){ #List threads in this board
154 $BODY->param(Board => $board->{board});
155 $BODY->param(Post => $board->{post});
156 $BODY->param(Moderate => $board->{moderate});
157 $BODY->param(Id => $board->{id});
158 my ($time) = $DBH->selectrow_array('SELECT now()::timestamp',undef);
159 $BODY->param(Date => $time);
160 $threads->execute($board->{id},$ND::UID,0) or $ND::ERROR .= p($DBH->errstr);
163 while (my $thread = $threads->fetchrow_hashref){
165 $thread->{Odd} = $i % 2;
166 push @threads,$thread;
168 $BODY->param(Threads => \@threads);
170 if ($board->{moderate}){
171 $categories->execute or $ND::ERROR .= p($DBH->errstr);
173 while (my $category = $categories->fetchrow_hashref){
174 $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
176 while (my $b = $boards->fetchrow_hashref){
177 next if (not $b->{post} or $b->{id} == $board->{id});
181 $category->{Boards} = \@boards;
182 delete $category->{id};
183 push @categories,$category if @boards;
185 $BODY->param(Categories => \@categories);
189 $BODY->param(Overview => 1);
190 $categories->execute or $ND::ERROR .= p($DBH->errstr);
191 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,date_trunc('seconds',max(fp.time)::timestamp) as last_post
192 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
193 WHERE fb.fcid = $1 AND
194 fb.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($2)))
195 GROUP BY fb.fbid, fb.board
198 while (my $category = $categories->fetchrow_hashref){
199 $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
202 while (my $board = $boards->fetchrow_hashref){
204 $board->{Odd} = $i % 2;
207 $category->{Boards} = \@boards;
208 delete $category->{id};
209 push @categories,$category if $i > 0;
211 $BODY->param(Categories => \@categories);