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