Changes:
[koha.git] / html-template / search.pl
1 #!/usr/bin/perl
2 use HTML::Template;
3 use strict;
4 require Exporter;
5 use C4::Database;
6 use CGI;
7 use C4::Search;
8  
9 my $query=new CGI;
10
11
12 my $language='french';
13 my $dbh=&C4Connect;  
14
15 my %configfile;
16 open (KC, "/etc/koha.conf");
17 while (<KC>) {
18  chomp;
19  (next) if (/^\s*#/);
20  if (/(.*)\s*=\s*(.*)/) {
21    my $variable=$1;
22    my $value=$2;
23    # Clean up white space at beginning and end
24    $variable=~s/^\s*//g;
25    $variable=~s/\s*$//g;
26    $value=~s/^\s*//g;
27    $value=~s/\s*$//g;
28    $configfile{$variable}=$value;
29  }
30 }
31 my $includes=$configfile{'includes'};
32 ($includes) || ($includes="/usr/local/www/hdl/htdocs/includes");
33 my $templatebase="$includes/templates/catalogue/searchresults/";
34 my $templatename=$query->param('template');
35 my $startfrom=$query->param('startfrom');
36 ($startfrom) || ($startfrom=0);
37 ($templatename) || ($templatename='default.tmpl');
38 $templatename=picktemplate($templatebase);
39
40
41
42
43 my $template = HTML::Template->new(filename => "$templatebase$templatename", die_on_bad_params => 0);
44
45 ##my @results;
46 #my $sth=$dbh->prepare("select * from biblio where author like 's%' order by author limit $startfrom,20");
47 #$sth->execute;
48 #while (my $data=$sth->fetchrow_hashref){    
49 #    push @results, $data;
50 #}
51
52 my $env;
53 $env->{itemcount}=1;
54 my %search;
55 my $keyword='tree';
56 $search{'keyword'}=$keyword;
57
58 my ($count, @results) = &KeywordSearch($env, 'intra', \%search, 20, $startfrom);
59
60
61 my $resultsarray=\@results;
62
63 $template->param(startfrom => $startfrom+1);
64 $template->param(endat => $startfrom+20);
65 $template->param(numrecords => $count);
66 my $nextstartfrom=($startfrom+20<$count-20) ? ($startfrom+20) : ($count-20);
67 my $prevstartfrom=($startfrom-20>0) ? ($startfrom-20) : (0);
68 $template->param(nextstartfrom => $nextstartfrom);
69 $template->param(prevstartfrom => $prevstartfrom);
70 $template->param(template => $templatename);
71 $template->param(SEARCH_RESULTS => $resultsarray);
72
73 print "Content-Type: text/html\n\n", $template->output;
74
75
76 sub picktemplate {
77     my ($base) = @_;
78     my $templates;
79     opendir (D, $base);
80     my @dirlist=readdir D;
81     foreach (@dirlist) {
82         (next) unless (/\.tmpl$/);
83         $templates->{$_}=1;
84     }
85     my $sth=$dbh->prepare("select value from systempreferences where variable='template'");
86     $sth->execute;
87     my ($preftemplate) = $sth->fetchrow;
88     $preftemplate.='.tmpl';
89     if ($templates->{$preftemplate}) {
90         return $preftemplate;
91     } else {
92         return 'default.tmpl';
93     }
94     
95 }