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