]> ruin.nu Git - ndwebbie.git/blob - forum.pl
restructure
[ndwebbie.git] / forum.pl
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 use strict;
21 use warnings FATAL => 'all';
22 no warnings 'uninitialized';
23 use ND::Web::Forum;
24
25 $ND::TEMPLATE->param(TITLE => 'Forum');
26
27 our $BODY;
28 our $DBH;
29 our $ERROR;
30
31
32 my $board;
33 if(param('b')){
34 my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
35 FROM forum_boards fb NATURAL JOIN forum_access fa
36 WHERE fb.fbid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
37         WHERE uid = $2))
38 GROUP BY fb.fbid,fb.board});
39         $board = $DBH->selectrow_hashref($boards,undef,param('b'),$ND::UID) or $ERROR .= p($DBH->errstr);
40 }
41
42 my $thread;
43 my $findThread = $DBH->prepare(q{SELECT ft.ftid AS id,ft.subject, bool_or(fa.post) AS post
44 FROM forum_boards fb NATURAL JOIN forum_access fa NATURAL JOIN forum_threads ft
45 WHERE ft.ftid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
46         WHERE uid = $2))
47 GROUP BY ft.ftid,ft.subject});
48 if(param('t')){
49         $thread = $DBH->selectrow_hashref($findThread,undef,param('t'),$ND::UID) or $ERROR .= p($DBH->errstr);
50 }
51
52 if (defined param('cmd') && param('cmd') eq 'forumpost'){
53         $DBH->begin_work;
54         if ($board && $board->{post}){
55                 my $insert = $DBH->prepare(q{INSERT INTO forum_threads (fbid,subject) VALUES($1,$2)});
56                 if ($insert->execute($board->{id},escapeHTML(param('subject')))){
57                         $thread = $DBH->selectrow_hashref($findThread,undef,
58                                 $DBH->last_insert_id(undef,undef,undef,undef,"forum_threads_ftid_seq"),$ND::UID)
59                                 or $ERROR .= p($DBH->errstr);
60                 }else{
61                         $ERROR .= p($DBH->errstr);
62                 }
63         }
64         if ($thread && $thread->{post}){
65                 addForumPost($DBH,$thread,$ND::UID,param('message'));
66         }
67         $DBH->commit or $ERROR .= p($DBH->errstr);
68 }
69
70 my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM forum_categories ORDER BY fcid});
71 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
72 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
73 WHERE ft.fbid = $1
74 GROUP BY ft.ftid, ft.subject
75 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= $3});
76
77 if ($thread){ #Display the thread
78         $BODY->param(Thread => viewForumThread $thread);
79
80 }elsif($board){ #List threads in this board
81         $BODY->param(Board => $board->{board});
82         $BODY->param(Post => $board->{post});
83         $BODY->param(Id => $board->{id});
84         $threads->execute($board->{id},$ND::UID,0) or $ERROR .= p($DBH->errstr);
85         my $i = 0;
86         my @threads;
87         while (my $thread = $threads->fetchrow_hashref){
88                 $i++;
89                 $thread->{Odd} = $i % 2;
90                 push @threads,$thread;
91         }
92         $BODY->param(Threads => \@threads);
93
94 }elsif(defined param('allUnread')){ #List threads in this board
95         $BODY->param(AllUnread => 1);
96         $BODY->param(Id => $board->{id});
97         $categories->execute or $ERROR .= p($DBH->errstr);
98         my @categories;
99         my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
100 FROM forum_boards fb NATURAL JOIN forum_access fa
101 WHERE fb.fcid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
102         WHERE uid = $2))
103 GROUP BY fb.fbid,fb.board
104 });
105         while (my $category = $categories->fetchrow_hashref){
106                 $boards->execute($category->{id},$ND::UID) or $ERROR .= p($DBH->errstr);
107                 my @boards;
108                 while (my $board = $boards->fetchrow_hashref){
109                         $threads->execute($board->{id},$ND::UID,1) or $ERROR .= p($DBH->errstr);
110                         my $i = 0;
111                         my @threads;
112                         while (my $thread = $threads->fetchrow_hashref){
113                                 $i++;
114                                 $thread->{Odd} = $i % 2;
115                                 push @threads,$thread;
116                         }
117                         $board->{Threads} = \@threads;
118                         delete $board->{post};
119                         push @boards,$board if $i > 0;
120                 }
121                 $category->{Boards} = \@boards;
122                 delete $category->{id};
123                 push @categories,$category if @boards;
124         }
125         $BODY->param(Categories => \@categories);
126
127 }else{ #List boards
128         $BODY->param(Overview => 1);
129         $categories->execute or $ERROR .= p($DBH->errstr);
130 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
131 FROM forum_boards fb NATURAL JOIN forum_access fa 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
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         my @categories;
137         while (my $category = $categories->fetchrow_hashref){
138                 $boards->execute($category->{id},$ND::UID) or $ERROR .= p($DBH->errstr);
139                 my @boards;
140                 my $i = 0;
141                 while (my $board = $boards->fetchrow_hashref){
142                         $i++;
143                         $board->{Odd} = $i % 2;
144                         push @boards,$board;
145                 }
146                 $category->{Boards} = \@boards;
147                 delete $category->{id};
148                 push @categories,$category if $i > 0;
149         }
150         $BODY->param(Categories => \@categories);
151
152 }
153
154 1;
155