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