bugfix
[koha.git] / search.pl
1 #!/usr/bin/perl
2 use HTML::Template;
3 #script to provide intranet (librarian) advanced search facility
4
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 use strict;
24 require Exporter;
25 use C4::Database;
26 use CGI;
27 use C4::Search;
28 use C4::Output; # no contains picktemplate
29   
30 my $query=new CGI;
31
32
33 my $language='french';
34
35
36 my %configfile;
37 open (KC, "/etc/koha.conf");
38 while (<KC>) {
39  chomp;
40  (next) if (/^\s*#/);
41  if (/(.*)\s*=\s*(.*)/) {
42    my $variable=$1;
43    my $value=$2;
44    # Clean up white space at beginning and end
45    $variable=~s/^\s*//g;
46    $variable=~s/\s*$//g;
47    $value=~s/^\s*//g;
48    $value=~s/\s*$//g;
49    $configfile{$variable}=$value;
50  }
51 }
52 #print $query->header;
53
54 my $includes=$configfile{'includes'};
55 ($includes) || ($includes="/usr/local/www/hdl/htdocs/includes");
56 my $templatebase="catalogue/searchresults.tmpl";
57 my $startfrom=$query->param('startfrom');
58 ($startfrom) || ($startfrom=0);
59 my $theme=picktemplate($includes, $templatebase);
60
61 my $subject=$query->param('subject');
62 # if its a subject we need to use the subject.tmpl
63 if ($subject) {
64     $templatebase=~ s/searchresults\.tmpl/subject\.tmpl/;
65     $theme=picktemplate($includes, $templatebase);
66 }
67
68 my $template = HTML::Template->new(filename => "$includes/templates/$theme/$templatebase", die_on_bad_params => 0, path => [$includes]);
69
70 my $env;
71 $env->{itemcount}=1;
72
73 # get all the search variables
74 # we assume that C4::Search will validate these values for us
75 my %search;
76 my $keyword=$query->param('keyword');
77 $search{'keyword'}=$keyword;
78
79 $search{'subject'}=$subject;
80 my $author=$query->param('author');
81 $search{'author'}=$author;
82 my $illustrator=$query->param('illustrator');
83 $search{'param'}=$illustrator;
84 my $itemnumber=$query->param('itemnumber');
85 $search{'itemnumber'}=$itemnumber;
86 my $isbn=$query->param('isbn');
87 $search{'isbn'}=$isbn;
88 my $datebefore=$query->param('date-before');
89 $search{'date-before'}=$datebefore;
90 my $class=$query->param('class');
91 $search{'class'}=$class;
92 my $dewey=$query->param('dewey');
93 $search{'dewey'};
94 my $branch=$query->param('branch');
95 $search{'branch'}=$branch;
96 my $title=$query->param('title');
97 $search{'title'}=$title;
98 my $abstract=$query->param('abstract');
99 $search{'abstract'}=$abstract;
100 my $publisher=$query->param('publisher');
101 $search{'publisher'}=$publisher;
102
103 my $ttype=$query->param('ttype');
104 $search{'ttype'}=$ttype;
105
106 my $forminputs;
107 ($keyword) && (push @$forminputs, { line => "keyword=$keyword"});
108 ($subject) && (push @$forminputs, { line => "subject=$subject"});
109 ($author) && (push @$forminputs, { line => "author=$author"});
110 ($illustrator) && (push @$forminputs, { line => "illustrator=$illustrator"});
111 ($itemnumber) && (push @$forminputs, { line => "itemnumber=$itemnumber"});
112 ($isbn) && (push @$forminputs, { line => "isbn=$isbn"});
113 ($datebefore) && (push @$forminputs, { line => "date-before=$datebefore"});
114 ($class) && (push @$forminputs, { line => "class=$class"});
115 ($dewey) && (push @$forminputs, { line => "dewey=$dewey"});
116 ($branch) && (push @$forminputs, { line => "branch=$branch"});
117 ($title) && (push @$forminputs, { line => "title=$title"});
118 ($ttype) && (push @$forminputs, { line => "ttype=$ttype"});
119 ($abstract) && (push @$forminputs, { line => "abstract=$abstract"});
120 ($publisher) && (push @$forminputs, { line => "publisher=$publisher"});
121 $template->param(FORMINPUTS => $forminputs);
122 # whats this for?
123 # I think it is (or was) a search from the "front" page...   [st]
124 $search{'front'}=$query->param('front');
125
126 my $num=10;
127 my ($count,@results)=catalogsearch($env,'',\%search,$num,$startfrom);
128
129 my $resultsarray=\@results;
130
131 my $search="num=20";
132 if ($keyword){
133     $search=$search."&keyword=$keyword";
134 }
135 if ($subject){
136     $search=$search."&subject=$subject";
137 }
138 if ($author){
139     $search=$search."&author=$author";
140 }
141 if ($class){
142     $search=$search."&class=$class";
143 }
144 if ($title){
145     $search=$search."&title=$title";
146 }
147 if ($dewey){
148     $search=$search."&dewey=$dewey";
149 }
150 $search.="&ttype=$ttype";
151
152 $search=~ s/ /%20/g;
153 $template->param(startfrom => $startfrom+1);
154 $template->param(endat => $startfrom+$num);
155 $template->param(numrecords => $count);
156 my $nextstartfrom=($startfrom+$num<$count-$num) ? ($startfrom+$num) : ($count-$num);
157 my $prevstartfrom=($startfrom-$num>0) ? ($startfrom-$num) : (0);
158 $template->param(nextstartfrom => $nextstartfrom);
159 $template->param(prevstartfrom => $prevstartfrom);
160 $template->param(search => $search);
161 $template->param(SEARCH_RESULTS => $resultsarray);
162 $template->param(includesdir => $includes);
163
164
165 print "Content-Type: text/html\n\n", $template->output;
166