]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/Forum.pm
3e8f30e01a3f7ccd2046764c2663e2946216484b
[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 FATAL => 'all';
23 no warnings 'uninitialized';
24 use ND::Web::Forum;
25 use CGI qw/:standard/;
26 use ND::Web::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         my $board;
47         if(param('b')){
48                 my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
49                         FROM forum_boards fb NATURAL JOIN forum_access fa
50                         WHERE fb.fbid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
51                         WHERE uid = $2))
52                         GROUP BY fb.fbid,fb.board});
53                 $board = $DBH->selectrow_hashref($boards,undef,param('b'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
54         }
55
56         my $thread;
57         my $findThread = $DBH->prepare(q{SELECT ft.ftid AS id,ft.subject, bool_or(fa.post) AS post
58                 FROM forum_boards fb NATURAL JOIN forum_access fa NATURAL JOIN forum_threads ft
59                 WHERE ft.ftid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
60                 WHERE uid = $2))
61                 GROUP BY ft.ftid,ft.subject});
62         if(param('t')){
63                 $thread = $DBH->selectrow_hashref($findThread,undef,param('t'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
64         }
65
66         if (defined param('cmd') && param('cmd') eq 'forumpost'){
67                 $DBH->begin_work;
68                 if ($board && $board->{post}){
69                         $thread = addForumThread $DBH,$board,$ND::UID,param('subject');
70                 }
71                 if ($thread && $thread->{post}){
72                         addForumPost($DBH,$thread,$ND::UID,param('message'));
73                 }
74                 $DBH->commit or $ND::ERROR .= p($DBH->errstr);
75         }
76
77         my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM forum_categories ORDER BY fcid});
78         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
79                 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
80                 WHERE ft.fbid = $1
81                 GROUP BY ft.ftid, ft.subject
82                 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= $3
83                 ORDER BY last_post DESC});
84
85         if ($thread){ #Display the thread
86                 $BODY->param(Thread => viewForumThread $thread);
87
88         }elsif($board){ #List threads in this board
89                 $BODY->param(Board => $board->{board});
90                 $BODY->param(Post => $board->{post});
91                 $BODY->param(Id => $board->{id});
92                 $threads->execute($board->{id},$ND::UID,0) or $ND::ERROR .= p($DBH->errstr);
93                 my $i = 0;
94                 my @threads;
95                 while (my $thread = $threads->fetchrow_hashref){
96                         $i++;
97                         $thread->{Odd} = $i % 2;
98                         push @threads,$thread;
99                 }
100                 $BODY->param(Threads => \@threads);
101
102         }elsif(defined param('allUnread')){ #List threads in this board
103                 $BODY->param(AllUnread => 1);
104                 $BODY->param(Id => $board->{id});
105                 $categories->execute or $ND::ERROR .= p($DBH->errstr);
106                 my @categories;
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
110                         WHERE uid = $2))
111                         GROUP BY fb.fbid,fb.board
112                         ORDER BY fb.fbid
113                         });
114                 while (my $category = $categories->fetchrow_hashref){
115                         $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
116                         my @boards;
117                         while (my $board = $boards->fetchrow_hashref){
118                                 $threads->execute($board->{id},$ND::UID,1) or $ND::ERROR .= p($DBH->errstr);
119                                 my $i = 0;
120                                 my @threads;
121                                 while (my $thread = $threads->fetchrow_hashref){
122                                         $i++;
123                                         $thread->{Odd} = $i % 2;
124                                         push @threads,$thread;
125                                 }
126                                 $board->{Threads} = \@threads;
127                                 delete $board->{post};
128                                 push @boards,$board if $i > 0;
129                         }
130                         $category->{Boards} = \@boards;
131                         delete $category->{id};
132                         push @categories,$category if @boards;
133                 }
134                 $BODY->param(Categories => \@categories);
135
136         }else{ #List boards
137                 $BODY->param(Overview => 1);
138                 $categories->execute or $ND::ERROR .= p($DBH->errstr);
139                 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
140                         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
141                         WHERE fb.fcid = $1 AND 
142                         fb.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($2)))
143                         GROUP BY fb.fbid, fb.board
144                         ORDER BY fb.fbid        });
145                 my @categories;
146                 while (my $category = $categories->fetchrow_hashref){
147                         $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
148                         my @boards;
149                         my $i = 0;
150                         while (my $board = $boards->fetchrow_hashref){
151                                 $i++;
152                                 $board->{Odd} = $i % 2;
153                                 push @boards,$board;
154                         }
155                         $category->{Boards} = \@boards;
156                         delete $category->{id};
157                         push @categories,$category if $i > 0;
158                 }
159                 $BODY->param(Categories => \@categories);
160
161         }
162         return $BODY;
163 }
164
165 1;
166