]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/Forum.pm
trunc to seconds for thread listing
[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
27 $ND::PAGES{forum} = {parse => \&parse, process => \&process, render=> \&render};
28
29 sub parse {
30         my ($uri) = @_;
31         if ($uri =~ m{^/.*/allUnread}){
32                 param('allUnread',1);
33         }
34 }
35
36 sub process {
37
38 }
39
40 sub render {
41         my ($DBH,$BODY) = @_;
42
43         $ND::TEMPLATE->param(TITLE => 'Forum');
44
45         $DBH->do(q{UPDATE users SET last_forum_visit = NOW() WHERE uid = $1},undef,$ND::UID) or $ND::ERROR .= p($DBH->errstr);
46
47         my $board;
48         if(param('b')){
49                 my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
50                         FROM forum_boards fb NATURAL JOIN forum_access fa
51                         WHERE fb.fbid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
52                         WHERE uid = $2))
53                         GROUP BY fb.fbid,fb.board});
54                 $board = $DBH->selectrow_hashref($boards,undef,param('b'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
55         }
56         if (param('markAsRead')){
57                 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
58                 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
59                 WHERE ((ft.fbid IS NULL AND $1 IS NULL) OR ft.fbid = $1) AND fp.time <= $3
60                 GROUP BY ft.ftid, ft.subject
61                 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= 1
62                 });
63
64                 $threads->bind_param('$1',$board->{id},{TYPE => DBI::SQL_INTEGER }) or $ND::ERROR .= p($DBH->errstr);
65                 $threads->bind_param('$2',$ND::UID,{TYPE => DBI::SQL_INTEGER }) or $ND::ERROR .= p($DBH->errstr);
66                 $threads->bind_param('$3',param('markAsRead')) or $ND::ERROR .= p($DBH->errstr);
67                 $threads->execute or $ND::ERROR .= p($DBH->errstr);
68                 while (my $thread = $threads->fetchrow_hashref){
69                         markThreadAsRead $thread->{id};
70                 }
71         }
72
73         my $thread;
74         my $findThread = $DBH->prepare(q{SELECT ft.ftid AS id,ft.subject, bool_or(fa.post) AS post
75                 FROM forum_boards fb NATURAL JOIN forum_access fa NATURAL JOIN forum_threads ft
76                 WHERE ft.ftid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
77                 WHERE uid = $2))
78                 GROUP BY ft.ftid,ft.subject});
79         if(param('t')){
80                 $thread = $DBH->selectrow_hashref($findThread,undef,param('t'),$ND::UID) or $ND::ERROR .= p($DBH->errstr);
81         }
82
83         if (defined param('cmd') && param('cmd') eq 'Submit'){
84                 $DBH->begin_work;
85                 if ($board && $board->{post}){
86                         $thread = addForumThread $DBH,$board,$ND::UID,param('subject');
87                 }
88                 if ($thread && $thread->{post}){
89                         addForumPost($DBH,$thread,$ND::UID,param('message'));
90                 }
91                 $DBH->commit or $ND::ERROR .= p($DBH->errstr);
92         }
93
94         my $categories = $DBH->prepare(q{SELECT fcid AS id,category FROM forum_categories ORDER BY fcid});
95         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
96                 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
97                 WHERE ft.fbid = $1
98                 GROUP BY ft.ftid, ft.subject
99                 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= $3
100                 ORDER BY last_post DESC});
101
102         if ($thread){ #Display the thread
103                 $BODY->param(Thread => viewForumThread $thread);
104
105         }elsif(defined param('allUnread')){ #List threads in this board
106                 $BODY->param(AllUnread => 1);
107                 $BODY->param(Id => $board->{id});
108                 my ($time) = $DBH->selectrow_array('SELECT now()::timestamp',undef);
109                 $BODY->param(Date => $time);
110                 $categories->execute or $ND::ERROR .= p($DBH->errstr);
111                 my @categories;
112                 my $boards = $DBH->prepare(q{SELECT fb.fbid AS id,fb.board, bool_or(fa.post) AS post
113                         FROM forum_boards fb NATURAL JOIN forum_access fa
114                         WHERE fb.fcid = $1 AND (gid = -1 OR gid IN (SELECT gid FROM groupmembers
115                         WHERE uid = $2))
116                         GROUP BY fb.fbid,fb.board
117                         ORDER BY fb.fbid
118                         });
119                 while (my $category = $categories->fetchrow_hashref){
120                         $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
121                         my @boards;
122                         while (my $board = $boards->fetchrow_hashref){
123                                 $threads->execute($board->{id},$ND::UID,1) or $ND::ERROR .= p($DBH->errstr);
124                                 my $i = 0;
125                                 my @threads;
126                                 while (my $thread = $threads->fetchrow_hashref){
127                                         $i++;
128                                         $thread->{Odd} = $i % 2;
129                                         push @threads,$thread;
130                                 }
131                                 $board->{Threads} = \@threads;
132                                 delete $board->{post};
133                                 push @boards,$board if $i > 0;
134                         }
135                         $category->{Boards} = \@boards;
136                         delete $category->{id};
137                         push @categories,$category if @boards;
138                 }
139                 $BODY->param(Categories => \@categories);
140
141         }elsif($board){ #List threads in this board
142                 $BODY->param(Board => $board->{board});
143                 $BODY->param(Post => $board->{post});
144                 $BODY->param(Id => $board->{id});
145                 my ($time) = $DBH->selectrow_array('SELECT now()::timestamp',undef);
146                 $BODY->param(Date => $time);
147                 $threads->execute($board->{id},$ND::UID,0) or $ND::ERROR .= p($DBH->errstr);
148                 my $i = 0;
149                 my @threads;
150                 while (my $thread = $threads->fetchrow_hashref){
151                         $i++;
152                         $thread->{Odd} = $i % 2;
153                         push @threads,$thread;
154                 }
155                 $BODY->param(Threads => \@threads);
156
157         }else{ #List boards
158                 $BODY->param(Overview => 1);
159                 $categories->execute or $ND::ERROR .= p($DBH->errstr);
160                 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
161                         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
162                         WHERE fb.fcid = $1 AND 
163                         fb.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($2)))
164                         GROUP BY fb.fbid, fb.board
165                         ORDER BY fb.fbid        });
166                 my @categories;
167                 while (my $category = $categories->fetchrow_hashref){
168                         $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
169                         my @boards;
170                         my $i = 0;
171                         while (my $board = $boards->fetchrow_hashref){
172                                 $i++;
173                                 $board->{Odd} = $i % 2;
174                                 push @boards,$board;
175                         }
176                         $category->{Boards} = \@boards;
177                         delete $category->{id};
178                         push @categories,$category if $i > 0;
179                 }
180                 $BODY->param(Categories => \@categories);
181
182         }
183         return $BODY;
184 }
185
186 1;
187