Adding 2 new fields, called "sort1" and "sort2"
[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.35  2004/04/07 22:43:04  rangi
23 # Fix for bug 217
24 #
25 # Revision 1.34  2004/02/11 08:35:31  tipaul
26 # synch'ing 2.0.0 branch and head
27 #
28 # Revision 1.33  2003/12/19 17:28:03  tipaul
29 # fix for #683
30 #
31 # Revision 1.32.2.2  2004/01/13 17:33:39  tipaul
32 # removing useless (& buggy here) checkauth
33 #
34 # Revision 1.32.2.1  2003/12/19 17:28:42  tipaul
35 # fix for 683
36 #
37 # Revision 1.32  2003/06/11 18:37:55  tonnesen
38 # Using boolean_preference instead of preference for 'marc' setting
39 #
40 # Revision 1.31  2003/05/11 07:31:37  rangi
41 # Removing duplicate use C4::Auth
42 #
43
44 use strict;
45 require Exporter;
46 use CGI;
47 use C4::Auth;
48 use HTML::Template;
49 use C4::Context;
50 use C4::Search;
51 use C4::Output;
52 use C4::Interface::CGI::Output;
53
54 my $query=new CGI;
55 my $type=$query->param('type');
56
57 my $startfrom=$query->param('startfrom');
58 ($startfrom) || ($startfrom=0);
59
60 my $subject=$query->param('subject');
61 # if it's a subject we need to use the subject.tmpl
62 my ($template, $loggedinuser, $cookie);
63 if ($subject) {
64         ($template, $loggedinuser, $cookie)
65                 = get_template_and_user({template_name => "catalogue/subject.tmpl",
66                              query => $query,
67                              type => "intranet",
68                              authnotrequired => 0,
69                              flagsrequired => {catalogue => 1},
70                              debug => 1,
71                              });
72 } else {
73         ($template, $loggedinuser, $cookie)
74                 = get_template_and_user({template_name => "catalogue/searchresults.tmpl",
75                              query => $query,
76                              type => "intranet",
77                              authnotrequired => 0,
78                              flagsrequired => {catalogue => 1},
79                              debug => 1,
80                              });
81 }
82
83 # %env
84 # Additional parameters for &catalogsearch
85 my %env = (
86         itemcount       => 1,   # If set to 1, &catalogsearch enumerates
87                                 # the results found and returns the number
88                                 # of items found, where they're located,
89                                 # etc.
90         );
91
92 # get all the search variables
93 # we assume that C4::Search will validate these values for us
94 my %search;                     # Search terms. If the key is "author",
95                                 # then $search{author} is the author the
96                                 # user is looking for.
97
98 my @forminputs;                 # This is used in the form template.
99
100 foreach my $term (qw(keyword subject author illustrator itemnumber
101                      isbn date-before class dewey branch title abstract
102                      publisher ttype))
103 {
104         my $value = $query->param($term);
105         next unless defined $value && $value ne "";
106                                 # Skip blank search terms
107         $search{$term} = $value;
108         push @forminputs, {     term => $term,
109                                                         value =>$value };
110 }
111
112 $template->param(FORMINPUTS => \@forminputs);
113
114 # whats this for?
115 # I think it is (or was) a search from the "front" page...   [st]
116 $search{'front'}=$query->param('front');
117
118 my $num=10;
119 my @results;
120 my $count;
121 if (my $subject=$query->param('subjectitems')) {
122     my $blah;
123     @results=subsearch(\$blah,$subject);
124     $count=$#results+1;
125 } else {
126     ($count,@results)=catalogsearch(\%env,'',\%search,$num,$startfrom);
127 }
128
129 #my $resultsarray=\@results;
130 my $resultsarray;
131
132 foreach my $result (@results) {
133     $result->{'authorhtmlescaped'}=$result->{'author'};
134     $result->{'authorhtmlescaped'}=~s/ /%20/g;
135     ($result->{'copyrightdate'}==0) && ($result->{'copyrightdate'}='');
136     ($type eq 'opac') ? ($result->{'opac'}=1) : ($result->{'opac'}=0);
137     push (@$resultsarray, $result);
138 }
139 ($resultsarray) || (@$resultsarray=());
140 my $search="num=20";
141 my $searchdesc='';
142 if ($search{"keyword"}) {
143     $search .= "&keyword=$search{keyword}";
144     $searchdesc.="keyword $search{keyword}, ";
145 }
146 if (my $subjectitems=$query->param('subjectitems')){
147     $search .= "&subjectitems=$subjectitems";
148     $searchdesc.="subject $subjectitems, ";
149 }
150 if ($subject){
151     $search .= "&subject=$subject";
152     $searchdesc.="subject $subject, ";
153 }
154 if ($search{"author"}){
155     $search .= "&author=$search{author}";
156     $searchdesc.="author $search{author}, ";
157 }
158 if ($search{"class"}){
159     $search .= "&class=$search{class}";
160     $searchdesc.="class $search{class}, ";
161 }
162 if ($search{"title"}){
163     $search .= "&title=$search{title}";
164     $searchdesc.="title $search{title}, ";
165 }
166 if ($search{"dewey"}){
167     $search .= "&dewey=$search{dewey}";
168     $searchdesc.="dewey $search{dewey}, ";
169 }
170 if ($search{"illustrator"}){
171     $search .= "&illustrator=$search{illustrator}";
172     $searchdesc.="illustrator $search{illustrator}, ";
173 }
174 if ($search{"itemnumber"}){
175     $search .= "&itemnumber=$search{itemnumber}";
176     $searchdesc.="barcode $search{itemnumber}, ";
177 }
178 $search.="&ttype=$search{ttype}";
179
180 $search=~ s/ /%20/g;
181 $template->param(startfrom => $startfrom+1);
182 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
183 $template->param(numrecords => $count);
184 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
185 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
186 $template->param(nextstartfrom => $nextstartfrom);
187 my $displaynext=1;
188 my $displayprev=0;
189 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
190 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
191 $template->param(displaynext => $displaynext);
192 $template->param(displayprev => $displayprev);
193 ($type eq 'opac') ? ($template->param(opac => 1)) : ($template->param(opac => 0));
194 $template->param(prevstartfrom => $prevstartfrom);
195 $template->param(search => $search);
196 $template->param(searchdesc => $searchdesc);
197 $template->param(SEARCH_RESULTS => $resultsarray);
198 #$template->param(includesdir => $includes);
199
200 my @numbers = ();
201 if ($count>10) {
202     for (my $i=1; $i<$count/10+1; $i++) {
203         if ($i<16) {
204             if ($search{"title"})
205             {
206                 push @forminputs, { line => "title=$search{title}"};
207             }
208             my $highlight=0;
209             ($startfrom==($i-1)*10) && ($highlight=1);
210             my $formelements='';
211             foreach (@forminputs) {
212                 my $line=$_->{line};
213                 $formelements.="$line&";
214             }
215             $formelements=~s/ /+/g;
216             push @numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => \@forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
217         }
218     }
219 }
220
221 $template->param(numbers => \@numbers);
222 if (C4::Context->boolean_preference('marc') eq '1') {
223         $template->param(script => "MARCdetail.pl");
224 } else {
225         $template->param(script => "detail.pl");
226 }
227
228 if ($search{"itemnumber"} && $count == 1){
229     # if its a barcode search by definition we will only have one result.
230     # And if we have a result
231     # lets jump straight to the detail.pl page
232     print $query->redirect("/cgi-bin/koha/detail.pl?type=intra&bib=$results[0]->{'biblionumber'}");
233 }
234 else {
235     # otherwise
236     # Print the page
237     output_html_with_http_headers $query, $cookie, $template->output;
238 }