]> ruin.nu Git - ndwebbie.git/blob - forum.pl
posting seems to work
[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 my $board;
30 if(param('b')){
31         my $query = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
32 FROM forum_boards fb NATURAL JOIN forum_access fa
33 WHERE fb.fbid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
34         WHERE uid = $2))
35 GROUP BY fb.fbid,fb.board});
36         $board = $DBH->selectrow_hashref($query,undef,param('b'),$ND::UID) or $ERROR .= p($DBH->errstr);
37 }
38
39 my $thread;
40 my $findThread = $DBH->prepare(q{SELECT ft.ftid AS id,ft.subject, bool_or(fa.post) AS post
41 FROM forum_boards fb NATURAL JOIN forum_access fa NATURAL JOIN forum_threads ft
42 WHERE ft.ftid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
43         WHERE uid = $2))
44 GROUP BY ft.ftid,ft.subject});
45 if(param('t')){
46         $thread = $DBH->selectrow_hashref($findThread,undef,param('t'),$ND::UID) or $ERROR .= p($DBH->errstr);
47 }
48
49 if (defined param('cmd') && param('cmd') eq 'submit'){
50         $DBH->begin_work;
51         if ($board && $board->{post}){
52                 my $insert = $DBH->prepare(q{INSERT INTO forum_threads (fbid,subject) VALUES($1,$2)});
53                 if ($insert->execute($board->{id},param('subject'))){
54                         $thread = $DBH->selectrow_hashref($findThread,undef,
55                                 $DBH->last_insert_id(undef,undef,undef,undef,"forum_threads_ftid_seq"),$ND::UID)
56                                 or $ERROR .= p($DBH->errstr);
57                 }else{
58                         $ERROR .= p($DBH->errstr);
59                 }
60         }
61         if ($thread && $thread->{post}){
62                 my $insert = $DBH->prepare(q{INSERT INTO forum_posts (ftid,message,uid) VALUES($1,$2,$3)});
63                 $insert->execute($thread->{id},escapeHTML(param('message')),$ND::UID) or $ERROR .= p($DBH->errstr);
64         }
65         $DBH->commit or $ERROR .= p($DBH->errstr);
66 }
67
68
69 if ($thread){ #Display the thread
70         $BODY->param(Thread => 1);
71         $BODY->param(Subject => $thread->{subject});
72         $BODY->param(Id => $thread->{id});
73         $BODY->param(Post => $thread->{post});
74
75         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
76 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
77 WHERE ft.ftid = $1
78 ORDER BY fp.time ASC});
79         $posts->execute($thread->{id},$ND::UID) or $ERROR .= p($DBH->errstr);
80         my @posts;
81         my $old = 1;
82         while (my $post = $posts->fetchrow_hashref){
83                 if ($old && $post->{unread}){
84                         $old = 0;
85                         $post->{NewPosts} = 1;
86                 }
87                 $post->{message} = parseMarkup($post->{message});
88                 push @posts,$post;
89         }
90         $BODY->param(Posts => \@posts);
91
92         markThreadAsRead($thread->{id});
93
94 }elsif($board){ #List threads in this board
95         $BODY->param(Board => $board->{board});
96         $BODY->param(Post => $board->{post});
97         $BODY->param(Id => $board->{id});
98         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
99 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
100 WHERE ft.fbid = $1
101 GROUP BY ft.ftid, ft.subject});
102         $threads->execute($board->{id},$ND::UID) or $ERROR .= p($DBH->errstr);
103         my $i = 0;
104         my @threads;
105         while (my $thread = $threads->fetchrow_hashref){
106                 $i++;
107                 $thread->{Odd} = $i % 2;
108                 push @threads,$thread;
109         }
110         $BODY->param(Threads => \@threads);
111
112 }else{ #List boards
113         $BODY->param(Overview => 1);
114         my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM
115                 forum_categories});
116         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
117 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
118 WHERE fb.fcid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
119                 WHERE uid = $2))
120 GROUP BY fb.fbid, fb.board
121                 });
122         $categories->execute or $ERROR .= p($DBH->errstr);
123         my @categories;
124         while (my $category = $categories->fetchrow_hashref){
125                 $boards->execute($category->{id},$ND::UID) or $ERROR .= p($DBH->errstr);
126                 my @boards;
127                 my $i = 0;
128                 while (my $board = $boards->fetchrow_hashref){
129                         $i++;
130                         $board->{Odd} = $i % 2;
131                         push @boards,$board;
132                 }
133                 $category->{Boards} = \@boards;
134                 delete $category->{id};
135                 push @categories,$category if $i > 0;
136         }
137         $BODY->param(Categories => \@categories);
138
139 }
140
141 1;
142