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 NDWeb::Pages::Forum;
24 use CGI qw/:standard/;
28 use base qw/NDWeb::XMLPage/;
30 $NDWeb::Page::PAGES{forum} = __PACKAGE__;
34 if ($self->{URI} =~ m{^/.*/allUnread}){
35 $self->{allUnread} = 1;
36 }elsif ($self->{URI} =~ m{^/.*/search(?:/(.*))?}){
37 bless $self, 'NDWeb::Pages::Forum::Search';
38 $self->{PAGE} = 'forum/search';
45 $self->{TITLE} = 'Forum';
46 my $DBH = $self->{DBH};
48 $DBH->do(q{UPDATE users SET last_forum_visit = NOW() WHERE uid = $1},undef,$ND::UID) or $ND::ERROR .= p($DBH->errstr);
52 my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post, bool_or(fa.moderate) AS moderate,fb.fcid
53 FROM forum_boards fb NATURAL JOIN forum_access fa
54 WHERE fb.fbid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
56 GROUP BY fb.fbid,fb.board,fb.fcid});
57 $board = $DBH->selectrow_hashref($boards,undef,param('b'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
59 if (param('markAsRead')){
60 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
61 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
62 WHERE ((ft.fbid IS NULL AND $1 IS NULL) OR ft.fbid = $1) AND fp.time <= $3
63 GROUP BY ft.ftid, ft.subject
64 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= 1
67 $threads->bind_param('$1',$board->{id},{TYPE => DBI::SQL_INTEGER }) or $ND::ERROR .= p($DBH->errstr);
68 $threads->bind_param('$2',$ND::UID,{TYPE => DBI::SQL_INTEGER }) or $ND::ERROR .= p($DBH->errstr);
69 $threads->bind_param('$3',param('markAsRead')) or $ND::ERROR .= p($DBH->errstr);
70 $threads->execute or $ND::ERROR .= p($DBH->errstr);
71 while (my $thread = $threads->fetchrow_hashref){
72 markThreadAsRead $thread->{id};
77 my $findThread = $DBH->prepare(q{SELECT ft.ftid AS id,ft.subject, bool_or(fa.post) AS post, bool_or(fa.moderate) AS moderate,ft.fbid,fb.board,fb.fcid,ft.sticky
78 FROM forum_boards fb NATURAL JOIN forum_access fa NATURAL JOIN forum_threads ft
79 WHERE ft.ftid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
81 GROUP BY ft.ftid,ft.subject,ft.fbid,fb.board,fb.fcid,ft.sticky});
83 $thread = $DBH->selectrow_hashref($findThread,undef,param('t'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
86 if (defined param('cmd')){
87 if(param('cmd') eq 'Submit' or param('cmd') eq 'Preview'){
89 if ($board && $board->{post}){
90 $thread = addForumThread $DBH,$board,$ND::UID,param('subject');
92 if (param('cmd') eq 'Submit' and $thread && $thread->{post}){
93 addForumPost($DBH,$thread,$ND::UID,param('message'));
94 $self->{RETURN} = 'REDIRECT';
95 $self->{REDIR_LOCATION} = "/forum?t=$thread->{id}#NewPosts";
97 $DBH->commit or $ND::ERROR .= p($DBH->errstr);
98 return if $self->{RETURN};
100 if(param('cmd') eq 'Move' && $board->{moderate}){
102 my $moveThread = $DBH->prepare(q{UPDATE forum_threads SET fbid = $1 WHERE ftid = $2 AND fbid = $3});
103 for my $param (param()){
104 if ($param =~ /t:(\d+)/){
105 $moveThread->execute(param('board'),$1,$board->{id}) or $ND::ERROR .= p($DBH->errstr);
106 if ($moveThread->rows > 0){
107 log_message $ND::UID, qq{Moved thread: $1 to board: }.param('board');
111 $DBH->commit or $ND::ERROR .= p($DBH->errstr);
113 if($thread && param('cmd') eq 'Sticky' && $thread->{moderate}){
114 if ($DBH->do(q{UPDATE forum_threads SET sticky = TRUE WHERE ftid = ?}, undef,$thread->{id})){
115 $thread->{sticky} = 1;
117 $ND::ERROR .= p($DBH->errstr);
120 if($thread && param('cmd') eq 'Unsticky' && $thread->{moderate}){
121 if ($DBH->do(q{UPDATE forum_threads SET sticky = FALSE WHERE ftid = ?}, undef,$thread->{id})){
122 $thread->{sticky} = 0;
124 $ND::ERROR .= p($DBH->errstr);
129 my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM forum_categories ORDER BY fcid});
130 my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
131 FROM forum_boards fb NATURAL JOIN forum_access fa
132 WHERE fb.fcid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
134 GROUP BY fb.fbid,fb.board
137 my $threads = $DBH->prepare(q{SELECT ft.ftid AS id,u.username,ft.subject,
138 count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread,count(fp.fpid) AS posts,
139 date_trunc('seconds',max(fp.time)::timestamp) as last_post,
140 min(fp.time)::date as posting_date, ft.sticky
141 FROM forum_threads ft JOIN forum_posts fp USING (ftid)
142 JOIN users u ON u.uid = ft.uid
143 LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
145 GROUP BY ft.ftid, ft.subject,ft.sticky,u.username
146 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= $3
147 ORDER BY sticky DESC,last_post DESC});
149 if ($thread){ #Display the thread
150 $BODY->param(Title => $thread->{subject});
151 $BODY->param(FBID => $thread->{fbid});
152 $BODY->param(Board => $thread->{board});
153 $BODY->param(FTID => $thread->{id});
154 $BODY->param(Moderate => $thread->{moderate});
155 $BODY->param(Sticky => $thread->{sticky} ? 'Unsticky' : 'Sticky');
156 $BODY->param(Thread => viewForumThread $thread);
157 my ($category) = $DBH->selectrow_array(q{SELECT category FROM forum_categories WHERE fcid = $1}
158 ,undef,$thread->{fcid}) or $ND::ERROR .= p($DBH->errstr);
159 $BODY->param(Category => $category);
161 }elsif(defined $self->{allUnread}){ #List threads in this board
162 $BODY->param(AllUnread => 1);
163 $BODY->param(Id => $board->{id});
164 my ($time) = $DBH->selectrow_array('SELECT now()::timestamp',undef);
165 $BODY->param(Date => $time);
166 $categories->execute or $ND::ERROR .= p($DBH->errstr);
168 while (my $category = $categories->fetchrow_hashref){
169 $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
171 while (my $board = $boards->fetchrow_hashref){
172 next if $board->{id} < 0;
173 $threads->execute($board->{id},$ND::UID,1) or $ND::ERROR .= p($DBH->errstr);
175 while (my $thread = $threads->fetchrow_hashref){
176 push @threads,$thread;
178 $board->{Threads} = \@threads;
179 delete $board->{post};
180 push @boards,$board if $threads->rows > 0;
182 $category->{Boards} = \@boards;
183 delete $category->{id};
184 push @categories,$category if @boards;
186 $BODY->param(Categories => \@categories);
188 }elsif($board){ #List threads in this board
189 $BODY->param(ViewBoard => 1);
190 $BODY->param(Title => $board->{board});
191 $BODY->param(Post => $board->{post});
192 $BODY->param(Moderate => $board->{moderate});
193 $BODY->param(Id => $board->{id});
194 $BODY->param(FBID => $board->{id});
195 $BODY->param(Board => $board->{board});
196 my ($time) = $DBH->selectrow_array('SELECT now()::timestamp',undef);
197 $BODY->param(Date => $time);
198 $threads->execute($board->{id},$ND::UID,0) or $ND::ERROR .= p($DBH->errstr);
200 while (my $thread = $threads->fetchrow_hashref){
201 push @threads,$thread;
203 $BODY->param(Threads => \@threads);
205 if ($board->{moderate}){
206 $categories->execute or $ND::ERROR .= p($DBH->errstr);
208 while (my $category = $categories->fetchrow_hashref){
209 $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
212 while (my $b = $boards->fetchrow_hashref){
213 next if (not $b->{post} or $b->{id} == $board->{id});
217 $category->{Boards} = \@boards;
218 delete $category->{id};
219 push @categories,$category if @boards;
221 $BODY->param(Categories => \@categories);
223 my ($category) = $DBH->selectrow_array(q{SELECT category FROM forum_categories WHERE fcid = $1}
224 ,undef,$board->{fcid}) or $ND::ERROR .= p($DBH->errstr);
225 $BODY->param(Category => $category);
227 $BODY->param(Overview => 1);
228 $categories->execute or $ND::ERROR .= p($DBH->errstr);
229 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
230 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
231 WHERE fb.fcid = $1 AND
232 fb.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($2)))
233 GROUP BY fb.fbid, fb.board
236 while (my $category = $categories->fetchrow_hashref){
237 $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
239 while (my $board = $boards->fetchrow_hashref){
242 $category->{Boards} = \@boards;
243 delete $category->{id};
244 push @categories,$category if $boards->rows > 0;
246 $BODY->param(Categories => \@categories);