*** empty log message ***
[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 CGI;
26 use C4::Context;
27 use C4::Search;
28 use C4::Output; # no contains picktemplate
29   
30 my $query=new CGI;
31
32 my $includes = C4::Context->config('includes') ||
33         "/usr/local/www/hdl/htdocs/includes";
34 my $templatebase="catalogue/searchresults.tmpl";
35 my $startfrom=$query->param('startfrom');
36 ($startfrom) || ($startfrom=0);
37 my $theme=picktemplate($includes, $templatebase);
38
39 my $subject=$query->param('subject');
40 # if its a subject we need to use the subject.tmpl
41 if ($subject) {
42     $templatebase=~ s/searchresults\.tmpl/subject\.tmpl/;
43     $theme=picktemplate($includes, $templatebase);
44 }
45
46 my $template = HTML::Template->new(filename => "$includes/templates/$theme/$templatebase", die_on_bad_params => 0, path => [$includes]);
47
48 my $env;
49 $env->{itemcount}=1;
50
51 # get all the search variables
52 # we assume that C4::Search will validate these values for us
53 # FIXME - This whole section, up to the &catalogsearch call, is crying
54 # out for
55 #       foreach $search_term (qw(keyword subject author ...))
56 #       { ... }
57 my %search;
58 my $keyword=$query->param('keyword');
59 $search{'keyword'}=$keyword;
60
61 $search{'subject'}=$subject;
62 my $author=$query->param('author');
63 $search{'author'}=$author;
64 my $illustrator=$query->param('illustrator');
65 $search{'param'}=$illustrator;
66 my $itemnumber=$query->param('itemnumber');
67 $search{'itemnumber'}=$itemnumber;
68 my $isbn=$query->param('isbn');
69 $search{'isbn'}=$isbn;
70 my $datebefore=$query->param('date-before');
71 $search{'date-before'}=$datebefore;
72 my $class=$query->param('class');
73 $search{'class'}=$class;
74 my $dewey=$query->param('dewey');
75 $search{'dewey'};               # FIXME - This should be $search{'dewey'} = $dewey, right?
76 my $branch=$query->param('branch');
77 $search{'branch'}=$branch;
78 my $title=$query->param('title');
79 $search{'title'}=$title;
80 my $abstract=$query->param('abstract');
81 $search{'abstract'}=$abstract;
82 my $publisher=$query->param('publisher');
83 $search{'publisher'}=$publisher;
84
85 my $ttype=$query->param('ttype');
86 $search{'ttype'}=$ttype;
87
88 my $forminputs;
89 ($keyword) && (push @$forminputs, { line => "keyword=$keyword"});
90 ($subject) && (push @$forminputs, { line => "subject=$subject"});
91 ($author) && (push @$forminputs, { line => "author=$author"});
92 ($illustrator) && (push @$forminputs, { line => "illustrator=$illustrator"});
93 ($itemnumber) && (push @$forminputs, { line => "itemnumber=$itemnumber"});
94 ($isbn) && (push @$forminputs, { line => "isbn=$isbn"});
95 ($datebefore) && (push @$forminputs, { line => "date-before=$datebefore"});
96 ($class) && (push @$forminputs, { line => "class=$class"});
97 ($dewey) && (push @$forminputs, { line => "dewey=$dewey"});
98 ($branch) && (push @$forminputs, { line => "branch=$branch"});
99 ($title) && (push @$forminputs, { line => "title=$title"});
100 ($ttype) && (push @$forminputs, { line => "ttype=$ttype"});
101 ($abstract) && (push @$forminputs, { line => "abstract=$abstract"});
102 ($publisher) && (push @$forminputs, { line => "publisher=$publisher"});
103 $template->param(FORMINPUTS => $forminputs);
104 # whats this for?
105 # I think it is (or was) a search from the "front" page...   [st]
106 $search{'front'}=$query->param('front');
107
108 my $num=10;
109 my ($count,@results)=catalogsearch($env,'',\%search,$num,$startfrom);
110
111 my $resultsarray=\@results;
112
113 my $search="num=20";
114 if ($keyword){
115     $search=$search."&keyword=$keyword";
116 }
117 if ($subject){
118     $search=$search."&subject=$subject";
119 }
120 if ($author){
121     $search=$search."&author=$author";
122 }
123 if ($class){
124     $search=$search."&class=$class";
125 }
126 if ($title){
127     $search=$search."&title=$title";
128 }
129 if ($dewey){
130     $search=$search."&dewey=$dewey";
131 }
132 $search.="&ttype=$ttype";
133
134 $search=~ s/ /%20/g;
135 $template->param(startfrom => $startfrom+1);
136 $template->param(endat => $startfrom+$num);
137 $template->param(numrecords => $count);
138 my $nextstartfrom=($startfrom+$num<$count-$num) ? ($startfrom+$num) : ($count-$num);
139 my $prevstartfrom=($startfrom-$num>0) ? ($startfrom-$num) : (0);
140 $template->param(nextstartfrom => $nextstartfrom);
141 $template->param(prevstartfrom => $prevstartfrom);
142 $template->param(search => $search);
143 $template->param(SEARCH_RESULTS => $resultsarray);
144 $template->param(includesdir => $includes);
145
146
147 print "Content-Type: text/html\n\n", $template->output;
148