]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/Forum.pm
Basic forum search
[ndwebbie.git] / NDWeb / Pages / Forum.pm
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 package NDWeb::Pages::Forum;
21 use strict;
22 use warnings;
23 use NDWeb::Forum;
24 use CGI qw/:standard/;
25 use NDWeb::Include;
26 use ND::Include;
27
28 use base qw/NDWeb::XMLPage/;
29
30 $NDWeb::Page::PAGES{forum} = __PACKAGE__;
31
32 sub parse {
33         my $self = shift;
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';
39         }
40 }
41
42 sub render_body {
43         my $self = shift;
44         my ($BODY) = @_;
45         $self->{TITLE} = 'Forum';
46         my $DBH = $self->{DBH};
47
48         $DBH->do(q{UPDATE users SET last_forum_visit = NOW() WHERE uid = $1},undef,$ND::UID) or $ND::ERROR .= p($DBH->errstr);
49
50         my $board;
51         if(param('b')){
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
55                         WHERE uid = $2))
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);
58         }
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
65                 });
66
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};
73                 }
74         }
75
76         my $thread;
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
80                 WHERE uid = $2))
81                 GROUP BY ft.ftid,ft.subject,ft.fbid,fb.board,fb.fcid,ft.sticky});
82         if(param('t')){
83                 $thread = $DBH->selectrow_hashref($findThread,undef,param('t'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
84         }
85
86         if (defined param('cmd')){
87                 if(param('cmd') eq 'Submit' or param('cmd') eq 'Preview'){
88                         $DBH->begin_work;
89                         if ($board && $board->{post}){
90                                 $thread = addForumThread $DBH,$board,$ND::UID,param('subject');
91                         }
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";
96                         }
97                         $DBH->commit or $ND::ERROR .= p($DBH->errstr);
98                         return if $self->{RETURN};
99                 }
100                 if(param('cmd') eq 'Move' && $board->{moderate}){
101                         $DBH->begin_work;
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');
108                                         }
109                                 }
110                         }
111                         $DBH->commit or $ND::ERROR .= p($DBH->errstr);
112                 }
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;
116                         }else{
117                                 $ND::ERROR .= p($DBH->errstr);
118                         }
119                 }
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;
123                         }else{
124                                 $ND::ERROR .= p($DBH->errstr);
125                         }
126                 }
127         }
128
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
133                 WHERE uid = $2))
134                 GROUP BY fb.fbid,fb.board
135                 ORDER BY fb.fbid
136                         });
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
144                 WHERE ft.fbid = $1
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});
148
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);
160
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);
167                 my @categories;
168                 while (my $category = $categories->fetchrow_hashref){
169                         $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
170                         my @boards;
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);
174                                 my @threads;
175                                 while (my $thread = $threads->fetchrow_hashref){
176                                         push @threads,$thread;
177                                 }
178                                 $board->{Threads} = \@threads;
179                                 delete $board->{post};
180                                 push @boards,$board if $threads->rows > 0;
181                         }
182                         $category->{Boards} = \@boards;
183                         delete $category->{id};
184                         push @categories,$category if @boards;
185                 }
186                 $BODY->param(Categories => \@categories);
187
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);
199                 my @threads;
200                 while (my $thread = $threads->fetchrow_hashref){
201                         push @threads,$thread;
202                 }
203                 $BODY->param(Threads => \@threads);
204
205                 if ($board->{moderate}){
206                         $categories->execute or $ND::ERROR .= p($DBH->errstr);
207                         my @categories;
208                         while (my $category = $categories->fetchrow_hashref){
209                                 $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
210
211                                 my @boards;
212                                 while (my $b = $boards->fetchrow_hashref){
213                                         next if (not $b->{post} or $b->{id} == $board->{id});
214                                         delete $b->{post};
215                                         push @boards,$b;
216                                 }
217                                 $category->{Boards} = \@boards;
218                                 delete $category->{id};
219                                 push @categories,$category if @boards;
220                         }
221                         $BODY->param(Categories => \@categories);
222                 }
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);
226         }else{ #List boards
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
234                         ORDER BY fb.fbid        });
235                 my @categories;
236                 while (my $category = $categories->fetchrow_hashref){
237                         $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
238                         my @boards;
239                         while (my $board = $boards->fetchrow_hashref){
240                                 push @boards,$board;
241                         }
242                         $category->{Boards} = \@boards;
243                         delete $category->{id};
244                         push @categories,$category if $boards->rows > 0;
245                 }
246                 $BODY->param(Categories => \@categories);
247
248         }
249         return $BODY;
250 }
251
252 1;
253