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