Now works with all searches, this plus the templates should now be close to
[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 #print $query->header;
32
33 my $includes=$configfile{'includes'};
34 ($includes) || ($includes="/usr/local/www/hdl/htdocs/includes");
35 my $templatebase="catalogue/searchresults.tmpl";
36 my $startfrom=$query->param('startfrom');
37 ($startfrom) || ($startfrom=0);
38 ($templatename) || ($templatename=picktemplate($templatebase));
39
40
41 my $template = HTML::Template->new(filename => "$includes/templates/$theme/$templatebase", die_on_bad_params => 0, path => [$includes]);
42
43 my $env;
44 $env->{itemcount}=1;
45
46 # get all the search variables
47 # we assume that C4::Search will validate these values for us
48 my %search;
49 my $keyword=$query->param('keyword');
50 $search{'keyword'}=$keyword;
51 my $subject=$query->param('subject');
52 $search{'subject'}=$subject;
53 my $author=$query->param('author');
54 $search{'author'}=$author;
55 my $illustrator=$query->param('illustrator');
56 $search{'param'}=$illustrator;
57 my $itemnumber=$query->param('itemnumber');
58 $search{'itemnumber'}=$itemnumber;
59 my $isbn=$query->param('isbn');
60 $search{'isbn'}=$isbn;
61 my $datebefore=$query->param('date-before');
62 $search{'date-before'}=$datebefore;
63 my $class=$query->param('class');
64 $search{'class'}=$class;
65 my $dewey=$query->param('dewey');
66 $search{'dewey'};
67 my $branch=$query->param('branch');
68 $search{'branch'}=$branch;
69 my $title=$query->param('title');
70 $search{'title'}=$title;
71 my $ttype=$query->param('ttype');
72 $search{'ttype'}=$ttype;
73
74 # whats this for?
75 $search{'front'}=$query->param('front');
76
77 my $num=20;
78 my ($count,@results)=catalogsearch($env,'',\%search,$num,$startfrom);
79
80 my $resultsarray=\@results;
81
82 my $search="num=20";
83 if ($keyword){
84     $search=$search."&keyword=$keyword";
85 }
86 if ($subject){
87     $search=$search."&subject=$subject";
88 }
89 if ($author){
90     $search=$search."&author=$author";
91 }
92 if ($class){
93     $search=$search."&class=$class";
94 }
95 if ($title){
96     $search=$search."&title=$title";
97 }
98 if ($dewey){
99     $search=$search."&dewey=$dewey";
100 }
101 $search.="&ttype=$ttype";
102
103 $search=~ s/ /%20/g;
104 $template->param(startfrom => $startfrom+1);
105 $template->param(endat => $startfrom+20);
106 $template->param(numrecords => $count);
107 my $nextstartfrom=($startfrom+20<$count-20) ? ($startfrom+20) : ($count-20);
108 my $prevstartfrom=($startfrom-20>0) ? ($startfrom-20) : (0);
109 $template->param(nextstartfrom => $nextstartfrom);
110 $template->param(prevstartfrom => $prevstartfrom);
111 $template->param(template => $templatename);
112 $template->param(search => $search);
113 $template->param(SEARCH_RESULTS => $resultsarray);
114 $template->param(includesdir => $includes);
115
116
117 print "Content-Type: text/html\n\n", $template->output;
118
119
120 sub picktemplate {
121     my ($includes, $base) = @_;
122     my $templates;
123     opendir (D, "$includes/templates");
124     my @dirlist=readdir D;
125     foreach (@dirlist) {
126         (next) if (/^\./);
127         #(next) unless (/\.tmpl$/);
128         (next) unless (-e "$includes/templates/$_/$base");
129         $templates->{$_}=1;
130     }
131     my $sth=$dbh->prepare("select value from systempreferences where variable='template'");
132     $sth->execute;
133     my ($preftemplate) = $sth->fetchrow;
134     $preftemplate.='.tmpl';
135     if ($templates->{$preftemplate}) {
136         return $preftemplate;
137     } else {
138         return 'default';
139     }
140     
141 }