synch'ing 2.2 and head
[koha.git] / search.pl
1 #!/usr/bin/perl
2
3 # $Id$
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 # $Log$
22 # Revision 1.36  2005/06/20 14:39:11  tipaul
23 # synch'ing 2.2 and head
24 #
25 # Revision 1.35.2.1  2005/05/27 10:02:50  hdl
26 # Bug Fixing : Using old search.pl with subjectitems In normal mode display.
27 # Problem : Changing Page doesn't work.
28 #
29 # Revision 1.35  2004/04/07 22:43:04  rangi
30 # Fix for bug 217
31 #
32 # Revision 1.34  2004/02/11 08:35:31  tipaul
33 # synch'ing 2.0.0 branch and head
34 #
35 # Revision 1.33  2003/12/19 17:28:03  tipaul
36 # fix for #683
37 #
38 # Revision 1.32.2.2  2004/01/13 17:33:39  tipaul
39 # removing useless (& buggy here) checkauth
40 #
41 # Revision 1.32.2.1  2003/12/19 17:28:42  tipaul
42 # fix for 683
43 #
44 # Revision 1.32  2003/06/11 18:37:55  tonnesen
45 # Using boolean_preference instead of preference for 'marc' setting
46 #
47 # Revision 1.31  2003/05/11 07:31:37  rangi
48 # Removing duplicate use C4::Auth
49 #
50
51 use strict;
52 require Exporter;
53 use CGI;
54 use C4::Auth;
55 use HTML::Template;
56 use C4::Context;
57 use C4::Search;
58 use C4::Output;
59 use C4::Interface::CGI::Output;
60
61 my $query=new CGI;
62 my $type=$query->param('type');
63
64 my $startfrom=$query->param('startfrom');
65 ($startfrom) || ($startfrom=0);
66
67 my $subject=$query->param('subject');
68 # if it's a subject we need to use the subject.tmpl
69 my ($template, $loggedinuser, $cookie);
70 if ($subject) {
71         ($template, $loggedinuser, $cookie)
72                 = get_template_and_user({template_name => "catalogue/subject.tmpl",
73                              query => $query,
74                              type => "intranet",
75                              authnotrequired => 0,
76                              flagsrequired => {catalogue => 1},
77                              debug => 1,
78                              });
79 } else {
80         ($template, $loggedinuser, $cookie)
81                 = get_template_and_user({template_name => "catalogue/searchresults.tmpl",
82                              query => $query,
83                              type => "intranet",
84                              authnotrequired => 0,
85                              flagsrequired => {catalogue => 1},
86                              debug => 1,
87                              });
88 }
89
90 # %env
91 # Additional parameters for &catalogsearch
92 my %env = (
93         itemcount       => 1,   # If set to 1, &catalogsearch enumerates
94                                 # the results found and returns the number
95                                 # of items found, where they're located,
96                                 # etc.
97         );
98
99 # get all the search variables
100 # we assume that C4::Search will validate these values for us
101 my %search;                     # Search terms. If the key is "author",
102                                 # then $search{author} is the author the
103                                 # user is looking for.
104
105 my @forminputs;                 # This is used in the form template.
106
107 foreach my $term (qw(keyword subject author illustrator itemnumber
108                      isbn date-before class dewey branch title abstract
109                      publisher ttype subjectitems))
110 {
111         my $value = $query->param($term);
112         next unless defined $value && $value ne "";
113                                 # Skip blank search terms
114         $search{$term} = $value;
115         push @forminputs, {     term => $term,
116                                                         value =>$value };
117 }
118
119 $template->param(FORMINPUTS => \@forminputs);
120
121 # whats this for?
122 # I think it is (or was) a search from the "front" page...   [st]
123 $search{'front'}=$query->param('front');
124
125 my $num=10;
126 my @results;
127 my $count;
128 if (my $subject=$query->param('subjectitems')) {
129     my $blah;
130     @results=subsearch(\$blah,$subject);
131     $count=$#results+1;
132 } else {
133     ($count,@results)=catalogsearch(\%env,'',\%search,$num,$startfrom);
134 }
135
136 #my $resultsarray=\@results;
137 my $resultsarray;
138
139 foreach my $result (@results) {
140     $result->{'authorhtmlescaped'}=$result->{'author'};
141     $result->{'authorhtmlescaped'}=~s/ /%20/g;
142     ($result->{'copyrightdate'}==0) && ($result->{'copyrightdate'}='');
143     ($type eq 'opac') ? ($result->{'opac'}=1) : ($result->{'opac'}=0);
144     push (@$resultsarray, $result);
145 }
146 ($resultsarray) || (@$resultsarray=());
147 my $search="num=20";
148 my $searchdesc='';
149 if ($search{"keyword"}) {
150     $search .= "&keyword=$search{keyword}";
151     $searchdesc.="keyword $search{keyword}, ";
152 }
153 if (my $subjectitems=$query->param('subjectitems')){
154     $search .= "&subjectitems=$subjectitems";
155     $searchdesc.="subject $subjectitems, ";
156 }
157 if ($subject){
158     $search .= "&subject=$subject";
159     $searchdesc.="subject $subject, ";
160 }
161 if ($search{"author"}){
162     $search .= "&author=$search{author}";
163     $searchdesc.="author $search{author}, ";
164 }
165 if ($search{"class"}){
166     $search .= "&class=$search{class}";
167     $searchdesc.="class $search{class}, ";
168 }
169 if ($search{"title"}){
170     $search .= "&title=$search{title}";
171     $searchdesc.="title $search{title}, ";
172 }
173 if ($search{"dewey"}){
174     $search .= "&dewey=$search{dewey}";
175     $searchdesc.="dewey $search{dewey}, ";
176 }
177 if ($search{"illustrator"}){
178     $search .= "&illustrator=$search{illustrator}";
179     $searchdesc.="illustrator $search{illustrator}, ";
180 }
181 if ($search{"itemnumber"}){
182     $search .= "&itemnumber=$search{itemnumber}";
183     $searchdesc.="barcode $search{itemnumber}, ";
184 }
185 $search.="&ttype=$search{ttype}";
186
187 $search=~ s/ /%20/g;
188 $template->param(startfrom => $startfrom+1);
189 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
190 $template->param(numrecords => $count);
191 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
192 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
193 $template->param(nextstartfrom => $nextstartfrom);
194 my $displaynext=1;
195 my $displayprev=0;
196 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
197 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
198 $template->param(displaynext => $displaynext);
199 $template->param(displayprev => $displayprev);
200 ($type eq 'opac') ? ($template->param(opac => 1)) : ($template->param(opac => 0));
201 $template->param(prevstartfrom => $prevstartfrom);
202 $template->param(search => $search);
203 $template->param(searchdesc => $searchdesc);
204 $template->param(SEARCH_RESULTS => $resultsarray);
205 #$template->param(includesdir => $includes);
206
207 my @numbers = ();
208 if ($count>10) {
209     for (my $i=1; $i<$count/10+1; $i++) {
210         if ($i<16) {
211             if ($search{"title"})
212             {
213                 push @forminputs, { line => "title=$search{title}"};
214             }
215             my $highlight=0;
216             ($startfrom==($i-1)*10) && ($highlight=1);
217             my $formelements='';
218             foreach (@forminputs) {
219                 my $line=$_->{line};
220                 $formelements.="$line&";
221             }
222             $formelements=~s/ /+/g;
223             push @numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => \@forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
224         }
225     }
226 }
227
228 $template->param(numbers => \@numbers);
229 if (C4::Context->boolean_preference('marc') eq '1') {
230         $template->param(script => "MARCdetail.pl");
231 } else {
232         $template->param(script => "detail.pl");
233 }
234
235 if ($search{"itemnumber"} && $count == 1){
236     # if its a barcode search by definition we will only have one result.
237     # And if we have a result
238     # lets jump straight to the detail.pl page
239     print $query->redirect("/cgi-bin/koha/detail.pl?type=intra&bib=$results[0]->{'biblionumber'}");
240 }
241 else {
242     # otherwise
243     # Print the page
244     output_html_with_http_headers $query, $cookie, $template->output;
245 }