commiting forgotten changes before releasing 1.3.2
[koha.git] / search.pl
blob81677bc0259a6c79f0872f7912f6e02fbe6bb0b9
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
21 require Exporter;
22 use CGI;
23 use HTML::Template;
24 use C4::Context;
25 use C4::Search;
26 use C4::Auth;
27 use C4::Output;
29 my $query=new CGI;
30 my $type=$query->param('type');
32 #(-e "opac") && ($type='opac');
34 my ($loggedinuser, $cookie, $sessionID) = checkauth($query, ($type eq 'opac') ? (1) : (0));
36 my $startfrom=$query->param('startfrom');
37 ($startfrom) || ($startfrom=0);
39 my $subject=$query->param('subject');
40 my $template;
41 # if it's a subject we need to use the subject.tmpl
42 if ($subject) {
43 $template = gettemplate("catalogue/subject.tmpl",0);
44 } else {
45 $template = gettemplate("catalogue/searchresults.tmpl", 0);
48 # %env
49 # Additional parameters for &catalogsearch
50 my %env = (
51 itemcount => 1, # If set to 1, &catalogsearch enumerates
52 # the results found and returns the number
53 # of items found, where they're located,
54 # etc.
57 # get all the search variables
58 # we assume that C4::Search will validate these values for us
59 my %search; # Search terms. If the key is "author",
60 # then $search{author} is the author the
61 # user is looking for.
63 my @forminputs; # This is used in the form template.
65 foreach my $term (qw(keyword subject author illustrator itemnumber
66 isbn date-before class dewey branch title abstract
67 publisher ttype))
69 my $value = $query->param($term);
71 next unless defined $value && $value ne "";
72 # Skip blank search terms
73 $search{$term} = $value;
74 push @forminputs, { line => "$term=$value" };
77 $template->param(FORMINPUTS => \@forminputs);
79 # whats this for?
80 # I think it is (or was) a search from the "front" page... [st]
81 $search{'front'}=$query->param('front');
83 my $num=10;
84 my @results;
85 my $count;
86 if (my $subject=$query->param('subjectitems')) {
87 my $blah;
88 @results=subsearch(\$blah,$subject);
89 $count=$#results+1;
90 } else {
91 ($count,@results)=catalogsearch(\%env,'',\%search,$num,$startfrom);
94 #my $resultsarray=\@results;
95 my $resultsarray;
97 foreach my $result (@results) {
98 $result->{'authorhtmlescaped'}=$result->{'author'};
99 $result->{'authorhtmlescaped'}=~s/ /%20/g;
100 ($result->{'copyrightdate'}==0) && ($result->{'copyrightdate'}='');
101 ($type eq 'opac') ? ($result->{'opac'}=1) : ($result->{'opac'}=0);
102 push (@$resultsarray, $result);
104 ($resultsarray) || (@$resultsarray=());
105 my $search="num=20";
106 my $searchdesc='';
107 if ($search{"keyword"}) {
108 $search .= "&keyword=$search{keyword}";
109 $searchdesc.="keyword $search{keyword}, ";
111 if (my $subjectitems=$query->param('subjectitems')){
112 $search .= "&subjectitems=$subjectitems";
113 $searchdesc.="subject $subjectitems, ";
115 if ($subject){
116 $search .= "&subject=$subject";
117 $searchdesc.="subject $subject, ";
119 if ($search{"author"}){
120 $search .= "&author=$search{author}";
121 $searchdesc.="author $search{author}, ";
123 if ($search{"class"}){
124 $search .= "&class=$search{class}";
125 $searchdesc.="class $search{class}, ";
127 if ($search{"title"}){
128 $search .= "&title=$search{title}";
129 $searchdesc.="title $search{title}, ";
131 if ($search{"dewey"}){
132 $search .= "&dewey=$search{dewey}";
133 $searchdesc.="dewey $search{dewey}, ";
135 $search.="&ttype=$search{ttype}";
137 $search=~ s/ /%20/g;
138 $template->param(startfrom => $startfrom+1);
139 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
140 $template->param(numrecords => $count);
141 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
142 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
143 $template->param(nextstartfrom => $nextstartfrom);
144 my $displaynext=1;
145 my $displayprev=0;
146 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
147 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
148 $template->param(displaynext => $displaynext);
149 $template->param(displayprev => $displayprev);
150 ($type eq 'opac') ? ($template->param(opac => 1)) : ($template->param(opac => 0));
151 $template->param(prevstartfrom => $prevstartfrom);
152 $template->param(search => $search);
153 $template->param(searchdesc => $searchdesc);
154 $template->param(SEARCH_RESULTS => $resultsarray);
155 #$template->param(includesdir => $includes);
156 $template->param(loggedinuser => $loggedinuser);
158 my @numbers = ();
159 if ($count>10) {
160 for (my $i=1; $i<$count/10+1; $i++) {
161 if ($i<16) {
162 if ($search{"title"})
164 push @forminputs, { line => "title=$search{title}"};
166 my $highlight=0;
167 ($startfrom==($i-1)*10) && ($highlight=1);
168 my $formelements='';
169 foreach (@forminputs) {
170 my $line=$_->{line};
171 $formelements.="$line&";
173 $formelements=~s/ /+/g;
174 push @numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => \@forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
179 $template->param(numbers => \@numbers);
180 if (C4::Context->preference('acquisitions') eq 'simple') {
181 $template->param(script => "MARCdetail.pl");
182 } else {
183 $template->param(script => "detail.pl");
186 # Print the page
187 print $query->header(-cookie => $cookie), $template->output;