]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/Forum.pm
oops
[ndwebbie.git] / ND / Web / 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 ND::Web::Pages::Forum;
21 use strict;
22 use warnings;
23 use ND::Web::Forum;
24 use CGI qw/:standard/;
25 use ND::Web::Include;
26 use ND::Include;
27
28 $ND::PAGES{forum} = {parse => \&parse, process => \&process, render=> \&render};
29
30 sub parse {
31         my ($uri) = @_;
32         if ($uri =~ m{^/.*/allUnread}){
33                 param('allUnread',1);
34         }
35 }
36
37 sub process {
38
39 }
40
41 sub render {
42         my ($DBH,$BODY) = @_;
43
44         $ND::TEMPLATE->param(TITLE => 'Forum');
45
46         $DBH->do(q{UPDATE users SET last_forum_visit = NOW() WHERE uid = $1},undef,$ND::UID) or $ND::ERROR .= p($DBH->errstr);
47
48         my $board;
49         if(param('b')){
50                 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
51                         FROM forum_boards fb NATURAL JOIN forum_access fa
52                         WHERE fb.fbid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
53                         WHERE uid = $2))
54                         GROUP BY fb.fbid,fb.board,fb.fcid});
55                 $board = $DBH->selectrow_hashref($boards,undef,param('b'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
56         }
57         if (param('markAsRead')){
58                 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
59                 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
60                 WHERE ((ft.fbid IS NULL AND $1 IS NULL) OR ft.fbid = $1) AND fp.time <= $3
61                 GROUP BY ft.ftid, ft.subject
62                 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= 1
63                 });
64
65                 $threads->bind_param('$1',$board->{id},{TYPE => DBI::SQL_INTEGER }) or $ND::ERROR .= p($DBH->errstr);
66                 $threads->bind_param('$2',$ND::UID,{TYPE => DBI::SQL_INTEGER }) or $ND::ERROR .= p($DBH->errstr);
67                 $threads->bind_param('$3',param('markAsRead')) or $ND::ERROR .= p($DBH->errstr);
68                 $threads->execute or $ND::ERROR .= p($DBH->errstr);
69                 while (my $thread = $threads->fetchrow_hashref){
70                         markThreadAsRead $thread->{id};
71                 }
72         }
73
74         my $thread;
75         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
76                 FROM forum_boards fb NATURAL JOIN forum_access fa NATURAL JOIN forum_threads ft
77                 WHERE ft.ftid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
78                 WHERE uid = $2))
79                 GROUP BY ft.ftid,ft.subject,ft.fbid,fb.board,fb.fcid,ft.sticky});
80         if(param('t')){
81                 $thread = $DBH->selectrow_hashref($findThread,undef,param('t'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
82         }
83
84         if (defined param('cmd')){
85                 if(param('cmd') eq 'Submit'){
86                         $DBH->begin_work;
87                         if ($board && $board->{post}){
88                                 $thread = addForumThread $DBH,$board,$ND::UID,param('subject');
89                         }
90                         if ($thread && $thread->{post}){
91                                 addForumPost($DBH,$thread,$ND::UID,param('message'));
92                         }
93                         $DBH->commit or $ND::ERROR .= p($DBH->errstr);
94                 }
95                 if(param('cmd') eq 'Move' && $board->{moderate}){
96                         $DBH->begin_work;
97                         my $moveThread = $DBH->prepare(q{UPDATE forum_threads SET fbid = $1 WHERE ftid = $2 AND fbid = $3});
98                         for my $param (param()){
99                                 if ($param =~ /t:(\d+)/){
100                                         $moveThread->execute(param('board'),$1,$board->{id}) or $ND::ERROR .= p($DBH->errstr);
101                                         if ($moveThread->rows > 0){
102                                                 log_message $ND::UID, qq{Moved thread: $1 to board: }.param('board');
103                                         }
104                                 }
105                         }
106                         $DBH->commit or $ND::ERROR .= p($DBH->errstr);
107                 }
108                 if($thread && param('cmd') eq 'Sticky' && $thread->{moderate}){
109                         if ($DBH->do(q{UPDATE forum_threads SET sticky = TRUE WHERE ftid = ?}, undef,$thread->{id})){
110                                 $thread->{sticky} = 1;
111                         }else{
112                                 $ND::ERROR .= p($DBH->errstr);
113                         }
114                 }
115                 if($thread && param('cmd') eq 'Unsticky' && $thread->{moderate}){
116                         if ($DBH->do(q{UPDATE forum_threads SET sticky = FALSE WHERE ftid = ?}, undef,$thread->{id})){
117                                 $thread->{sticky} = 0;
118                         }else{
119                                 $ND::ERROR .= p($DBH->errstr);
120                         }
121                 }
122         }
123
124         my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM forum_categories ORDER BY fcid});
125         my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
126                 FROM forum_boards fb NATURAL JOIN forum_access fa
127                 WHERE fb.fcid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
128                 WHERE uid = $2))
129                 GROUP BY fb.fbid,fb.board
130                 ORDER BY fb.fbid
131                         });
132         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, ft.sticky
133                 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
134                 WHERE ft.fbid = $1
135                 GROUP BY ft.ftid, ft.subject,ft.sticky
136                 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= $3
137                 ORDER BY sticky DESC,last_post DESC});
138
139         if ($thread){ #Display the thread
140                 $BODY->param(Title =>  $thread->{subject});
141                 $BODY->param(FBID =>  $thread->{fbid});
142                 $BODY->param(Board =>  $thread->{board});
143                 $BODY->param(FTID =>  $thread->{id});
144                 $BODY->param(Moderate =>  $thread->{moderate});
145                 $BODY->param(Sticky =>  $thread->{sticky} ? 'Unsticky' : 'Sticky');
146                 $BODY->param(Thread => viewForumThread $thread);
147                 my ($category) = $DBH->selectrow_array(q{SELECT category FROM forum_categories WHERE fcid = $1}
148                         ,undef,$thread->{fcid}) or $ND::ERROR .= p($DBH->errstr);
149                 $BODY->param(Category =>  $category);
150
151         }elsif(defined param('allUnread')){ #List threads in this board
152                 $BODY->param(AllUnread => 1);
153                 $BODY->param(Id => $board->{id});
154                 my ($time) = $DBH->selectrow_array('SELECT now()::timestamp',undef);
155                 $BODY->param(Date => $time);
156                 $categories->execute or $ND::ERROR .= p($DBH->errstr);
157                 my @categories;
158                 while (my $category = $categories->fetchrow_hashref){
159                         $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
160                         my @boards;
161                         while (my $board = $boards->fetchrow_hashref){
162                                 $threads->execute($board->{id},$ND::UID,1) or $ND::ERROR .= p($DBH->errstr);
163                                 my $i = 0;
164                                 my @threads;
165                                 while (my $thread = $threads->fetchrow_hashref){
166                                         $i++;
167                                         $thread->{Odd} = $i % 2;
168                                         push @threads,$thread;
169                                 }
170                                 $board->{Threads} = \@threads;
171                                 delete $board->{post};
172                                 push @boards,$board if $i > 0;
173                         }
174                         $category->{Boards} = \@boards;
175                         delete $category->{id};
176                         push @categories,$category if @boards;
177                 }
178                 $BODY->param(Categories => \@categories);
179
180         }elsif($board){ #List threads in this board
181                 $BODY->param(ViewBoard => 1);
182                 $BODY->param(Title => $board->{board});
183                 $BODY->param(Post => $board->{post});
184                 $BODY->param(Moderate => $board->{moderate});
185                 $BODY->param(Id => $board->{id});
186                 $BODY->param(FBID => $board->{id});
187                 $BODY->param(Board => $board->{board});
188                 my ($time) = $DBH->selectrow_array('SELECT now()::timestamp',undef);
189                 $BODY->param(Date => $time);
190                 $threads->execute($board->{id},$ND::UID,0) or $ND::ERROR .= p($DBH->errstr);
191                 my $i = 0;
192                 my @threads;
193                 while (my $thread = $threads->fetchrow_hashref){
194                         $i++;
195                         $thread->{Odd} = $i % 2;
196                         push @threads,$thread;
197                 }
198                 $BODY->param(Threads => \@threads);
199
200                 if ($board->{moderate}){
201                         $categories->execute or $ND::ERROR .= p($DBH->errstr);
202                         my @categories;
203                         while (my $category = $categories->fetchrow_hashref){
204                                 $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
205
206                                 my @boards;
207                                 while (my $b = $boards->fetchrow_hashref){
208                                         next if (not $b->{post} or $b->{id} == $board->{id});
209                                         delete $b->{post};
210                                         push @boards,$b;
211                                 }
212                                 $category->{Boards} = \@boards;
213                                 delete $category->{id};
214                                 push @categories,$category if @boards;
215                         }
216                         $BODY->param(Categories => \@categories);
217                 }
218                 my ($category) = $DBH->selectrow_array(q{SELECT category FROM forum_categories WHERE fcid = $1}
219                         ,undef,$board->{fcid}) or $ND::ERROR .= p($DBH->errstr);
220                 $BODY->param(Category =>  $category);
221
222         }else{ #List boards
223                 $BODY->param(Overview => 1);
224                 $categories->execute or $ND::ERROR .= p($DBH->errstr);
225                 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
226                         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
227                         WHERE fb.fcid = $1 AND 
228                         fb.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($2)))
229                         GROUP BY fb.fbid, fb.board
230                         ORDER BY fb.fbid        });
231                 my @categories;
232                 while (my $category = $categories->fetchrow_hashref){
233                         $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
234                         my @boards;
235                         my $i = 0;
236                         while (my $board = $boards->fetchrow_hashref){
237                                 $i++;
238                                 $board->{Odd} = $i % 2;
239                                 push @boards,$board;
240                         }
241                         $category->{Boards} = \@boards;
242                         delete $category->{id};
243                         push @categories,$category if $i > 0;
244                 }
245                 $BODY->param(Categories => \@categories);
246
247         }
248         return $BODY;
249 }
250
251 1;
252