]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/Forum.pm
Replaced ND::Web with NDWeb
[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                 param('allUnread',1);
36         }
37 }
38
39 sub render_body {
40         my $self = shift;
41         my ($BODY) = @_;
42         $self->{TITLE} = 'Forum';
43         my $DBH = $self->{DBH};
44
45         $DBH->do(q{UPDATE users SET last_forum_visit = NOW() WHERE uid = $1},undef,$ND::UID) or $ND::ERROR .= p($DBH->errstr);
46
47         my $board;
48         if(param('b')){
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,fb.fcid
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
52                         WHERE uid = $2))
53                         GROUP BY fb.fbid,fb.board,fb.fcid});
54                 $board = $DBH->selectrow_hashref($boards,undef,param('b'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
55         }
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
62                 });
63
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};
70                 }
71         }
72
73         my $thread;
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,ft.fbid,fb.board,fb.fcid,ft.sticky
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
77                 WHERE uid = $2))
78                 GROUP BY ft.ftid,ft.subject,ft.fbid,fb.board,fb.fcid,ft.sticky});
79         if(param('t')){
80                 $thread = $DBH->selectrow_hashref($findThread,undef,param('t'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
81         }
82
83         if (defined param('cmd')){
84                 if(param('cmd') eq 'Submit' or param('cmd') eq 'Preview'){
85                         $DBH->begin_work;
86                         if ($board && $board->{post}){
87                                 $thread = addForumThread $DBH,$board,$ND::UID,param('subject');
88                         }
89                         if (param('cmd') eq 'Submit' and $thread && $thread->{post}){
90                                 addForumPost($DBH,$thread,$ND::UID,param('message'));
91                                 $self->{RETURN} = 'REDIRECT';
92                                 $self->{REDIR_LOCATION} = "/forum?t=$thread->{id}#NewPosts";
93                         }
94                         $DBH->commit or $ND::ERROR .= p($DBH->errstr);
95                         return if $self->{RETURN};
96                 }
97                 if(param('cmd') eq 'Move' && $board->{moderate}){
98                         $DBH->begin_work;
99                         my $moveThread = $DBH->prepare(q{UPDATE forum_threads SET fbid = $1 WHERE ftid = $2 AND fbid = $3});
100                         for my $param (param()){
101                                 if ($param =~ /t:(\d+)/){
102                                         $moveThread->execute(param('board'),$1,$board->{id}) or $ND::ERROR .= p($DBH->errstr);
103                                         if ($moveThread->rows > 0){
104                                                 log_message $ND::UID, qq{Moved thread: $1 to board: }.param('board');
105                                         }
106                                 }
107                         }
108                         $DBH->commit or $ND::ERROR .= p($DBH->errstr);
109                 }
110                 if($thread && param('cmd') eq 'Sticky' && $thread->{moderate}){
111                         if ($DBH->do(q{UPDATE forum_threads SET sticky = TRUE WHERE ftid = ?}, undef,$thread->{id})){
112                                 $thread->{sticky} = 1;
113                         }else{
114                                 $ND::ERROR .= p($DBH->errstr);
115                         }
116                 }
117                 if($thread && param('cmd') eq 'Unsticky' && $thread->{moderate}){
118                         if ($DBH->do(q{UPDATE forum_threads SET sticky = FALSE WHERE ftid = ?}, undef,$thread->{id})){
119                                 $thread->{sticky} = 0;
120                         }else{
121                                 $ND::ERROR .= p($DBH->errstr);
122                         }
123                 }
124         }
125
126         my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM forum_categories ORDER BY fcid});
127         my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
128                 FROM forum_boards fb NATURAL JOIN forum_access fa
129                 WHERE fb.fcid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
130                 WHERE uid = $2))
131                 GROUP BY fb.fbid,fb.board
132                 ORDER BY fb.fbid
133                         });
134         my $threads = $DBH->prepare(q{SELECT ft.ftid AS id,u.username,ft.subject,
135                 count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread,count(fp.fpid) AS posts,
136                 date_trunc('seconds',max(fp.time)::timestamp) as last_post,
137                 min(fp.time)::date as posting_date, ft.sticky
138                 FROM forum_threads ft JOIN forum_posts fp USING (ftid) 
139                         JOIN users u ON u.uid = ft.uid
140                         LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
141                 WHERE ft.fbid = $1
142                 GROUP BY ft.ftid, ft.subject,ft.sticky,u.username
143                 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= $3
144                 ORDER BY sticky DESC,last_post DESC});
145
146         if ($thread){ #Display the thread
147                 $BODY->param(Title =>  $thread->{subject});
148                 $BODY->param(FBID =>  $thread->{fbid});
149                 $BODY->param(Board =>  $thread->{board});
150                 $BODY->param(FTID =>  $thread->{id});
151                 $BODY->param(Moderate =>  $thread->{moderate});
152                 $BODY->param(Sticky =>  $thread->{sticky} ? 'Unsticky' : 'Sticky');
153                 $BODY->param(Thread => viewForumThread $thread);
154                 my ($category) = $DBH->selectrow_array(q{SELECT category FROM forum_categories WHERE fcid = $1}
155                         ,undef,$thread->{fcid}) or $ND::ERROR .= p($DBH->errstr);
156                 $BODY->param(Category =>  $category);
157
158         }elsif(defined param('allUnread')){ #List threads in this board
159                 $BODY->param(AllUnread => 1);
160                 $BODY->param(Id => $board->{id});
161                 my ($time) = $DBH->selectrow_array('SELECT now()::timestamp',undef);
162                 $BODY->param(Date => $time);
163                 $categories->execute or $ND::ERROR .= p($DBH->errstr);
164                 my @categories;
165                 while (my $category = $categories->fetchrow_hashref){
166                         $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
167                         my @boards;
168                         while (my $board = $boards->fetchrow_hashref){
169                                 next if $board->{id} < 0;
170                                 $threads->execute($board->{id},$ND::UID,1) or $ND::ERROR .= p($DBH->errstr);
171                                 my $i = 0;
172                                 my @threads;
173                                 while (my $thread = $threads->fetchrow_hashref){
174                                         $i++;
175                                         $thread->{Odd} = $i % 2;
176                                         push @threads,$thread;
177                                 }
178                                 $board->{Threads} = \@threads;
179                                 delete $board->{post};
180                                 push @boards,$board if $i > 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 $i = 0;
200                 my @threads;
201                 while (my $thread = $threads->fetchrow_hashref){
202                         $i++;
203                         $thread->{Odd} = $i % 2;
204                         push @threads,$thread;
205                 }
206                 $BODY->param(Threads => \@threads);
207
208                 if ($board->{moderate}){
209                         $categories->execute or $ND::ERROR .= p($DBH->errstr);
210                         my @categories;
211                         while (my $category = $categories->fetchrow_hashref){
212                                 $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
213
214                                 my @boards;
215                                 while (my $b = $boards->fetchrow_hashref){
216                                         next if (not $b->{post} or $b->{id} == $board->{id});
217                                         delete $b->{post};
218                                         push @boards,$b;
219                                 }
220                                 $category->{Boards} = \@boards;
221                                 delete $category->{id};
222                                 push @categories,$category if @boards;
223                         }
224                         $BODY->param(Categories => \@categories);
225                 }
226                 my ($category) = $DBH->selectrow_array(q{SELECT category FROM forum_categories WHERE fcid = $1}
227                         ,undef,$board->{fcid}) or $ND::ERROR .= p($DBH->errstr);
228                 $BODY->param(Category =>  $category);
229
230         }elsif($self->{URI} =~ m{^/.*/search/(.*)}){ #List threads in this board
231         }else{ #List boards
232                 $BODY->param(Overview => 1);
233                 $categories->execute or $ND::ERROR .= p($DBH->errstr);
234                 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
235                         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
236                         WHERE fb.fcid = $1 AND 
237                         fb.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($2)))
238                         GROUP BY fb.fbid, fb.board
239                         ORDER BY fb.fbid        });
240                 my @categories;
241                 while (my $category = $categories->fetchrow_hashref){
242                         $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
243                         my @boards;
244                         my $i = 0;
245                         while (my $board = $boards->fetchrow_hashref){
246                                 $i++;
247                                 $board->{Odd} = $i % 2;
248                                 push @boards,$board;
249                         }
250                         $category->{Boards} = \@boards;
251                         delete $category->{id};
252                         push @categories,$category if $i > 0;
253                 }
254                 $BODY->param(Categories => \@categories);
255
256         }
257         return $BODY;
258 }
259
260 1;
261