]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/Forum/Search.pm
1789bc71760f7502133a7380b1f49d2ea469a0dd
[ndwebbie.git] / NDWeb / Pages / Forum / Search.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 NDWeb::Pages::Forum::Search;
21 use strict;
22 use warnings;
23 use NDWeb::Forum;
24 use CGI qw/:standard/;
25 use NDWeb::Include;
26 use ND::Include;
27
28 use base qw/NDWeb::Pages::Forum/;
29
30 sub render_body {
31         my $self = shift;
32         my ($BODY) = @_;
33         my $DBH = $self->{DBH};
34         $self->{TITLE} = 'Forum';
35         my @queries;
36         if (param('search')){
37                 push @queries,'('.param('search').')';
38         }
39         my %cat = (body => 'D', topic => 'A', author => 'B');
40         for ('body','topic','author'){
41                 if (param($_)){
42                         my @words = split /\W+/,param($_);
43                         my $op = param('all'.$_) ? '&' : '|';
44                         my $cat = $cat{$_};
45                         my $query = join " $op ", map {"$_:$cat"} @words;
46                         push @queries,"($query)";
47                 }
48         }
49         my $search = join ' & ', @queries;
50
51         if ($search){
52                 my $posts = $DBH->prepare(q{SELECT fp.ftid,u.username,ft.subject
53                         ,ts_headline(fp.message,to_tsquery($2)) AS headline
54                         ,ts_rank_cd(fp.textsearch, to_tsquery($2),32) AS rank
55                         FROM forum_boards fb 
56                                 JOIN forum_threads ft USING (fbid)
57                                 JOIN forum_posts fp USING (ftid)
58                                 JOIN users u ON fp.uid = u.uid
59                         WHERE fb.fbid IN (SELECT fbid FROM forum_access 
60                                         WHERE gid IN (SELECT groups($1)))
61                                 AND fp.textsearch @@@ to_tsquery($2)
62                         ORDER BY rank DESC
63                 }) or warn $DBH->errstr;
64                 $posts->execute($ND::UID,$search) or warn escapeHTML $DBH->errstr;
65                 my @posts;
66                 while (my $post = $posts->fetchrow_hashref){
67                         push @posts,$post;
68                 }
69                 $BODY->param(SearchResult => \@posts);
70         }
71         return $BODY;
72 }
73
74 1;