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